1 package integration
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "testing"
11
12 "gotest.tools/v3/assert"
13
14 "edge-infra.dev/pkg/f8n/ipranger"
15 "edge-infra.dev/pkg/f8n/ipranger/server"
16 "edge-infra.dev/test/f2"
17 "edge-infra.dev/test/f2/integration"
18 )
19
20 var (
21 f f2.Framework
22 )
23
24 func TestMain(m *testing.M) {
25 f = f2.New(
26 context.Background()).
27 Priviledged("GCP Network admin").
28 Disruptive().
29 Setup(func(ctx f2.Context) (f2.Context, error) {
30
31
32
33
34
35
36
37
38
39
40
41
42
43 return ctx, nil
44 })
45 os.Exit(f.Run(m))
46 }
47
48 func TestIPRanger(t *testing.T) {
49 integration.SkipIfNot(t, integration.L2)
50 var ipr *ipranger.IPRanger
51 var projectID string
52 feat := f2.NewFeature("Finds or creates subnets").
53 Setup("IPRanger setup", func(ctx f2.Context, t *testing.T) f2.Context {
54 ipr, err := ipranger.New(ctx)
55 assert.NilError(t, err, "failed to create IPRanger instance")
56 projectID = "TODO: L2 project id"
57 initErr := ipr.InitState(projectID)
58 assert.NilError(t, initErr, "failed to fetch subnet state from GCP")
59 return ctx
60 }).
61 Test("Creates subnet", func(ctx f2.Context, t *testing.T) f2.Context {
62 sn, err := ipr.CreateNewSubnet(projectID, "us-east1")
63 assert.NilError(t, err, "failed to create subnet")
64 assert.Assert(t, sn != ipranger.Subnet{})
65 return ctx
66 }).
67 Test("Finds available subnet", func(ctx f2.Context, t *testing.T) f2.Context {
68 ok, sn := ipr.FindAvailableSubnet(projectID, "region0")
69 assert.Assert(t, ok, "failed to find subnet with available quota")
70 assert.Assert(t, sn != ipranger.Subnet{})
71 return ctx
72 }).
73 Feature()
74 f.Test(t, feat)
75 }
76
77 func TestClientServerIntegration(t *testing.T) {
78 integration.SkipIfNot(t, integration.L2)
79
80 var s *server.Server
81 var projectID string
82 feat := f2.NewFeature("Client requests netcfg from server").
83 Setup("IPRanger server setup", func(ctx f2.Context, t *testing.T) f2.Context {
84 projectID = "TODO: L2 project id"
85 var err error
86 opts := []server.Option{}
87 s, err = server.NewServer(opts...)
88 assert.NilError(t, err, "failed to create ipranger server")
89 return ctx
90 }).
91 Test("Netcfg request", func(ctx f2.Context, t *testing.T) f2.Context {
92 req, err := http.NewRequest(
93 "GET",
94 fmt.Sprintf("%s?project=%s®ion=us-east1-c&cluster=%s",
95 server.NetcfgEndpoint,
96 projectID,
97 "test%2Fescaped+infra0",
98 ),
99 nil,
100 )
101 assert.NilError(t, err)
102
103 w := httptest.NewRecorder()
104 s.Router.ServeHTTP(w, req)
105 assert.Assert(t, w.Code == 200, "expected http 200, got %v. body: %v", w.Code, w.Body.String())
106
107 var netcfg server.NetcfgResp
108 d := json.NewDecoder(w.Body)
109 d.DisallowUnknownFields()
110 assert.NilError(t, d.Decode(&netcfg))
111 assert.Assert(t, netcfg != server.NetcfgResp{})
112 assert.Assert(t, netcfg.Netmask != "")
113 assert.Assert(t, netcfg.Subnetwork != "")
114 assert.Assert(t, netcfg.Network != "")
115
116 return ctx
117 }).
118 Feature()
119 f.Test(t, feat)
120 }
121
View as plain text