Skip to content

Commit 512ffed

Browse files
authored
Add IPAM unit tests && Using ginkgo replace the origin go test (#508)
* 1. Add IPAM unit tests 2. Using ginkgo instead of the origin go test * fix go mod
1 parent 00da4e2 commit 512ffed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+170195
-578
lines changed

cni/ipam/ipam_test.go

Lines changed: 170 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
package ipam
55

66
import (
7+
"encoding/json"
78
"fmt"
89
"net/http"
910
"net/url"
10-
"os"
1111
"testing"
1212

13+
cniSkel "github.com/containernetworking/cni/pkg/skel"
14+
cniTypesCurr "github.com/containernetworking/cni/pkg/types/current"
15+
. "github.com/onsi/ginkgo"
16+
. "github.com/onsi/gomega"
17+
1318
"github.com/Azure/azure-container-networking/common"
19+
"github.com/Azure/azure-container-networking/platform"
1420
)
1521

16-
var plugin *ipamPlugin
17-
1822
var ipamQueryUrl = "localhost:42424"
1923
var ipamQueryResponse = "" +
2024
"<Interfaces>" +
@@ -27,70 +31,176 @@ var ipamQueryResponse = "" +
2731
" </Interface>" +
2832
"</Interfaces>"
2933

30-
var localAsId string
31-
var poolId1 string
32-
var address1 string
33-
34-
// Wraps the test run with plugin setup and teardown.
35-
func TestMain(m *testing.M) {
36-
var config common.PluginConfig
37-
38-
// Create a fake local agent to handle requests from IPAM plugin.
39-
u, _ := url.Parse("tcp://" + ipamQueryUrl)
40-
testAgent, err := common.NewListener(u)
41-
if err != nil {
42-
fmt.Printf("Failed to create agent, err:%v.\n", err)
43-
return
44-
}
45-
testAgent.AddHandler("/", handleIpamQuery)
46-
47-
err = testAgent.Start(make(chan error, 1))
48-
if err != nil {
49-
fmt.Printf("Failed to start agent, err:%v.\n", err)
50-
return
51-
}
52-
53-
// Create the plugin.
54-
plugin, err = NewPlugin("ipamtest", &config)
55-
if err != nil {
56-
fmt.Printf("Failed to create IPAM plugin, err:%v.\n", err)
57-
return
58-
}
59-
60-
// Configure test mode.
61-
plugin.SetOption(common.OptEnvironment, common.OptEnvironmentAzure)
62-
plugin.SetOption(common.OptAPIServerURL, "null")
63-
plugin.SetOption(common.OptIpamQueryUrl, "http://"+ipamQueryUrl)
64-
65-
// Start the plugin.
66-
err = plugin.Start(&config)
67-
if err != nil {
68-
fmt.Printf("Failed to start IPAM plugin, err:%v.\n", err)
69-
return
70-
}
71-
72-
// Run tests.
73-
exitCode := m.Run()
74-
75-
// Cleanup.
76-
plugin.Stop()
77-
testAgent.Stop()
78-
79-
os.Exit(exitCode)
34+
func TestIpam(t *testing.T) {
35+
RegisterFailHandler(Fail)
36+
RunSpecs(t, "Ipam Suite")
8037
}
8138

8239
// Handles queries from IPAM source.
8340
func handleIpamQuery(w http.ResponseWriter, r *http.Request) {
8441
w.Write([]byte(ipamQueryResponse))
8542
}
8643

87-
//
88-
// CNI IPAM API compliance tests
89-
// https://github.com/containernetworking/cni/blob/master/SPEC.md
90-
//
91-
92-
func TestAddSuccess(t *testing.T) {
44+
func parseResult(stdinData []byte) (*cniTypesCurr.Result, error) {
45+
result := &cniTypesCurr.Result{}
46+
if err := json.Unmarshal(stdinData, result); err != nil {
47+
return nil, err
48+
}
49+
return result, nil
9350
}
9451

95-
func TestDelSuccess(t *testing.T) {
52+
func getStdinData(cniversion, subnet, ipAddress string) []byte {
53+
stdinData := fmt.Sprintf(
54+
`{
55+
"cniversion": "%s",
56+
"ipam": {
57+
"type": "internal",
58+
"subnet": "%s",
59+
"ipAddress": "%s"
60+
}
61+
}`, cniversion, subnet, ipAddress)
62+
return []byte(stdinData)
9663
}
64+
65+
var (
66+
67+
plugin *ipamPlugin
68+
testAgent *common.Listener
69+
arg *cniSkel.CmdArgs
70+
err error
71+
72+
_ = BeforeSuite(func() {
73+
// Create a fake local agent to handle requests from IPAM plugin.
74+
u, _ := url.Parse("tcp://" + ipamQueryUrl)
75+
testAgent, err = common.NewListener(u)
76+
Expect(err).NotTo(HaveOccurred())
77+
78+
testAgent.AddHandler("/", handleIpamQuery)
79+
80+
err = testAgent.Start(make(chan error, 1))
81+
Expect(err).NotTo(HaveOccurred())
82+
83+
arg = &cniSkel.CmdArgs{}
84+
})
85+
86+
_ = AfterSuite(func() {
87+
// Cleanup.
88+
plugin.Stop()
89+
testAgent.Stop()
90+
})
91+
92+
_ = Describe("Test IPAM", func() {
93+
94+
Context("IPAM start", func() {
95+
96+
var config common.PluginConfig
97+
98+
It("Create IPAM plugin", func() {
99+
// Create the plugin.
100+
plugin, err = NewPlugin("ipamtest", &config)
101+
Expect(err).NotTo(HaveOccurred())
102+
})
103+
104+
It("Start IPAM plugin", func() {
105+
// Configure test mode.
106+
plugin.SetOption(common.OptEnvironment, common.OptEnvironmentAzure)
107+
plugin.SetOption(common.OptAPIServerURL, "null")
108+
plugin.SetOption(common.OptIpamQueryUrl, "http://"+ipamQueryUrl)
109+
// Start the plugin.
110+
err = plugin.Start(&config)
111+
Expect(err).NotTo(HaveOccurred())
112+
})
113+
})
114+
115+
Describe("Test IPAM ADD and DELETE pool", func() {
116+
117+
var result *cniTypesCurr.Result
118+
119+
Context("When ADD with nothing, call for ipam triggering request pool and address", func() {
120+
It("Request pool and ADD successfully", func() {
121+
arg.StdinData = getStdinData("0.4.0", "", "")
122+
err = plugin.Add(arg)
123+
Expect(err).ShouldNot(HaveOccurred())
124+
result, err = parseResult(arg.StdinData)
125+
Expect(err).ShouldNot(HaveOccurred())
126+
address1, _ := platform.ConvertStringToIPNet("10.0.0.5/16")
127+
address2, _ := platform.ConvertStringToIPNet("10.0.0.6/16")
128+
Expect(result.IPs[0].Address.IP).Should(Or(Equal(address1.IP), Equal(address2.IP)))
129+
Expect(result.IPs[0].Address.Mask).Should(Equal(address1.Mask))
130+
})
131+
})
132+
133+
Context("When DELETE with subnet and address, call for ipam triggering release address", func() {
134+
It("DELETE address successfully", func() {
135+
arg.StdinData = getStdinData("0.4.0", "10.0.0.0/16", result.IPs[0].Address.IP.String())
136+
err = plugin.Delete(arg)
137+
Expect(err).ShouldNot(HaveOccurred())
138+
})
139+
})
140+
141+
Context("When DELETE with subnet, call for ipam triggering releasing pool", func() {
142+
It("DELETE pool successfully", func() {
143+
arg.StdinData = getStdinData("0.4.0", "10.0.0.0/16", "")
144+
err = plugin.Delete(arg)
145+
Expect(err).ShouldNot(HaveOccurred())
146+
})
147+
})
148+
})
149+
150+
Describe("Test IPAM ADD and DELETE address", func() {
151+
152+
Context("When address is given", func() {
153+
It("Request pool and address successfully", func() {
154+
arg.StdinData = getStdinData("0.4.0", "", "10.0.0.6")
155+
err = plugin.Add(arg)
156+
Expect(err).ShouldNot(HaveOccurred())
157+
result, err := parseResult(arg.StdinData)
158+
Expect(err).ShouldNot(HaveOccurred())
159+
address, _ := platform.ConvertStringToIPNet("10.0.0.6/16")
160+
Expect(result.IPs[0].Address.IP).Should(Equal(address.IP))
161+
Expect(result.IPs[0].Address.Mask).Should(Equal(address.Mask))
162+
})
163+
})
164+
165+
Context("When subnet is given", func() {
166+
It("Request a usable address successfully", func() {
167+
arg.StdinData = getStdinData("0.4.0", "10.0.0.0/16", "")
168+
err = plugin.Add(arg)
169+
Expect(err).ShouldNot(HaveOccurred())
170+
result, err := parseResult(arg.StdinData)
171+
Expect(err).ShouldNot(HaveOccurred())
172+
address, _ := platform.ConvertStringToIPNet("10.0.0.5/16")
173+
Expect(result.IPs[0].Address.IP).Should(Equal(address.IP))
174+
Expect(result.IPs[0].Address.Mask).Should(Equal(address.Mask))
175+
})
176+
})
177+
})
178+
179+
Describe("Test IPAM DELETE", func() {
180+
181+
Context("When address and subnet is given", func() {
182+
It("Release address successfully", func() {
183+
arg.StdinData = getStdinData("0.4.0", "10.0.0.0/16", "10.0.0.5")
184+
err = plugin.Delete(arg)
185+
Expect(err).ShouldNot(HaveOccurred())
186+
})
187+
})
188+
189+
Context("When address and subnet is given", func() {
190+
It("Release address successfully", func() {
191+
arg.StdinData = getStdinData("0.4.0", "10.0.0.0/16", "10.0.0.6")
192+
err = plugin.Delete(arg)
193+
Expect(err).ShouldNot(HaveOccurred())
194+
})
195+
})
196+
197+
Context("When subnet is given", func() {
198+
It("Release pool successfully", func() {
199+
arg.StdinData = getStdinData("0.4.0", "10.0.0.0/16", "")
200+
err = plugin.Delete(arg)
201+
Expect(err).ShouldNot(HaveOccurred())
202+
})
203+
})
204+
})
205+
})
206+
)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ require (
1717
github.com/hashicorp/golang-lru v0.5.3 // indirect
1818
github.com/imdario/mergo v0.3.8 // indirect
1919
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
20-
github.com/onsi/ginkgo v1.12.0 // indirect
21-
github.com/onsi/gomega v1.9.0 // indirect
20+
github.com/onsi/ginkgo v1.12.0
21+
github.com/onsi/gomega v1.9.0
2222
github.com/petar/GoLLRB v0.0.0-20190514000832-33fb24c13b99 // indirect
2323
github.com/satori/go.uuid v1.2.0 // indirect
2424
github.com/sirupsen/logrus v1.4.2 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
102102
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
103103
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
104104
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
105+
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
105106
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
106107
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
107108
github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
@@ -134,10 +135,12 @@ github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB
134135
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
135136
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
136137
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
138+
github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU=
137139
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
138140
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
139141
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
140142
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
143+
github.com/onsi/gomega v1.9.0 h1:R1uwffexN6Pr340GtYRIdZmAiN4J+iw6WG4wog1DUXg=
141144
github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
142145
github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
143146
github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
@@ -241,6 +244,7 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
241244
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
242245
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
243246
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
247+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
244248
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
245249
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
246250
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
@@ -256,9 +260,11 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
256260
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
257261
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
258262
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
263+
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
259264
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
260265
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
261266
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
267+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
262268
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
263269
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
264270
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

ipam/azure.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,5 @@ func (s *azureSource) refresh() error {
177177
}
178178

179179
// Set the local address space as active.
180-
s.sink.setAddressSpace(local)
181-
182-
return nil
180+
return s.sink.setAddressSpace(local)
183181
}

0 commit comments

Comments
 (0)