|
| 1 | +// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +package provider |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/resource" |
| 10 | + "github.com/hashicorp/terraform/terraform" |
| 11 | + "github.com/oracle/oci-go-sdk/core" |
| 12 | + "github.com/stretchr/testify/suite" |
| 13 | +) |
| 14 | + |
| 15 | +type ResourceCoreImageTestSuite struct { |
| 16 | + suite.Suite |
| 17 | + Providers map[string]terraform.ResourceProvider |
| 18 | + Config string |
| 19 | + OperatingSystem string |
| 20 | + OperatingSystemVersion string |
| 21 | +} |
| 22 | + |
| 23 | +func (s *ResourceCoreImageTestSuite) SetupTest() { |
| 24 | + s.Providers = testAccProviders |
| 25 | + testAccPreCheck(s.T()) |
| 26 | + s.Config = legacyTestProviderConfig() + instanceConfig + DefinedTagsDependencies |
| 27 | + s.OperatingSystem = "Oracle Linux" |
| 28 | +} |
| 29 | + |
| 30 | +func (s *ResourceCoreImageTestSuite) TestAccResourceCoreImage_objectStorageImageSources() { |
| 31 | + /* |
| 32 | + * This test requires an image to have been exported relative to bucket_name and object_name below. |
| 33 | + * bucket_name: test-fixtures-bucket |
| 34 | + * object_name: test-fixtures-image-export |
| 35 | + * as well as an image PAR url supplied via env var image_par |
| 36 | + */ |
| 37 | + imagePar := getEnvSettingWithBlankDefault("image_par") |
| 38 | + if imagePar == "" { |
| 39 | + s.T().Skip("Dependency image_par not defined for test") |
| 40 | + } |
| 41 | + |
| 42 | + resource.Test(s.T(), resource.TestCase{ |
| 43 | + Providers: s.Providers, |
| 44 | + Steps: []resource.TestStep{ |
| 45 | + { |
| 46 | + Config: legacyTestProviderConfig() + ` |
| 47 | + data "oci_objectstorage_namespace" "t" {} |
| 48 | + resource "oci_core_image" "i1" { |
| 49 | + compartment_id = "${var.tenancy_ocid}" |
| 50 | + image_source_details { |
| 51 | + source_type = "objectStorageTuple" |
| 52 | + namespace_name = "${data.oci_objectstorage_namespace.t.namespace}" |
| 53 | + bucket_name = "test-fixtures-bucket" |
| 54 | + object_name = "test-fixtures-image-export" |
| 55 | + } |
| 56 | + timeouts { |
| 57 | + create = "30m" |
| 58 | + } |
| 59 | + } |
| 60 | + resource "oci_core_image" "i2" { |
| 61 | + compartment_id = "${var.tenancy_ocid}" |
| 62 | + image_source_details { |
| 63 | + source_type = "objectStorageUri" |
| 64 | + source_uri = "` + imagePar + `" |
| 65 | + source_image_type = "QCOW2" |
| 66 | + } |
| 67 | + timeouts { |
| 68 | + create = "30m" |
| 69 | + } |
| 70 | + } |
| 71 | + `, |
| 72 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 73 | + resource.TestCheckNoResourceAttr("oci_core_image.i1", "instance_id"), |
| 74 | + resource.TestCheckNoResourceAttr("oci_core_image.i1", "base_image_id"), |
| 75 | + resource.TestCheckResourceAttr("oci_core_image.i1", "operating_system", "Custom"), |
| 76 | + resource.TestCheckResourceAttr("oci_core_image.i1", "operating_system_version", "Custom"), |
| 77 | + |
| 78 | + resource.TestCheckNoResourceAttr("oci_core_image.i2", "instance_id"), |
| 79 | + resource.TestCheckNoResourceAttr("oci_core_image.i2", "base_image_id"), |
| 80 | + resource.TestCheckResourceAttr("oci_core_image.i2", "operating_system", "Custom"), |
| 81 | + resource.TestCheckResourceAttr("oci_core_image.i2", "operating_system_version", "Custom"), |
| 82 | + ), |
| 83 | + }, |
| 84 | + // data source |
| 85 | + { |
| 86 | + Config: s.Config + fmt.Sprintf(` |
| 87 | + data "oci_core_images" "allOracleImages" { |
| 88 | + compartment_id = "${var.tenancy_ocid}" |
| 89 | + operating_system = "%s" |
| 90 | + shape = "VM.Standard2.1" |
| 91 | + } |
| 92 | +
|
| 93 | + data "oci_core_images" "t" { |
| 94 | + compartment_id = "${var.tenancy_ocid}" |
| 95 | + operating_system = "${lookup(data.oci_core_images.allOracleImages.images[0], "operating_system")}" |
| 96 | + operating_system_version = "${lookup(data.oci_core_images.allOracleImages.images[0], "operating_system_version")}" |
| 97 | +
|
| 98 | + filter { |
| 99 | + name = "launch_options.is_pv_encryption_in_transit_enabled" |
| 100 | + values = ["true"] |
| 101 | + } |
| 102 | + }`, s.OperatingSystem), |
| 103 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 104 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.create_image_allowed", "true"), |
| 105 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.state", string(core.ImageLifecycleStateAvailable)), |
| 106 | + resource.TestCheckResourceAttrSet("data.oci_core_images.t", "images.0.launch_mode"), |
| 107 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.launch_options.#", "1"), |
| 108 | + resource.TestCheckResourceAttrSet("data.oci_core_images.t", "images.0.launch_options.0.boot_volume_type"), |
| 109 | + resource.TestCheckResourceAttrSet("data.oci_core_images.t", "images.0.launch_options.0.firmware"), |
| 110 | + resource.TestCheckResourceAttrSet("data.oci_core_images.t", "images.0.launch_options.0.network_type"), |
| 111 | + resource.TestCheckResourceAttrSet("data.oci_core_images.t", "images.0.launch_options.0.remote_data_volume_type"), |
| 112 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.launch_options.0.is_pv_encryption_in_transit_enabled", "true"), |
| 113 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.operating_system", s.OperatingSystem), |
| 114 | + TestCheckResourceAttributesEqual("data.oci_core_images.t", "images.0.operating_system_version", "data.oci_core_images.allOracleImages", "images.0.operating_system_version"), |
| 115 | + resource.TestCheckResourceAttrSet("data.oci_core_images.t", "images.0.time_created"), |
| 116 | + // This test filters to official images, which do not derive from another so the below properties are expected to be null |
| 117 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.base_image_id", ""), |
| 118 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.instance_id", ""), |
| 119 | + resource.TestCheckResourceAttr("data.oci_core_images.t", "images.0.compartment_id", ""), |
| 120 | + ), |
| 121 | + }, |
| 122 | + }, |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func TestResourceCoreImageTestSuite(t *testing.T) { |
| 127 | + suite.Run(t, new(ResourceCoreImageTestSuite)) |
| 128 | +} |
0 commit comments