forked from rackspace/php-opencloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenStackTest.php
More file actions
219 lines (213 loc) · 6.06 KB
/
OpenStackTest.php
File metadata and controls
219 lines (213 loc) · 6.06 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Unit Tests
*
* @copyright 2012-2013 Rackspace Hosting, Inc.
* See COPYING for licensing information
*
* @version 1.0.0
* @author Glen Campbell <glen.campbell@rackspace.com>
*/
require_once('openstack.inc');
require_once('stub_conn.inc'); // stub Connection class
if (!defined('TEST_DOMAIN')) define('TEST_DOMAIN', 'http://local.test');
/**
* stub classes for testing the request() method (which is overridden in the
* StubConnection class used for testing everything else).
*/
class TestingConnection extends OpenCloud\OpenStack {
public function GetHttpRequestObject($url, $method='GET') {
return new StubRequest($url, $method);
}
}
class OpenStackTest extends PHPUnit_Framework_TestCase
{
private $my; // my Connection
public function __construct() {
$this->my = new StubConnection(TEST_DOMAIN,
array('username'=>'Foo', 'password'=>'Bar'));
}
/**
* Tests
*/
public function test__construct() {
$this->assertEquals(
'StubConnection',
get_class($this->my));
}
/**
* @expectedException OpenCloud\DomainError
*/
public function test__options() {
$test = new StubConnection(TEST_DOMAIN,
array('username'=>'Foo', 'password'=>'Bar'),
'bad option');
}
public function testUrl() {
$this->assertEquals(TEST_DOMAIN.'/tokens', $this->my->Url());
}
public function testSecret() {
$arr = $this->my->Secret();
$this->assertEquals($arr['username'], 'Foo');
}
public function testToken() {
$this->assertEquals('TOKEN-ID', $this->my->Token());
}
public function testTenant() {
$this->assertEquals('TENANT-ID', $this->my->Tenant());
}
public function testExpiration() {
$this->assertEquals('978374510', $this->my->Expiration());
}
public function testServiceCatalog() {
$cat = $this->my->serviceCatalog();
$this->assertEquals('DFW', $cat[0]->endpoints[0]->region);
}
public function testCredentials() {
$this->assertRegExp(
'/"passwordCredentials"/',
$this->my->Credentials());
}
public function testAuthenticate() {
$this->my->Authenticate();
$this->assertEquals('TOKEN-ID', $this->my->Token());
}
/**
* Since the request() method is overridden in the StubConnection class,
* we need to get this one to use the real code.
*/
public function testRequest() {
$conn = new TestingConnection('http://example.com',
array('username'=>'Foo', 'password'=>'Bar'));
$response = $conn->Request('GOOD');
$this->assertEquals(200, $response->HttpStatus());
}
/**
* @expectedException OpenCloud\HttpUnauthorizedError
*/
public function test_request_2() {
$conn = new TestingConnection('http://example.com',
array('username'=>'Foo', 'password'=>'Bar'));
$response = $conn->Request('401');
$this->assertEquals(200, $response->HttpStatus());
}
/**
* @expectedException OpenCloud\HttpForbiddenError
*/
public function test_request_3() {
$conn = new TestingConnection('http://example.com',
array('username'=>'Foo', 'password'=>'Bar'));
$response = $conn->Request('403');
$this->assertEquals(200, $response->HttpStatus());
}
/**
* @expectedException OpenCloud\HttpOverLimitError
*/
public function test_request_4() {
$conn = new TestingConnection('http://example.com',
array('username'=>'Foo', 'password'=>'Bar'));
$response = $conn->Request('413');
$this->assertEquals(200, $response->HttpStatus());
}
public function testAppendUserAgent() {
$this->my->AppendUserAgent('FOOBAR');
$this->assertEquals(
RAXSDK_USER_AGENT.';FOOBAR',
$this->my->useragent);
}
public function testSetDefaults() {
$this->my->SetDefaults('Compute','cloudServersOpenStack','DFW');
$comp = $this->my->Compute();
$this->assertRegExp('/dfw.servers/', $comp->Url());
}
public function testSetTimeouts() {
$this->assertEquals(NULL, $this->my->SetTimeouts(10, 10));
}
public function testSetUploadProgressCallback() {
$this->my->SetUploadProgressCallback('foo');
}
public function testSetDownloadProgressCallback() {
$this->my->SetDownloadProgressCallback('bar');
}
public function test_read_cb() {
$fp = fopen('/dev/null', 'r');
$this->assertEquals(
'',
$this->my->_read_cb(NULL, $fp, 1024));
fclose($fp);
}
/**
* @expectedException OpenCloud\HttpUrlError
*/
public function test_write_cb() {
$ch = curl_init('file:/dev/null');
$len = $this->my->_write_cb($ch, 'FOOBAR');
$this->assertEquals(6, $len);
}
public function testExportCredentials() {
$this->my->Authenticate();
$arr = $this->my->ExportCredentials();
$this->assertEquals(
TRUE,
is_array($arr));
$this->assertEquals(
TRUE,
is_array($arr['catalog']));
}
public function testImportCredentials() {
$this->my->Authenticate();
$arr = $this->my->ExportCredentials();
$conn = new StubConnection(TEST_DOMAIN,
array('username'=>'Foo', 'password'=>'Bar'));
$conn->ImportCredentials($arr);
$this->assertEquals(
$arr['token'],
$conn->Token());
}
public function testObjectStore() {
$objs = $this->my->ObjectStore(
'cloudFiles',
'DFW',
'publicURL'
);
$this->assertEquals('OpenCloud\ObjectStore', get_class($objs));
}
public function testCompute1() {
$comp = $this->my->Compute(
'cloudServersOpenStack',
'DFW',
'publicURL'
);
$this->assertEquals('OpenCloud\Compute', get_class($comp));
}
/**
* @expectedException OpenCloud\ServiceValueError
*/
public function testCompute2() {
$comp = $this->my->Compute();
}
/**
* @expectedException OpenCloud\EndpointError
*/
public function testComputeFail() {
$comp = $this->my->Compute(
'FOOBAR',
'DFW',
'publicURL'
);
$this->assertEquals('OpenCloud\Compute', get_class($comp));
}
public function testVolumeService() {
$cbs = $this->my->VolumeService('cloudBlockStorage', 'DFW');
$this->assertEquals(
'OpenCloud\VolumeService',
get_class($cbs));
}
public function testServiceList() {
$list = $this->my->ServiceList();
while($item = $list->Next())
$this->assertEquals(
'OpenCloud\ServiceCatalogItem',
get_class($item));
}
}