diff --git a/internal/api/agent/subnet_test.go b/internal/api/agent/subnet_test.go index 23745a0..ca6ec2c 100644 --- a/internal/api/agent/subnet_test.go +++ b/internal/api/agent/subnet_test.go @@ -161,6 +161,51 @@ func TestPostSubnet_VPCDeleting(t *testing.T) { } } +func TestPostSubnet_BridgeMode_Success(t *testing.T) { + s, db := newTestServer(t) + kv.AddInDB(db, "vpc/vpc-1/state", "created") + req := SubnetCreateRequest{ + Name: "sn-br", + VPC: "vpc-1", + Mode: "bridge", + IfaceType: "vms", + GatewayIP: "10.0.0.1", + CIDR: "10.0.0.0/24", + } + body, _ := json.Marshal(req) + w := httptest.NewRecorder() + s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body))) + if w.Code != http.StatusAccepted { + t.Fatalf("attendu 202, obtenu %d: %s", w.Code, w.Body.String()) + } + var result Subnet + json.NewDecoder(w.Body).Decode(&result) + if result.Mode != "bridge" { + t.Errorf("mode attendu bridge, obtenu %q", result.Mode) + } + if result.VxlanID != 0 { + t.Errorf("vxlan_id devrait être 0 en mode bridge, obtenu %d", result.VxlanID) + } +} + +func TestPostSubnet_UnknownMode(t *testing.T) { + s, db := newTestServer(t) + kv.AddInDB(db, "vpc/vpc-1/state", "created") + req := SubnetCreateRequest{ + Name: "sn-1", + VPC: "vpc-1", + Mode: "vlan", + GatewayIP: "10.0.0.1", + CIDR: "10.0.0.0/24", + } + body, _ := json.Marshal(req) + w := httptest.NewRecorder() + s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body))) + if w.Code != http.StatusUnprocessableEntity { + t.Errorf("attendu 422, obtenu %d", w.Code) + } +} + func TestPostSubnet_InvalidBody(t *testing.T) { s, _ := newTestServer(t) w := httptest.NewRecorder() diff --git a/internal/dispatcher/agent/subnet_commands_test.go b/internal/dispatcher/agent/subnet_commands_test.go index 3b118bc..b0fba3d 100644 --- a/internal/dispatcher/agent/subnet_commands_test.go +++ b/internal/dispatcher/agent/subnet_commands_test.go @@ -111,6 +111,68 @@ func TestCreateSubnetCommand_Prepare_VPCDeleted(t *testing.T) { } } +func TestCreateSubnetCommand_Prepare_DefaultsToVxlanMode(t *testing.T) { + _, db := newTestDispatcher(t) + kv.AddInDB(db, "vpc/vpc-1/state", "created") + cmd := CreateSubnetCommand{ + Name: "sn-1", VPC: "vpc-1", VxlanID: 100, + IfaceType: "vms", GatewayIP: "10.0.0.1", CIDR: "10.0.0.0/24", + } + cmd.Prepare(db, testCfg()) + mode, _ := kv.GetFromDB(db, "subnet/sn-1/mode") + if mode != "vxlan" { + t.Errorf("mode attendu vxlan, obtenu %q", mode) + } + if _, err := kv.GetFromDB(db, "subnet/sn-1/vxlan_id"); err != nil { + t.Error("vxlan_id devrait être écrit en mode vxlan") + } +} + +func TestCreateSubnetCommand_Prepare_BridgeMode_Success(t *testing.T) { + _, db := newTestDispatcher(t) + kv.AddInDB(db, "vpc/vpc-1/state", "created") + cmd := CreateSubnetCommand{ + Name: "sn-1", VPC: "vpc-1", Mode: "bridge", + IfaceType: "vms", GatewayIP: "10.0.0.1", CIDR: "10.0.0.0/24", + } + if err := cmd.Prepare(db, testCfg()); err != nil { + t.Fatalf("Prepare a échoué : %v", err) + } + mode, _ := kv.GetFromDB(db, "subnet/sn-1/mode") + if mode != "bridge" { + t.Errorf("mode attendu bridge, obtenu %q", mode) + } + iface, _ := kv.GetFromDB(db, "subnet/sn-1/local_iface") + if iface != "br-vms" { + t.Errorf("local_iface attendu br-vms, obtenu %q", iface) + } +} + +func TestCreateSubnetCommand_Prepare_BridgeMode_NoVxlanID(t *testing.T) { + _, db := newTestDispatcher(t) + kv.AddInDB(db, "vpc/vpc-1/state", "created") + cmd := CreateSubnetCommand{ + Name: "sn-1", VPC: "vpc-1", Mode: "bridge", + IfaceType: "vms", GatewayIP: "10.0.0.1", CIDR: "10.0.0.0/24", + } + cmd.Prepare(db, testCfg()) + if _, err := kv.GetFromDB(db, "subnet/sn-1/vxlan_id"); err == nil { + t.Error("vxlan_id ne devrait pas être écrit en mode bridge") + } +} + +func TestCreateSubnetCommand_Prepare_UnknownMode(t *testing.T) { + _, db := newTestDispatcher(t) + kv.AddInDB(db, "vpc/vpc-1/state", "created") + cmd := CreateSubnetCommand{ + Name: "sn-1", VPC: "vpc-1", Mode: "vlan", + IfaceType: "vms", GatewayIP: "10.0.0.1", CIDR: "10.0.0.0/24", + } + if err := cmd.Prepare(db, testCfg()); err == nil { + t.Error("Prepare devrait échouer pour un mode inconnu") + } +} + // --- DeleteSubnetCommand.Prepare --- func TestDeleteSubnetCommand_Prepare_Success(t *testing.T) {