@@ -17,6 +17,7 @@ limitations under the License.
1717package  api
1818
1919import  (
20+ 	"errors" 
2021	"testing" 
2122
2223	"github.com/netbox-community/go-netbox/v3/netbox/client/ipam" 
@@ -166,8 +167,10 @@ func TestIPAddressClaim(t *testing.T) {
166167			},
167168		})
168169
170+ 		expectedErrMsg  :=  "failed to fetch parent prefix: not found" 
171+ 
169172		// assert error 
170- 		AssertError (t , err , "parent prefix not found" )
173+ 		AssertError (t , err , expectedErrMsg )
171174		// assert nil output 
172175		assert .Nil (t , actual )
173176	})
@@ -231,3 +234,78 @@ func TestIPAddressClaim(t *testing.T) {
231234		assert .Equal (t , ipAddressRestore , actual .IpAddress )
232235	})
233236}
237+ 
238+ func  TestIPAddressClaim_GetNoAvailableIPAddressWithTenancyChecks (t  * testing.T ) {
239+ 	ctrl  :=  gomock .NewController (t )
240+ 	defer  ctrl .Finish ()
241+ 
242+ 	mockTenancy  :=  mock_interfaces .NewMockTenancyInterface (ctrl )
243+ 
244+ 	parentPrefix  :=  "10.112.140.0/24" 
245+ 	t .Run ("No IP address asigned with an error when getting the tenant list" , func (t  * testing.T ) {
246+ 
247+ 		tenantName  :=  "Tenant1" 
248+ 
249+ 		inputTenant  :=  tenancy .NewTenancyTenantsListParams ().WithName (& tenantName )
250+ 
251+ 		// expected error 
252+ 		expectedErrorMsg  :=  "cannot get the list"  // testcase-defined error 
253+ 
254+ 		mockTenancy .EXPECT ().TenancyTenantsList (inputTenant , nil ).Return (nil , errors .New (expectedErrorMsg )).AnyTimes ()
255+ 
256+ 		// init client 
257+ 		client  :=  & NetboxClient {
258+ 			Tenancy : mockTenancy ,
259+ 		}
260+ 
261+ 		actual , err  :=  client .GetAvailableIpAddressByClaim (& models.IPAddressClaim {
262+ 			ParentPrefix : parentPrefix ,
263+ 			Metadata : & models.NetboxMetadata {
264+ 				Tenant : tenantName ,
265+ 			},
266+ 		})
267+ 
268+ 		// assert error 
269+ 		assert .Containsf (t , err .Error (), expectedErrorMsg , "Error should contain: %v, got: %v" , expectedErrorMsg , err .Error ())
270+ 		// assert nil output 
271+ 		assert .Equal (t , actual , (* models .IPAddress )(nil ))
272+ 	})
273+ 
274+ 	t .Run ("No IP address asigned with non-existing tenant" , func (t  * testing.T ) {
275+ 
276+ 		// non existing tenant 
277+ 		nonExistingTenant  :=  "non-existing-tenant" 
278+ 
279+ 		inputTenant  :=  tenancy .NewTenancyTenantsListParams ().WithName (& nonExistingTenant )
280+ 
281+ 		// empty tenant list 
282+ 		emptyTenantList  :=  & tenancy.TenancyTenantsListOK {
283+ 			Payload : & tenancy.TenancyTenantsListOKBody {
284+ 				Results : []* netboxModels.Tenant {},
285+ 			},
286+ 		}
287+ 
288+ 		// expected error 
289+ 		expectedErrorMsg  :=  "failed to fetch tenant: not found" 
290+ 
291+ 		// mock empty list call 
292+ 		mockTenancy .EXPECT ().TenancyTenantsList (inputTenant , nil ).Return (emptyTenantList , nil ).AnyTimes ()
293+ 
294+ 		// init client 
295+ 		client  :=  & NetboxClient {
296+ 			Tenancy : mockTenancy ,
297+ 		}
298+ 
299+ 		actual , err  :=  client .GetAvailableIpAddressByClaim (& models.IPAddressClaim {
300+ 			ParentPrefix : parentPrefix ,
301+ 			Metadata : & models.NetboxMetadata {
302+ 				Tenant : nonExistingTenant ,
303+ 			},
304+ 		})
305+ 
306+ 		// assert error 
307+ 		assert .EqualErrorf (t , err , expectedErrorMsg , "Error should be: %v, got: %v" , expectedErrorMsg , err )
308+ 		// assert nil output 
309+ 		assert .Equal (t , actual , (* models .IPAddress )(nil ))
310+ 	})
311+ }
0 commit comments