|
| 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 | + "slices" |
| 22 | + "testing" |
| 23 | + |
| 24 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 25 | + "github.com/cozystack/blockstor/pkg/store" |
| 26 | +) |
| 27 | + |
| 28 | +// TestNodeEvacuateMarksFlag: POST /v1/nodes/{node}/evacuate adds the |
| 29 | +// EVICTED flag to the Node. Replica migration is the reconciler's job; |
| 30 | +// the REST endpoint only marks intent. |
| 31 | +func TestNodeEvacuateMarksFlag(t *testing.T) { |
| 32 | + st := store.NewInMemory() |
| 33 | + if err := st.Nodes().Create(t.Context(), &apiv1.Node{Name: "n1"}); err != nil { |
| 34 | + t.Fatalf("seed: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + base, stop := startServerWithStore(t, st) |
| 38 | + defer stop() |
| 39 | + |
| 40 | + resp := httpPost(t, base+"/v1/nodes/n1/evacuate", nil) |
| 41 | + _ = resp.Body.Close() |
| 42 | + |
| 43 | + if resp.StatusCode != http.StatusOK { |
| 44 | + t.Fatalf("status: got %d, want 200", resp.StatusCode) |
| 45 | + } |
| 46 | + |
| 47 | + got, err := st.Nodes().Get(t.Context(), "n1") |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("get: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + if !slices.Contains(got.Flags, "EVICTED") { |
| 53 | + t.Errorf("expected EVICTED flag; got %v", got.Flags) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// TestNodeRestoreClearsFlag: POST /v1/nodes/{node}/restore removes |
| 58 | +// the EVICTED flag. |
| 59 | +func TestNodeRestoreClearsFlag(t *testing.T) { |
| 60 | + st := store.NewInMemory() |
| 61 | + if err := st.Nodes().Create(t.Context(), &apiv1.Node{ |
| 62 | + Name: "n1", |
| 63 | + Flags: []string{"EVICTED"}, |
| 64 | + }); err != nil { |
| 65 | + t.Fatalf("seed: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + base, stop := startServerWithStore(t, st) |
| 69 | + defer stop() |
| 70 | + |
| 71 | + resp := httpPost(t, base+"/v1/nodes/n1/restore", nil) |
| 72 | + _ = resp.Body.Close() |
| 73 | + |
| 74 | + if resp.StatusCode != http.StatusOK { |
| 75 | + t.Fatalf("status: got %d, want 200", resp.StatusCode) |
| 76 | + } |
| 77 | + |
| 78 | + got, err := st.Nodes().Get(t.Context(), "n1") |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("get: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if slices.Contains(got.Flags, "EVICTED") { |
| 84 | + t.Errorf("EVICTED still present: %v", got.Flags) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestNodeLostMarksFlag: POST /v1/nodes/{node}/lost adds LOST and |
| 89 | +// EVICTED — `lost` is a permanent action. |
| 90 | +func TestNodeLostMarksFlag(t *testing.T) { |
| 91 | + st := store.NewInMemory() |
| 92 | + if err := st.Nodes().Create(t.Context(), &apiv1.Node{Name: "n1"}); err != nil { |
| 93 | + t.Fatalf("seed: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + base, stop := startServerWithStore(t, st) |
| 97 | + defer stop() |
| 98 | + |
| 99 | + resp := httpPost(t, base+"/v1/nodes/n1/lost", nil) |
| 100 | + _ = resp.Body.Close() |
| 101 | + |
| 102 | + if resp.StatusCode != http.StatusOK { |
| 103 | + t.Fatalf("status: got %d, want 200", resp.StatusCode) |
| 104 | + } |
| 105 | + |
| 106 | + got, err := st.Nodes().Get(t.Context(), "n1") |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("get: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + for _, want := range []string{"LOST", "EVICTED"} { |
| 112 | + if !slices.Contains(got.Flags, want) { |
| 113 | + t.Errorf("expected %s flag; got %v", want, got.Flags) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// TestNodeEvacuateUnknown: 404 if the node doesn't exist. |
| 119 | +func TestNodeEvacuateUnknown(t *testing.T) { |
| 120 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 121 | + defer stop() |
| 122 | + |
| 123 | + resp := httpPost(t, base+"/v1/nodes/ghost/evacuate", nil) |
| 124 | + _ = resp.Body.Close() |
| 125 | + |
| 126 | + if resp.StatusCode != http.StatusNotFound { |
| 127 | + t.Errorf("status: got %d, want 404", resp.StatusCode) |
| 128 | + } |
| 129 | +} |
0 commit comments