11package kubecontroller
22
33import (
4- "reflect"
54 "strconv"
65 "testing"
76
87 "github.com/Azure/azure-container-networking/cns"
98 "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
9+ "github.com/stretchr/testify/assert"
1010)
1111
1212const (
1313 uuid = "539970a2-c2dd-11ea-b3de-0242ac130004"
1414 defaultGateway = "10.0.0.2"
15- ipInCIDR = "10.0.0.1/32"
15+ ipIsCIDR = "10.0.0.1/32"
1616 ipMalformed = "10.0.0.0.0"
1717 ncID = "160005ba-cd02-11ea-87d0-0242ac130003"
1818 primaryIP = "10.0.0.1"
@@ -74,24 +74,30 @@ var validRequest = cns.CreateNetworkContainerRequest{
7474func TestConvertNNCStatusToNCRequest (t * testing.T ) {
7575 tests := []struct {
7676 name string
77- status v1alpha.NodeNetworkConfigStatus
78- ncreq cns.CreateNetworkContainerRequest
77+ input v1alpha.NodeNetworkConfigStatus
78+ want cns.CreateNetworkContainerRequest
7979 wantErr bool
8080 }{
81+ {
82+ name : "valid" ,
83+ input : validStatus ,
84+ wantErr : false ,
85+ want : validRequest ,
86+ },
8187 {
8288 name : "no nc" ,
83- status : v1alpha.NodeNetworkConfigStatus {},
89+ input : v1alpha.NodeNetworkConfigStatus {},
8490 wantErr : false ,
85- ncreq : cns.CreateNetworkContainerRequest {},
91+ want : cns.CreateNetworkContainerRequest {},
8692 },
8793 {
8894 name : ">1 nc" ,
89- status : invalidStatusMultiNC ,
95+ input : invalidStatusMultiNC ,
9096 wantErr : true ,
9197 },
9298 {
9399 name : "malformed primary IP" ,
94- status : v1alpha.NodeNetworkConfigStatus {
100+ input : v1alpha.NodeNetworkConfigStatus {
95101 NetworkContainers : []v1alpha.NetworkContainer {
96102 {
97103 PrimaryIP : ipMalformed ,
@@ -110,7 +116,7 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
110116 },
111117 {
112118 name : "malformed IP assignment" ,
113- status : v1alpha.NodeNetworkConfigStatus {
119+ input : v1alpha.NodeNetworkConfigStatus {
114120 NetworkContainers : []v1alpha.NetworkContainer {
115121 {
116122 PrimaryIP : primaryIP ,
@@ -129,34 +135,41 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
129135 },
130136 {
131137 name : "IP is CIDR" ,
132- status : v1alpha.NodeNetworkConfigStatus {
138+ input : v1alpha.NodeNetworkConfigStatus {
133139 NetworkContainers : []v1alpha.NetworkContainer {
134140 {
135- PrimaryIP : ipInCIDR ,
141+ PrimaryIP : ipIsCIDR ,
136142 ID : ncID ,
137143 IPAssignments : []v1alpha.IPAssignment {
138144 {
139145 Name : uuid ,
140146 IP : testSecIP ,
141147 },
142148 },
149+ SubnetName : subnetName ,
150+ DefaultGateway : defaultGateway ,
143151 SubnetAddressSpace : subnetAddressSpace ,
152+ Version : version ,
144153 },
145154 },
155+ Scaler : v1alpha.Scaler {
156+ BatchSize : 1 ,
157+ },
146158 },
147- wantErr : true ,
159+ wantErr : false ,
160+ want : validRequest ,
148161 },
149162 {
150163 name : "IP assignment is CIDR" ,
151- status : v1alpha.NodeNetworkConfigStatus {
164+ input : v1alpha.NodeNetworkConfigStatus {
152165 NetworkContainers : []v1alpha.NetworkContainer {
153166 {
154167 PrimaryIP : primaryIP ,
155168 ID : ncID ,
156169 IPAssignments : []v1alpha.IPAssignment {
157170 {
158171 Name : uuid ,
159- IP : ipInCIDR ,
172+ IP : ipIsCIDR ,
160173 },
161174 },
162175 SubnetAddressSpace : subnetAddressSpace ,
@@ -167,7 +180,7 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
167180 },
168181 {
169182 name : "address space is not CIDR" ,
170- status : v1alpha.NodeNetworkConfigStatus {
183+ input : v1alpha.NodeNetworkConfigStatus {
171184 NetworkContainers : []v1alpha.NetworkContainer {
172185 {
173186 PrimaryIP : primaryIP ,
@@ -184,24 +197,17 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
184197 },
185198 wantErr : true ,
186199 },
187- {
188- name : "valid" ,
189- status : validStatus ,
190- wantErr : false ,
191- ncreq : validRequest ,
192- },
193200 }
194201 for _ , tt := range tests {
195202 tt := tt
196203 t .Run (tt .name , func (t * testing.T ) {
197- got , err := CRDStatusToNCRequest (& tt .status )
198- if ( err != nil ) != tt .wantErr {
199- t . Errorf ( "ConvertNNCStatusToNCRequest() error = %v, wantErr %v" , err , tt . wantErr )
204+ got , err := CRDStatusToNCRequest (& tt .input )
205+ if tt .wantErr {
206+ assert . Error ( t , err )
200207 return
201208 }
202- if ! reflect .DeepEqual (got , tt .ncreq ) {
203- t .Errorf ("ConvertNNCStatusToNCRequest()\n have: %+v\n want: %+v" , got , tt .ncreq )
204- }
209+ assert .NoError (t , err )
210+ assert .EqualValues (t , tt .want , got )
205211 })
206212 }
207213}
0 commit comments