-
Notifications
You must be signed in to change notification settings - Fork 36
/
annotation_test.go
281 lines (234 loc) · 6.44 KB
/
annotation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package controller
import (
"net"
"testing"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/gorilla/websocket"
apicapi "github.com/noironetworks/aci-containers/pkg/apicapi"
"github.com/noironetworks/aci-containers/pkg/ipam"
tu "github.com/noironetworks/aci-containers/pkg/testutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"time"
)
type podTest struct {
namespace string
name string
eg string
sg string
qp string
}
var podTests = []podTest{
{
"testns",
"pod1",
egAnnot,
sgAnnot,
qpAnnot,
},
}
const egAnnot = "{\"tenant\": \"testps\", " +
"\"app-profile\": \"test\", \"name\": \"test-eg\"}"
const sgAnnot = "[{\"tenant\": \"testps\", \"name\": \"test-sg\"}]"
const qpAnnot = "{\"tenant\": \"testps\", " +
"\"app-profile\": \"test\", \"name\": \"test-qp\"}"
type loginSucc struct {
cert bool
}
type SocketHandler struct {
ts *testServer
socketConn *websocket.Conn
err error
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type Request struct {
method string
uri string
body apicapi.ApicObject
}
type Recorder struct {
requests []Request
}
func (h *Recorder) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var reqBody apicapi.ApicObject
json.NewDecoder(req.Body).Decode(&reqBody)
fmt.Println(req.Method, req.URL)
h.requests = append(h.requests, Request{
method: req.Method,
uri: req.URL.RequestURI(),
body: reqBody,
})
}
func (h *loginSucc) ServeHTTP(w http.ResponseWriter, req *http.Request) {
class := "aaaLogin"
if h.cert {
class = "webtokenSession"
}
result := map[string]interface{}{
"imdata": []interface{}{
map[string]interface{}{
class: map[string]interface{}{
"attributes": map[string]interface{}{
"token": "testtoken",
},
},
},
},
}
json.NewEncoder(w).Encode(result)
}
func (h *SocketHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c, err := upgrader.Upgrade(w, req, nil)
if err != nil {
h.err = err
return
}
go func() {
defer c.Close()
for {
_, _, err := c.ReadMessage()
if _, k := err.(*websocket.CloseError); k {
break
}
}
}()
h.socketConn, h.err = c, err
}
type testServer struct {
mux *http.ServeMux
server *httptest.Server
sh *SocketHandler
}
func newTestServer() *testServer {
mux := http.NewServeMux()
server := httptest.NewTLSServer(mux)
ts := &testServer{
mux: mux,
server: server,
}
ts.sh = &SocketHandler{
ts: ts,
}
return ts
}
func existingState() apicapi.ApicSlice {
bd := apicapi.NewFvBD("common", "testbd1")
subnet := apicapi.NewFvSubnet(bd.GetDn(), "10.42.10.1/16")
subnet2 := apicapi.NewFvSubnet(bd.GetDn(), "10.43.10.1/16")
bd.AddChild(subnet)
bd.AddChild(subnet2)
bd2 := apicapi.NewFvBD("common", "testbd2")
bd0 := apicapi.NewFvBD("common", "testbd0")
s := apicapi.ApicSlice{bd0, bd, bd2}
return s
}
func (server *testServer) testConn(key []byte) (*apicapi.ApicConnection, error) {
u, _ := url.Parse(server.server.URL)
apic := fmt.Sprintf("%s:%s", u.Hostname(), u.Port())
log := logrus.New()
log.Level = logrus.DebugLevel
log.Formatter = &logrus.TextFormatter{
DisableColors: true,
}
cert := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: server.server.TLS.Certificates[0].Certificate[0],
})
n, err := apicapi.New(log, []string{apic}, "admin", "noir0123", key, cert, "kube",
60, 5)
if err != nil {
return nil, err
}
n.ReconnectInterval = 5 * time.Millisecond
return n, nil
}
func TestEpgAnnotation(t *testing.T) {
initCont := func() *testAciController {
cont := testController()
cont.config.NodeServiceIpPool = []ipam.IpRange{
{Start: net.ParseIP("10.1.1.2"), End: net.ParseIP("10.1.1.3")},
}
cont.config.PodIpPool = []ipam.IpRange{
{Start: net.ParseIP("10.1.1.2"), End: net.ParseIP("10.1.255.254")},
}
cont.AciController.initIpam()
return cont
}
cont := initCont()
cont.run()
for _, pt := range podTests {
actual := true
namespace := namespace(pt.name, pt.eg, pt.sg)
nskey := cont.aciNameForKey("ns", namespace.Name)
if nskey == "" {
cont.log.Error("Could not retrieve namespace key")
return
}
cont.cachedVRFDns = append(cont.cachedVRFDns, "uni/tn-bashokba/ap-aci-containers-bashokba/epg-aci-containers-system")
expected := cont.handleEpgAnnotationUpdate(nskey, 11, critical, namespace.Name, pt.eg)
assert.Equal(t, actual, expected)
cont.checkIfEpgExistNs(namespace) //Checks for annotation of namespace for invalid epg
deployment := deployment(pt.namespace, pt.name, pt.eg, pt.sg)
cont.checkIfEpgExistDep(deployment) //checks for annotation of deployment for invalid epg
pod := pod(pt.namespace, pt.name, pt.eg, pt.sg)
cont.checkIfEpgExistPod(pod) //checks for annotation of pod for invalid epg
cont.stop()
}
}
func TestFaultDnPost(t *testing.T) {
initCont := func() *testAciController {
cont := testController()
cont.config.NodeServiceIpPool = []ipam.IpRange{
{Start: net.ParseIP("10.1.1.2"), End: net.ParseIP("10.1.1.3")},
}
cont.config.PodIpPool = []ipam.IpRange{
{Start: net.ParseIP("10.1.1.2"), End: net.ParseIP("10.1.255.254")},
}
cont.AciController.initIpam()
return cont
}
cont := initCont()
cont.run()
for _, pt := range podTests {
namespace := namespace(pt.name, pt.eg, pt.sg)
nskey := cont.aciNameForKey("ns", namespace.Name)
if nskey == "" {
cont.log.Error("Could not retrieve namespace key")
return
}
aObj := apicapi.NewVmmClusterFaultInfo("comp/prov-kube/ctrlr-[domain]-cont/injcont/info", strconv.Itoa(10))
aObj.SetAttr("faultDesc", "Namespace annotation failed:Reason being Invalid EPG")
aObj.SetAttr("faultCode", strconv.Itoa(10))
aObj.SetAttr("faultSeverity", strconv.Itoa(critical))
server := newTestServer()
defer server.server.Close()
server.mux.Handle("/api/aaaLogin.json", &loginSucc{})
server.mux.Handle("/sockettesttoken", server.sh)
rec := &Recorder{}
server.mux.Handle("/api/mo/uni/tn-common/", rec)
server.mux.Handle("/api/mo/comp/", rec)
conn, err := server.testConn(nil)
assert.Nil(t, err)
slice := apicapi.ApicSlice{aObj}
apicapi.PrepareApicSlice(existingState(), "kube", nskey)
conn.WriteApicObjects(nskey, slice)
stopCh := make(chan struct{})
go conn.Run(stopCh)
tu.WaitFor(t, "sync", 500*time.Millisecond,
func(last bool) (bool, error) {
return tu.WaitNotNil(t, last, server.sh.socketConn,
"socket connection"), nil
})
close(stopCh)
}
cont.stop()
}