OpenStackTest.php
6.07 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* PHP OpenCloud library.
*
* @copyright 2014 Rackspace Hosting, Inc. See LICENSE for information.
* @license https://www.apache.org/licenses/LICENSE-2.0
* @author Jamie Hannaford <jamie.hannaford@rackspace.com>
* @author Glen Campbell <glen.campbell@rackspace.com>
*/
namespace OpenCloud\Tests;
use OpenCloud\OpenStack;
use OpenCloud\Rackspace;
class OpenStackTest extends \PHPUnit_Framework_TestCase
{
private $client;
private $credentials = array('username' => 'foo', 'password' => 'bar', 'tenantName' => 'baz');
public function __construct()
{
$this->client = new OpenStack(Rackspace::US_IDENTITY_ENDPOINT, $this->credentials);
$this->client->addSubscriber(new MockSubscriber());
}
public function test__construct()
{
$client = new OpenStack(Rackspace::US_IDENTITY_ENDPOINT, $this->credentials);
}
public function test_Credentials()
{
$client = clone $this->client;
$this->assertEquals($this->credentials, $client->getSecret());
$this->assertEquals(
json_encode(array('auth' => array('passwordCredentials' => array('username' => 'foo', 'password' => 'bar'), 'tenantName' => 'baz'))),
$client->getCredentials()
);
$client->setSecret(array(
'username' => 'foo', 'password' => 'bar', 'tenantId' => 'baz'
));
$this->assertEquals(
json_encode(array('auth' => array('passwordCredentials' => array('username' => 'foo', 'password' => 'bar'), 'tenantId' => 'baz'))),
$client->getCredentials()
);
}
public function test_Auth_Methods()
{
$this->client->authenticate();
// catalog
$this->assertInstanceOf('OpenCloud\Common\Service\Catalog', $this->client->getCatalog());
// token
$this->assertNotNull('string', gettype($this->client->getToken()));
$this->assertInstanceOf('OpenCloud\Identity\Resource\Token', $this->client->getTokenObject());
$this->assertEquals($this->client->getTokenObject()->getExpires(), $this->client->getExpiration());
$this->assertEquals($this->client->getTokenObject()->hasExpired(), $this->client->hasExpired());
// tenant
$this->assertEquals('string', gettype($this->client->getTenant()));
$this->assertInstanceOf('OpenCloud\Identity\Resource\Tenant', $this->client->getTenantObject());
// misc
$this->assertNotNull($this->client->getUrl());
}
public function test_Logger()
{
$this->assertInstanceOf(
'OpenCloud\Common\Log\LoggerInterface',
$this->client->getLogger()
);
}
/**
* @expectedException OpenCloud\Common\Exceptions\CredentialError
*/
public function test_Credentials_Fail()
{
$client = new OpenStack(Rackspace::US_IDENTITY_ENDPOINT, array());
$client->getCredentials();
}
public function test_Auth_Url()
{
$this->assertEquals(Rackspace::US_IDENTITY_ENDPOINT, (string) $this->client->getAuthUrl());
$this->client->setAuthUrl(Rackspace::UK_IDENTITY_ENDPOINT);
$this->assertEquals(Rackspace::UK_IDENTITY_ENDPOINT, (string) $this->client->getAuthUrl());
}
public function test_Factory_Methods()
{
$this->assertInstanceOf(
'OpenCloud\Compute\Service',
$this->client->computeService('cloudServersOpenStack', 'DFW')
);
$this->assertInstanceOf(
'OpenCloud\ObjectStore\Service',
$this->client->objectStoreService('cloudFiles', 'DFW')
);
$this->assertInstanceOf(
'OpenCloud\Volume\Service',
$this->client->volumeService('cloudBlockStorage', 'DFW')
);
}
public function test_User_Agent()
{
$this->assertEquals($this->client->getUserAgent(), $this->client->getDefaultUserAgent());
}
public function test_Export_Credentials()
{
$this->client->setExpiration(time() - 1);
$credentials = $this->client->exportCredentials();
$this->assertTrue(is_array($credentials));
$this->assertCount(4, $credentials);
}
public function test_Import_Credentials()
{
$this->client->importCredentials(array(
'token' => '{token}',
'expiration' => '{expiration}',
'tenant' => '{tenant}',
'catalog' => array(
(object) array(
'endpoints' => array(
(object) array(
'region' => 'DFW',
'publicURL' => 'foo',
'internalURL' => 'bar'
)
),
'name' => 'testService',
'type' => 'someServiceType'
)
)
));
$this->assertEquals('{token}', $this->client->getToken());
$this->assertEquals('{expiration}', $this->client->getExpiration());
$this->assertEquals('{tenant}', $this->client->getTenant());
}
public function test_Import_Credentials_Numeric_Tenant()
{
$randomNumericTenant = (string) mt_rand();
$this->client->importCredentials(array(
'token' => '{token}',
'expiration' => '{expiration}',
'tenant' => $randomNumericTenant,
'catalog' => array(
(object) array(
'endpoints' => array(
(object) array(
'region' => 'DFW',
'publicURL' => 'foo',
'internalURL' => 'bar'
)
),
'name' => 'testService',
'type' => 'someServiceType'
)
)
));
$this->assertEquals('{token}', $this->client->getToken());
$this->assertEquals('{expiration}', $this->client->getExpiration());
$this->assertEquals($randomNumericTenant, $this->client->getTenant());
}
}