forked from rackspace/php-opencloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smoketest.php
550 lines (472 loc) · 13.9 KB
/
smoketest.php
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<?php
/**
* (c)2012 Rackspace Hosting. See COPYING for license details
*
* The purpose of this smoketest is simply to ensure that the core
* functionality of the library is present. It is not an exhaustive
* integration test, nor is it a unit test. The goal is to rapidly
* identify major problems if a code change breaks something.
*
* @author Glen Campbell <[email protected]>
*/
$start = time();
if (strpos($_ENV['NOVA_URL'], 'staging.identity.api.rackspacecloud')) {
define('RAXSDK_SSL_VERIFYHOST', 0);
define('RAXSDK_SSL_VERIFYPEER', 0);
}
define('INSTANCENAME', 'SmokeTestInstance');
define('SERVERNAME', 'SmokeTestServer');
define('NETWORKNAME', 'SMOKETEST');
define('MYREGION', $_ENV['OS_REGION_NAME']);
define('VOLUMENAME', 'SmokeTestVolume');
define('VOLUMESIZE', 103);
define('LBNAME', 'SmokeTestLoadBalancer');
define('CACHEFILE', '/tmp/smoketest.credentials');
define('TESTDOMAIN', 'fipplefippleraxdrg.info');
define('RAXSDK_STRICT_PROPERTY_CHECKS', false);
require_once 'lib/php-opencloud.php';
/**
* Relies upon environment variable settings — these are the same environment
* variables that are used by python-novaclient. Just make sure that they're
* set to the right values before running this test.
*/
define('AUTHURL', $_ENV['NOVA_URL']);
define('USERNAME', $_ENV['OS_USERNAME']);
define('APIKEY', $_ENV['NOVA_API_KEY']);
$debug = new \OpenCloud\Base\Debug;
/**
* numbers each step
*/
function step($msg,$p1=NULL,$p2=NULL,$p3=NULL) {
global $STEPCOUNTER;
printf("\nStep %d. %s\n", ++$STEPCOUNTER, sprintf($msg,$p1,$p2,$p3));
}
function info($msg,$p1=NULL,$p2=NULL,$p3=NULL,$p4=NULL,$p5=NULL) {
printf(" %s\n", sprintf($msg,$p1,$p2,$p3,$p4,$p5));
}
define('TIMEFORMAT', 'r');
// parse command-line arguments
if ($argc > 1) {
foreach($argv as $arg) {
switch($arg) {
case '-D':
case '--debug':
printf("Debug ON\n");
$debug->setDebug(TRUE);
break;
case '-H':
case '--help':
printf(<<<ENDHELP
Switches:
-D --debug Turn debug mode ON
-H --help Display help message
ENDHELP
);
exit;
default:
}
}
}
if (!USERNAME || !APIKEY || !AUTHURL) {
die('No environment values set');
}
/**
* START THE TESTS!
*/
printf("SmokeTest started at %s\n", date(TIMEFORMAT, $start));
printf("Using endpoint [%s]\n", $_ENV['NOVA_URL']);
printf("Using region [%s]\n", MYREGION);
step('Authenticate');
$secret = array('username' => USERNAME, 'apiKey' => APIKEY);
$rackspace = new \OpenCloud\Rackspace(AUTHURL, $secret);
$rackspace->AppendUserAgent('(PHP SDK SMOKETEST)');
/**
* load cached credentials
*/
$fp = @fopen(CACHEFILE, 'r');
if (!$fp) { // no cached credentials
info('Saving credentials in %s', CACHEFILE);
$rackspace->Authenticate();
$cred = $rackspace->ExportCredentials();
$fp = @fopen(CACHEFILE, 'w');
if (!$fp)
die(sprintf("Cannot open cache file %s for writing\n", CACHEFILE));
fwrite($fp, serialize($cred));
fclose($fp);
}
else { // load cached credentials
info('Loading credentials from %s', CACHEFILE);
$str = fread($fp, 99999); // read it all
fclose($fp);
$rackspace->ImportCredentials(unserialize($str));
}
/**
* Cloud DNS
*/
step('Connect to Cloud DNS');
$dns = $rackspace->DNS();
step('Try to add a domain %s', TESTDOMAIN);
$domain = $dns->Domain();
$aresp = $domain->Create(array(
'name' => TESTDOMAIN,
'emailAddress' => '[email protected]',
'ttl' => 3600));
$aresp->WaitFor('COMPLETED', 300, 'dotter', 1);
if ($aresp->Status() == 'ERROR') {
info('Error condition (this may be valid if the domain exists');
info('[%d] %s - %s',
$aresp->error->code, $aresp->error->message, $aresp->error->details);
}
step("Adding a CNAME record www.%s", TESTDOMAIN);
$dlist = $dns->DomainList(array('name'=>TESTDOMAIN));
$domain = $dlist->Next();
$record = $domain->Record();
$aresp = $record->Create(array(
'type' => 'CNAME', 'ttl' => 600, 'name' => 'www.'.TESTDOMAIN,
'data' => 'developer.rackspace.com'));
$aresp->WaitFor('COMPLETED', 300, 'dotter', 1);
if ($aresp->Status() == 'ERROR') {
info('Error status:');
info('[%d] $s - %s', $aresp->error->code, $aresp->error->message,
$aresp->error->details);
}
step('List domains and records');
$dlist = $dns->DomainList();
while($domain = $dlist->Next()) {
info('%s [%s]',
$domain->Name(), $domain->emailAddress);
// list records
info('Domain Records:');
$rlist = $domain->RecordList();
while($rec = $rlist->Next()) {
info('- %s %d %s %s',
$rec->type, $rec->ttl, $rec->Name(), $rec->data);
}
}
/**
* Cloud Files
*/
step('Connect to Cloud Files');
$cloudfiles = $rackspace->ObjectStore('cloudFiles', MYREGION);
step('Create Container');
$container = $cloudfiles->Container();
$container->Create(array('name' => 'SmokeTestContainer'));
step('Create Object from this file');
$object = $container->DataObject();
$resp = $object->Create(
array('name'=>'SmokeTestObject','content_type'=>'text/plain'), __FILE__);
step('Publish Container to CDN');
$container->PublishToCDN(600); // 600-second TTL
info('CDN URL: %s', $container->CDNUrl());
info('Public URL: %s', $container->PublicURL());
info('Object Public URL: %s', $object->PublicURL());
info('Object SSL URL: %s', $object->PublicURL('SSL'));
info('Object Streaming URL: %s', $object->PublicURL('Streaming'));
step('Verify Object PublicURL (CDN)');
$url = $object->PublicURL();
system("curl -s -I $url|grep HTTP");
step('Copy Object');
$target = $container->DataObject();
$target->name = 'COPY-of-SmokeTestObject';
$object->Copy($target);
step('List Containers');
$list = $cloudfiles->ContainerList();
while($c = $list->Next())
info('Container: %s', $c->name);
step('List Objects in Container %s', $container->name);
$list = $container->ObjectList();
while($o = $list->Next())
info('Object: %s', $o->name);
step('Disable Container CDN');
$container->DisableCDN();
step('Delete Object');
$list = $container->ObjectList();
while($o = $list->Next()) {
info('Deleting: %s', $o->name);
$o->Delete();
}
step('Delete Container: %s', $container->name);
$container->Delete();
/**
* Cloud Load Balancers
*/
step('Connect to the Load Balancer Service');
$lbservice = $rackspace->LoadBalancerService('cloudLoadBalancers', MYREGION);
step('Create a Load Balancer');
$lb = $lbservice->LoadBalancer();
$lb->AddVirtualIp('public');
$lb->AddNode('192.168.0.1', 80);
$lb->AddNode('192.168.0.2', 80);
$response = $lb->Create(array(
'name' => LBNAME,
'protocol' => 'HTTP',
'port' => 80));
$lb->WaitFor('ACTIVE', 300, 'dotter');
step('Add a metadata item');
$met = $lb->Metadata();
$met->key = 'author';
$met->value = 'Glen Campbell';
$met->Create();
step('Add a public IPv6 address');
//setDebug(TRUE);
$lb->AddVirtualIp('PUBLIC', 6);
$debug->setDebug(FALSE);
// allowed domains
$adlist = $lbservice->AllowedDomainList();
while($ad = $adlist->Next()) {
info('Allowed domain: [%s]', $ad->Name());
}
// protocols
info('Protocols:');
$prolist = $lbservice->ProtocolList();
while($prot = $prolist->Next()) {
info(' %s %4d',
substr($prot->Name().'.....................',0,20), $prot->port);
}
// algorithms
info('Algorithms:');
$alist = $lbservice->AlgorithmList();
while($al = $alist->Next()) {
info(' %s', $al->Name());
}
// list load balancers
$list = $lbservice->LoadBalancerList();
if ($list->Size()) {
step('Load balancers:');
while($lb = $list->Next()) {
info('[%s] %s in %s', $lb->id, $lb->Name(), $lb->Region());
info(' Status: [%s]', $lb->Status());
// Nodes
$list = $lb->NodeList();
if ($list->Size() == 0)
info(' No nodes');
else {
while($node = $list->Next()) {
info(' Node: [%s] %s:%d %s/%s',
$node->Id(), $node->address, $node->port,
$node->condition, $node->status);
}
}
// NodeEvents
$list = $lb->NodeEventList();
if ($list->Size() == 0)
info(' No node events');
else {
while($event = $list->Next()) {
info(' * Event: %s (%s)',
$event->detailedMessage, $event->author);
}
}
// SSL Termination
try {
$ssl = $lb->SSLTermination();
info(' SSL terminated');
} catch (\OpenCloud\Base\Exceptions\InstanceNotFound $e) {
info(' No SSL termination');
}
// Metadata
$list = $lb->MetadataList();
while($meta = $list->Next()) {
info(' [Metadata #%s] %s=%s',
$meta->Id(), $meta->key, $meta->value);
}
}
}
else
step('There are no load balancers');
// list Billable LBs
$start = date('Y-m-d', time()-(3600*24*30));
$end = date('Y-m-d');
step('Billable Load Balancers from %s to %s', $start, $end);
$list = $lbservice->BillableLoadBalancerList(
TRUE,
array('startTime'=>$start, 'endTime'=>$end));
if ($list->Size() > 0) {
while($lb = $list->Next()) {
info('%10s %s', $lb->Id(), $lb->Name());
info('%10s created: %s', '', $lb->created->time);
}
}
else
info('None');
/**
* Cloud Servers
*/
try {
$USE_SERVERS=TRUE;
step('Connect to Cloud Servers');
$cloudservers = $rackspace->Compute('cloudServersOpenStack', MYREGION);
} catch (Exception $e) {
if (get_class($e) == 'OpenCloud\EndpointError')
$USE_SERVERS=FALSE;
}
if ($USE_SERVERS) {
step('List Flavors');
$flavorlist = $cloudservers->FlavorList();
$flavorlist->Sort('id');
while($f = $flavorlist->Next())
info('%s: %sMB', $f->name, $f->ram);
step('List Images');
$imagelist = $cloudservers->ImageList();
$imagelist->Sort('name');
while($i = $imagelist->Next()) {
info($i->name);
// save a CentOS image for later
if (!isset($centos) && isset($i->metadata->os_distro) &&
$i->metadata->os_distro == 'centos') {
$centos = $i;
}
}
step('Create Network');
$network = $cloudservers->Network();
try {
$network->Create(array('label'=>NETWORKNAME, 'cidr'=>'192.168.0.0/24'));
} catch (Exception $e) {}
step('List Networks');
$netlist = $cloudservers->NetworkList();
$netlist->Sort('label');
while($net = $netlist->Next())
info('%s: %s (%s)', $net->id, $net->label, $net->cidr);
step('Connect to the VolumeService');
$cbs = $rackspace->VolumeService('cloudBlockStorage', MYREGION);
step('Volume Types');
$list = $cbs->VolumeTypeList();
while($vtype = $list->Next()) {
info('%s - %s', $vtype->id, $vtype->name);
}
step('Create a new Volume');
$volume = $cbs->Volume();
$volume->Create(array(
'display_name' => VOLUMENAME,
'display_description' => 'A sample volume for testing',
'size' => VOLUMESIZE,
'volume_type' => $cbs->VolumeType(2)
));
$volume = $cbs->Volume($volume->id);
step('Listing volumes');
$list = $cbs->VolumeList();
while($vol = $list->Next()) {
info('Volume: %s %s [%s] size=%d',
$vol->id,
$vol->display_name,
$vol->display_description,
$vol->size);
}
step('Create Server');
$server = $cloudservers->Server();
$server->Create(array(
'name'=>'FOOBAR',
'image'=>$centos,
'flavor'=>$flavorlist->First(),
'networks'=>array($network, $cloudservers->Network(RAX_PUBLIC))
));
step('Wait for Server create');
$server->WaitFor('ACTIVE', 600, 'dotter');
// check for error
if ($server->Status() == 'ERROR')
die("Server create failed with ERROR\n");
step('Attach the volume');
$server->AttachVolume($volume);
$volume->WaitFor('in-use', 600, 'dotter');
step('Update the server name');
$server->Update(array('name'=>SERVERNAME));
$server->WaitFor('ACTIVE', 300, 'dotter');
step('Reboot Server');
$server->Reboot();
$server->WaitFor('ACTIVE', 300, 'dotter');
step('List Servers');
$list = $cloudservers->ServerList();
$list->Sort('name');
while($s = $list->Next())
info($s->name);
step('Listing the server volume attachments');
$list = $server->VolumeAttachmentList();
while($vol = $list->Next())
info('%s %-20s', $vol->id, $vol->Name());
//exit;
step('Detaching the volume');
$server->DetachVolume($volume);
$volume->WaitFor('available', 600, 'dotter');
step('Creating a snapshot');
$snap = $cbs->Snapshot(); // empty snapshot object
$snap->Create(array(
'display_name' => 'Smoketest Snapshot',
'volume_id' => $volume->id
));
step('Deleting the test server(s)');
$list = $cloudservers->ServerList();
while($s = $list->Next()) {
if ($s->name == SERVERNAME) {
info('Deleting %s', $s->id);
$s->Delete();
}
}
}
/**
* Cloud Databases
*/
step('Connect to Cloud Databases');
$dbaas = $rackspace->DbService('cloudDatabases', MYREGION, 'publicURL');
step('Get Database Flavors');
$dbflist = $dbaas->FlavorList();
$dbflist->Sort();
while($flavor = $dbflist->Next())
info('%s - %dM', $flavor->name, $flavor->ram);
step('Creating a Database Instance');
$instance = $dbaas->Instance();
$instance->name = INSTANCENAME;
$instance->flavor = $dbaas->Flavor(1);
$instance->volume->size = 1;
$instance->Create();
$instance->WaitFor('ACTIVE', 300, 'dotter');
step('Is the root user enabled?');
if ($instance->IsRootEnabled())
info('Yes');
else
info('No');
step('Creating a new database');
$db = $instance->Database();
$db->Create(array('name'=>'fooDb'));
step('Creating a new database user');
$user = $instance->User();
$user->Create(array('name'=>'FOO','password'=>'BAR','databases'=>array('fooDb')));
step('List database instances');
$dblist = $dbaas->InstanceList();
do {
while($dbitem = $dblist->Next())
info('Database Instance: %s (id %s)', $dbitem->name, $dbitem->id);
} while ($dblist = $dblist->NextPage());
step('Deleting the database user');
$user->Delete();
step('Deleting the database');
$db->Delete();
step('Deleting the test instance(s)');
$ilist = $dbaas->InstanceList();
while($inst = $ilist->Next())
if ($inst->name == INSTANCENAME) {
info('Deleting %s', $inst->id);
$inst->Delete();
}
/**
* Cleanup
*/
step('Deleting the test network(s)');
$list = $cloudservers->NetworkList();
while($network = $list->Next()) {
if ($network->label == NETWORKNAME) {
info('Deleting: %s %s', $network->id, $network->label);
$network->Delete();
}
}
step('FINISHED at %s in %d seconds', date(TIMEFORMAT), time()-$start);
info('Remember to manually delete the volume and snapshot created');
exit();
/**
* Callback for the WaitFor() method
*/
function dotter($obj) {
info('...waiting on %s/%-12s %4s',
$obj->Name(),
$obj->Status(),
isset($obj->progress) ? $obj->progress.'%' : 0);
}