|
| 1 | +/* |
| 2 | +Copyright 2026 Cozystack contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package rest |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "testing" |
| 22 | + |
| 23 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 24 | + "github.com/cozystack/blockstor/pkg/store" |
| 25 | +) |
| 26 | + |
| 27 | +// TestAdjustAllOnExistingRD: posting to /v1/resource-definitions/{rd}/adjust |
| 28 | +// returns 200 — the controller's job is to mark the RD for resync; the |
| 29 | +// per-replica work happens out-of-band via the satellite reconciler. |
| 30 | +func TestAdjustAllOnExistingRD(t *testing.T) { |
| 31 | + st := store.NewInMemory() |
| 32 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "pvc-1"}); err != nil { |
| 33 | + t.Fatalf("seed: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + base, stop := startServerWithStore(t, st) |
| 37 | + defer stop() |
| 38 | + |
| 39 | + resp := httpPost(t, base+"/v1/resource-definitions/pvc-1/adjust", nil) |
| 40 | + _ = resp.Body.Close() |
| 41 | + |
| 42 | + if resp.StatusCode != http.StatusOK { |
| 43 | + t.Errorf("status: got %d, want 200", resp.StatusCode) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// TestAdjustAllUnknownRD: 404 on missing RD. |
| 48 | +func TestAdjustAllUnknownRD(t *testing.T) { |
| 49 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 50 | + defer stop() |
| 51 | + |
| 52 | + resp := httpPost(t, base+"/v1/resource-definitions/ghost/adjust", nil) |
| 53 | + _ = resp.Body.Close() |
| 54 | + |
| 55 | + if resp.StatusCode != http.StatusNotFound { |
| 56 | + t.Errorf("status: got %d, want 404", resp.StatusCode) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TestAdjustResource: POST /resources/{node}/adjust nudges one replica. |
| 61 | +func TestAdjustResource(t *testing.T) { |
| 62 | + st := store.NewInMemory() |
| 63 | + ctx := t.Context() |
| 64 | + |
| 65 | + if err := st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{Name: "pvc-1"}); err != nil { |
| 66 | + t.Fatalf("seed RD: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + if err := st.Resources().Create(ctx, &apiv1.Resource{Name: "pvc-1", NodeName: "n1"}); err != nil { |
| 70 | + t.Fatalf("seed Resource: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + base, stop := startServerWithStore(t, st) |
| 74 | + defer stop() |
| 75 | + |
| 76 | + resp := httpPost(t, base+"/v1/resource-definitions/pvc-1/resources/n1/adjust", nil) |
| 77 | + _ = resp.Body.Close() |
| 78 | + |
| 79 | + if resp.StatusCode != http.StatusOK { |
| 80 | + t.Errorf("status: got %d, want 200", resp.StatusCode) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TestAdjustResourceUnknown: 404 if the per-replica Resource is missing. |
| 85 | +func TestAdjustResourceUnknown(t *testing.T) { |
| 86 | + st := store.NewInMemory() |
| 87 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "pvc-1"}); err != nil { |
| 88 | + t.Fatalf("seed: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + base, stop := startServerWithStore(t, st) |
| 92 | + defer stop() |
| 93 | + |
| 94 | + resp := httpPost(t, base+"/v1/resource-definitions/pvc-1/resources/n1/adjust", nil) |
| 95 | + _ = resp.Body.Close() |
| 96 | + |
| 97 | + if resp.StatusCode != http.StatusNotFound { |
| 98 | + t.Errorf("status: got %d, want 404", resp.StatusCode) |
| 99 | + } |
| 100 | +} |
0 commit comments