f-57: api: gateway et adresse d'émission calculées en vxlan, adresses réservées refusées pour les VM #57
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
1014712366
commit
5d314e45e1
14 changed files with 591 additions and 46 deletions
|
|
@ -33,6 +33,7 @@ type Subnet struct {
|
|||
VxlanID int `json:"vxlan_id"`
|
||||
LocalIface string `json:"local_iface"`
|
||||
InterfaceIP string `json:"interface_ip"`
|
||||
RelayIP string `json:"relay_ip,omitempty"`
|
||||
CIDR string `json:"cidr"`
|
||||
DefaultRoute bool `json:"default_route"`
|
||||
Gateway string `json:"gateway"`
|
||||
|
|
|
|||
|
|
@ -55,6 +55,8 @@ func (s *Server) getSubnet(w http.ResponseWriter, _ *http.Request, name string)
|
|||
sub.LocalIface = value
|
||||
case "interface_ip":
|
||||
sub.InterfaceIP = value
|
||||
case "relay_ip":
|
||||
sub.RelayIP = value
|
||||
case "cidr":
|
||||
sub.CIDR = value
|
||||
case "default_route":
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
|
@ -57,11 +58,10 @@ func TestPostSubnet_Created(t *testing.T) {
|
|||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-new",
|
||||
VPC: "vpc-1",
|
||||
IfaceType: "vms",
|
||||
InterfaceIP: "10.0.0.1",
|
||||
CIDR: "10.0.0.0/24",
|
||||
Name: "sn-new",
|
||||
VPC: "vpc-1",
|
||||
IfaceType: "vms",
|
||||
CIDR: "10.0.0.0/24",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
w := httptest.NewRecorder()
|
||||
|
|
@ -93,10 +93,9 @@ func TestPostSubnet_IfaceTypeOptional(t *testing.T) {
|
|||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-opt",
|
||||
VPC: "vpc-1",
|
||||
InterfaceIP: "10.0.0.1",
|
||||
CIDR: "10.0.0.0/24",
|
||||
Name: "sn-opt",
|
||||
VPC: "vpc-1",
|
||||
CIDR: "10.0.0.0/24",
|
||||
// IfaceType omis — doit utiliser default_interface
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
|
@ -110,11 +109,10 @@ func TestPostSubnet_IfaceTypeOptional(t *testing.T) {
|
|||
func TestPostSubnet_VPCNotFound(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-1",
|
||||
VPC: "vpc-inexistant",
|
||||
IfaceType: "vms",
|
||||
InterfaceIP: "10.0.0.1",
|
||||
CIDR: "10.0.0.0/24",
|
||||
Name: "sn-1",
|
||||
VPC: "vpc-inexistant",
|
||||
IfaceType: "vms",
|
||||
CIDR: "10.0.0.0/24",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
w := httptest.NewRecorder()
|
||||
|
|
@ -129,11 +127,10 @@ func TestPostSubnet_Duplicate(t *testing.T) {
|
|||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-exist/state", "running")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-exist",
|
||||
VPC: "vpc-1",
|
||||
IfaceType: "vms",
|
||||
InterfaceIP: "10.0.0.1",
|
||||
CIDR: "10.0.0.0/24",
|
||||
Name: "sn-exist",
|
||||
VPC: "vpc-1",
|
||||
IfaceType: "vms",
|
||||
CIDR: "10.0.0.0/24",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
w := httptest.NewRecorder()
|
||||
|
|
@ -147,11 +144,10 @@ func TestPostSubnet_VPCDeleting(t *testing.T) {
|
|||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-dying/state", "deleting")
|
||||
req := SubnetCreateRequest{
|
||||
Name: "sn-1",
|
||||
VPC: "vpc-dying",
|
||||
IfaceType: "vms",
|
||||
InterfaceIP: "10.0.0.1",
|
||||
CIDR: "10.0.0.0/24",
|
||||
Name: "sn-1",
|
||||
VPC: "vpc-dying",
|
||||
IfaceType: "vms",
|
||||
CIDR: "10.0.0.0/24",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
w := httptest.NewRecorder()
|
||||
|
|
@ -206,6 +202,17 @@ func TestPostSubnet_UnknownMode(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPostSubnet_UnknownModeWithoutInterfaceIP(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
body, _ := json.Marshal(SubnetCreateRequest{Name: "sn-1", VPC: "vpc-1", Mode: "vlan", CIDR: "10.0.0.0/24"})
|
||||
w := httptest.NewRecorder()
|
||||
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
|
||||
if w.Code != http.StatusUnprocessableEntity || !strings.Contains(w.Body.String(), "unknown subnet mode") {
|
||||
t.Errorf("attendu 422 mode inconnu, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostSubnet_InvalidBody(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
w := httptest.NewRecorder()
|
||||
|
|
@ -337,3 +344,99 @@ func TestCreateSubnet_GatewayRoundTrip(t *testing.T) {
|
|||
t.Errorf("gateway absente de la réponse GET : %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func postSubnetRequest(t *testing.T, s *Server, req SubnetCreateRequest) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
body, _ := json.Marshal(req)
|
||||
w := httptest.NewRecorder()
|
||||
s.SubnetsHandler(w, httptest.NewRequest(http.MethodPost, "/subnets", bytes.NewReader(body)))
|
||||
return w
|
||||
}
|
||||
|
||||
func TestPostSubnet_VxlanRejectsInterfaceIP(t *testing.T) {
|
||||
for _, mode := range []string{"", "vxlan"} {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
w := postSubnetRequest(t, s, SubnetCreateRequest{
|
||||
Name: "sn-1", VPC: "vpc-1", Mode: mode,
|
||||
InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24",
|
||||
})
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("mode %q : attendu 400, obtenu %d : %s", mode, w.Code, w.Body.String())
|
||||
}
|
||||
if _, err := kv.GetFromDB(db, "subnet/sn-1/state"); err == nil {
|
||||
t.Errorf("mode %q : aucun subnet ne doit être créé", mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostSubnet_BridgeRequiresInterfaceIP(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
w := postSubnetRequest(t, s, SubnetCreateRequest{
|
||||
Name: "sn-br", VPC: "vpc-1", Mode: "bridge", CIDR: "10.0.0.0/24",
|
||||
})
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("attendu 400, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostSubnet_VxlanReturnsComputedAddresses(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
w := postSubnetRequest(t, s, SubnetCreateRequest{
|
||||
Name: "sn-vx", VPC: "vpc-1", Mode: "vxlan", CIDR: "10.0.0.0/24",
|
||||
})
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
var got Subnet
|
||||
json.NewDecoder(w.Body).Decode(&got)
|
||||
if got.InterfaceIP != "10.0.0.254" {
|
||||
t.Errorf("interface_ip attendue 10.0.0.254, obtenu %q", got.InterfaceIP)
|
||||
}
|
||||
if got.RelayIP != "10.0.0.253" {
|
||||
t.Errorf("relay_ip attendu 10.0.0.253, obtenu %q", got.RelayIP)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSubnet_RelayIPOnlyForVxlan(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vpc-1/state", "running")
|
||||
if w := postSubnetRequest(t, s, SubnetCreateRequest{
|
||||
Name: "sn-vx", VPC: "vpc-1", CIDR: "10.0.0.0/24",
|
||||
}); w.Code != http.StatusAccepted {
|
||||
t.Fatalf("vxlan : attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w := postSubnetRequest(t, s, SubnetCreateRequest{
|
||||
Name: "sn-br", VPC: "vpc-1", Mode: "bridge", InterfaceIP: "10.1.0.1", CIDR: "10.1.0.0/24",
|
||||
}); w.Code != http.StatusAccepted {
|
||||
t.Fatalf("bridge : attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
s.SubnetByNameHandler(w, httptest.NewRequest(http.MethodGet, "/subnets/sn-vx", nil))
|
||||
var vx Subnet
|
||||
json.NewDecoder(w.Body).Decode(&vx)
|
||||
if vx.RelayIP != "10.0.0.253" {
|
||||
t.Errorf("relay_ip attendu 10.0.0.253, obtenu %q", vx.RelayIP)
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
s.SubnetByNameHandler(w, httptest.NewRequest(http.MethodGet, "/subnets/sn-br", nil))
|
||||
if strings.Contains(w.Body.String(), "relay_ip") {
|
||||
t.Errorf("relay_ip ne doit pas apparaître pour un subnet bridge : %s", w.Body.String())
|
||||
}
|
||||
|
||||
w = httptest.NewRecorder()
|
||||
s.SubnetsHandler(w, httptest.NewRequest(http.MethodGet, "/subnets", nil))
|
||||
var list []Subnet
|
||||
json.NewDecoder(w.Body).Decode(&list)
|
||||
relays := map[string]string{}
|
||||
for _, sub := range list {
|
||||
relays[sub.Name] = sub.RelayIP
|
||||
}
|
||||
if relays["sn-vx"] != "10.0.0.253" || relays["sn-br"] != "" {
|
||||
t.Errorf("relay_ip inattendus dans la liste : %v", relays)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
|
||||
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||
"git.g3e.fr/syonad/two/internal/subnet"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
|
|
@ -52,6 +53,8 @@ func (s *Server) listSubnets(w http.ResponseWriter, _ *http.Request) {
|
|||
subnets[name].LocalIface = value
|
||||
case "interface_ip":
|
||||
subnets[name].InterfaceIP = value
|
||||
case "relay_ip":
|
||||
subnets[name].RelayIP = value
|
||||
case "cidr":
|
||||
subnets[name].CIDR = value
|
||||
case "default_route":
|
||||
|
|
@ -73,9 +76,25 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
|||
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
|
||||
return
|
||||
}
|
||||
if req.Name == "" || req.VPC == "" || req.InterfaceIP == "" || req.CIDR == "" {
|
||||
if req.Name == "" || req.VPC == "" || req.CIDR == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, interface_ip and cidr are required"})
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc and cidr are required"})
|
||||
return
|
||||
}
|
||||
if req.Mode != "" && !subnet.ValidMode(req.Mode) {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "unknown subnet mode " + strconv.Quote(req.Mode)})
|
||||
return
|
||||
}
|
||||
vxlan := req.Mode == "" || req.Mode == subnet.ModeVxlan
|
||||
if !vxlan && req.InterfaceIP == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "interface_ip is required for " + req.Mode + " subnets"})
|
||||
return
|
||||
}
|
||||
if vxlan && req.InterfaceIP != "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "interface_ip is computed for vxlan subnets, do not set it"})
|
||||
return
|
||||
}
|
||||
cmd := dispatcher.CreateSubnetCommand{
|
||||
|
|
@ -124,6 +143,8 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
|||
sub.LocalIface = value
|
||||
case "interface_ip":
|
||||
sub.InterfaceIP = value
|
||||
case "relay_ip":
|
||||
sub.RelayIP = value
|
||||
case "cidr":
|
||||
sub.CIDR = value
|
||||
case "default_route":
|
||||
|
|
|
|||
|
|
@ -99,6 +99,7 @@ func TestStartVM_MultiDisk(t *testing.T) {
|
|||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-10",
|
||||
|
|
@ -133,6 +134,7 @@ func TestStartVM_StorageReturnedInResponse(t *testing.T) {
|
|||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-11",
|
||||
|
|
@ -167,6 +169,7 @@ func postVM(t *testing.T, name string, meta VMMetadata) (*httptest.ResponseRecor
|
|||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: name,
|
||||
|
|
@ -291,9 +294,10 @@ func TestVmFromDB_MultipleInterfacesSortedByIndex(t *testing.T) {
|
|||
|
||||
func TestStartVM_StoresAllInterfaces(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
for _, sn := range []string{"sn-1", "sn-2"} {
|
||||
for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} {
|
||||
kv.AddInDB(db, "subnet/"+sn+"/state", "running")
|
||||
kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr)
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
|
|
@ -332,9 +336,10 @@ func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) {
|
|||
}
|
||||
for label, ifaces := range cases {
|
||||
s, db := newTestServer(t)
|
||||
for _, sn := range []string{"sn-1", "sn-2"} {
|
||||
for sn, cidr := range map[string]string{"sn-1": "10.1.0.0/24", "sn-2": "10.2.0.0/24"} {
|
||||
kv.AddInDB(db, "subnet/"+sn+"/state", "running")
|
||||
kv.AddInDB(db, "subnet/"+sn+"/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/"+sn+"/cidr", cidr)
|
||||
}
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-bad",
|
||||
|
|
@ -348,3 +353,71 @@ func TestStartVM_RejectsZeroOrTwoPrimaries(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartVM_RejectsReservedOrForeignAddresses(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"passerelle anycast": "10.0.0.254",
|
||||
"relais": "10.0.0.253",
|
||||
"adresse réseau": "10.0.0.0",
|
||||
"broadcast": "10.0.0.255",
|
||||
"hors cidr": "10.0.1.5",
|
||||
"gateway explicite": "10.0.0.1",
|
||||
}
|
||||
for label, ip := range cases {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||
kv.AddInDB(db, "subnet/sn-1/interface_ip", "10.0.0.254")
|
||||
kv.AddInDB(db, "subnet/sn-1/relay_ip", "10.0.0.253")
|
||||
kv.AddInDB(db, "subnet/sn-1/gateway", "10.0.0.1")
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-ip",
|
||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: ip, Primary: true}},
|
||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||
if w.Code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("%s : attendu 422, obtenu %d — %s", label, w.Code, w.Body.String())
|
||||
}
|
||||
if _, err := kv.GetFromDB(db, "vm/vm-ip/state"); err == nil {
|
||||
t.Errorf("%s : la VM ne doit pas être créée", label)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartVM_AcceptsHostAddressBesideReservedOnes(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||
kv.AddInDB(db, "subnet/sn-1/interface_ip", "10.0.0.254")
|
||||
kv.AddInDB(db, "subnet/sn-1/relay_ip", "10.0.0.253")
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-ip",
|
||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.252", Primary: true}},
|
||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartVM_RejectsSubnetWithoutCIDR(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-ip",
|
||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.5", Primary: true}},
|
||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}},
|
||||
})
|
||||
w := httptest.NewRecorder()
|
||||
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||
if w.Code != http.StatusUnprocessableEntity || !strings.Contains(w.Body.String(), "has no cidr") {
|
||||
t.Errorf("attendu 422 « has no cidr », obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue