|
| 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 | + "encoding/json" |
| 21 | + "net/http" |
| 22 | + "testing" |
| 23 | + |
| 24 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 25 | + "github.com/cozystack/blockstor/pkg/store" |
| 26 | +) |
| 27 | + |
| 28 | +// TestNetInterfaceCreateAppendsToNode adds a NetInterface to a Node |
| 29 | +// without disturbing the rest of the spec. The interface lives |
| 30 | +// inline on Node.Spec.NetInterfaces[]; we don't mint a separate CRD. |
| 31 | +func TestNetInterfaceCreateAppendsToNode(t *testing.T) { |
| 32 | + st := store.NewInMemory() |
| 33 | + ctx := t.Context() |
| 34 | + |
| 35 | + if err := st.Nodes().Create(ctx, &apiv1.Node{ |
| 36 | + Name: "n1", |
| 37 | + Type: apiv1.NodeTypeSatellite, |
| 38 | + NetInterfaces: []apiv1.NetInterface{ |
| 39 | + {Name: "default", Address: "10.0.0.1"}, |
| 40 | + }, |
| 41 | + }); err != nil { |
| 42 | + t.Fatalf("seed node: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + base, stop := startServerWithStore(t, st) |
| 46 | + defer stop() |
| 47 | + |
| 48 | + body, _ := json.Marshal(apiv1.NetInterface{ |
| 49 | + Name: "replication", Address: "10.10.0.1", SatellitePort: 7000, |
| 50 | + }) |
| 51 | + |
| 52 | + resp := httpPost(t, base+"/v1/nodes/n1/net-interfaces", body) |
| 53 | + _ = resp.Body.Close() |
| 54 | + |
| 55 | + if resp.StatusCode != http.StatusOK { |
| 56 | + t.Fatalf("status: got %d, want 200", resp.StatusCode) |
| 57 | + } |
| 58 | + |
| 59 | + got, err := st.Nodes().Get(ctx, "n1") |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("get: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + if len(got.NetInterfaces) != 2 { |
| 65 | + t.Fatalf("interface count: got %d, want 2", len(got.NetInterfaces)) |
| 66 | + } |
| 67 | + |
| 68 | + names := map[string]bool{} |
| 69 | + for _, n := range got.NetInterfaces { |
| 70 | + names[n.Name] = true |
| 71 | + } |
| 72 | + |
| 73 | + if !names["default"] || !names["replication"] { |
| 74 | + t.Errorf("expected both default + replication; got %v", got.NetInterfaces) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// TestNetInterfaceCreateReplacesSameName: idempotent — second create |
| 79 | +// with the same name overwrites in place rather than duplicating. |
| 80 | +func TestNetInterfaceCreateReplacesSameName(t *testing.T) { |
| 81 | + st := store.NewInMemory() |
| 82 | + ctx := t.Context() |
| 83 | + |
| 84 | + _ = st.Nodes().Create(ctx, &apiv1.Node{ |
| 85 | + Name: "n1", |
| 86 | + Type: apiv1.NodeTypeSatellite, |
| 87 | + NetInterfaces: []apiv1.NetInterface{ |
| 88 | + {Name: "default", Address: "10.0.0.1"}, |
| 89 | + }, |
| 90 | + }) |
| 91 | + |
| 92 | + base, stop := startServerWithStore(t, st) |
| 93 | + defer stop() |
| 94 | + |
| 95 | + body, _ := json.Marshal(apiv1.NetInterface{Name: "default", Address: "192.168.1.5"}) |
| 96 | + |
| 97 | + resp := httpPost(t, base+"/v1/nodes/n1/net-interfaces", body) |
| 98 | + _ = resp.Body.Close() |
| 99 | + |
| 100 | + got, _ := st.Nodes().Get(ctx, "n1") |
| 101 | + if len(got.NetInterfaces) != 1 { |
| 102 | + t.Errorf("idempotent create grew the list: %v", got.NetInterfaces) |
| 103 | + } |
| 104 | + |
| 105 | + if got.NetInterfaces[0].Address != "192.168.1.5" { |
| 106 | + t.Errorf("address not overwritten: %v", got.NetInterfaces[0]) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TestNetInterfaceUpdatePathNameWins: the URL's {name} is the |
| 111 | +// authoritative interface identifier; a name in the body is ignored. |
| 112 | +func TestNetInterfaceUpdatePathNameWins(t *testing.T) { |
| 113 | + st := store.NewInMemory() |
| 114 | + ctx := t.Context() |
| 115 | + |
| 116 | + _ = st.Nodes().Create(ctx, &apiv1.Node{ |
| 117 | + Name: "n1", |
| 118 | + Type: apiv1.NodeTypeSatellite, |
| 119 | + NetInterfaces: []apiv1.NetInterface{ |
| 120 | + {Name: "replication", Address: "10.10.0.1"}, |
| 121 | + }, |
| 122 | + }) |
| 123 | + |
| 124 | + base, stop := startServerWithStore(t, st) |
| 125 | + defer stop() |
| 126 | + |
| 127 | + // Send body with a different name to prove the path wins. |
| 128 | + body, _ := json.Marshal(apiv1.NetInterface{Name: "evil", Address: "10.10.0.99"}) |
| 129 | + |
| 130 | + resp := httpPut(t, base+"/v1/nodes/n1/net-interfaces/replication", body) |
| 131 | + _ = resp.Body.Close() |
| 132 | + |
| 133 | + if resp.StatusCode != http.StatusOK { |
| 134 | + t.Fatalf("status: got %d, want 200", resp.StatusCode) |
| 135 | + } |
| 136 | + |
| 137 | + got, _ := st.Nodes().Get(ctx, "n1") |
| 138 | + if len(got.NetInterfaces) != 1 { |
| 139 | + t.Fatalf("count: got %d, want 1; interfaces=%v", len(got.NetInterfaces), got.NetInterfaces) |
| 140 | + } |
| 141 | + |
| 142 | + if got.NetInterfaces[0].Name != "replication" { |
| 143 | + t.Errorf("name overridden by body: %v", got.NetInterfaces[0]) |
| 144 | + } |
| 145 | + |
| 146 | + if got.NetInterfaces[0].Address != "10.10.0.99" { |
| 147 | + t.Errorf("address not updated: %v", got.NetInterfaces[0]) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// TestNetInterfaceDelete drops the interface; subsequent delete is a |
| 152 | +// no-op (idempotent). |
| 153 | +func TestNetInterfaceDelete(t *testing.T) { |
| 154 | + st := store.NewInMemory() |
| 155 | + ctx := t.Context() |
| 156 | + |
| 157 | + _ = st.Nodes().Create(ctx, &apiv1.Node{ |
| 158 | + Name: "n1", |
| 159 | + Type: apiv1.NodeTypeSatellite, |
| 160 | + NetInterfaces: []apiv1.NetInterface{ |
| 161 | + {Name: "default", Address: "10.0.0.1"}, |
| 162 | + {Name: "replication", Address: "10.10.0.1"}, |
| 163 | + }, |
| 164 | + }) |
| 165 | + |
| 166 | + base, stop := startServerWithStore(t, st) |
| 167 | + defer stop() |
| 168 | + |
| 169 | + for range 2 { |
| 170 | + resp := httpDelete(t, base+"/v1/nodes/n1/net-interfaces/replication") |
| 171 | + _ = resp.Body.Close() |
| 172 | + |
| 173 | + if resp.StatusCode != http.StatusNoContent { |
| 174 | + t.Fatalf("delete status: got %d, want 204", resp.StatusCode) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + got, _ := st.Nodes().Get(ctx, "n1") |
| 179 | + if len(got.NetInterfaces) != 1 { |
| 180 | + t.Errorf("count: got %d, want 1; interfaces=%v", len(got.NetInterfaces), got.NetInterfaces) |
| 181 | + } |
| 182 | + |
| 183 | + if got.NetInterfaces[0].Name != "default" { |
| 184 | + t.Errorf("wrong interface survived: %v", got.NetInterfaces[0]) |
| 185 | + } |
| 186 | +} |
0 commit comments