-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathTestCase.php
More file actions
118 lines (97 loc) · 3.23 KB
/
TestCase.php
File metadata and controls
118 lines (97 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace OpenStack\Sample\Compute\v2;
use OpenStack\Compute\v2\Models\Server;
use OpenStack\Compute\v2\Service;
use OpenStack\Networking\v2\Models\Network;
use RuntimeException;
abstract class TestCase extends \OpenStack\Sample\TestCase
{
protected function getService(): Service
{
return $this->getCachedService(Service::class);
}
protected function getNetworkService(): \OpenStack\Networking\v2\Service
{
return $this->getCachedService(\OpenStack\Networking\v2\Service::class);
}
protected function searchImageId(): string
{
foreach ($this->getService()->listImages() as $image) {
if (str_starts_with($image->name, 'cirros')) {
return $image->id;
}
}
throw new RuntimeException('Unable to find image "cirros". Make sure this image is available for integration test.');
}
protected function sampleFile(string $path, array $replacements = []): string
{
return parent::sampleFile("Compute/v2/$path", $replacements);
}
/**
* Creates a server and all dependencies for testing
*/
protected function createServer(): Server
{
$flavorId = getenv('OS_FLAVOR');
if (!$flavorId) {
throw new RuntimeException('OS_FLAVOR env var must be set');
}
$network = $this->getNetworkService()->createNetwork(['name' => $this->randomStr()]);
$this->getNetworkService()->createSubnet(
[
'name' => $this->randomStr(),
'networkId' => $network->id,
'ipVersion' => 4,
'cidr' => '10.20.30.0/24',
]
);
$server = $this->getService()->createServer(
[
'name' => $this->randomStr(),
'imageId' => $this->searchImageId(),
'flavorId' => $flavorId,
'networks' => [
['uuid' => $network->id],
],
]
);
$server->waitUntilActive(300);
$this->assertEquals('ACTIVE', $server->status);
return $server;
}
/**
* Deletes server and all dependencies
*/
protected function deleteServer(Server $server): void
{
$server->retrieve();
$networks = array_keys($server->addresses);
$server->delete();
$server->waitUntilDeleted();
foreach ($networks as $networkName) {
$network = $this->getNetworkService()->listNetworks(['name' => $networkName])->current();
if ($network == null){
continue;
}
$this->deleteNetwork($network);
}
}
/**
* Deletes network and all dependencies
*/
protected function deleteNetwork(Network $network): void
{
foreach ($network->subnets as $subnetId) {
$subnet = $this->getNetworkService()->getSubnet($subnetId);
$subnet->delete();
}
foreach ($this->getNetworkService()->listPorts(['networkId' => $network->id]) as $port) {
if ($port->deviceOwner) {
continue;
}
$port->delete();
$port->waitUntilDeleted();
}
$network->delete();
}
}