Skip to content

Commit f249bc0

Browse files
author
Jamie Hannaford
committed
Fix tests
1 parent 00352cc commit f249bc0

File tree

10 files changed

+189
-17
lines changed

10 files changed

+189
-17
lines changed

tests/integration/BlockStorage/V2Test.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use OpenStack\BlockStorage\v2\Models\Volume;
77
use OpenStack\BlockStorage\v2\Models\VolumeType;
88
use OpenCloud\Integration\TestCase;
9-
use OpenCloud\Integration\Utils;
10-
use OpenStack\OpenStack;
9+
use OpenStack\Integration\Utils;
1110

1211
class V2Test extends TestCase
1312
{
@@ -19,7 +18,7 @@ class V2Test extends TestCase
1918
private function getService()
2019
{
2120
if (null === $this->service) {
22-
$this->service = (new OpenStack())->blockStorageV2(Utils::getAuthOpts());
21+
$this->service = Utils::getOpenStack()->blockStorageV2();
2322
}
2423

2524
return $this->service;

tests/integration/Compute/V2Test.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use OpenStack\Compute\v2\Models\Image;
77
use OpenStack\Compute\v2\Models\Server;
88
use OpenCloud\Integration\TestCase;
9-
use OpenCloud\Integration\Utils;
10-
use OpenStack\OpenStack;
9+
use OpenStack\Integration\Utils;
1110

1211
class V2Test extends TestCase
1312
{
@@ -19,7 +18,7 @@ class V2Test extends TestCase
1918
private function getService()
2019
{
2120
if (null === $this->service) {
22-
$this->service = (new OpenStack())->computeV2(Utils::getAuthOpts());
21+
$this->service = Utils::getOpenStack()->computeV2();
2322
}
2423

2524
return $this->service;

tests/integration/Identity/V3Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use OpenStack\Identity\v3\Models;
66
use OpenCloud\Integration\TestCase;
7-
use OpenCloud\Integration\Utils;
7+
use OpenStack\Integration\Utils;
88
use OpenStack\OpenStack;
99

1010
class V3Test extends TestCase
@@ -17,7 +17,7 @@ class V3Test extends TestCase
1717
private function getService()
1818
{
1919
if (null === $this->service) {
20-
$this->service = (new OpenStack())->identityV3(Utils::getAuthOpts());
20+
$this->service = Utils::getOpenStack()->identityV3();
2121
}
2222

2323
return $this->service;

tests/integration/Images/V2Test.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
use OpenStack\Images\v2\Models\Image;
66
use OpenStack\Images\v2\Models\Member;
77
use OpenCloud\Integration\TestCase;
8-
use OpenCloud\Integration\Utils;
9-
use OpenStack\OpenStack;
8+
use OpenStack\Integration\Utils;
109

1110
class V2Test extends TestCase
1211
{
@@ -18,7 +17,7 @@ class V2Test extends TestCase
1817
private function getService()
1918
{
2019
if (null === $this->service) {
21-
$this->service = (new OpenStack())->imagesV2(Utils::getAuthOpts());
20+
$this->service = Utils::getOpenStack()->imagesV2();
2221
}
2322

2423
return $this->service;

tests/integration/Networking/V2Test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace OpenStack\integration\Networking;
44

5+
use OpenStack\Integration\Utils;
56
use OpenStack\Networking\v2\Models\Network;
67
use OpenStack\Networking\v2\Models\Port;
78
use OpenStack\Networking\v2\Models\Subnet;
89
use OpenCloud\Integration\TestCase;
9-
use OpenStack\OpenStack;
1010

1111
class V2Test extends TestCase
1212
{
@@ -15,7 +15,7 @@ class V2Test extends TestCase
1515
private function getService()
1616
{
1717
if (null === $this->service) {
18-
$this->service = (new OpenStack())->networkingV2(['region' => getenv('OS_REGION')]);
18+
$this->service = Utils::getOpenStack()->networkingV2();
1919
}
2020

2121
return $this->service;

tests/integration/ObjectStore/V1Test.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace OpenStack\integration\ObjectStore;
44

55
use OpenCloud\Integration\TestCase;
6-
use OpenCloud\Integration\Utils;
7-
use OpenStack\OpenStack;
6+
use OpenStack\Integration\Utils;
87
use Psr\Http\Message\StreamInterface;
98

109
class V1Test extends TestCase
@@ -17,7 +16,7 @@ class V1Test extends TestCase
1716
protected function getService()
1817
{
1918
if (null === $this->service) {
20-
$this->service = (new OpenStack())->objectStoreV1(Utils::getAuthOpts());
19+
$this->service = Utils::getOpenStack()->objectStoreV1();
2120
}
2221

2322
return $this->service;

tests/integration/SampleManager.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace OpenStack\Integration;
4+
5+
use OpenCloud\Integration\SampleManagerInterface;
6+
7+
class SampleManager implements SampleManagerInterface
8+
{
9+
protected $basePath;
10+
protected $paths = [];
11+
protected $verbosity;
12+
13+
public function __construct($basePath, $verbosity)
14+
{
15+
$this->basePath = $basePath;
16+
$this->verbosity = $verbosity;
17+
}
18+
19+
public function deletePaths()
20+
{
21+
if (!empty($this->paths)) {
22+
foreach ($this->paths as $path) {
23+
unlink($path);
24+
}
25+
}
26+
}
27+
28+
protected function getGlobalReplacements()
29+
{
30+
return [
31+
'{userId}' => getenv('OS_USER_ID'),
32+
'{username}' => getenv('OS_USERNAME'),
33+
'{password}' => getenv('OS_PASSWORD'),
34+
'{domainId}' => getenv('OS_DOMAIN_ID'),
35+
'{authUrl}' => getenv('OS_AUTH_URL'),
36+
'{tenantId}' => getenv('OS_TENANT_ID'),
37+
'{region}' => getenv('OS_REGION'),
38+
'{projectId}' => getenv('OS_PROJECT_ID'),
39+
'{projectName}' => getenv('OS_PROJECT_NAME'),
40+
];
41+
}
42+
43+
protected function getConnectionTemplate()
44+
{
45+
if ($this->verbosity === 1) {
46+
$subst = <<<'EOL'
47+
use OpenCloud\Integration\DefaultLogger;
48+
use OpenStack\Integration\Utils;
49+
use GuzzleHttp\MessageFormatter;
50+
51+
$options = [
52+
'debugLog' => true,
53+
'logger' => new DefaultLogger(),
54+
'messageFormatter' => new MessageFormatter(),
55+
];
56+
$openstack = new OpenStack\OpenStack(Utils::getAuthOpts($options));
57+
EOL;
58+
} elseif ($this->verbosity === 2) {
59+
$subst = <<<'EOL'
60+
use OpenCloud\Integration\DefaultLogger;
61+
use OpenStack\Integration\Utils;
62+
use GuzzleHttp\MessageFormatter;
63+
64+
$options = [
65+
'debugLog' => true,
66+
'logger' => new DefaultLogger(),
67+
'messageFormatter' => new MessageFormatter(MessageFormatter::DEBUG),
68+
];
69+
$openstack = new OpenStack\OpenStack(Utils::getAuthOpts($options));
70+
EOL;
71+
} else {
72+
$subst = <<<'EOL'
73+
use OpenStack\Integration\Utils;
74+
75+
$openstack = new OpenStack\OpenStack(Utils::getAuthOpts());
76+
EOL;
77+
}
78+
79+
return $subst;
80+
}
81+
82+
public function write($path, array $replacements)
83+
{
84+
$replacements = array_merge($this->getGlobalReplacements(), $replacements);
85+
86+
$sampleFile = rtrim($this->basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $path;
87+
88+
if (!file_exists($sampleFile) || !is_readable($sampleFile)) {
89+
throw new \RuntimeException(sprintf("%s either does not exist or is not readable", $sampleFile));
90+
}
91+
92+
$content = strtr(file_get_contents($sampleFile), $replacements);
93+
$content = str_replace("'vendor/'", "'" . dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . "vendor'", $content);
94+
95+
$subst = $this->getConnectionTemplate();
96+
$content = preg_replace('/\([^)]+\)/', '', $content, 1);
97+
$content = str_replace('$openstack = new OpenStack\OpenStack;', $subst, $content);
98+
99+
$tmp = tempnam(sys_get_temp_dir(), 'openstack');
100+
file_put_contents($tmp, $content);
101+
102+
$this->paths[] = $tmp;
103+
104+
return $tmp;
105+
}
106+
}

tests/integration/Utils.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace OpenStack\Integration;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\HandlerStack;
7+
use OpenStack\Identity\v2\Service;
8+
use OpenCloud\Common\Transport\Utils as TransportUtils;
9+
use OpenStack\OpenStack;
10+
11+
class Utils extends \OpenCloud\Integration\Utils
12+
{
13+
public static function getOpenStack(): OpenStack
14+
{
15+
return new OpenStack(self::getAuthOpts());
16+
}
17+
18+
public static function getAuthOptsV3()
19+
{
20+
return [
21+
'authUrl' => getenv('OS_AUTH_URL'),
22+
'region' => getenv('OS_REGION_NAME'),
23+
'user' => [
24+
'id' => getenv('OS_USER_ID'),
25+
'password' => getenv('OS_PASSWORD'),
26+
],
27+
'scope' => [
28+
'project' => [
29+
'id' => getenv('OS_PROJECT_ID'),
30+
]
31+
]
32+
];
33+
}
34+
35+
public static function getAuthOptsV2()
36+
{
37+
$httpClient = new Client([
38+
'base_uri' => TransportUtils::normalizeUrl(getenv('OS_AUTH_URL')),
39+
'handler' => HandlerStack::create(),
40+
]);
41+
42+
return [
43+
'authUrl' => getenv('OS_AUTH_URL'),
44+
'region' => getenv('OS_REGION_NAME'),
45+
'username' => getenv('OS_USERNAME'),
46+
'password' => getenv('OS_PASSWORD'),
47+
'tenantName' => getenv('OS_TENANT_NAME'),
48+
'identityService' => Service::factory($httpClient),
49+
];
50+
}
51+
52+
public static function getAuthOpts(array $options = [])
53+
{
54+
$authOptions = getenv('OS_IDENTITY_API_VERSION') == '2.0'
55+
? self::getAuthOptsV2()
56+
: self::getAuthOptsV3();
57+
58+
return array_merge($authOptions, $options);
59+
}
60+
}

tests/integration/run.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$rootDir = dirname(dirname(__DIR__));
4+
5+
require_once $rootDir . '/vendor/autoload.php';
6+
7+
$basePath = $rootDir . '/samples';
8+
9+
$runner = new \OpenCloud\Integration\Runner($basePath, 'OpenStack\\Integration');
10+
$runner->runServices();

tests/unit/OpenStackTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class OpenStackTest extends TestCase
1616
public function setUp()
1717
{
1818
$this->builder = $this->prophesize(Builder::class);
19-
$this->openstack = new OpenStack([], $this->builder->reveal());
19+
$this->openstack = new OpenStack(['authUrl' => ''], $this->builder->reveal());
2020
}
2121

2222
public function test_it_supports_compute_v2()

0 commit comments

Comments
 (0)