2
2
3
3
namespace OpenStack \Integration \Compute \v2 ;
4
4
5
+ use OpenStack \BlockStorage \v2 \Models \Volume ;
5
6
use OpenStack \Compute \v2 \Models \Flavor ;
6
7
use OpenStack \Compute \v2 \Models \Image ;
8
+ use OpenStack \Compute \v2 \Models \Keypair ;
9
+ use OpenStack \Compute \v2 \Models \Limit ;
7
10
use OpenStack \Compute \v2 \Models \Server ;
8
11
use OpenCloud \Integration \TestCase ;
9
12
use 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 ;
10
17
11
18
class CoreTest extends TestCase
12
19
{
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
13
41
private $ service ;
14
42
private $ serverId ;
15
43
private $ adminPass ;
16
44
private $ imageId ;
45
+ private $ flavorId ;
46
+ private $ keypairName ;
47
+ private $ volumeAttachmentId ;
17
48
18
49
private function getService ()
19
50
{
@@ -24,6 +55,26 @@ private function getService()
24
55
return $ this ->service ;
25
56
}
26
57
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
+
27
78
private function searchImages ($ name )
28
79
{
29
80
foreach ($ this ->getService ()->listImages () as $ image ) {
@@ -36,11 +87,46 @@ private function searchImages($name)
36
87
$ this ->logger ->emergency ('No image found ' );
37
88
}
38
89
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
+
39
121
public function runTests ()
40
122
{
41
- $ this ->searchImages ('cirros ' );
42
123
$ this ->startTimer ();
43
124
125
+ // Manually trigger setUp
126
+ $ this ->setUp ();
127
+
128
+ $ this ->searchImages ('cirros ' );
129
+
44
130
// Servers
45
131
$ this ->createServer ();
46
132
@@ -57,7 +143,18 @@ public function runTests()
57
143
$ this ->createServerImage ();
58
144
$ this ->rebootServer ();
59
145
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
+
60
156
// Flavors
157
+ $ this ->createFlavor ();
61
158
$ this ->listFlavors ();
62
159
$ this ->getFlavor ();
63
160
@@ -66,20 +163,37 @@ public function runTests()
66
163
$ this ->getImage ();
67
164
$ this ->imageMetadata ();
68
165
$ 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
+
69
177
} finally {
70
178
// Teardown
71
179
$ this ->deleteServer ();
180
+ $ this ->deleteFlavor ();
181
+ $ this ->subnet ->delete ();
182
+ $ this ->network ->delete ();
183
+ $ this ->volume ->delete ();
72
184
}
73
185
74
186
$ this ->outputTimeTaken ();
75
187
}
76
188
77
189
private function createServer ()
78
190
{
191
+
79
192
$ replacements = [
80
193
'{serverName} ' => $ this ->randomStr (),
81
194
'{imageId} ' => $ this ->imageId ,
82
195
'{flavorId} ' => 1 ,
196
+ '{networkId} ' => $ this ->network ->id
83
197
];
84
198
85
199
/** @var $server \OpenStack\Compute\v2\Models\Server */
@@ -127,6 +241,8 @@ private function deleteServer()
127
241
$ path = $ this ->sampleFile ($ replacements , 'servers/delete_server.php ' );
128
242
require_once $ path ;
129
243
244
+ // Needed so that subnet and network can be removed
245
+ $ server ->waitUntilDeleted ();
130
246
$ this ->logStep ('Deleted server ID ' , ['ID ' => $ this ->serverId ]);
131
247
}
132
248
@@ -240,6 +356,32 @@ private function rebootServer()
240
356
$ this ->logStep ('Rebooted server {serverId} ' , $ replacements );
241
357
}
242
358
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
+
243
385
private function listFlavors ()
244
386
{
245
387
require_once $ this ->sampleFile ([], 'flavors/list_flavors.php ' );
@@ -314,4 +456,152 @@ private function deleteServerImage()
314
456
require_once $ this ->sampleFile ($ replacements , 'images/delete_image.php ' );
315
457
$ this ->logStep ('Deleted image {imageId} ' , $ replacements );
316
458
}
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
+ }
317
607
}
0 commit comments