| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package govcd |
| 9 |
|
| 10 |
import ( |
| 11 |
"net/url" |
| 12 |
"os" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/hmrc/vmware-govcd/testutil" |
| 16 |
. "gopkg.in/check.v1" |
| 17 |
) |
| 18 |
|
| 19 |
var au, _ = url.ParseRequestURI("http://localhost:4444/api") |
| 20 |
var aus, _ = url.ParseRequestURI("http://localhost:4444/api/vchs/services") |
| 21 |
var auc, _ = url.ParseRequestURI("http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000") |
| 22 |
var aucs, _ = url.ParseRequestURI("http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession") |
| 23 |
|
| 24 |
func Test(t *testing.T) { TestingT(t) } |
| 25 |
|
| 26 |
type S struct { |
| 27 |
client *VAClient |
| 28 |
vdc Vdc |
| 29 |
vapp *VApp |
| 30 |
} |
| 31 |
|
| 32 |
var _ = Suite(&S{}) |
| 33 |
|
| 34 |
var testServer = testutil.NewHTTPServer() |
| 35 |
|
| 36 |
var authheader = map[string]string{"x-vchs-authorization": "012345678901234567890123456789"} |
| 37 |
|
| 38 |
func (s *S) SetUpSuite(c *C) { |
| 39 |
testServer.Start() |
| 40 |
var err error |
| 41 |
|
| 42 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 43 |
|
| 44 |
s.client, err = NewVAClient() |
| 45 |
if err != nil { |
| 46 |
panic(err) |
| 47 |
} |
| 48 |
|
| 49 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 50 |
"/api/vchs/sessions": testutil.Response{201, authheader, vaauthorization}, |
| 51 |
"/api/vchs/services": testutil.Response{200, nil, vaservices}, |
| 52 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vacompute}, |
| 53 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{201, nil, vabackend}, |
| 54 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vdcExample}, |
| 55 |
}) |
| 56 |
|
| 57 |
s.vdc, err = s.client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 58 |
s.vapp = NewVApp(&s.client.Client) |
| 59 |
_ = testServer.WaitRequests(5) |
| 60 |
testServer.Flush() |
| 61 |
if err != nil { |
| 62 |
panic(err) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func (s *S) TearDownTest(c *C) { |
| 67 |
testServer.Flush() |
| 68 |
} |
| 69 |
|
| 70 |
func TestClient_vaauthorize(t *testing.T) { |
| 71 |
testServer.Start() |
| 72 |
var err error |
| 73 |
|
| 74 |
|
| 75 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 76 |
|
| 77 |
client, err := NewVAClient() |
| 78 |
if err != nil { |
| 79 |
t.Fatalf("err: %v", err) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
testServer.Response(201, authheader, vaauthorization) |
| 84 |
_, err = client.vaauthorize("username", "password") |
| 85 |
_ = testServer.WaitRequest() |
| 86 |
if err != nil { |
| 87 |
t.Fatalf("err: %v", err) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if client.VAToken != "012345678901234567890123456789" { |
| 92 |
t.Fatalf("VAtoken not set on client: %s", client.VAToken) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
testServer.Response(404, authheader, notfoundErr) |
| 99 |
_, err = client.vaauthorize("username", "password") |
| 100 |
_ = testServer.WaitRequest() |
| 101 |
if err == nil { |
| 102 |
t.Fatalf("Request error not caught: %v", err) |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
testServer.Response(500, authheader, vcdError) |
| 107 |
_, err = client.vaauthorize("username", "password") |
| 108 |
_ = testServer.WaitRequest() |
| 109 |
if err == nil { |
| 110 |
t.Fatalf("Request error not caught: %v", err) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
testServer.Response(200, authheader, vaauthorizationErr) |
| 115 |
_, err = client.vaauthorize("username", "password") |
| 116 |
_ = testServer.WaitRequest() |
| 117 |
if err == nil { |
| 118 |
t.Fatalf("Request error not caught: %v", err) |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
testServer.Response(200, authheader, notfoundErr) |
| 123 |
_, err = client.vaauthorize("username", "password") |
| 124 |
_ = testServer.WaitRequest() |
| 125 |
if err == nil { |
| 126 |
t.Fatalf("Request error not caught: %v", err) |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
func TestClient_vaacquireservice(t *testing.T) { |
| 132 |
testServer.Start() |
| 133 |
var err error |
| 134 |
|
| 135 |
|
| 136 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 137 |
client, err := NewVAClient() |
| 138 |
if err != nil { |
| 139 |
t.Fatalf("err: %v", err) |
| 140 |
} |
| 141 |
client.VAToken = "012345678901234567890123456789" |
| 142 |
|
| 143 |
|
| 144 |
testServer.Response(200, nil, vaservices) |
| 145 |
vacomputehref, err := client.vaacquireservice(*aus, "CI123456-789") |
| 146 |
_ = testServer.WaitRequest() |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("err: %v", err) |
| 149 |
} |
| 150 |
|
| 151 |
if vacomputehref.String() != "http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000" { |
| 152 |
t.Fatalf("VAComputeHREF not set on client: %s", vacomputehref) |
| 153 |
} |
| 154 |
|
| 155 |
if client.Region != "US - Anywhere" { |
| 156 |
t.Fatalf("Region not set on client: %s", client.Region) |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
testServer.Response(404, nil, notfoundErr) |
| 163 |
_, err = client.vaacquireservice(*aus, "CI123456-789") |
| 164 |
_ = testServer.WaitRequest() |
| 165 |
if err == nil { |
| 166 |
t.Fatalf("Request error not caught: %v", err) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
testServer.Response(500, nil, vcdError) |
| 171 |
_, err = client.vaacquireservice(*aus, "CI123456-789") |
| 172 |
_ = testServer.WaitRequest() |
| 173 |
if err == nil { |
| 174 |
t.Fatalf("Request error not caught: %v", err) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
testServer.Response(200, nil, vaservices) |
| 179 |
_, err = client.vaacquireservice(*aus, "NOTVALID-789") |
| 180 |
_ = testServer.WaitRequest() |
| 181 |
if err == nil { |
| 182 |
t.Fatalf("Request error not caught: %v", err) |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
testServer.Response(200, nil, notfoundErr) |
| 187 |
_, err = client.vaacquireservice(*aus, "CI123456-789") |
| 188 |
_ = testServer.WaitRequest() |
| 189 |
if err == nil { |
| 190 |
t.Fatalf("Request error not caught: %v", err) |
| 191 |
} |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
func TestClient_vaacquirecompute(t *testing.T) { |
| 196 |
testServer.Start() |
| 197 |
var err error |
| 198 |
|
| 199 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 200 |
client, err := NewVAClient() |
| 201 |
if err != nil { |
| 202 |
t.Fatalf("err: %v", err) |
| 203 |
} |
| 204 |
client.VAToken = "012345678901234567890123456789" |
| 205 |
client.Region = "US - Anywhere" |
| 206 |
|
| 207 |
testServer.Response(200, nil, vacompute) |
| 208 |
vavdchref, err := client.vaacquirecompute(*auc, "VDC12345-6789") |
| 209 |
_ = testServer.WaitRequest() |
| 210 |
if err != nil { |
| 211 |
t.Fatalf("err: %v", err) |
| 212 |
} |
| 213 |
|
| 214 |
if vavdchref.String() != "http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession" { |
| 215 |
t.Fatalf("VAVDCHREF not set on client: %s", vavdchref) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
testServer.Response(404, nil, notfoundErr) |
| 222 |
_, err = client.vaacquirecompute(*auc, "VDC12345-6789") |
| 223 |
_ = testServer.WaitRequest() |
| 224 |
if err == nil { |
| 225 |
t.Fatalf("Request error not caught: %v", err) |
| 226 |
} |
| 227 |
|
| 228 |
testServer.Response(500, nil, vcdError) |
| 229 |
_, err = client.vaacquirecompute(*auc, "VDC12345-6789") |
| 230 |
_ = testServer.WaitRequest() |
| 231 |
if err == nil { |
| 232 |
t.Fatalf("Request error not caught: %v", err) |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
testServer.Response(200, nil, vacompute) |
| 237 |
_, err = client.vaacquirecompute(*auc, "INVALID-6789") |
| 238 |
_ = testServer.WaitRequest() |
| 239 |
if err == nil { |
| 240 |
t.Fatalf("Request error not caught: %v", err) |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
testServer.Response(200, nil, notfoundErr) |
| 245 |
_, err = client.vaacquirecompute(*auc, "VDC12345-6789") |
| 246 |
_ = testServer.WaitRequest() |
| 247 |
if err == nil { |
| 248 |
t.Fatalf("Request error not caught: %v", err) |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
func TestClient_vagetbackendauth(t *testing.T) { |
| 254 |
testServer.Start() |
| 255 |
var err error |
| 256 |
|
| 257 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 258 |
client, err := NewVAClient() |
| 259 |
if err != nil { |
| 260 |
t.Fatalf("err: %v", err) |
| 261 |
} |
| 262 |
client.VAToken = "012345678901234567890123456789" |
| 263 |
client.Region = "US - Anywhere" |
| 264 |
|
| 265 |
testServer.Response(201, nil, vabackend) |
| 266 |
err = client.vagetbackendauth(*aucs, "CI123456-789") |
| 267 |
_ = testServer.WaitRequest() |
| 268 |
if err != nil { |
| 269 |
t.Fatalf("err: %v", err) |
| 270 |
} |
| 271 |
|
| 272 |
if client.Client.VCDToken != "01234567890123456789012345678901" { |
| 273 |
t.Fatalf("VCDToken not set on client: %s", client.Client.VCDToken) |
| 274 |
} |
| 275 |
if client.Client.VCDAuthHeader != "x-vcloud-authorization" { |
| 276 |
t.Fatalf("VCDAuthHeader not set on client: %s", client.Client.VCDAuthHeader) |
| 277 |
} |
| 278 |
if client.Client.VCDVDCHREF.String() != "http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000000" { |
| 279 |
t.Fatalf("VDC not set on client: %s", client.Client.VCDVDCHREF) |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
testServer.Response(404, nil, notfoundErr) |
| 286 |
err = client.vagetbackendauth(*aucs, "VDC12345-6789") |
| 287 |
_ = testServer.WaitRequest() |
| 288 |
if err == nil { |
| 289 |
t.Fatalf("Request error not caught: %v", err) |
| 290 |
} |
| 291 |
|
| 292 |
testServer.Response(500, nil, vcdError) |
| 293 |
err = client.vagetbackendauth(*aucs, "VDC12345-6789") |
| 294 |
_ = testServer.WaitRequest() |
| 295 |
if err == nil { |
| 296 |
t.Fatalf("Request error not caught: %v", err) |
| 297 |
} |
| 298 |
|
| 299 |
testServer.Response(201, nil, vabackend) |
| 300 |
err = client.vagetbackendauth(*aucs, "INVALID-6789") |
| 301 |
_ = testServer.WaitRequest() |
| 302 |
if err == nil { |
| 303 |
t.Fatalf("Request error not caught: %v", err) |
| 304 |
} |
| 305 |
|
| 306 |
testServer.Response(201, nil, notfoundErr) |
| 307 |
err = client.vagetbackendauth(*aucs, "VDC12345-6789") |
| 308 |
_ = testServer.WaitRequest() |
| 309 |
if err == nil { |
| 310 |
t.Fatalf("Request error not caught: %v", err) |
| 311 |
} |
| 312 |
|
| 313 |
testServer.Response(201, nil, vabackendErr) |
| 314 |
err = client.vagetbackendauth(*aucs, "VDC12345-6789") |
| 315 |
_ = testServer.WaitRequest() |
| 316 |
if err == nil { |
| 317 |
t.Fatalf("Request error not caught: %v", err) |
| 318 |
} |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
func TestClient_vaauthorize_env(t *testing.T) { |
| 325 |
|
| 326 |
os.Setenv("VCLOUDAIR_USERNAME", "username") |
| 327 |
os.Setenv("VCLOUDAIR_PASSWORD", "password") |
| 328 |
|
| 329 |
testServer.Start() |
| 330 |
var err error |
| 331 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 332 |
client, err := NewVAClient() |
| 333 |
if err != nil { |
| 334 |
t.Fatalf("err: %v", err) |
| 335 |
} |
| 336 |
|
| 337 |
testServer.Response(201, authheader, vaauthorization) |
| 338 |
_, err = client.vaauthorize("", "") |
| 339 |
_ = testServer.WaitRequest() |
| 340 |
if err != nil { |
| 341 |
t.Fatalf("err: %v", err) |
| 342 |
} |
| 343 |
|
| 344 |
if client.VAToken != "012345678901234567890123456789" { |
| 345 |
t.Fatalf("VAtoken not set on client: %s", client.VAToken) |
| 346 |
} |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
func TestClient_vaacquireservice_env(t *testing.T) { |
| 351 |
|
| 352 |
os.Setenv("VCLOUDAIR_COMPUTEID", "CI123456-789") |
| 353 |
|
| 354 |
testServer.Start() |
| 355 |
var err error |
| 356 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 357 |
client, err := NewVAClient() |
| 358 |
if err != nil { |
| 359 |
t.Fatalf("err: %v", err) |
| 360 |
} |
| 361 |
client.VAToken = "012345678901234567890123456789" |
| 362 |
|
| 363 |
testServer.Response(200, nil, vaservices) |
| 364 |
vacomputehref, err := client.vaacquireservice(*aus, "") |
| 365 |
_ = testServer.WaitRequest() |
| 366 |
if err != nil { |
| 367 |
t.Fatalf("err: %v", err) |
| 368 |
} |
| 369 |
|
| 370 |
if vacomputehref.String() != "http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000" { |
| 371 |
t.Fatalf("VAComputeHREF not set on client: %s", vacomputehref) |
| 372 |
} |
| 373 |
|
| 374 |
if client.Region != "US - Anywhere" { |
| 375 |
t.Fatalf("Region not set on client: %s", client.Region) |
| 376 |
} |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
func TestClient_vaacquirecompute_env(t *testing.T) { |
| 381 |
|
| 382 |
os.Setenv("VCLOUDAIR_VDCID", "VDC12345-6789") |
| 383 |
|
| 384 |
testServer.Start() |
| 385 |
var err error |
| 386 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 387 |
client, err := NewVAClient() |
| 388 |
if err != nil { |
| 389 |
t.Fatalf("err: %v", err) |
| 390 |
} |
| 391 |
client.VAToken = "012345678901234567890123456789" |
| 392 |
client.Region = "US - Anywhere" |
| 393 |
|
| 394 |
testServer.Response(200, nil, vacompute) |
| 395 |
vavdchref, err := client.vaacquirecompute(*auc, "") |
| 396 |
_ = testServer.WaitRequest() |
| 397 |
if err != nil { |
| 398 |
t.Fatalf("err: %v", err) |
| 399 |
} |
| 400 |
|
| 401 |
if vavdchref.String() != "http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession" { |
| 402 |
t.Fatalf("VAVDCHREF not set on client: %s", vavdchref) |
| 403 |
} |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
func TestClient_vagetbackendauth_env(t *testing.T) { |
| 408 |
|
| 409 |
os.Setenv("VCLOUDAIR_VDCID", "VDC12345-6789") |
| 410 |
|
| 411 |
testServer.Start() |
| 412 |
var err error |
| 413 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 414 |
client, err := NewVAClient() |
| 415 |
if err != nil { |
| 416 |
t.Fatalf("err: %v", err) |
| 417 |
} |
| 418 |
client.VAToken = "012345678901234567890123456789" |
| 419 |
client.Region = "US - Anywhere" |
| 420 |
|
| 421 |
testServer.Response(201, nil, vabackend) |
| 422 |
err = client.vagetbackendauth(*aucs, "") |
| 423 |
_ = testServer.WaitRequest() |
| 424 |
if err != nil { |
| 425 |
t.Fatalf("err: %v", err) |
| 426 |
} |
| 427 |
|
| 428 |
if client.Client.VCDToken != "01234567890123456789012345678901" { |
| 429 |
t.Fatalf("VCDToken not set on client: %s", client.Client.VCDToken) |
| 430 |
} |
| 431 |
if client.Client.VCDAuthHeader != "x-vcloud-authorization" { |
| 432 |
t.Fatalf("VCDAuthHeader not set on client: %s", client.Client.VCDAuthHeader) |
| 433 |
} |
| 434 |
if client.Client.VCDVDCHREF.String() != "http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000000" { |
| 435 |
t.Fatalf("VDC not set on client: %s", client.Client.VCDVDCHREF) |
| 436 |
} |
| 437 |
|
| 438 |
} |
| 439 |
|
| 440 |
func TestClient_NewClient(t *testing.T) { |
| 441 |
|
| 442 |
var err error |
| 443 |
os.Setenv("VCLOUDAIR_ENDPOINT", "") |
| 444 |
if _, err = NewVAClient(); err != nil { |
| 445 |
t.Fatalf("err: %v", err) |
| 446 |
} |
| 447 |
|
| 448 |
os.Setenv("VCLOUDAIR_ENDPOINT", "💩") |
| 449 |
if _, err = NewVAClient(); err == nil { |
| 450 |
t.Fatalf("err: %v", err) |
| 451 |
} |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
func TestClient_Disconnect(t *testing.T) { |
| 456 |
|
| 457 |
testServer.Start() |
| 458 |
var err error |
| 459 |
|
| 460 |
|
| 461 |
c := makeClient(t) |
| 462 |
testServer.Response(201, nil, "") |
| 463 |
err = c.Disconnect() |
| 464 |
if err != nil { |
| 465 |
t.Fatalf("err: %v", err) |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
client, err := NewVAClient() |
| 470 |
if err != nil { |
| 471 |
t.Fatalf("err: %v", err) |
| 472 |
} |
| 473 |
err = client.Disconnect() |
| 474 |
if err == nil { |
| 475 |
t.Fatalf("err: %v", err) |
| 476 |
} |
| 477 |
|
| 478 |
} |
| 479 |
|
| 480 |
func TestVAClient_Authenticate(t *testing.T) { |
| 481 |
|
| 482 |
testServer.Start() |
| 483 |
var err error |
| 484 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 485 |
client, err := NewVAClient() |
| 486 |
if err != nil { |
| 487 |
t.Fatalf("err: %v", err) |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 492 |
"/api/vchs/sessions": testutil.Response{401, nil, vcdError}, |
| 493 |
"/api/vchs/services": testutil.Response{200, nil, vaservices}, |
| 494 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vacompute}, |
| 495 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{201, nil, vabackend}, |
| 496 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vdcExample}, |
| 497 |
}) |
| 498 |
|
| 499 |
_, err = client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 500 |
_ = testServer.WaitRequests(1) |
| 501 |
testServer.Flush() |
| 502 |
if err == nil { |
| 503 |
t.Fatalf("Uncatched error: %v", err) |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 508 |
"/api/vchs/sessions": testutil.Response{201, authheader, vaauthorization}, |
| 509 |
"/api/vchs/services": testutil.Response{500, nil, vcdError}, |
| 510 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vacompute}, |
| 511 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{201, nil, vabackend}, |
| 512 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vdcExample}, |
| 513 |
}) |
| 514 |
|
| 515 |
_, err = client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 516 |
_ = testServer.WaitRequests(2) |
| 517 |
testServer.Flush() |
| 518 |
if err == nil { |
| 519 |
t.Fatalf("Uncatched error: %v", err) |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 524 |
"/api/vchs/sessions": testutil.Response{201, authheader, vaauthorization}, |
| 525 |
"/api/vchs/services": testutil.Response{200, nil, vaservices}, |
| 526 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{500, nil, vcdError}, |
| 527 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{201, nil, vabackend}, |
| 528 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vdcExample}, |
| 529 |
}) |
| 530 |
|
| 531 |
_, err = client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 532 |
_ = testServer.WaitRequests(3) |
| 533 |
testServer.Flush() |
| 534 |
if err == nil { |
| 535 |
t.Fatalf("Uncatched error: %v", err) |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 540 |
"/api/vchs/sessions": testutil.Response{201, authheader, vaauthorization}, |
| 541 |
"/api/vchs/services": testutil.Response{200, nil, vaservices}, |
| 542 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vacompute}, |
| 543 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{500, nil, vcdError}, |
| 544 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vdcExample}, |
| 545 |
}) |
| 546 |
|
| 547 |
_, err = client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 548 |
_ = testServer.WaitRequests(4) |
| 549 |
testServer.Flush() |
| 550 |
if err == nil { |
| 551 |
t.Fatalf("Uncatched error: %v", err) |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 556 |
"/api/vchs/sessions": testutil.Response{201, authheader, vaauthorization}, |
| 557 |
"/api/vchs/services": testutil.Response{200, nil, vaservices}, |
| 558 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vacompute}, |
| 559 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{201, nil, vabackend}, |
| 560 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{500, nil, vcdError}, |
| 561 |
}) |
| 562 |
|
| 563 |
_, err = client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 564 |
_ = testServer.WaitRequests(5) |
| 565 |
testServer.Flush() |
| 566 |
if err == nil { |
| 567 |
t.Fatalf("Uncatched error: %v", err) |
| 568 |
} |
| 569 |
|
| 570 |
} |
| 571 |
|
| 572 |
func makeClient(t *testing.T) VAClient { |
| 573 |
|
| 574 |
testServer.Start() |
| 575 |
var err error |
| 576 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 577 |
client, err := NewVAClient() |
| 578 |
if err != nil { |
| 579 |
t.Fatalf("err: %v", err) |
| 580 |
} |
| 581 |
|
| 582 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 583 |
"/api/vchs/sessions": testutil.Response{201, authheader, vaauthorization}, |
| 584 |
"/api/vchs/services": testutil.Response{200, nil, vaservices}, |
| 585 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vacompute}, |
| 586 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{201, nil, vabackend}, |
| 587 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vdcExample}, |
| 588 |
}) |
| 589 |
|
| 590 |
_, err = client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 591 |
|
| 592 |
_ = testServer.WaitRequests(5) |
| 593 |
testServer.Flush() |
| 594 |
|
| 595 |
if err != nil { |
| 596 |
t.Fatalf("err: %v", err) |
| 597 |
} |
| 598 |
|
| 599 |
if client.VAToken != "012345678901234567890123456789" { |
| 600 |
t.Fatalf("VAtoken not set on client: %s", client.VAToken) |
| 601 |
} |
| 602 |
|
| 603 |
if client.Region != "US - Anywhere" { |
| 604 |
t.Fatalf("Region not set on client: %s", client.Region) |
| 605 |
} |
| 606 |
|
| 607 |
if client.Client.VCDToken != "01234567890123456789012345678901" { |
| 608 |
t.Fatalf("VCDToken not set on client: %s", client.Client.VCDToken) |
| 609 |
} |
| 610 |
if client.Client.VCDAuthHeader != "x-vcloud-authorization" { |
| 611 |
t.Fatalf("VCDAuthHeader not set on client: %s", client.Client.VCDAuthHeader) |
| 612 |
} |
| 613 |
if client.Client.VCDVDCHREF.String() != "http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000000" { |
| 614 |
t.Fatalf("VDC not set on client: %s", client.Client.VCDVDCHREF) |
| 615 |
} |
| 616 |
|
| 617 |
return *client |
| 618 |
} |
| 619 |
|
| 620 |
func TestClient_parseErr(t *testing.T) { |
| 621 |
testServer.Start() |
| 622 |
var err error |
| 623 |
os.Setenv("VCLOUDAIR_ENDPOINT", "http://localhost:4444/api") |
| 624 |
client, err := NewVAClient() |
| 625 |
if err != nil { |
| 626 |
t.Fatalf("err: %v", err) |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 631 |
"/api/vchs/sessions": testutil.Response{201, authheader, vaauthorization}, |
| 632 |
"/api/vchs/services": testutil.Response{200, nil, vaservices}, |
| 633 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vacompute}, |
| 634 |
"/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession": testutil.Response{418, nil, notfoundErr}, |
| 635 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{200, nil, vdcExample}, |
| 636 |
}) |
| 637 |
|
| 638 |
_, err = client.Authenticate("username", "password", "CI123456-789", "VDC12345-6789") |
| 639 |
|
| 640 |
_ = testServer.WaitRequests(4) |
| 641 |
testServer.Flush() |
| 642 |
|
| 643 |
if err == nil { |
| 644 |
t.Fatalf("Uncatched error: %v", err) |
| 645 |
} |
| 646 |
|
| 647 |
} |
| 648 |
|
| 649 |
func TestClient_NewRequest(t *testing.T) { |
| 650 |
c := makeClient(t) |
| 651 |
|
| 652 |
params := map[string]string{ |
| 653 |
"foo": "bar", |
| 654 |
"baz": "bar", |
| 655 |
} |
| 656 |
|
| 657 |
uri, _ := url.ParseRequestURI("http://localhost:4444/api/bar") |
| 658 |
|
| 659 |
req := c.Client.NewRequest(params, "POST", *uri, nil) |
| 660 |
|
| 661 |
encoded := req.URL.Query() |
| 662 |
if encoded.Get("foo") != "bar" { |
| 663 |
t.Fatalf("bad: %v", encoded) |
| 664 |
} |
| 665 |
|
| 666 |
if encoded.Get("baz") != "bar" { |
| 667 |
t.Fatalf("bad: %v", encoded) |
| 668 |
} |
| 669 |
|
| 670 |
if req.URL.String() != "http://localhost:4444/api/bar?baz=bar&foo=bar" { |
| 671 |
t.Fatalf("bad base url: %v", req.URL.String()) |
| 672 |
} |
| 673 |
|
| 674 |
if req.Header.Get("x-vcloud-authorization") != "01234567890123456789012345678901" { |
| 675 |
t.Fatalf("bad auth header: %v", req.Header) |
| 676 |
} |
| 677 |
|
| 678 |
if req.Method != "POST" { |
| 679 |
t.Fatalf("bad method: %v", req.Method) |
| 680 |
} |
| 681 |
|
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
var notfoundErr = ` |
| 686 |
<html> |
| 687 |
<head><title>404 Not Found</title></head> |
| 688 |
<body bgcolor="white"> |
| 689 |
<center><h1>404 Not Found</h1></center> |
| 690 |
<hr><center>nginx/1.4.6 (Ubuntu)</center> |
| 691 |
</body> |
| 692 |
</html> |
| 693 |
` |
| 694 |
|
| 695 |
|
| 696 |
var vaauthorization = ` |
| 697 |
<?xml version="1.0" ?> |
| 698 |
<Session href="http://localhost:4444/api/vchs/session" type="application/xml;class=vnd.vmware.vchs.session" xmlns="http://www.vmware.com/vchs/v5.6" xmlns:tns="http://www.vmware.com/vchs/v5.6" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 699 |
<Link href="http://localhost:4444/api/vchs/services" rel="down" type="application/xml;class=vnd.vmware.vchs.servicelist"/> |
| 700 |
<Link href="http://localhost:4444/api/vchs/session" rel="remove"/> |
| 701 |
</Session> |
| 702 |
` |
| 703 |
var vaauthorizationErr = ` |
| 704 |
<?xml version="1.0" ?> |
| 705 |
<Session href="http://localhost:4444/api/vchs/session" type="application/xml;class=vnd.vmware.vchs.session" xmlns="http://www.vmware.com/vchs/v5.6" xmlns:tns="http://www.vmware.com/vchs/v5.6" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 706 |
<Link href="http://localhost:4444/api/vchs/session" rel="remove"/> |
| 707 |
</Session> |
| 708 |
` |
| 709 |
|
| 710 |
|
| 711 |
var vaservices = ` |
| 712 |
<?xml version="1.0" ?> |
| 713 |
<Services href="http://localhost:4444/api/vchs/services" type="application/xml;class=vnd.vmware.vchs.servicelist" xmlns="http://www.vmware.com/vchs/v5.6" xmlns:tns="http://www.vmware.com/vchs/v5.6" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 714 |
<Service href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000" region="US - Anywhere" serviceId="CI123456-789" serviceType="compute:vpc" type="application/xml;class=vnd.vmware.vchs.compute"/> |
| 715 |
</Services> |
| 716 |
` |
| 717 |
|
| 718 |
|
| 719 |
var vacompute = ` |
| 720 |
<?xml version="1.0" ?> |
| 721 |
<Compute href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000" serviceId="CI123456-789" serviceType="compute:vpc" type="application/xml;class=vnd.vmware.vchs.compute" xmlns="http://www.vmware.com/vchs/v5.6" xmlns:tns="http://www.vmware.com/vchs/v5.6" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 722 |
<Link href="http://localhost:4444/api/vchs/services" name="services" rel="up" type="application/xml;class=vnd.vmware.vchs.servicelist"/> |
| 723 |
<VdcRef href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000" name="VDC12345-6789" status="Active" type="application/xml;class=vnd.vmware.vchs.vdcref"> |
| 724 |
<Link href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession" name="VDC12345-6789" rel="down" type="application/xml;class=vnd.vmware.vchs.vcloudsession"/> |
| 725 |
</VdcRef> |
| 726 |
</Compute> |
| 727 |
` |
| 728 |
|
| 729 |
|
| 730 |
var vabackend = ` |
| 731 |
<?xml version="1.0" ?> |
| 732 |
<VCloudSession href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession" name="CI123456-789-session" type="application/xml;class=vnd.vmware.vchs.vcloudsession" xmlns="http://www.vmware.com/vchs/v5.6" xmlns:tns="http://www.vmware.com/vchs/v5.6" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 733 |
<Link href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000" name="vdc" rel="up" type="application/xml;class=vnd.vmware.vchs.vdcref"/> |
| 734 |
<VdcLink authorizationHeader="x-vcloud-authorization" authorizationToken="01234567890123456789012345678901" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000000" name="CI123456-789" rel="vcloudvdc" type="application/vnd.vmware.vcloud.vdc+xml"/> |
| 735 |
</VCloudSession> |
| 736 |
` |
| 737 |
|
| 738 |
|
| 739 |
var vabackendErr = ` |
| 740 |
<?xml version="1.0" ?> |
| 741 |
<VCloudSession href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000/vcloudsession" name="CI123456-789-session" type="application/xml;class=vnd.vmware.vchs.vcloudsession" xmlns="http://www.vmware.com/vchs/v5.6" xmlns:tns="http://www.vmware.com/vchs/v5.6" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 742 |
<Link href="http://localhost:4444/api/vchs/compute/00000000-0000-0000-0000-000000000000/vdc/00000000-0000-0000-0000-000000000000" name="vdc" rel="up" type="application/xml;class=vnd.vmware.vchs.vdcref"/> |
| 743 |
<VdcLink authorizationHeader="x-vcloud-authorization" authorizationToken="01234567890123456789012345678901" href="http://$£$%£%$:4444/api/vdc/00000000-0000-0000-0000-000000000000" name="CI123456-789" rel="vcloudvdc" type="application/vnd.vmware.vcloud.vdc+xml"/> |
| 744 |
</VCloudSession> |
| 745 |
` |
| 746 |
|
| 747 |
var vcdError = ` |
| 748 |
<Error xmlns="http://www.vmware.com/vcloud/v1.5" message="Error Message" majorErrorCode="500" minorErrorCode="Server Error" vendorSpecificErrorCode="NoSpecificError" stackTrace="Hello my name is Stack Trace"/> |
| 749 |
` |