11package jenkins .plugins .openstack .compute .internal ;
22
33import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .Matchers .anEmptyMap ;
45import static org .hamcrest .Matchers .containsString ;
56import static org .hamcrest .Matchers .equalTo ;
67import static org .junit .Assert .fail ;
8+ import static org .mockito .Matchers .any ;
9+ import static org .mockito .Matchers .anyMapOf ;
10+ import static org .mockito .Matchers .argThat ;
711import static org .mockito .Mockito .*;
812
13+ import org .junit .Before ;
914import org .junit .Test ;
1015import org .mockito .invocation .InvocationOnMock ;
1116import org .mockito .stubbing .Answer ;
1217import org .openstack4j .api .OSClient ;
1318import org .openstack4j .api .compute .ext .ZoneService ;
1419import org .openstack4j .api .image .v2 .ImageService ;
1520import org .openstack4j .api .networking .NetFloatingIPService ;
21+ import org .openstack4j .api .networking .NetworkService ;
1622import org .openstack4j .api .networking .PortService ;
23+ import org .openstack4j .api .networking .ext .NetworkIPAvailabilityService ;
1724import org .openstack4j .api .storage .BlockVolumeSnapshotService ;
1825import org .openstack4j .model .common .ActionResponse ;
1926import org .openstack4j .model .compute .Fault ;
2229import org .openstack4j .model .compute .ext .AvailabilityZone ;
2330import org .openstack4j .model .image .v2 .Image ;
2431import org .openstack4j .model .network .NetFloatingIP ;
32+ import org .openstack4j .model .network .Network ;
2533import org .openstack4j .model .network .Port ;
34+ import org .openstack4j .model .network .ext .NetworkIPAvailability ;
2635import org .openstack4j .model .network .options .PortListOptions ;
2736import org .openstack4j .model .storage .block .Volume ;
2837import org .openstack4j .model .storage .block .VolumeSnapshot ;
2938import org .openstack4j .openstack .networking .domain .NeutronFloatingIP ;
39+ import org .openstack4j .openstack .networking .domain .NeutronNetwork ;
3040
41+ import java .math .BigInteger ;
3142import java .util .ArrayList ;
3243import java .util .Arrays ;
3344import java .util .Collection ;
4455})
4556public class OpenstackTest {
4657
58+ private OSClient osClient ;
59+ private Openstack openstack ;
60+
61+ @ Before
62+ public void setUp () throws Exception {
63+ osClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
64+ openstack = spy (new Openstack (osClient ));
65+ }
66+
4767 @ Test
4868 public void getImagesReturnsImagesIndexedByNameSortedByAge () {
4969 final Image mockImageWithNullName = mock (Image .class );
@@ -68,15 +88,15 @@ public void getImagesReturnsImagesIndexedByNameSortedByAge() {
6888 when (mockImageNamedBar3 .getCreatedAt ()).thenReturn (new Date (1000 ));
6989 final List images = Arrays .asList (mockImageNamedBar1 , mockImageWithNullName , mockImageNamedBar2 ,
7090 mockImageNamedFoo , mockImageNamedBar3 );
71- final OSClient mockClient = mock ( OSClient . class , RETURNS_DEEP_STUBS );
72- when (mockClient .imagesV2 ().list (any ())).thenReturn (images );
91+
92+ when (osClient .imagesV2 ().list (any ())).thenReturn (images );
7393 final Collection <Image > images0 = new ArrayList <>(
7494 Arrays .asList (mockImageNamedBar2 , mockImageNamedBar3 , mockImageNamedBar1 ));
7595 final Collection <Image > images1 = new ArrayList <>(Arrays .asList (mockImageNamedFoo ));
7696 final Collection <Image > images2 = new ArrayList <>(Arrays .asList (mockImageWithNullName ));
7797
78- final Openstack instance = new Openstack ( mockClient );
79- final Map <String , List <Image >> actual = instance .getImages ();
98+
99+ final Map <String , List <Image >> actual = openstack .getImages ();
80100
81101 // Result keys should be in name order
82102 final Iterator <Map .Entry <String , List <Image >>> iterator = actual .entrySet ().iterator ();
@@ -123,16 +143,16 @@ public void getVolumeSnapshotsReturnsVolumeSnapshotsIndexedByNameSortedByAge() {
123143 when (mockVolumeSnapshotUnavailable .getStatus ()).thenReturn (Volume .Status .ATTACHING );
124144 final List volumeSnapshots = Arrays .asList (mockVolumeSnapshotNamedBar1 , mockVolumeSnapshotWithNullName ,
125145 mockVolumeSnapshotNamedBar2 , mockVolumeSnapshotNamedFoo , mockVolumeSnapshotNamedBar3 , mockVolumeSnapshotUnavailable );
126- final OSClient mockClient = mock ( OSClient . class , RETURNS_DEEP_STUBS );
127- when (mockClient .blockStorage ().snapshots ().list ()).thenReturn (volumeSnapshots );
146+
147+ when (osClient .blockStorage ().snapshots ().list ()).thenReturn (volumeSnapshots );
128148 final Collection <VolumeSnapshot > volumeSnapshots0 = new ArrayList <>(
129149 Arrays .asList (mockVolumeSnapshotNamedBar2 , mockVolumeSnapshotNamedBar3 , mockVolumeSnapshotNamedBar1 ));
130150 final Collection <VolumeSnapshot > volumeSnapshots1 = new ArrayList <>(Arrays .asList (mockVolumeSnapshotNamedFoo ));
131151 final Collection <VolumeSnapshot > volumeSnapshots2 = new ArrayList <>(
132152 Arrays .asList (mockVolumeSnapshotWithNullName ));
133153
134- final Openstack instance = new Openstack ( mockClient );
135- final Map <String , List <VolumeSnapshot >> actual = instance .getVolumeSnapshots ();
154+
155+ final Map <String , List <VolumeSnapshot >> actual = openstack .getVolumeSnapshots ();
136156
137157 // Result keys should be in name order
138158 final Iterator <Map .Entry <String , List <VolumeSnapshot >>> iterator = actual .entrySet ().iterator ();
@@ -170,11 +190,11 @@ public void getImageIdsForGivenNameThenReturnsMatchingImageIdsSortedByAge() {
170190 when (mockIS .list (anyMapOf (String .class , String .class ))).thenReturn (images );
171191 final ArrayList <String > expected = new ArrayList <>(
172192 Arrays .asList ("mockImageNamedBar2Id" , "mockImageNamedBar3Id" , "mockImageNamedBar1Id" ));
173- final OSClient mockClient = mock (OSClient .class );
174- when (mockClient .imagesV2 ()).thenReturn (mockIS );
193+ final OSClient osClient = mock (OSClient .class );
194+ when (osClient .imagesV2 ()).thenReturn (mockIS );
195+
175196
176- final Openstack instance = new Openstack (mockClient );
177- final List <String > actual = instance .getImageIdsFor ("Bar" );
197+ final List <String > actual = openstack .getImageIdsFor ("Bar" );
178198
179199 final Map <String , String > expectedFilteringParams = new HashMap <>(2 );
180200 expectedFilteringParams .put ("name" , "Bar" );
@@ -188,12 +208,12 @@ public void getImageIdsForGivenNameThenReturnsMatchingImageIdsSortedByAge() {
188208 public void getImageIdsForGivenUnknownThenReturnsEmpty () {
189209 final ImageService mockIS = mock (ImageService .class );
190210 when (mockIS .list (anyMapOf (String .class , String .class ))).thenReturn (Collections .EMPTY_LIST );
191- final OSClient mockClient = mock (OSClient .class );
192- when (mockClient .imagesV2 ()).thenReturn (mockIS );
211+ final OSClient osClient = mock (OSClient .class );
212+ when (osClient .imagesV2 ()).thenReturn (mockIS );
193213 final ArrayList <String > expected = new ArrayList <>();
194214
195- final Openstack instance = new Openstack ( mockClient );
196- final List <String > actual = instance .getImageIdsFor ("NameNotFound" );
215+
216+ final List <String > actual = openstack .getImageIdsFor ("NameNotFound" );
197217
198218 final Map <String , String > expectedFilteringParams = new HashMap <>(2 );
199219 expectedFilteringParams .put ("name" , "NameNotFound" );
@@ -213,12 +233,12 @@ public void getImageIdsForGivenIdOfActiveImageThenReturnsId() {
213233 final ImageService mockIS = mock (ImageService .class );
214234 when (mockIS .list (anyMapOf (String .class , String .class ))).thenReturn (Collections .EMPTY_LIST );
215235 when (mockIS .get (imageId )).thenReturn (mockImageNamedFoo );
216- final OSClient mockClient = mock (OSClient .class );
217- when (mockClient .imagesV2 ()).thenReturn (mockIS );
236+ final OSClient osClient = mock (OSClient .class );
237+ when (osClient .imagesV2 ()).thenReturn (mockIS );
218238 final ArrayList <String > expected = new ArrayList <>(Arrays .asList (mockImageNamedFoo .getId ()));
219239
220- final Openstack instance = new Openstack ( mockClient );
221- final List <String > actual = instance .getImageIdsFor (imageId );
240+
241+ final List <String > actual = openstack .getImageIdsFor (imageId );
222242
223243 verify (mockIS ).list (anyMapOf (String .class , String .class ));
224244 verify (mockIS ).get (imageId );
@@ -251,13 +271,13 @@ public void getVolumeSnapshotIdsForGivenNameThenReturnsMatchingVolumeSnapshotIds
251271 final List volumeSnapshots = Arrays .asList (mockVolumeSnapshotNamedFoo , mockVolumeSnapshotNamedBar1 ,
252272 mockVolumeSnapshotNamedBar2 , mockVolumeSnapshotNamedBar3 );
253273 when (mockBVSS .list ()).thenReturn (volumeSnapshots );
254- final OSClient mockClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
255- when (mockClient .blockStorage ().snapshots ()).thenReturn (mockBVSS );
274+ final OSClient osClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
275+ when (osClient .blockStorage ().snapshots ()).thenReturn (mockBVSS );
256276 final ArrayList <String > expected = new ArrayList <>(Arrays .asList ("mockVolumeSnapshotNamedBar2Id" ,
257277 "mockVolumeSnapshotNamedBar3Id" , "mockVolumeSnapshotNamedBar1Id" ));
258278
259- final Openstack instance = new Openstack ( mockClient );
260- final List <String > actual = instance .getVolumeSnapshotIdsFor ("Bar" );
279+
280+ final List <String > actual = openstack .getVolumeSnapshotIdsFor ("Bar" );
261281
262282 final Map <String , String > expectedFilteringParams = new HashMap <>(2 );
263283 expectedFilteringParams .put ("name" , "Bar" );
@@ -292,12 +312,12 @@ public void getVolumeSnapshotIdsForGivenUnknownThenReturnsEmpty() {
292312 final List volumeSnapshots = Arrays .asList (mockVolumeSnapshotNamedFoo , mockVolumeSnapshotNamedBar1 ,
293313 mockVolumeSnapshotNamedBar2 , mockVolumeSnapshotNamedBar3 );
294314 when (mockBVSS .list ()).thenReturn (volumeSnapshots );
295- final OSClient mockClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
296- when (mockClient .blockStorage ().snapshots ()).thenReturn (mockBVSS );
315+ final OSClient osClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
316+ when (osClient .blockStorage ().snapshots ()).thenReturn (mockBVSS );
297317 final ArrayList <String > expected = new ArrayList <>();
298318
299- final Openstack instance = new Openstack ( mockClient );
300- final List <String > actual = instance .getVolumeSnapshotIdsFor ("NameNotFound" );
319+
320+ final List <String > actual = openstack .getVolumeSnapshotIdsFor ("NameNotFound" );
301321
302322 verify (mockBVSS ).list ();
303323 verifyNoMoreInteractions (mockBVSS );
@@ -331,12 +351,12 @@ public void getVolumeSnapshotIdsForGivenIdOfActiveVolumeSnapshotThenReturnsId()
331351 final BlockVolumeSnapshotService mockBVSS = mock (BlockVolumeSnapshotService .class );
332352 when (mockBVSS .list ()).thenReturn (volumeSnapshots );
333353 when (mockBVSS .get (volumeSnapshotId )).thenReturn (mockVolumeSnapshotNamedFoo );
334- final OSClient mockClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
335- when (mockClient .blockStorage ().snapshots ()).thenReturn (mockBVSS );
354+ final OSClient osClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
355+ when (osClient .blockStorage ().snapshots ()).thenReturn (mockBVSS );
336356 final ArrayList <String > expected = new ArrayList <>(Arrays .asList (mockVolumeSnapshotNamedFoo .getId ()));
337357
338- final Openstack instance = new Openstack ( mockClient );
339- final List <String > actual = instance .getVolumeSnapshotIdsFor (volumeSnapshotId );
358+
359+ final List <String > actual = openstack .getVolumeSnapshotIdsFor (volumeSnapshotId );
340360
341361 verify (mockBVSS ).list ();
342362 verify (mockBVSS ).get (volumeSnapshotId );
@@ -354,16 +374,66 @@ public void getAvailabilityZonesReturnsAZsSortedByName() {
354374 when (mockAZ3 .getZoneName ()).thenReturn ("Flibble" );
355375 final ZoneService mockZS = mock (ZoneService .class );
356376 doReturn (Arrays .asList (mockAZ1 , mockAZ2 , mockAZ3 )).when (mockZS ).list ();
357- final OSClient mockClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
358- when (mockClient .compute ().zones ()).thenReturn (mockZS );
377+ final OSClient osClient = mock (OSClient .class , RETURNS_DEEP_STUBS );
378+ when (osClient .compute ().zones ()).thenReturn (mockZS );
359379 final ArrayList <AvailabilityZone > expected = new ArrayList <>(Arrays .asList (mockAZ2 , mockAZ3 , mockAZ1 ));
360380
361- final Openstack instance = new Openstack (mockClient );
362- final List <? extends AvailabilityZone > actual = instance .getAvailabilityZones ();
381+ final List <? extends AvailabilityZone > actual = openstack .getAvailabilityZones ();
363382
364383 assertThat (new ArrayList <>(actual ), equalTo (expected ));
365384 }
366385
386+ @ Test
387+ public void cacheNetworks () {
388+ NeutronNetwork net = mock (NeutronNetwork .class );
389+ when (net .getId ()).thenReturn ("foo" );
390+
391+ NetworkService netService = osClient .networking ().network ();
392+ doReturn (Collections .singletonList (net )).when (netService ).list ();
393+
394+ Map <String , Network > foo = openstack .getNetworks (Collections .singletonList ("foo" ));
395+ assertThat (foo , equalTo (Collections .singletonMap ("foo" , net )));
396+ verify (netService , times (1 )).list ();
397+
398+ foo = openstack .getNetworks (Collections .singletonList ("foo" ));
399+ assertThat (foo , equalTo (Collections .singletonMap ("foo" , net )));
400+ verify (netService , times (1 )).list ();
401+
402+ verify (openstack , times (2 ))._listNetworks ();
403+
404+ // Query again for new instance of Openstack
405+ new Openstack (osClient ).getNetworks (Collections .singletonList ("foo" ));
406+ verify (netService , times (2 )).list ();
407+ }
408+
409+ @ Test
410+ public void cacheNetworkAvailability () {
411+ NetworkIPAvailability awail = mock (NetworkIPAvailability .class );
412+ when (awail .getNetworkId ()).thenReturn ("42" );
413+ when (awail .getTotalIps ()).thenReturn (BigInteger .valueOf (10 ));
414+ when (awail .getUsedIps ()).thenReturn (BigInteger .valueOf (4 ));
415+
416+ NeutronNetwork net = mock (NeutronNetwork .class );
417+ when (net .getId ()).thenReturn ("42" );
418+
419+ NetworkIPAvailabilityService netService = osClient .networking ().networkIPAvailability ();
420+ doReturn (Collections .singletonList (awail )).when (netService ).get ();
421+
422+ Map <Network , Integer > foo = openstack .getNetworksCapacity (Collections .singletonMap ("42" , net ));
423+ assertThat (foo , equalTo (Collections .singletonMap (net , 6 )));
424+ verify (netService , times (1 )).get ();
425+
426+ foo = openstack .getNetworksCapacity (Collections .singletonMap ("42" , net ));
427+ assertThat (foo , equalTo (Collections .singletonMap (net , 6 )));
428+ verify (netService , times (1 )).get ();
429+
430+ verify (openstack , times (2 )).getNetworkIPAvailability ();
431+
432+ // Query again for new instance of Openstack
433+ new Openstack (osClient ).getNetworksCapacity (Collections .singletonMap ("42" , net ));
434+ verify (netService , times (2 )).get ();
435+ }
436+
367437 @ Test
368438 public void deleteAfterFailedBoot () {
369439 Openstack os = mock (Openstack .class , CALLS_REAL_METHODS );
0 commit comments