22
33namespace OpenStack \Integration \Compute \v2 ;
44
5+ use OpenStack \BlockStorage \v2 \Models \Volume ;
56use OpenStack \Compute \v2 \Models \Flavor ;
67use OpenStack \Compute \v2 \Models \Image ;
8+ use OpenStack \Compute \v2 \Models \Keypair ;
9+ use OpenStack \Compute \v2 \Models \Limit ;
710use OpenStack \Compute \v2 \Models \Server ;
811use OpenCloud \Integration \TestCase ;
912use OpenStack \Integration \Utils ;
13+ use OpenStack \Networking \v2 \Models \Network ;
14+ use OpenStack \Networking \v2 \Models \Subnet ;
15+ use OpenStack \Networking \v2 \Service as NetworkService ;
16+ use OpenStack \BlockStorage \v2 \Service as BlockStorageService ;
1017
1118class CoreTest extends TestCase
1219{
20+ // Test environment constants
21+ const NETWORK = 'phptest_network ' ;
22+ const SUBNET = 'phptest_subnet ' ;
23+ const VOLUME = 'phptest_volume ' ;
24+
25+ /** @var NetworkService */
26+ private $ networkService ;
27+
28+ /** @var BlockStorageService */
29+ private $ blockStorageService ;
30+
31+ /** @var Network */
32+ private $ network ;
33+
34+ /** @var Subnet */
35+ private $ subnet ;
36+
37+ /** @var Volume */
38+ private $ volume ;
39+
40+ // Core test
1341 private $ service ;
1442 private $ serverId ;
1543 private $ adminPass ;
1644 private $ imageId ;
45+ private $ flavorId ;
46+ private $ keypairName ;
47+ private $ volumeAttachmentId ;
1748
1849 private function getService ()
1950 {
@@ -24,6 +55,26 @@ private function getService()
2455 return $ this ->service ;
2556 }
2657
58+ private function getNetworkService ()
59+ {
60+ if (!$ this ->networkService )
61+ {
62+ $ this ->networkService = Utils::getOpenStack ()->networkingV2 ();
63+ }
64+
65+ return $ this ->networkService ;
66+ }
67+
68+ private function getBlockStorageService ()
69+ {
70+ if (!$ this ->blockStorageService )
71+ {
72+ $ this ->blockStorageService = Utils::getOpenStack ()->blockStorageV2 ();
73+ }
74+
75+ return $ this ->blockStorageService ;
76+ }
77+
2778 private function searchImages ($ name )
2879 {
2980 foreach ($ this ->getService ()->listImages () as $ image ) {
@@ -36,11 +87,46 @@ private function searchImages($name)
3687 $ this ->logger ->emergency ('No image found ' );
3788 }
3889
90+ protected function setUp ()
91+ {
92+ $ this ->network = $ this ->getNetworkService ()->createNetwork (
93+ [
94+ 'name ' => self ::NETWORK ,
95+ 'adminStateUp ' => true ,
96+ ]
97+ );
98+
99+ $ this ->subnet = $ this ->getNetworkService ()->createSubnet (
100+ [
101+ 'name ' => self ::SUBNET ,
102+ 'networkId ' => $ this ->network ->id ,
103+ 'ipVersion ' => 4 ,
104+ 'cidr ' => '10.20.30.0/24 ' ,
105+ ]
106+ );
107+
108+ $ this ->volume = $ this ->getBlockStorageService ()->createVolume (
109+ [
110+ 'name ' => self ::VOLUME ,
111+ 'description ' => '' ,
112+ 'size ' => 1
113+ ]
114+ );
115+
116+ $ this ->logger ->info (sprintf ('Created network %s with id %s ' , $ this ->network ->name , $ this ->network ->id ));
117+ $ this ->logger ->info (sprintf ('Created subnet %s with id %s ' , $ this ->subnet ->name , $ this ->subnet ->id ));
118+ $ this ->logger ->info (sprintf ('Created volume %s with id %s ' , $ this ->volume ->name , $ this ->volume ->id ));
119+ }
120+
39121 public function runTests ()
40122 {
41- $ this ->searchImages ('cirros ' );
42123 $ this ->startTimer ();
43124
125+ // Manually trigger setUp
126+ $ this ->setUp ();
127+
128+ $ this ->searchImages ('cirros ' );
129+
44130 // Servers
45131 $ this ->createServer ();
46132
@@ -57,7 +143,18 @@ public function runTests()
57143 $ this ->createServerImage ();
58144 $ this ->rebootServer ();
59145
146+ // Security groups
147+ $ this ->addSecurityGroupToServer ();
148+ $ this ->listServerSecurityGroups ();
149+ $ this ->removeServerSecurityGroup ();
150+
151+ // Volume attachments
152+ $ this ->attachVolumeToServer ();
153+ $ this ->listVolumeAttachmentsForServer ();
154+ $ this ->detachVolumeFromServer ();
155+
60156 // Flavors
157+ $ this ->createFlavor ();
61158 $ this ->listFlavors ();
62159 $ this ->getFlavor ();
63160
@@ -66,20 +163,37 @@ public function runTests()
66163 $ this ->getImage ();
67164 $ this ->imageMetadata ();
68165 $ this ->deleteServerImage ();
166+
167+ // Keypairs
168+ $ this ->listKeypairs ();
169+ $ this ->createKeypair ();
170+ $ this ->getKeypair ();
171+ $ this ->deleteKeypair ();
172+
173+ // Limits
174+ $ this ->getLimits ();
175+
176+
69177 } finally {
70178 // Teardown
71179 $ this ->deleteServer ();
180+ $ this ->deleteFlavor ();
181+ $ this ->subnet ->delete ();
182+ $ this ->network ->delete ();
183+ $ this ->volume ->delete ();
72184 }
73185
74186 $ this ->outputTimeTaken ();
75187 }
76188
77189 private function createServer ()
78190 {
191+
79192 $ replacements = [
80193 '{serverName} ' => $ this ->randomStr (),
81194 '{imageId} ' => $ this ->imageId ,
82195 '{flavorId} ' => 1 ,
196+ '{networkId} ' => $ this ->network ->id
83197 ];
84198
85199 /** @var $server \OpenStack\Compute\v2\Models\Server */
@@ -127,6 +241,8 @@ private function deleteServer()
127241 $ path = $ this ->sampleFile ($ replacements , 'servers/delete_server.php ' );
128242 require_once $ path ;
129243
244+ // Needed so that subnet and network can be removed
245+ $ server ->waitUntilDeleted ();
130246 $ this ->logStep ('Deleted server ID ' , ['ID ' => $ this ->serverId ]);
131247 }
132248
@@ -240,6 +356,32 @@ private function rebootServer()
240356 $ this ->logStep ('Rebooted server {serverId} ' , $ replacements );
241357 }
242358
359+ private function createFlavor ()
360+ {
361+ $ replacements = [
362+ '{flavorName} ' => $ this ->randomStr ()
363+ ];
364+
365+ /** @var $flavor \OpenStack\Compute\v2\Models\Flavor */
366+ $ path = $ this ->sampleFile ($ replacements , 'flavors/create_flavor.php ' );
367+ require_once $ path ;
368+
369+ $ this ->assertInstanceOf ('\OpenStack\Compute\v2\Models\Flavor ' , $ flavor );
370+
371+ $ this ->flavorId = $ flavor ->id ;
372+ $ this ->logStep ('Created flavor {id} ' , ['{id} ' => $ flavor ->id ]);
373+ }
374+
375+ private function deleteFlavor ()
376+ {
377+ $ replacements = ['{flavorId} ' => $ this ->flavorId ];
378+
379+ $ path = $ this ->sampleFile ($ replacements , 'flavors/delete_flavor.php ' );
380+ require_once $ path ;
381+
382+ $ this ->logStep ('Deleted flavor ID ' , ['ID ' => $ this ->flavorId ]);
383+ }
384+
243385 private function listFlavors ()
244386 {
245387 require_once $ this ->sampleFile ([], 'flavors/list_flavors.php ' );
@@ -314,4 +456,152 @@ private function deleteServerImage()
314456 require_once $ this ->sampleFile ($ replacements , 'images/delete_image.php ' );
315457 $ this ->logStep ('Deleted image {imageId} ' , $ replacements );
316458 }
459+
460+ private function listKeypairs ()
461+ {
462+ /** @var $keypairs \Generator */
463+ require_once $ this ->sampleFile ([], 'keypairs/list_keypairs.php ' );
464+
465+ $ this ->assertInstanceOf (\Generator::class, $ keypairs );
466+
467+ $ this ->logStep ('Listed all keypairs ' );
468+ }
469+
470+ private function createKeypair ()
471+ {
472+ $ replacements = [
473+ '{name} ' => $ this ->randomStr (),
474+ '{publicKey} ' => 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCp4H/vDGnLi0QgWgMsQkv//FEz0xgv/mujVX+XCh6fHXxc/PbaASY+MsoI2Xr238cG9eaeAAUvbpJuEuHQ0M9WX97bvsWaWzLQ9F6hzLAwUBGxcG8cSh1nB3Ah7alR2nbIZ1N94yE72hXLb1AGogJ97NBVIph438BCXUNejqoOBsXL8UBP3RGdPnTHJ/6XSMaNTQAJruQMoQwecyGFQmuS2IEy2mBOmSldD6JZirHpj7PTCKJY4CS89QChGpKIeOymKn4tEQQVVtNFUyULEMdin88H1yMftPfq7QqH+ULFT2X2XvP3CI+sESq84lrIcVu7LjJCRIwlKsnMu2ESYCdz foo@bar.com '
475+ ];
476+
477+ require_once $ this ->sampleFile ($ replacements , 'keypairs/create_keypair.php ' );
478+ /**@var Keypair $keypair */
479+
480+ $ this ->assertInstanceOf (Keypair::class, $ keypair );
481+ $ this ->assertEquals ($ replacements ['{name} ' ], $ keypair ->name );
482+ $ this ->assertEquals ($ replacements ['{publicKey} ' ], $ keypair ->publicKey );
483+
484+ $ this ->keypairName = $ keypair ->name ;
485+ $ this ->logStep ('Created keypair name {name} ' , ['{name} ' => $ keypair ->name ]);
486+ }
487+
488+ private function getKeypair ()
489+ {
490+ $ replacements = [
491+ '{name} ' => $ this ->keypairName ,
492+ ];
493+
494+ require_once $ this ->sampleFile ($ replacements , 'keypairs/get_keypair.php ' );
495+
496+ /**@var Keypair $keypair */
497+ $ this ->assertInstanceOf (Keypair::class, $ keypair );
498+
499+ $ this ->assertEquals ($ replacements ['{name} ' ], $ keypair ->name );
500+
501+ $ this ->logStep ('Retrieved details for keypair {name} ' , $ replacements );
502+ }
503+
504+ private function deleteKeypair ()
505+ {
506+ $ replacements = [
507+ '{name} ' => $ this ->keypairName ,
508+ ];
509+
510+ require_once $ this ->sampleFile ($ replacements , 'keypairs/delete_keypair.php ' );
511+ $ this ->logStep ('Deleted keypair name {name} ' , ['{name} ' => $ this ->keypairName ]);
512+ }
513+
514+ private function getLimits ()
515+ {
516+ require_once $ this ->sampleFile ([], 'limits/get_limits.php ' );
517+
518+ /**@var Limit $limit */
519+ $ this ->assertInstanceOf (Limit::class, $ limit );
520+
521+ $ this ->logStep ('Retrieved tenant limit ' );
522+ }
523+
524+ private function addSecurityGroupToServer ()
525+ {
526+ $ replacements = [
527+ '{serverId} ' => $ this ->serverId ,
528+ '{secGroupName} ' => 'default '
529+ ];
530+
531+ require_once $ this ->sampleFile ($ replacements , 'servers/add_security_group.php ' );
532+
533+ /**@var Server $server*/
534+ $ this ->logStep ('Added security group {secGroupName} to server {serverId} ' , $ replacements );
535+
536+ }
537+
538+ private function listServerSecurityGroups ()
539+ {
540+ $ replacements = [
541+ '{serverId} ' => $ this ->serverId
542+ ];
543+
544+ require_once $ this ->sampleFile ($ replacements , 'servers/list_security_groups.php ' );
545+
546+ /**@var \Generator $securityGroups */
547+ $ this ->assertInstanceOf (\Generator::class, $ securityGroups );
548+
549+ $ this ->logStep ('Listed all security groups attached to server {serverId} ' , $ replacements );
550+ }
551+
552+ private function removeServerSecurityGroup ()
553+ {
554+ $ replacements = [
555+ '{serverId} ' => $ this ->serverId ,
556+ '{secGroupName} ' => 'default '
557+ ];
558+
559+ require_once $ this ->sampleFile ($ replacements , 'servers/remove_security_group.php ' );
560+
561+ $ this ->logStep ('Delete security group {secGroupName} from server {serverId} ' , $ replacements );
562+ }
563+
564+ private function attachVolumeToServer ()
565+ {
566+
567+ $ replacements = [
568+ '{serverId} ' => $ this ->serverId ,
569+ '{volumeId} ' => $ this ->volume ->id
570+ ];
571+
572+ require_once $ this ->sampleFile ($ replacements , 'servers/attach_volume_attachment.php ' );
573+ /**@var VolumeAttachment $volumeAttachment */
574+ $ this ->volumeAttachmentId = $ volumeAttachment ->id ;
575+
576+ $ this ->volume ->waitUntil ('in-use ' );
577+
578+ $ this ->logStep ('Attached volume {volumeId} to server {serverId} with volume attachment id {volumeAttachmentId} ' ,
579+ array_merge ($ replacements , ['{volumeAttachmentId} ' => $ volumeAttachment ->id ])
580+ );
581+ }
582+
583+ private function listVolumeAttachmentsForServer ()
584+ {
585+ $ replacements = [
586+ '{serverId} ' => $ this ->serverId
587+ ];
588+
589+ require_once $ this ->sampleFile ($ replacements , 'servers/list_volume_attachments.php ' );
590+
591+ $ this ->logStep ('Retrieved volume attachments for server {serverId} ' , $ replacements );
592+ }
593+
594+ private function detachVolumeFromServer ()
595+ {
596+ $ replacements = [
597+ '{serverId} ' => $ this ->serverId ,
598+ '{volumeAttachmentId} ' => $ this ->volumeAttachmentId ,
599+ ];
600+
601+ require_once $ this ->sampleFile ($ replacements , 'servers/detach_volume_attachment.php ' );
602+
603+ $ this ->volume ->waitUntil ('available ' );
604+
605+ $ this ->logStep ('Detached volume attachments for server {serverId} ' , $ replacements );
606+ }
317607}
0 commit comments