Skip to content

Commit b305ca8

Browse files
committed
Improve CNM tests
1 parent da269ca commit b305ca8

File tree

6 files changed

+40
-34
lines changed

6 files changed

+40
-34
lines changed

cnm/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type activateRequest struct {
2727
}
2828

2929
// Response sent by plugin for activation.
30-
type activateResponse struct {
30+
type ActivateResponse struct {
31+
Err string
3132
Implements []string
3233
}

cnm/ipam/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type getCapabilitiesRequest struct {
2727

2828
// Response sent by plugin when registering its capabilities with libnetwork.
2929
type getCapabilitiesResponse struct {
30+
Err string
3031
RequiresMACAddress bool
3132
RequiresRequestReplay bool
3233
}
@@ -37,6 +38,7 @@ type getDefaultAddressSpacesRequest struct {
3738

3839
// Response sent by plugin when returning the default address space names.
3940
type getDefaultAddressSpacesResponse struct {
41+
Err string
4042
LocalDefaultAddressSpace string
4143
GlobalDefaultAddressSpace string
4244
}
@@ -52,6 +54,7 @@ type requestPoolRequest struct {
5254

5355
// Response sent by plugin when an address pool is successfully referenced.
5456
type requestPoolResponse struct {
57+
Err string
5558
PoolID string
5659
Pool string
5760
Data map[string]string
@@ -64,6 +67,7 @@ type releasePoolRequest struct {
6467

6568
// Response sent by plugin when an address pool is successfully released.
6669
type releasePoolResponse struct {
70+
Err string
6771
}
6872

6973
// Request sent when querying address pool information.
@@ -73,6 +77,7 @@ type getPoolInfoRequest struct {
7377

7478
// Response sent by plugin when returning address pool information.
7579
type getPoolInfoResponse struct {
80+
Err string
7681
Capacity int
7782
Available int
7883
UnhealthyAddresses []string
@@ -87,6 +92,7 @@ type requestAddressRequest struct {
8792

8893
// Response sent by plugin when an address is successfully reserved.
8994
type requestAddressResponse struct {
95+
Err string
9096
Address string
9197
Data map[string]string
9298
}
@@ -100,4 +106,5 @@ type releaseAddressRequest struct {
100106

101107
// Response sent by plugin when an address is successfully released.
102108
type releaseAddressResponse struct {
109+
Err string
103110
}

cnm/ipam/ipam_test.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"testing"
1717

18+
"github.com/Azure/azure-container-networking/cnm"
1819
"github.com/Azure/azure-container-networking/common"
1920
"github.com/Azure/azure-container-networking/ipam"
2021
)
@@ -33,7 +34,6 @@ var ipamQueryResponse = "" +
3334
" <IPAddress Address=\"10.0.0.7\" IsPrimary=\"false\"/>" +
3435
" <IPAddress Address=\"10.0.0.8\" IsPrimary=\"false\"/>" +
3536
" <IPAddress Address=\"10.0.0.9\" IsPrimary=\"false\"/>" +
36-
" <IPAddress Address=\"10.0.0.10\" IsPrimary=\"false\"/>" +
3737
" </IPSubnet>" +
3838
" </Interface>" +
3939
"</Interfaces>"
@@ -118,9 +118,7 @@ func decodeResponse(w *httptest.ResponseRecorder, response interface{}) error {
118118

119119
// Tests Plugin.Activate functionality.
120120
func TestActivate(t *testing.T) {
121-
var resp struct {
122-
Implements []string
123-
}
121+
var resp cnm.ActivateResponse
124122

125123
req, err := http.NewRequest(http.MethodGet, "/Plugin.Activate", nil)
126124
if err != nil {
@@ -132,16 +130,14 @@ func TestActivate(t *testing.T) {
132130

133131
err = decodeResponse(w, &resp)
134132

135-
if err != nil || resp.Implements[0] != "IpamDriver" {
133+
if err != nil || resp.Err != "" || resp.Implements[0] != "IpamDriver" {
136134
t.Errorf("Activate response is invalid %+v", resp)
137135
}
138136
}
139137

140138
// Tests IpamDriver.GetCapabilities functionality.
141139
func TestGetCapabilities(t *testing.T) {
142-
var resp struct {
143-
RequiresMACAddress bool
144-
}
140+
var resp getCapabilitiesResponse
145141

146142
req, err := http.NewRequest(http.MethodGet, getCapabilitiesPath, nil)
147143
if err != nil {
@@ -153,7 +149,7 @@ func TestGetCapabilities(t *testing.T) {
153149

154150
err = decodeResponse(w, &resp)
155151

156-
if err != nil {
152+
if err != nil || resp.Err != "" {
157153
t.Errorf("GetCapabilities response is invalid %+v", resp)
158154
}
159155
}
@@ -172,7 +168,7 @@ func TestGetDefaultAddressSpaces(t *testing.T) {
172168

173169
err = decodeResponse(w, &resp)
174170

175-
if err != nil || resp.LocalDefaultAddressSpace == "" {
171+
if err != nil || resp.Err != "" || resp.LocalDefaultAddressSpace == "" {
176172
t.Errorf("GetDefaultAddressSpaces response is invalid %+v", resp)
177173
}
178174

@@ -200,7 +196,7 @@ func TestRequestPool(t *testing.T) {
200196

201197
err = decodeResponse(w, &resp)
202198

203-
if err != nil {
199+
if err != nil || resp.Err != "" {
204200
t.Errorf("RequestPool response is invalid %+v", resp)
205201
}
206202

@@ -230,7 +226,7 @@ func TestRequestAddress(t *testing.T) {
230226

231227
err = decodeResponse(w, &resp)
232228

233-
if err != nil {
229+
if err != nil || resp.Err != "" {
234230
t.Errorf("RequestAddress response is invalid %+v", resp)
235231
}
236232

@@ -260,7 +256,7 @@ func TestReleaseAddress(t *testing.T) {
260256

261257
err = decodeResponse(w, &resp)
262258

263-
if err != nil {
259+
if err != nil || resp.Err != "" {
264260
t.Errorf("ReleaseAddress response is invalid %+v", resp)
265261
}
266262
}
@@ -286,7 +282,7 @@ func TestReleasePool(t *testing.T) {
286282

287283
err = decodeResponse(w, &resp)
288284

289-
if err != nil {
285+
if err != nil || resp.Err != "" {
290286
t.Errorf("ReleasePool response is invalid %+v", resp)
291287
}
292288
}
@@ -312,7 +308,7 @@ func TestGetPoolInfo(t *testing.T) {
312308

313309
err = decodeResponse(w, &resp)
314310

315-
if err != nil {
311+
if err != nil || resp.Err != "" {
316312
t.Errorf("GetPoolInfo response is invalid %+v", resp)
317313
}
318314
}
@@ -367,7 +363,6 @@ func TestRequestAddressWithID(t *testing.T) {
367363
var ipList [2]string
368364

369365
for i := 0; i < 2; i++ {
370-
371366
payload := &requestAddressRequest{
372367
PoolID: poolId1,
373368
Address: "",

cnm/network/api.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type getCapabilitiesRequest struct {
2727

2828
// Response sent by plugin when registering its capabilities with libnetwork.
2929
type getCapabilitiesResponse struct {
30+
Err string
3031
Scope string
3132
}
3233

@@ -48,6 +49,7 @@ type ipamData struct {
4849

4950
// Response sent by plugin when a network is created.
5051
type createNetworkResponse struct {
52+
Err string
5153
}
5254

5355
// Request sent by libnetwork when deleting an existing network.
@@ -57,6 +59,7 @@ type deleteNetworkRequest struct {
5759

5860
// Response sent by plugin when a network is deleted.
5961
type deleteNetworkResponse struct {
62+
Err string
6063
}
6164

6265
// Request sent by libnetwork when creating a new endpoint.
@@ -76,6 +79,7 @@ type endpointInterface struct {
7679

7780
// Response sent by plugin when an endpoint is created.
7881
type createEndpointResponse struct {
82+
Err string
7983
Interface endpointInterface
8084
}
8185

@@ -87,6 +91,7 @@ type deleteEndpointRequest struct {
8791

8892
// Response sent by plugin when an endpoint is deleted.
8993
type deleteEndpointResponse struct {
94+
Err string
9095
}
9196

9297
// Request sent by libnetwork when joining an endpoint to a sandbox.
@@ -99,6 +104,7 @@ type joinRequest struct {
99104

100105
// Response sent by plugin when an endpoint is joined to a sandbox.
101106
type joinResponse struct {
107+
Err string
102108
InterfaceName interfaceName
103109
Gateway string
104110
GatewayIPv6 string
@@ -127,6 +133,7 @@ type leaveRequest struct {
127133

128134
// Response sent by plugin when an endpoint is removed from its sandbox.
129135
type leaveResponse struct {
136+
Err string
130137
}
131138

132139
// Request sent by libnetwork when querying operational info of an endpoint.
@@ -137,5 +144,6 @@ type endpointOperInfoRequest struct {
137144

138145
// Response sent by plugin when returning operational info of an endpoint.
139146
type endpointOperInfoResponse struct {
147+
Err string
140148
Value map[string]interface{}
141149
}

cnm/network/network_test.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"testing"
1515

16+
"github.com/Azure/azure-container-networking/cnm"
1617
"github.com/Azure/azure-container-networking/common"
1718
"github.com/Azure/azure-container-networking/netlink"
1819
driverApi "github.com/docker/libnetwork/driverapi"
@@ -48,7 +49,13 @@ func TestMain(m *testing.M) {
4849
}
4950

5051
// Create a dummy test network interface.
51-
err = netlink.AddLink(anyInterface, "dummy")
52+
err = netlink.AddLink(&netlink.DummyLink{
53+
LinkInfo: netlink.LinkInfo{
54+
Type: netlink.LINK_TYPE_DUMMY,
55+
Name: "dummy",
56+
},
57+
})
58+
5259
if err != nil {
5360
fmt.Printf("Failed to create test network interface, err:%v.\n", err)
5461
os.Exit(3)
@@ -93,11 +100,7 @@ func decodeResponse(w *httptest.ResponseRecorder, response interface{}) error {
93100

94101
// Tests Plugin.Activate functionality.
95102
func TestActivate(t *testing.T) {
96-
fmt.Println("Test: Activate")
97-
98-
var resp struct {
99-
Implements []string
100-
}
103+
var resp cnm.ActivateResponse
101104

102105
req, err := http.NewRequest(http.MethodGet, "/Plugin.Activate", nil)
103106
if err != nil {
@@ -109,15 +112,13 @@ func TestActivate(t *testing.T) {
109112

110113
err = decodeResponse(w, &resp)
111114

112-
if err != nil || resp.Implements[0] != "NetworkDriver" {
115+
if err != nil || resp.Err != "" || resp.Implements[0] != "NetworkDriver" {
113116
t.Errorf("Activate response is invalid %+v", resp)
114117
}
115118
}
116119

117120
// Tests NetworkDriver.GetCapabilities functionality.
118121
func TestGetCapabilities(t *testing.T) {
119-
fmt.Println("Test: GetCapabilities")
120-
121122
var resp remoteApi.GetCapabilityResponse
122123

123124
req, err := http.NewRequest(http.MethodGet, getCapabilitiesPath, nil)
@@ -137,8 +138,6 @@ func TestGetCapabilities(t *testing.T) {
137138

138139
// Tests NetworkDriver.CreateNetwork functionality.
139140
func TestCreateNetwork(t *testing.T) {
140-
fmt.Println("Test: CreateNetwork")
141-
142141
var body bytes.Buffer
143142
var resp remoteApi.CreateNetworkResponse
144143

@@ -172,8 +171,6 @@ func TestCreateNetwork(t *testing.T) {
172171

173172
// Tests NetworkDriver.DeleteNetwork functionality.
174173
func TestDeleteNetwork(t *testing.T) {
175-
fmt.Println("Test: DeleteNetwork")
176-
177174
var body bytes.Buffer
178175
var resp remoteApi.DeleteNetworkResponse
179176

@@ -200,8 +197,6 @@ func TestDeleteNetwork(t *testing.T) {
200197

201198
// Tests NetworkDriver.EndpointOperInfo functionality.
202199
func TestEndpointOperInfo(t *testing.T) {
203-
fmt.Println("Test: EndpointOperInfo")
204-
205200
var body bytes.Buffer
206201
var resp remoteApi.EndpointInfoResponse
207202

@@ -222,7 +217,7 @@ func TestEndpointOperInfo(t *testing.T) {
222217

223218
err = decodeResponse(w, &resp)
224219

225-
if err != nil {
220+
if err != nil || resp.Err != "" {
226221
t.Errorf("EndpointOperInfo response is invalid %+v", resp)
227222
}
228223
}

cnm/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (plugin *Plugin) activate(w http.ResponseWriter, r *http.Request) {
121121

122122
log.Request(plugin.Name, &req, nil)
123123

124-
resp := activateResponse{Implements: plugin.Listener.GetEndpoints()}
124+
resp := ActivateResponse{Implements: plugin.Listener.GetEndpoints()}
125125
err := plugin.Listener.Encode(w, &resp)
126126

127127
log.Response(plugin.Name, &resp, err)

0 commit comments

Comments
 (0)