22
33namespace OpenStack \Integration \Networking \v2 ;
44
5+ use Guzzle \Tests \Service \Mock \Command \Sub \Sub ;
56use OpenCloud \Integration \TestCase ;
67use OpenStack \Networking \v2 \Extensions \Layer3 \Models \FloatingIp ;
78use OpenStack \Networking \v2 \Models \Network ;
@@ -36,50 +37,89 @@ public function teardown()
3637 $ this ->deleteItems ($ this ->getService ()->listFloatingIps ());
3738 }
3839
39- private function createNetwork (): Network
40+ private function createNetwork (bool $ routerAccessible = true ): Network
4041 {
41- $ network = $ this ->getV2Service ()->createNetwork (['name ' => $ this ->randomStr (), 'routerAccessible ' => true ]);
42+ $ network = $ this ->getV2Service ()->createNetwork ([
43+ 'name ' => $ this ->randomStr (),
44+ 'routerAccessible ' => $ routerAccessible ,
45+ ]);
4246 $ network ->waitUntilActive ();
4347 return $ network ;
4448 }
4549
46- private function createSubnet (Network $ network ): Subnet
50+ private function createSubnet (Network $ network, string $ cidr = ' 192.168.199.0/24 ' ): Subnet
4751 {
4852 return $ this ->getV2Service ()->createSubnet ([
4953 'networkId ' => $ network ->id ,
5054 'name ' => $ this ->randomStr (),
5155 'ipVersion ' => 4 ,
52- 'cidr ' => ' 192.168.199.0/24 ' ,
56+ 'cidr ' => $ cidr ,
5357 ]);
5458 }
5559
5660 private function createPort (Network $ network ): Port
5761 {
58- return $ this ->getV2Service ()->createPort (['networkId ' => $ network ->id , 'name ' => $ this ->randomStr ()]);
62+ return $ this ->getV2Service ()->createPort ([
63+ 'networkId ' => $ network ->id ,
64+ 'name ' => $ this ->randomStr (),
65+ ]);
66+ }
67+
68+ private function findSubnetIp (Port $ port , Subnet $ subnet ): string
69+ {
70+ foreach ($ port ->fixedIps as $ fixedIp ) {
71+ if ($ fixedIp ['subnet_id ' ] == $ subnet ->id ) {
72+ return $ fixedIp ['ip_address ' ];
73+ }
74+ }
75+
76+ return '' ;
5977 }
6078
6179 public function floatingIps ()
6280 {
63- $ this ->logStep ('Creating network ' );
64- $ network = $ this ->createNetwork ();
81+ $ this ->logStep ('Creating external network ' );
82+ $ externalNetwork = $ this ->createNetwork ();
83+
84+ $ this ->logStep ('Creating subnet for external network %id% ' , ['%id% ' => $ externalNetwork ->id ]);
85+ $ this ->createSubnet ($ externalNetwork , '10.0.0.0/24 ' );
6586
66- $ this ->logStep ('Creating subnet for network %id% ' , [ ' %id% ' => $ network -> id ] );
67- $ this ->createSubnet ( $ network );
87+ $ this ->logStep ('Creating internal network ' );
88+ $ internalNetwork = $ this ->createNetwork ( false );
6889
69- $ this ->logStep ('Creating port for network %id% ' , ['%id% ' => $ network ->id ]);
70- $ port1 = $ this ->createPort ($ network );
90+ $ this ->logStep ('Creating subnet for internal network %id% ' , ['%id% ' => $ internalNetwork ->id ]);
91+ $ subnet = $ this ->createSubnet ($ internalNetwork );
92+
93+ $ this ->logStep ('Creating router for external network %id% ' , ['%id% ' => $ externalNetwork ->id ]);
94+ $ router = $ this ->getService ()->createRouter ([
95+ 'name ' => $ this ->randomStr (),
96+ 'externalGatewayInfo ' => [
97+ 'networkId ' => $ externalNetwork ->id ,
98+ 'enableSnat ' => true ,
99+ ],
100+ ]);
101+
102+ $ this ->logStep ('Create interface for subnet %subnet% and router %router% ' , [
103+ '%subnet% ' => $ subnet ->id , '%router% ' => $ router ->id ,
104+ ]);
105+ $ router ->addInterface (['subnetId ' => $ subnet ->id ]);
106+
107+ $ this ->logStep ('Creating port for internal network %id% ' , ['%id% ' => $ internalNetwork ->id ]);
108+ $ port1 = $ this ->createPort ($ internalNetwork );
109+ $ fixedIp = $ this ->findSubnetIp ($ port1 , $ subnet );
71110
72111 $ replacements = [
73- '{networkId} ' => $ network ->id ,
74- '{portId} ' => $ port1 ->id ,
112+ '{networkId} ' => $ externalNetwork ->id ,
113+ '{portId} ' => $ port1 ->id ,
114+ '{fixedIpAddress} ' => $ fixedIp ,
75115 ];
76116
77117 $ this ->logStep ('Create floating IP ' );
78118 /** @var FloatingIp $ip */
79119 $ path = $ this ->sampleFile ($ replacements , 'floatingIPs/create.php ' );
80120 require_once $ path ;
81121 $ this ->assertInstanceOf (FloatingIp::class, $ ip );
82- $ this ->assertEquals ($ network ->id , $ ip ->floatingNetworkId );
122+ $ this ->assertEquals ($ externalNetwork ->id , $ ip ->floatingNetworkId );
83123 $ this ->assertEquals ($ port1 ->id , $ ip ->portId );
84124
85125 $ this ->logStep ('List floating IPs ' );
@@ -93,17 +133,26 @@ public function floatingIps()
93133 $ this ->assertInstanceOf (FloatingIp::class, $ ip );
94134
95135 $ this ->logStep ('Update floating IP ' );
96- $ port2 = $ this ->createPort ($ network );
136+ $ port2 = $ this ->createPort ($ internalNetwork );
97137 $ replacements ['{newPortId} ' ] = $ port2 ->id ;
98138 $ path = $ this ->sampleFile ($ replacements , 'floatingIPs/update.php ' );
99139 require_once $ path ;
100140
101141 $ this ->logStep ('Delete floating IP ' );
102- $ path = $ this ->sampleFile ($ replacements , 'floatingIPs/update .php ' );
142+ $ path = $ this ->sampleFile ($ replacements , 'floatingIPs/delete .php ' );
103143 require_once $ path ;
104144
145+ $ router ->removeInterface (['subnetId ' => $ subnet ->id ]);
146+ $ router ->delete ();
147+ $ router ->waitUntilDeleted ();
148+
105149 $ port1 ->delete ();
106150 $ port2 ->delete ();
107- $ network ->delete ();
151+
152+ $ internalNetwork ->delete ();
153+ $ internalNetwork ->waitUntilDeleted ();
154+
155+ $ externalNetwork ->delete ();
156+ $ externalNetwork ->waitUntilDeleted ();
108157 }
109158}
0 commit comments