-
Notifications
You must be signed in to change notification settings - Fork 882
/
sandbox_test.go
261 lines (209 loc) · 6.03 KB
/
sandbox_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
package libnetwork
import (
"fmt"
"testing"
"github.com/docker/libnetwork/config"
"github.com/docker/libnetwork/ipamapi"
"github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/options"
"github.com/docker/libnetwork/osl"
"github.com/docker/libnetwork/testutils"
)
func getTestEnv(t *testing.T, opts ...[]NetworkOption) (NetworkController, []Network) {
netType := "bridge"
option := options.Generic{
"EnableIPForwarding": true,
}
genericOption := make(map[string]interface{})
genericOption[netlabel.GenericData] = option
cfgOptions, err := OptionBoltdbWithRandomDBFile()
if err != nil {
t.Fatal(err)
}
c, err := New(append(cfgOptions, config.OptionDriverConfig(netType, genericOption))...)
if err != nil {
t.Fatal(err)
}
if len(opts) == 0 {
return c, nil
}
nwList := make([]Network, 0, len(opts))
for i, opt := range opts {
name := fmt.Sprintf("test_nw_%d", i)
netOption := options.Generic{
netlabel.GenericData: options.Generic{
"BridgeName": name,
},
}
newOptions := make([]NetworkOption, 1, len(opt)+1)
newOptions[0] = NetworkOptionGeneric(netOption)
newOptions = append(newOptions, opt...)
n, err := c.NewNetwork(netType, name, "", newOptions...)
if err != nil {
t.Fatal(err)
}
nwList = append(nwList, n)
}
return c, nwList
}
func TestSandboxAddEmpty(t *testing.T) {
c, _ := getTestEnv(t)
ctrlr := c.(*controller)
sbx, err := ctrlr.NewSandbox("sandbox0")
if err != nil {
t.Fatal(err)
}
if err := sbx.Delete(); err != nil {
t.Fatal(err)
}
if len(ctrlr.sandboxes) != 0 {
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
}
osl.GC()
}
// // If different priorities are specified, internal option and ipv6 addresses mustn't influence endpoint order
func TestSandboxAddMultiPrio(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
opts := [][]NetworkOption{
{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
{NetworkOptionInternalNetwork()},
{},
}
c, nws := getTestEnv(t, opts...)
ctrlr := c.(*controller)
sbx, err := ctrlr.NewSandbox("sandbox1")
if err != nil {
t.Fatal(err)
}
sid := sbx.ID()
ep1, err := nws[0].CreateEndpoint("ep1")
if err != nil {
t.Fatal(err)
}
ep2, err := nws[1].CreateEndpoint("ep2")
if err != nil {
t.Fatal(err)
}
ep3, err := nws[2].CreateEndpoint("ep3")
if err != nil {
t.Fatal(err)
}
if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
t.Fatal(err)
}
if err := ep2.Join(sbx, JoinOptionPriority(2)); err != nil {
t.Fatal(err)
}
if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil {
t.Fatal(err)
}
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
}
if len(sbx.Endpoints()) != 3 {
t.Fatal("Expected 3 endpoints to be connected to the sandbox.")
}
if err := ep3.Leave(sbx); err != nil {
t.Fatal(err)
}
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
t.Fatal("Expected ep2 to be at the top of the heap after removing ep3. But did not find ep2 at the top of the heap")
}
if err := ep2.Leave(sbx); err != nil {
t.Fatal(err)
}
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
t.Fatal("Expected ep1 to be at the top of the heap after removing ep2. But did not find ep1 at the top of the heap")
}
// Re-add ep3 back
if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil {
t.Fatal(err)
}
if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
t.Fatal("Expected ep3 to be at the top of the heap after adding ep3 back. But did not find ep3 at the top of the heap")
}
if err := sbx.Delete(); err != nil {
t.Fatal(err)
}
if len(ctrlr.sandboxes) != 0 {
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
}
osl.GC()
}
func TestSandboxAddSamePrio(t *testing.T) {
if !testutils.IsRunningInContainer() {
defer testutils.SetupTestOSContext(t)()
}
opts := [][]NetworkOption{
{},
{},
{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
{NetworkOptionInternalNetwork()},
}
c, nws := getTestEnv(t, opts...)
ctrlr := c.(*controller)
sbx, err := ctrlr.NewSandbox("sandbox1")
if err != nil {
t.Fatal(err)
}
sid := sbx.ID()
epNw1, err := nws[1].CreateEndpoint("ep1")
if err != nil {
t.Fatal(err)
}
epIPv6, err := nws[2].CreateEndpoint("ep2")
if err != nil {
t.Fatal(err)
}
epInternal, err := nws[3].CreateEndpoint("ep3")
if err != nil {
t.Fatal(err)
}
epNw0, err := nws[0].CreateEndpoint("ep4")
if err != nil {
t.Fatal(err)
}
if err := epNw1.Join(sbx); err != nil {
t.Fatal(err)
}
if err := epIPv6.Join(sbx); err != nil {
t.Fatal(err)
}
if err := epInternal.Join(sbx); err != nil {
t.Fatal(err)
}
if err := epNw0.Join(sbx); err != nil {
t.Fatal(err)
}
// order should now be: epIPv6, epNw0, epNw1, epInternal
if len(sbx.Endpoints()) != 4 {
t.Fatal("Expected 4 endpoints to be connected to the sandbox.")
}
// IPv6 has precedence over IPv4
if ctrlr.sandboxes[sid].endpoints[0].ID() != epIPv6.ID() {
t.Fatal("Expected epIPv6 to be at the top of the heap. But did not find epIPv6 at the top of the heap")
}
// internal network has lowest precedence
if ctrlr.sandboxes[sid].endpoints[3].ID() != epInternal.ID() {
t.Fatal("Expected epInternal to be at the bottom of the heap. But did not find epInternal at the bottom of the heap")
}
if err := epIPv6.Leave(sbx); err != nil {
t.Fatal(err)
}
// 'test_nw_0' has precedence over 'test_nw_1'
if ctrlr.sandboxes[sid].endpoints[0].ID() != epNw0.ID() {
t.Fatal("Expected epNw0 to be at the top of the heap after removing epIPv6. But did not find epNw0 at the top of the heap")
}
if err := epNw1.Leave(sbx); err != nil {
t.Fatal(err)
}
if err := sbx.Delete(); err != nil {
t.Fatal(err)
}
if len(ctrlr.sandboxes) != 0 {
t.Fatalf("controller containers is not empty. len = %d", len(ctrlr.sandboxes))
}
osl.GC()
}