f-41: api, vpc: champ l3vni des VPC #41
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
bf0e83862f
commit
3dd94f35dd
11 changed files with 295 additions and 10 deletions
|
|
@ -5,9 +5,12 @@ import (
|
|||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
// --- VpcsHandler ---
|
||||
|
|
@ -236,3 +239,84 @@ func TestVpcByName_InvalidMethod(t *testing.T) {
|
|||
t.Errorf("attendu 405, obtenu %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func newBGPTestServer(t *testing.T) (*Server, *badger.DB) {
|
||||
cfg := &configuration.Config{DefaultInterface: "br-test"}
|
||||
cfg.BGP.ASN = 64600
|
||||
cfg.BGP.RouteReflectors = []string{"10.255.255.1"}
|
||||
return newTestServerWith(t, cfg)
|
||||
}
|
||||
|
||||
func postVpcJSON(s *Server, body string) *httptest.ResponseRecorder {
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcsHandler(w, httptest.NewRequest(http.MethodPost, "/vpcs", strings.NewReader(body)))
|
||||
return w
|
||||
}
|
||||
|
||||
func TestPostVpc_WithL3VNI(t *testing.T) {
|
||||
s, db := newBGPTestServer(t)
|
||||
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":4001}`)
|
||||
if w.Code != http.StatusAccepted {
|
||||
t.Fatalf("attendu 202, obtenu %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), `"l3vni":4001`) {
|
||||
t.Errorf("réponse sans l3vni : %s", w.Body.String())
|
||||
}
|
||||
if got, _ := kv.GetFromDB(db, "vpc/vp-1/l3vni"); got != "4001" {
|
||||
t.Errorf("l3vni en DB = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostVpc_WithoutL3VNIOmitsTheField(t *testing.T) {
|
||||
s, _ := newBGPTestServer(t)
|
||||
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16"}`)
|
||||
if w.Code != http.StatusAccepted || strings.Contains(w.Body.String(), "l3vni") {
|
||||
t.Errorf("%d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostVpc_RejectsAnInvalidL3VNIAsABadRequest(t *testing.T) {
|
||||
s, db := newBGPTestServer(t)
|
||||
for _, body := range []string{
|
||||
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":0}`,
|
||||
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":255}`,
|
||||
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":16777216}`,
|
||||
`{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":-1}`,
|
||||
} {
|
||||
if w := postVpcJSON(s, body); w.Code != http.StatusBadRequest {
|
||||
t.Errorf("%s : attendu 400, obtenu %d: %s", body, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if entries, _ := kv.ListByPrefix(db, "vpc/"); len(entries) != 0 {
|
||||
t.Errorf("écritures malgré le refus : %v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostVpc_L3VNIWithoutBGPIsAConflict(t *testing.T) {
|
||||
s, _ := newTestServer(t)
|
||||
w := postVpcJSON(s, `{"name":"vp-1","cidr":"10.0.0.0/16","l3vni":4001}`)
|
||||
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), "requires the bgp section") {
|
||||
t.Errorf("%d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAndListVpc_ReturnTheL3VNI(t *testing.T) {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "vpc/vp-1/state", "running")
|
||||
kv.AddInDB(db, "vpc/vp-1/cidr", "10.0.0.0/16")
|
||||
kv.AddInDB(db, "vpc/vp-1/l3vni", "4001")
|
||||
w := httptest.NewRecorder()
|
||||
s.VpcByNameHandler(w, httptest.NewRequest(http.MethodGet, "/vpcs/vp-1", nil))
|
||||
var got VPC
|
||||
json.NewDecoder(w.Body).Decode(&got)
|
||||
if got.L3VNI != 4001 {
|
||||
t.Errorf("GET : l3vni = %d", got.L3VNI)
|
||||
}
|
||||
w = httptest.NewRecorder()
|
||||
s.VpcsHandler(w, httptest.NewRequest(http.MethodGet, "/vpcs", nil))
|
||||
var list []VPC
|
||||
json.NewDecoder(w.Body).Decode(&list)
|
||||
if len(list) != 1 || list[0].L3VNI != 4001 {
|
||||
t.Errorf("liste : %+v", list)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue