8989import org .junit .BeforeClass ;
9090import org .junit .Test ;
9191import org .junit .runner .RunWith ;
92+ import org .mockito .ArgumentCaptor ;
9293import org .mockito .InjectMocks ;
9394import org .mockito .Mock ;
9495import org .mockito .Mockito ;
@@ -498,7 +499,7 @@ public void testCreateIpv4SubnetForGuestNetworkResponse() {
498499
499500 @ Test
500501 public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidr1 () {
501- when (ipv4GuestSubnetNetworkMapDao .findBySubnet (subnet )).thenReturn (ipv4GuestSubnetNetworkMap );
502+ when (ipv4GuestSubnetNetworkMapDao .findBySubnetAndDataCenterId (subnet , zoneId )).thenReturn (ipv4GuestSubnetNetworkMap );
502503 when (ipv4GuestSubnetNetworkMap .getNetworkId ()).thenReturn (null );
503504 when (ipv4GuestSubnetNetworkMap .getVpcId ()).thenReturn (null );
504505 when (ipv4GuestSubnetNetworkMap .getParentId ()).thenReturn (zoneSubnetId );
@@ -511,7 +512,7 @@ public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidr1() {
511512
512513 @ Test
513514 public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidr2 () {
514- when (ipv4GuestSubnetNetworkMapDao .findBySubnet (subnet )).thenReturn (null );
515+ when (ipv4GuestSubnetNetworkMapDao .findBySubnetAndDataCenterId (subnet , zoneId )).thenReturn (null );
515516
516517 routedIpv4Manager .getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal (subnet , domainId , accountId , zoneId );
517518
@@ -520,7 +521,7 @@ public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidr2() {
520521
521522 @ Test
522523 public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidr3 () {
523- when (ipv4GuestSubnetNetworkMapDao .findBySubnet (subnet )).thenReturn (null );
524+ when (ipv4GuestSubnetNetworkMapDao .findBySubnetAndDataCenterId (subnet , zoneId )).thenReturn (null );
524525 when (routedIpv4Manager .getParentOfNetworkCidr (zoneId , subnet )).thenReturn (subnetVO );
525526 when (subnetVO .getDomainId ()).thenReturn (domainId );
526527 when (subnetVO .getAccountId ()).thenReturn (accountId );
@@ -530,6 +531,20 @@ public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidr3() {
530531 verify (ipv4GuestSubnetNetworkMapDao ).persist (any (Ipv4GuestSubnetNetworkMapVO .class ));
531532 }
532533
534+ @ Test
535+ public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidrInAnotherZone () {
536+ Long otherZoneId = zoneId + 1L ;
537+ // The same subnet is already allocated in another zone, which must not block this zone
538+ when (ipv4GuestSubnetNetworkMapDao .findBySubnetAndDataCenterId (subnet , otherZoneId )).thenReturn (null );
539+
540+ routedIpv4Manager .getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal (subnet , domainId , accountId , otherZoneId );
541+
542+ ArgumentCaptor <Ipv4GuestSubnetNetworkMapVO > captor = ArgumentCaptor .forClass (Ipv4GuestSubnetNetworkMapVO .class );
543+ verify (ipv4GuestSubnetNetworkMapDao ).persist (captor .capture ());
544+ Assert .assertEquals (otherZoneId , captor .getValue ().getDataCenterId ());
545+ Assert .assertEquals (subnet , captor .getValue ().getSubnet ());
546+ }
547+
533548 @ Test
534549 public void testGetOrCreateIpv4SubnetForGuestNetworkOrVpcInternalByCidrSize1 () {
535550 Ipv4GuestSubnetNetworkMap result = routedIpv4Manager .getOrCreateIpv4SubnetForGuestNetworkOrVpcInternal (cidrSize , domainId , accountId , zoneId );
@@ -629,8 +644,8 @@ public void testCreateIpv4SubnetFromParentSubnetByCidrSize() {
629644 when (subnetVO .getId ()).thenReturn (zoneSubnetId );
630645 when (dataCenterIpv4GuestSubnetDao .findById (zoneSubnetId )).thenReturn (subnetVO );
631646
632- Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap1 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , "172.16.1.0/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
633- Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap2 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , "172.16.1.64/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
647+ Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap1 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , zoneId , "172.16.1.0/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
648+ Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap2 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , zoneId , "172.16.1.64/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
634649 when (ipv4GuestSubnetNetworkMapDao .listByParent (zoneSubnetId )).thenReturn (Arrays .asList (ipv4GuestSubnetNetworkMap1 , ipv4GuestSubnetNetworkMap2 ));
635650 when (subnetVO .getSubnet ()).thenReturn ("172.16.1.0/24" );
636651
@@ -643,8 +658,8 @@ public void testCreateIpv4SubnetFromParentSubnetByCidrSize() {
643658 networkCidr = routedIpv4Manager .createIpv4SubnetStringFromParentSubnet (subnetVO , 25 );
644659 Assert .assertEquals ("172.16.1.128/25" , networkCidr );
645660
646- Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap3 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , "172.16.1.16/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
647- Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap4 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , "172.16.1.128/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
661+ Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap3 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , zoneId , "172.16.1.16/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
662+ Ipv4GuestSubnetNetworkMapVO ipv4GuestSubnetNetworkMap4 = new Ipv4GuestSubnetNetworkMapVO (zoneSubnetId , zoneId , "172.16.1.128/28" , null , Ipv4GuestSubnetNetworkMap .State .Free );
648663 when (ipv4GuestSubnetNetworkMapDao .listByParent (zoneSubnetId )).thenReturn (Arrays .asList (ipv4GuestSubnetNetworkMap1 , ipv4GuestSubnetNetworkMap2 ,
649664 ipv4GuestSubnetNetworkMap3 , ipv4GuestSubnetNetworkMap4 ));
650665 networkCidr = routedIpv4Manager .createIpv4SubnetStringFromParentSubnet (subnetVO , 28 );
@@ -665,7 +680,8 @@ public void testCreateIpv4SubnetFromParentSubnetByCidrSize() {
665680 public void testAssignIpv4SubnetToNetwork () {
666681 when (network .getId ()).thenReturn (networkId );
667682 when (network .getCidr ()).thenReturn (subnet );
668- when (ipv4GuestSubnetNetworkMapDao .findBySubnet (subnet )).thenReturn (ipv4GuestSubnetNetworkMap );
683+ when (network .getDataCenterId ()).thenReturn (zoneId );
684+ when (ipv4GuestSubnetNetworkMapDao .findBySubnetAndDataCenterId (subnet , zoneId )).thenReturn (ipv4GuestSubnetNetworkMap );
669685 when (ipv4GuestSubnetNetworkMap .getId ()).thenReturn (ipv4GuestSubnetNetworkMapId );
670686
671687 routedIpv4Manager .assignIpv4SubnetToNetwork (network );
@@ -677,7 +693,8 @@ public void testAssignIpv4SubnetToNetwork() {
677693 public void testAssignIpv4SubnetToVpc () {
678694 when (vpc .getId ()).thenReturn (vpcId );
679695 when (vpc .getCidr ()).thenReturn (subnet );
680- when (ipv4GuestSubnetNetworkMapDao .findBySubnet (subnet )).thenReturn (ipv4GuestSubnetNetworkMap );
696+ when (vpc .getZoneId ()).thenReturn (zoneId );
697+ when (ipv4GuestSubnetNetworkMapDao .findBySubnetAndDataCenterId (subnet , zoneId )).thenReturn (ipv4GuestSubnetNetworkMap );
681698 when (ipv4GuestSubnetNetworkMap .getId ()).thenReturn (ipv4GuestSubnetNetworkMapId );
682699
683700 routedIpv4Manager .assignIpv4SubnetToVpc (vpc );
0 commit comments