github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/openstack/series_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package openstack 5 6 import ( 7 "bytes" 8 "fmt" 9 "html/template" 10 "strings" 11 12 "github.com/go-goose/goose/v5/identity" 13 "github.com/go-goose/goose/v5/nova" 14 "github.com/go-goose/goose/v5/swift" 15 "github.com/juju/errors" 16 17 corebase "github.com/juju/juju/core/base" 18 "github.com/juju/juju/core/constraints" 19 "github.com/juju/juju/core/network" 20 "github.com/juju/juju/environs" 21 "github.com/juju/juju/environs/imagemetadata" 22 "github.com/juju/juju/environs/instances" 23 "github.com/juju/juju/environs/simplestreams" 24 envstorage "github.com/juju/juju/environs/storage" 25 envtesting "github.com/juju/juju/environs/testing" 26 ) 27 28 // MetadataStorage returns a Storage instance which is used to store simplestreams metadata for tests. 29 func MetadataStorage(e environs.Environ) envstorage.Storage { 30 env := e.(*Environ) 31 ecfg := env.ecfg() 32 container := "juju-dist-test" 33 34 factory := NewClientFactory(env.cloud(), ecfg) 35 newClient, err := factory.getClientState() 36 if err != nil { 37 panic(fmt.Errorf("cannot create %s container: %v", container, err)) 38 } 39 40 metadataStorage := &openstackstorage{ 41 containerName: container, 42 swift: swift.New(newClient), 43 } 44 45 // Ensure the container exists. 46 err = metadataStorage.makeContainer(container, swift.PublicRead) 47 if err != nil { 48 panic(fmt.Errorf("cannot create %s container: %v", container, err)) 49 } 50 return metadataStorage 51 } 52 53 func InstanceAddress(publicIP string, addresses map[string][]nova.IPAddress) string { 54 addr, _ := convertNovaAddresses(publicIP, addresses).OneMatchingScope(network.ScopeMatchPublic) 55 return addr.Value 56 } 57 58 // Include images for arches currently supported. i386 is no longer 59 // supported, so it can be excluded. 60 // TODO (stickupkid): Refactor this to actually build this for the given LTS. 61 var indexData = ` 62 { 63 "index": { 64 "com.ubuntu.cloud:released:openstack": { 65 "updated": "Wed, 01 May 2013 13:31:26 +0000", 66 "clouds": [ 67 { 68 "region": "{{.Region}}", 69 "endpoint": "{{.URL}}" 70 } 71 ], 72 "cloudname": "test", 73 "datatype": "image-ids", 74 "format": "products:1.0", 75 "products": [ 76 "com.ubuntu.cloud:server:22.04:s390x", 77 "com.ubuntu.cloud:server:22.04:amd64", 78 "com.ubuntu.cloud:server:22.04:arm64", 79 "com.ubuntu.cloud:server:22.04:ppc64el", 80 "com.ubuntu.cloud:server:20.04:s390x", 81 "com.ubuntu.cloud:server:20.04:amd64", 82 "com.ubuntu.cloud:server:20.04:arm64", 83 "com.ubuntu.cloud:server:20.04:ppc64el", 84 "com.ubuntu.cloud:server:18.04:s390x", 85 "com.ubuntu.cloud:server:18.04:amd64", 86 "com.ubuntu.cloud:server:18.04:arm64", 87 "com.ubuntu.cloud:server:18.04:ppc64el", 88 "com.ubuntu.cloud:server:16.04:s390x", 89 "com.ubuntu.cloud:server:16.04:amd64", 90 "com.ubuntu.cloud:server:16.04:arm64", 91 "com.ubuntu.cloud:server:16.04:ppc64el", 92 "com.ubuntu.cloud:server:14.04:s390x", 93 "com.ubuntu.cloud:server:14.04:amd64", 94 "com.ubuntu.cloud:server:14.04:arm64", 95 "com.ubuntu.cloud:server:14.04:ppc64el", 96 "com.ubuntu.cloud:server:12.10:amd64", 97 "com.ubuntu.cloud:server:13.04:amd64" 98 ], 99 "path": "image-metadata/products.json" 100 } 101 }, 102 "updated": "Wed, 01 May 2013 13:31:26 +0000", 103 "format": "index:1.0" 104 } 105 ` 106 107 var imagesData = ` 108 { 109 "content_id": "com.ubuntu.cloud:released:openstack", 110 "products": { 111 "com.ubuntu.cloud:server:22.04:amd64": { 112 "release": "jammy", 113 "version": "22.04", 114 "arch": "amd64", 115 "versions": { 116 "20121218": { 117 "items": { 118 "inst1": { 119 "root_store": "ebs", 120 "virt": "pv", 121 "region": "some-region", 122 "id": "1" 123 }, 124 "inst2": { 125 "root_store": "ebs", 126 "virt": "pv", 127 "region": "another-region", 128 "id": "2" 129 } 130 }, 131 "pubname": "ubuntu-jammy-22.04-amd64-server-20121218", 132 "label": "release" 133 }, 134 "20121111": { 135 "items": { 136 "inst3": { 137 "root_store": "ebs", 138 "virt": "pv", 139 "region": "some-region", 140 "id": "3" 141 } 142 }, 143 "pubname": "ubuntu-jammy-22.04-amd64-server-20121111", 144 "label": "release" 145 } 146 } 147 }, 148 "com.ubuntu.cloud:server:22.04:arm64": { 149 "release": "jammy", 150 "version": "22.04", 151 "arch": "arm64", 152 "versions": { 153 "20121111": { 154 "items": { 155 "inst1604arm64": { 156 "root_store": "ebs", 157 "virt": "pv", 158 "region": "some-region", 159 "id": "id-1604arm64" 160 } 161 }, 162 "pubname": "ubuntu-jammy-22.04-arm64-server-20121111", 163 "label": "release" 164 } 165 } 166 }, 167 "com.ubuntu.cloud:server:22.04:ppc64el": { 168 "release": "jammy", 169 "version": "22.04", 170 "arch": "ppc64el", 171 "versions": { 172 "20121111": { 173 "items": { 174 "inst1604ppc64el": { 175 "root_store": "ebs", 176 "virt": "pv", 177 "region": "some-region", 178 "id": "id-1604ppc64el" 179 } 180 }, 181 "pubname": "ubuntu-jammy-22.04-ppc64el-server-20121111", 182 "label": "release" 183 } 184 } 185 }, 186 "com.ubuntu.cloud:server:22.04:s390x": { 187 "release": "jammy", 188 "version": "22.04", 189 "arch": "s390x", 190 "versions": { 191 "20121111": { 192 "items": { 193 "inst1604s390x": { 194 "root_store": "ebs", 195 "virt": "pv", 196 "region": "some-region", 197 "id": "id-1604s390x" 198 } 199 }, 200 "pubname": "ubuntu-jammy-22.04-s390x-server-20121111", 201 "label": "release" 202 } 203 } 204 }, 205 "com.ubuntu.cloud:server:20.04:amd64": { 206 "release": "focal", 207 "version": "20.04", 208 "arch": "amd64", 209 "versions": { 210 "20121218": { 211 "items": { 212 "inst1": { 213 "root_store": "ebs", 214 "virt": "pv", 215 "region": "some-region", 216 "id": "1" 217 }, 218 "inst2": { 219 "root_store": "ebs", 220 "virt": "pv", 221 "region": "another-region", 222 "id": "2" 223 } 224 }, 225 "pubname": "ubuntu-focal-20.04-amd64-server-20121218", 226 "label": "release" 227 }, 228 "20121111": { 229 "items": { 230 "inst3": { 231 "root_store": "ebs", 232 "virt": "pv", 233 "region": "some-region", 234 "id": "3" 235 } 236 }, 237 "pubname": "ubuntu-focal-20.04-amd64-server-20121111", 238 "label": "release" 239 } 240 } 241 }, 242 "com.ubuntu.cloud:server:20.04:arm64": { 243 "release": "focal", 244 "version": "20.04", 245 "arch": "arm64", 246 "versions": { 247 "20121111": { 248 "items": { 249 "inst1604arm64": { 250 "root_store": "ebs", 251 "virt": "pv", 252 "region": "some-region", 253 "id": "id-1604arm64" 254 } 255 }, 256 "pubname": "ubuntu-focal-20.04-arm64-server-20121111", 257 "label": "release" 258 } 259 } 260 }, 261 "com.ubuntu.cloud:server:20.04:ppc64el": { 262 "release": "focal", 263 "version": "20.04", 264 "arch": "ppc64el", 265 "versions": { 266 "20121111": { 267 "items": { 268 "inst1604ppc64el": { 269 "root_store": "ebs", 270 "virt": "pv", 271 "region": "some-region", 272 "id": "id-1604ppc64el" 273 } 274 }, 275 "pubname": "ubuntu-focal-20.04-ppc64el-server-20121111", 276 "label": "release" 277 } 278 } 279 }, 280 "com.ubuntu.cloud:server:20.04:s390x": { 281 "release": "focal", 282 "version": "20.04", 283 "arch": "s390x", 284 "versions": { 285 "20121111": { 286 "items": { 287 "inst1604s390x": { 288 "root_store": "ebs", 289 "virt": "pv", 290 "region": "some-region", 291 "id": "id-1604s390x" 292 } 293 }, 294 "pubname": "ubuntu-focal-20.04-s390x-server-20121111", 295 "label": "release" 296 } 297 } 298 }, 299 "com.ubuntu.cloud:server:18.04:amd64": { 300 "release": "bionic", 301 "version": "18.04", 302 "arch": "amd64", 303 "versions": { 304 "20121218": { 305 "items": { 306 "inst1": { 307 "root_store": "ebs", 308 "virt": "pv", 309 "region": "some-region", 310 "id": "1" 311 }, 312 "inst2": { 313 "root_store": "ebs", 314 "virt": "pv", 315 "region": "another-region", 316 "id": "2" 317 } 318 }, 319 "pubname": "ubuntu-bionic-18.04-amd64-server-20121218", 320 "label": "release" 321 }, 322 "20121111": { 323 "items": { 324 "inst3": { 325 "root_store": "ebs", 326 "virt": "pv", 327 "region": "some-region", 328 "id": "3" 329 } 330 }, 331 "pubname": "ubuntu-bionic-18.04-amd64-server-20121111", 332 "label": "release" 333 } 334 } 335 }, 336 "com.ubuntu.cloud:server:18.04:arm64": { 337 "release": "bionic", 338 "version": "18.04", 339 "arch": "arm64", 340 "versions": { 341 "20121111": { 342 "items": { 343 "inst1604arm64": { 344 "root_store": "ebs", 345 "virt": "pv", 346 "region": "some-region", 347 "id": "id-1604arm64" 348 } 349 }, 350 "pubname": "ubuntu-bionic-18.04-arm64-server-20121111", 351 "label": "release" 352 } 353 } 354 }, 355 "com.ubuntu.cloud:server:18.04:ppc64el": { 356 "release": "bionic", 357 "version": "18.04", 358 "arch": "ppc64el", 359 "versions": { 360 "20121111": { 361 "items": { 362 "inst1604ppc64el": { 363 "root_store": "ebs", 364 "virt": "pv", 365 "region": "some-region", 366 "id": "id-1604ppc64el" 367 } 368 }, 369 "pubname": "ubuntu-bionic-18.04-ppc64el-server-20121111", 370 "label": "release" 371 } 372 } 373 }, 374 "com.ubuntu.cloud:server:18.04:s390x": { 375 "release": "bionic", 376 "version": "18.04", 377 "arch": "s390x", 378 "versions": { 379 "20121111": { 380 "items": { 381 "inst1604s390x": { 382 "root_store": "ebs", 383 "virt": "pv", 384 "region": "some-region", 385 "id": "id-1604s390x" 386 } 387 }, 388 "pubname": "ubuntu-bionic-18.04-s390x-server-20121111", 389 "label": "release" 390 } 391 } 392 }, 393 "com.ubuntu.cloud:server:16.04:amd64": { 394 "release": "trusty", 395 "version": "16.04", 396 "arch": "amd64", 397 "versions": { 398 "20121218": { 399 "items": { 400 "inst1": { 401 "root_store": "ebs", 402 "virt": "pv", 403 "region": "some-region", 404 "id": "1" 405 }, 406 "inst2": { 407 "root_store": "ebs", 408 "virt": "pv", 409 "region": "another-region", 410 "id": "2" 411 } 412 }, 413 "pubname": "ubuntu-trusty-16.04-amd64-server-20121218", 414 "label": "release" 415 }, 416 "20121111": { 417 "items": { 418 "inst3": { 419 "root_store": "ebs", 420 "virt": "pv", 421 "region": "some-region", 422 "id": "3" 423 } 424 }, 425 "pubname": "ubuntu-trusty-16.04-amd64-server-20121111", 426 "label": "release" 427 } 428 } 429 }, 430 "com.ubuntu.cloud:server:16.04:arm64": { 431 "release": "xenial", 432 "version": "16.04", 433 "arch": "arm64", 434 "versions": { 435 "20121111": { 436 "items": { 437 "inst1604arm64": { 438 "root_store": "ebs", 439 "virt": "pv", 440 "region": "some-region", 441 "id": "id-1604arm64" 442 } 443 }, 444 "pubname": "ubuntu-xenial-16.04-arm64-server-20121111", 445 "label": "release" 446 } 447 } 448 }, 449 "com.ubuntu.cloud:server:16.04:ppc64el": { 450 "release": "xenial", 451 "version": "16.04", 452 "arch": "ppc64el", 453 "versions": { 454 "20121111": { 455 "items": { 456 "inst1604ppc64el": { 457 "root_store": "ebs", 458 "virt": "pv", 459 "region": "some-region", 460 "id": "id-1604ppc64el" 461 } 462 }, 463 "pubname": "ubuntu-xenial-16.04-ppc64el-server-20121111", 464 "label": "release" 465 } 466 } 467 }, 468 "com.ubuntu.cloud:server:14.04:amd64": { 469 "release": "trusty", 470 "version": "14.04", 471 "arch": "amd64", 472 "versions": { 473 "20121218": { 474 "items": { 475 "inst1": { 476 "root_store": "ebs", 477 "virt": "pv", 478 "region": "some-region", 479 "id": "1" 480 }, 481 "inst2": { 482 "root_store": "ebs", 483 "virt": "pv", 484 "region": "another-region", 485 "id": "2" 486 } 487 }, 488 "pubname": "ubuntu-trusty-14.04-amd64-server-20121218", 489 "label": "release" 490 }, 491 "20121111": { 492 "items": { 493 "inst3": { 494 "root_store": "ebs", 495 "virt": "pv", 496 "region": "some-region", 497 "id": "3" 498 } 499 }, 500 "pubname": "ubuntu-trusty-14.04-amd64-server-20121111", 501 "label": "release" 502 } 503 } 504 }, 505 "com.ubuntu.cloud:server:14.04:arm64": { 506 "release": "trusty", 507 "version": "14.04", 508 "arch": "arm64", 509 "versions": { 510 "20121111": { 511 "items": { 512 "inst33": { 513 "root_store": "ebs", 514 "virt": "pv", 515 "region": "some-region", 516 "id": "33" 517 } 518 }, 519 "pubname": "ubuntu-trusty-14.04-arm64-server-20121111", 520 "label": "release" 521 } 522 } 523 }, 524 "com.ubuntu.cloud:server:14.04:ppc64el": { 525 "release": "trusty", 526 "version": "14.04", 527 "arch": "ppc64el", 528 "versions": { 529 "20121111": { 530 "items": { 531 "inst33": { 532 "root_store": "ebs", 533 "virt": "pv", 534 "region": "some-region", 535 "id": "33" 536 } 537 }, 538 "pubname": "ubuntu-trusty-14.04-ppc64el-server-20121111", 539 "label": "release" 540 } 541 } 542 }, 543 "com.ubuntu.cloud:server:12.10:amd64": { 544 "release": "quantal", 545 "version": "12.10", 546 "arch": "amd64", 547 "versions": { 548 "20121218": { 549 "items": { 550 "inst3": { 551 "root_store": "ebs", 552 "virt": "pv", 553 "region": "region-1", 554 "id": "id-1" 555 }, 556 "inst4": { 557 "root_store": "ebs", 558 "virt": "pv", 559 "region": "region-2", 560 "id": "id-2" 561 } 562 }, 563 "pubname": "ubuntu-quantal-12.14-amd64-server-20121218", 564 "label": "release" 565 } 566 } 567 }, 568 "com.ubuntu.cloud:server:13.04:amd64": { 569 "release": "raring", 570 "version": "13.04", 571 "arch": "amd64", 572 "versions": { 573 "20121218": { 574 "items": { 575 "inst5": { 576 "root_store": "ebs", 577 "virt": "pv", 578 "region": "some-region", 579 "id": "id-y" 580 }, 581 "inst6": { 582 "root_store": "ebs", 583 "virt": "pv", 584 "region": "another-region", 585 "id": "id-z" 586 } 587 }, 588 "pubname": "ubuntu-raring-13.04-amd64-server-20121218", 589 "label": "release" 590 } 591 } 592 }, 593 "com.ubuntu.cloud:server:14.04:s390x": { 594 "release": "trusty", 595 "version": "14.04", 596 "arch": "s390x", 597 "versions": { 598 "20121218": { 599 "items": { 600 "inst5": { 601 "root_store": "ebs", 602 "virt": "pv", 603 "region": "some-region", 604 "id": "id-y" 605 }, 606 "inst6": { 607 "root_store": "ebs", 608 "virt": "pv", 609 "region": "another-region", 610 "id": "id-z" 611 } 612 }, 613 "pubname": "ubuntu-trusty-14.04-s390x-server-20121218", 614 "label": "release" 615 } 616 } 617 }, 618 "com.ubuntu.cloud:server:16.04:s390x": { 619 "release": "xenial", 620 "version": "16.04", 621 "arch": "s390x", 622 "versions": { 623 "20121218": { 624 "items": { 625 "inst5": { 626 "root_store": "ebs", 627 "virt": "pv", 628 "region": "some-region", 629 "id": "id-y" 630 }, 631 "inst6": { 632 "root_store": "ebs", 633 "virt": "pv", 634 "region": "another-region", 635 "id": "id-z" 636 } 637 }, 638 "pubname": "ubuntu-xenial-16.04-s390x-server-20121218", 639 "label": "release" 640 } 641 } 642 } 643 }, 644 "format": "products:1.0" 645 } 646 ` 647 648 const productMetadatafile = "image-metadata/products.json" 649 650 func UseTestImageData(stor envstorage.Storage, cred *identity.Credentials) { 651 // Put some image metadata files into the public storage. 652 t := template.Must(template.New("").Parse(indexData)) 653 var metadata bytes.Buffer 654 if err := t.Execute(&metadata, cred); err != nil { 655 panic(fmt.Errorf("cannot generate index metdata: %v", err)) 656 } 657 data := metadata.Bytes() 658 stor.Put(simplestreams.UnsignedIndex("v1", 1), bytes.NewReader(data), int64(len(data))) 659 stor.Put( 660 productMetadatafile, strings.NewReader(imagesData), int64(len(imagesData))) 661 662 envtesting.SignTestTools(stor) 663 } 664 665 func RemoveTestImageData(stor envstorage.Storage) { 666 stor.RemoveAll() 667 } 668 669 // DiscardSecurityGroup cleans up a security group, it is not an error to 670 // delete something that doesn't exist. 671 func DiscardSecurityGroup(e environs.Environ, name string) error { 672 env := e.(*Environ) 673 neutronClient := env.neutron() 674 groups, err := neutronClient.SecurityGroupByNameV2(name) 675 if err != nil || len(groups) == 0 { 676 if errors.IsNotFound(err) { 677 // Group already deleted, done 678 return nil 679 } 680 } 681 for _, group := range groups { 682 err = neutronClient.DeleteSecurityGroupV2(group.Id) 683 if err != nil { 684 return err 685 } 686 } 687 return nil 688 } 689 690 func FindInstanceSpec( 691 e environs.Environ, 692 base corebase.Base, arch, cons string, 693 imageMetadata []*imagemetadata.ImageMetadata, 694 ) (spec *instances.InstanceSpec, err error) { 695 env := e.(*Environ) 696 return findInstanceSpec(env, instances.InstanceConstraint{ 697 Base: base, 698 Arch: arch, 699 Region: env.cloud().Region, 700 Constraints: constraints.MustParse(cons), 701 }, imageMetadata) 702 }