-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
218 lines (197 loc) · 7.26 KB
/
test.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
<?php
// ================ Setup Data
define('USERNAME','');
define('APIKEY','');
define('REGION','ORD');
$lb_config = array(
'base' => 'stroz-test-',
'port' => 80,
'protocol' => 'HTTP',
'nodes' => array('10.180.175.174'),
'vip' => 'PUBLIC'
);
$ts = time();
// ================= Configure tests to run:
// authenticate => required, authenticate to API
// create => optional, create and delete a Load Balancer
// list => optional, test listing of LB/LB Details
// info => optional, get protocol and algorithm lists
function dotest($testname) {
$tests = array('authenticate','create','list','info');
return (array_search($testname,$tests) === FALSE ? FALSE : TRUE);
}
// ================= Set the Debug Level
define('DEBUG',0);
// ================= Require the use of the library files we need
// First, make sure that the document root (where php-cloudservers lives) is available to us
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
require('php-cloudservers/authentication.php');
require('create.php');
require('list.php');
require('delete.php');
?>
<html>
<head>
<title>php-loadbalancers test suite</title>
<style type="text/css">
.fail {
color: red;
}
.pass {
color: green;
}
</style>
</head>
<body>
<?php
if (dotest('authenticate')) {
print "<h1>Authentication</h1>";
$auth = authenticate(USERNAME, APIKEY);
if (DEBUG > 5) print $auth['Status'];
if ( $auth["Status"] == "Success" ) {
print "<p class=\"pass\">Authentication Succeeded</p>";
$xauthtoken = $auth['XAuthToken'];
preg_match("(ord|dfw|lon)",$auth['XStorageUrl'],$regions);
$region = $regions[0];
$accountid = basename($auth['XServerManagementUrl']);
print "<ul><li>Auth Token: $xauthtoken</li>";
print "<li>Region: $region</li>";
print "<li>Account ID: $accountid</li>";
print "</ul></p>";
} else {
print "<p class=\"fail\">Authentication Failed, Is your username and api key right?</p>";
exit;
}
}
flush();
if (dotest('create')) {
print "<h1>Creating a Load Balancer</h1>";
$lb = create($xauthtoken, $region, $accountid, $lb_config['base'] . $ts, $lb_config['port'], $lb_config['protocol'], $lb_config['nodes'], $lb_config['vip']);
if ( !array_key_exists("Status", $lb) && $lb['name'] == $lb_config['base'] . $ts ) {
// == Save this LB as $todelete so we can clean up ==
$todelete = $lb['id'];
print "<p class=\"pass\">Load Balancer Creation Succeeded</p>";
print "<ul><li>Load Balancer: {$lb['name']} - ID: {$lb['id']} - Status: {$lb['status']}</li>";
print "<li>Protocol: {$lb['protocol']} on port {$lb['port']}</li>";
print "<li>Building in: {$lb['cluster']['name']} with IPs:";
print "<ul>";
foreach ( $lb['virtualIps'] as $ip )
print "<li>{$ip['ipVersion']}: {$ip['address']}</li>";
print "</ul>";
print "<li>Pointed at nodes:";
foreach ( $lb['nodes'] as $node ) print " {$node['address']}";
print "</li></ul>";
} else {
print "<p class=\"fail\">Load Balancer Creation Failed, {$lb['message']}</p>";
exit;
}
}
flush();
if (dotest('list')) {
print "<h1>Listing Load Balancers</h1>";
print "<h2>Load Balancers on account $accountid</h2>";
$lblist = list_loadbalancers($xauthtoken, $region, $accountid);
if (DEBUG > 5) print_r ($lblist);
if ( !array_key_exists("Status", $lblist)) {
print "<p class=\"pass\">Load Balancers:</p>";
foreach ($lblist as $lb) {
print "<table class=\"lbarray\">";
foreach ($lb as $key => $value) {
if ( $key == 'created' || $key == 'updated' )
$value = $value['time'];
elseif ( $key == 'virtualIps' ) {
$v = '';
foreach ( $value as $vip )
$v .= $vip['address'] . '<br />';
$value = $v;
}
print "<tr><td>$key</td><td>$value</td></tr>";
}
print "</table>";
}
} else {
print "<p class=\"fail\">Load Balancer Listing Failed, {$lblist['message']}</p>";
exit;
}
print "<h2>Details for Load Balancer #$todelete</h2>";
$lb = list_loadbalancer_details($xauthtoken, $region, $accountid, $todelete);
if (DEBUG > 5) print_r ($lb);
if ( !array_key_exists("Status", $lblist)) {
print "<p class=\"pass\">Load Balancer:</p>";
print "<table class=\"lbarray\">";
foreach ($lb as $key => $value) {
if ( $key == 'created' || $key == 'updated' )
$value = $value['time'];
elseif ( $key == 'cluster' )
$value = $value['name'];
elseif ( $key == 'connectionLogging' )
$value = ($value['ENABLED'] != '' ? "Enabled" : "Disabled");
elseif ( $key == 'virtualIps' || $key == 'nodes' ) {
$v = '';
foreach ( $value as $vip )
$v .= $vip['address'] . '<br />';
$value = $v;
}
print "<tr><td>$key</td><td>$value</td></tr>";
}
print "</table>";
} else {
print "<p class=\"fail\">Load Balancer Listing Failed, {$lb['message']}</p>";
}
}
flush();
if (dotest('info')) {
print "<h1>Semi-Static Content GETs</h1>";
print "<h2>List Available Protocols</h2>";
$protocols = list_protocols($xauthtoken, $region, $accountid);
if (DEBUG > 5) print_r( $protocols );
if ( !array_key_exists("Status", $protocols)) {
print "<table class=\"lbarray\">";
print "<tr><th>Protocol Name</th><th>Default Port</th></tr>\n";
foreach ($protocols as $protocol)
print "<tr><td>{$protocol['name']}</td><td>{$protocol['port']}</td></tr>\n";
print "</table>";
}
print "<h2>List Available Algorithms</h2>";
$algorithms = list_algorithms($xauthtoken, $region, $accountid);
if (DEBUG > 5) print_r( $algorithms );
if ( !array_key_exists("Status", $algorithms)) {
print "<p>Available Algorithms: \n<ul>";
foreach ($algorithms as $algorithm)
print "<li>{$algorithm['name']}</li>\n";
print "</ul></p>";
}
}
flush();
if (dotest('create')) {
// Part 2 of 'create' time to cleanup $todelete
print "<h2>Waiting for Load Balancer #$todelete to finish Build (to delete)</h2><ul>";
while (true) {
$lb = list_loadbalancer_details($xauthtoken, $region, $accountid, $todelete);
flush();
print "<li>";
if ($lb['Status'] !== "Failure" && $lb['status'] != 'ACTIVE') {
print "Load Balancer is still in Build Status: Waiting 60 seconds... ";
flush();
sleep(30);
print "30 seconds";
flush();
sleep(30);
} else if ($lb['Status'] === 'Failure') {
die("Failed!?");
} else {
print "Success!</li></ul>";
break;
}
print "</li>";
}
$response = remove($xauthtoken, $region, $accountid, $todelete);
if (DEBUG > 0) print_r ($response);
if ($response['status'] == "success") {
print "<p class=\"pass\">Removed Load Balancer successfully</p>";
} else {
print "<p class=\"fail\">Load Balancer: $todelete is not deleted</p>";
}
}
/* vi: ts=4 et */
?>