forked from projectcalico/cni-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
calico_cni_ipam_test.go
234 lines (215 loc) · 7.68 KB
/
calico_cni_ipam_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
package main_test
import (
"fmt"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
. "github.com/projectcalico/cni-plugin/test_utils"
"github.com/projectcalico/libcalico-go/lib/testutils"
)
var plugin = "calico-ipam"
var _ = Describe("Calico IPAM Tests", func() {
cniVersion := os.Getenv("CNI_SPEC_VERSION")
BeforeEach(func() {
WipeEtcd()
testutils.CreateNewIPPool(*calicoClient, "192.168.0.0/16", false, false, true)
testutils.CreateNewIPPool(*calicoClient, "fd80:24e2:f998:72d6::/64", false, false, true)
})
Describe("Run IPAM plugin", func() {
Context("Do it", func() {
DescribeTable("Request different numbers of IP addresses",
func(expectedIPv4, expectedIPv6 bool, netconf string) {
result, _, _ := RunIPAMPlugin(netconf, "ADD", "", cniVersion)
var ip4Mask, ip6Mask string
for _, ip := range result.IPs {
if ip.Version == "4" {
ip4Mask = ip.Address.Mask.String()
} else if ip.Version == "6" {
ip6Mask = ip.Address.Mask.String()
}
}
if expectedIPv4 {
Expect(ip4Mask).Should(Equal("ffffffff"))
}
if expectedIPv6 {
Expect(ip6Mask).Should(Equal("ffffffffffffffffffffffffffffffff"))
}
_, _, exitCode := RunIPAMPlugin(netconf, "DEL", "", cniVersion)
Expect(exitCode).Should(Equal(0))
},
Entry("IPAM with no configuration", true, false, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s"
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)),
Entry("IPAM with IPv4 (explicit)", true, false, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s",
"assign_ipv4": "true"
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)),
Entry("IPAM with IPv6 only", false, true, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s",
"assign_ipv4": "false",
"assign_ipv6": "true"
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)),
Entry("IPAM with IPv4 and IPv6", true, true, fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"assign_ipv6": "true"
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)),
)
})
})
Describe("Run IPAM plugin - Verify IP Pools", func() {
Context("Pass valid pools", func() {
It("Uses the ipv4 pool", func() {
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.168.0.0/16" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)
result, _, _ := RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(result.IPs[0].Address.IP.String()).Should(HavePrefix("192.168."))
})
})
Context("Pass more than one pool", func() {
It("Uses one of the ipv4 pools", func() {
testutils.CreateNewIPPool(*calicoClient, "192.169.1.0/24", false, false, true)
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.169.1.0/24", "192.168.0.0/16" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)
result, _, _ := RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(result.IPs[0].Address.IP.String()).Should(Or(HavePrefix("192.168."), HavePrefix("192.169.1")))
})
})
Context("Pass an invalid pool", func() {
It("fails to get an IP", func() {
// Put the bogus pool last in the array
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.168.0.0/16", "192.169.1.0/24" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)
_, error, _ := RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(error.Msg).Should(ContainSubstring("192.169.1.0/24) does not exist"))
})
It("fails to get an IP", func() {
// Put the bogus pool first in the array
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s",
"assign_ipv4": "true",
"ipv4_pools": [ "192.168.0.0/16", "192.169.1.0/24" ]
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)
_, error, _ := RunIPAMPlugin(netconf, "ADD", "", cniVersion)
Expect(error.Msg).Should(ContainSubstring("192.169.1.0/24) does not exist"))
})
})
})
Describe("Run IPAM plugin", func() {
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s"
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)
Context("Pass explicit IP address", func() {
It("Return the expected IP", func() {
result, _, _ := RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
})
It("Return the expected IP twice after deleting in the middle", func() {
result, _, _ := RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
_, _, _ = RunIPAMPlugin(netconf, "DEL", "IP=192.168.123.123", cniVersion)
result, _, _ = RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
})
It("Doesn't allow an explicit IP to be assigned twice", func() {
result, _, _ := RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(len(result.IPs)).Should(Equal(1))
Expect(result.IPs[0].Address.String()).Should(Equal("192.168.123.123/32"))
result, _, exitCode := RunIPAMPlugin(netconf, "ADD", "IP=192.168.123.123", cniVersion)
Expect(exitCode).Should(BeNumerically(">", 0))
})
})
})
Describe("Run IPAM DEL", func() {
netconf := fmt.Sprintf(`
{
"cniVersion": "%s",
"name": "net1",
"type": "calico",
"etcd_endpoints": "http://%s:2379",
"ipam": {
"type": "%s"
}
}`, cniVersion, os.Getenv("ETCD_IP"), plugin)
It("should exit successfully even if no address exists", func() {
_, _, exitCode := RunIPAMPlugin(netconf, "DEL", "IP=192.168.123.123", cniVersion)
Expect(exitCode).Should(Equal(0))
})
})
})