github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_images_image_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" 8 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccImagesImageV2_basic(t *testing.T) { 14 var image images.Image 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckImagesImageV2Destroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccImagesImageV2_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 25 resource.TestCheckResourceAttr( 26 "openstack_images_image_v2.image_1", "name", "Rancher TerraformAccTest"), 27 resource.TestCheckResourceAttr( 28 "openstack_images_image_v2.image_1", "container_format", "bare"), 29 resource.TestCheckResourceAttr( 30 "openstack_images_image_v2.image_1", "disk_format", "qcow2"), 31 resource.TestCheckResourceAttr( 32 "openstack_images_image_v2.image_1", "schema", "/v2/schemas/image"), 33 ), 34 }, 35 }, 36 }) 37 } 38 39 func TestAccImagesImageV2_name(t *testing.T) { 40 var image images.Image 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckImagesImageV2Destroy, 46 Steps: []resource.TestStep{ 47 resource.TestStep{ 48 Config: testAccImagesImageV2_name_1, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 51 resource.TestCheckResourceAttr( 52 "openstack_images_image_v2.image_1", "name", "Rancher TerraformAccTest"), 53 ), 54 }, 55 resource.TestStep{ 56 Config: testAccImagesImageV2_name_2, 57 Check: resource.ComposeTestCheckFunc( 58 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 59 resource.TestCheckResourceAttr( 60 "openstack_images_image_v2.image_1", "name", "TerraformAccTest Rancher"), 61 ), 62 }, 63 }, 64 }) 65 } 66 67 func TestAccImagesImageV2_tags(t *testing.T) { 68 var image images.Image 69 70 resource.Test(t, resource.TestCase{ 71 PreCheck: func() { testAccPreCheck(t) }, 72 Providers: testAccProviders, 73 CheckDestroy: testAccCheckImagesImageV2Destroy, 74 Steps: []resource.TestStep{ 75 resource.TestStep{ 76 Config: testAccImagesImageV2_tags_1, 77 Check: resource.ComposeTestCheckFunc( 78 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 79 testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "foo"), 80 testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "bar"), 81 testAccCheckImagesImageV2TagCount("openstack_images_image_v2.image_1", 2), 82 ), 83 }, 84 resource.TestStep{ 85 Config: testAccImagesImageV2_tags_2, 86 Check: resource.ComposeTestCheckFunc( 87 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 88 testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "foo"), 89 testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "bar"), 90 testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "baz"), 91 testAccCheckImagesImageV2TagCount("openstack_images_image_v2.image_1", 3), 92 ), 93 }, 94 resource.TestStep{ 95 Config: testAccImagesImageV2_tags_3, 96 Check: resource.ComposeTestCheckFunc( 97 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 98 testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "foo"), 99 testAccCheckImagesImageV2HasTag("openstack_images_image_v2.image_1", "baz"), 100 testAccCheckImagesImageV2TagCount("openstack_images_image_v2.image_1", 2), 101 ), 102 }, 103 }, 104 }) 105 } 106 107 func TestAccImagesImageV2_visibility(t *testing.T) { 108 var image images.Image 109 110 resource.Test(t, resource.TestCase{ 111 PreCheck: func() { 112 testAccPreCheck(t) 113 testAccPreCheckAdminOnly(t) 114 }, 115 Providers: testAccProviders, 116 CheckDestroy: testAccCheckImagesImageV2Destroy, 117 Steps: []resource.TestStep{ 118 resource.TestStep{ 119 Config: testAccImagesImageV2_visibility_1, 120 Check: resource.ComposeTestCheckFunc( 121 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 122 resource.TestCheckResourceAttr( 123 "openstack_images_image_v2.image_1", "visibility", "private"), 124 ), 125 }, 126 resource.TestStep{ 127 Config: testAccImagesImageV2_visibility_2, 128 Check: resource.ComposeTestCheckFunc( 129 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 130 resource.TestCheckResourceAttr( 131 "openstack_images_image_v2.image_1", "visibility", "public"), 132 ), 133 }, 134 }, 135 }) 136 } 137 138 func TestAccImagesImageV2_timeout(t *testing.T) { 139 var image images.Image 140 141 resource.Test(t, resource.TestCase{ 142 PreCheck: func() { testAccPreCheck(t) }, 143 Providers: testAccProviders, 144 CheckDestroy: testAccCheckImagesImageV2Destroy, 145 Steps: []resource.TestStep{ 146 resource.TestStep{ 147 Config: testAccImagesImageV2_timeout, 148 Check: resource.ComposeTestCheckFunc( 149 testAccCheckImagesImageV2Exists("openstack_images_image_v2.image_1", &image), 150 ), 151 }, 152 }, 153 }) 154 } 155 156 func testAccCheckImagesImageV2Destroy(s *terraform.State) error { 157 config := testAccProvider.Meta().(*Config) 158 imageClient, err := config.imageV2Client(OS_REGION_NAME) 159 if err != nil { 160 return fmt.Errorf("Error creating OpenStack Image: %s", err) 161 } 162 163 for _, rs := range s.RootModule().Resources { 164 if rs.Type != "openstack_images_image_v2" { 165 continue 166 } 167 168 _, err := images.Get(imageClient, rs.Primary.ID).Extract() 169 if err == nil { 170 return fmt.Errorf("Image still exists") 171 } 172 } 173 174 return nil 175 } 176 177 func testAccCheckImagesImageV2Exists(n string, image *images.Image) resource.TestCheckFunc { 178 return func(s *terraform.State) error { 179 rs, ok := s.RootModule().Resources[n] 180 if !ok { 181 return fmt.Errorf("Not found: %s", n) 182 } 183 184 if rs.Primary.ID == "" { 185 return fmt.Errorf("No ID is set") 186 } 187 188 config := testAccProvider.Meta().(*Config) 189 imageClient, err := config.imageV2Client(OS_REGION_NAME) 190 if err != nil { 191 return fmt.Errorf("Error creating OpenStack Image: %s", err) 192 } 193 194 found, err := images.Get(imageClient, rs.Primary.ID).Extract() 195 if err != nil { 196 return err 197 } 198 199 if found.ID != rs.Primary.ID { 200 return fmt.Errorf("Image not found") 201 } 202 203 *image = *found 204 205 return nil 206 } 207 } 208 209 func testAccCheckImagesImageV2HasTag(n, tag string) resource.TestCheckFunc { 210 return func(s *terraform.State) error { 211 rs, ok := s.RootModule().Resources[n] 212 if !ok { 213 return fmt.Errorf("Not found: %s", n) 214 } 215 216 if rs.Primary.ID == "" { 217 return fmt.Errorf("No ID is set") 218 } 219 220 config := testAccProvider.Meta().(*Config) 221 imageClient, err := config.imageV2Client(OS_REGION_NAME) 222 if err != nil { 223 return fmt.Errorf("Error creating OpenStack Image: %s", err) 224 } 225 226 found, err := images.Get(imageClient, rs.Primary.ID).Extract() 227 if err != nil { 228 return err 229 } 230 231 if found.ID != rs.Primary.ID { 232 return fmt.Errorf("Image not found") 233 } 234 235 for _, v := range found.Tags { 236 if tag == v { 237 return nil 238 } 239 } 240 241 return fmt.Errorf("Tag not found: %s", tag) 242 } 243 } 244 245 func testAccCheckImagesImageV2TagCount(n string, expected int) resource.TestCheckFunc { 246 return func(s *terraform.State) error { 247 rs, ok := s.RootModule().Resources[n] 248 if !ok { 249 return fmt.Errorf("Not found: %s", n) 250 } 251 252 if rs.Primary.ID == "" { 253 return fmt.Errorf("No ID is set") 254 } 255 256 config := testAccProvider.Meta().(*Config) 257 imageClient, err := config.imageV2Client(OS_REGION_NAME) 258 if err != nil { 259 return fmt.Errorf("Error creating OpenStack Image: %s", err) 260 } 261 262 found, err := images.Get(imageClient, rs.Primary.ID).Extract() 263 if err != nil { 264 return err 265 } 266 267 if found.ID != rs.Primary.ID { 268 return fmt.Errorf("Image not found") 269 } 270 271 if len(found.Tags) != expected { 272 return fmt.Errorf("Expecting %d tags, found %d", expected, len(found.Tags)) 273 } 274 275 return nil 276 } 277 } 278 279 var testAccImagesImageV2_basic = ` 280 resource "openstack_images_image_v2" "image_1" { 281 name = "Rancher TerraformAccTest" 282 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 283 container_format = "bare" 284 disk_format = "qcow2" 285 }` 286 287 var testAccImagesImageV2_name_1 = ` 288 resource "openstack_images_image_v2" "image_1" { 289 name = "Rancher TerraformAccTest" 290 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 291 container_format = "bare" 292 disk_format = "qcow2" 293 }` 294 295 var testAccImagesImageV2_name_2 = ` 296 resource "openstack_images_image_v2" "image_1" { 297 name = "TerraformAccTest Rancher" 298 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 299 container_format = "bare" 300 disk_format = "qcow2" 301 }` 302 303 var testAccImagesImageV2_tags_1 = ` 304 resource "openstack_images_image_v2" "image_1" { 305 name = "Rancher TerraformAccTest" 306 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 307 container_format = "bare" 308 disk_format = "qcow2" 309 tags = ["foo","bar"] 310 }` 311 312 var testAccImagesImageV2_tags_2 = ` 313 resource "openstack_images_image_v2" "image_1" { 314 name = "Rancher TerraformAccTest" 315 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 316 container_format = "bare" 317 disk_format = "qcow2" 318 tags = ["foo","bar","baz"] 319 }` 320 321 var testAccImagesImageV2_tags_3 = ` 322 resource "openstack_images_image_v2" "image_1" { 323 name = "Rancher TerraformAccTest" 324 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 325 container_format = "bare" 326 disk_format = "qcow2" 327 tags = ["foo","baz"] 328 }` 329 330 var testAccImagesImageV2_visibility_1 = ` 331 resource "openstack_images_image_v2" "image_1" { 332 name = "Rancher TerraformAccTest" 333 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 334 container_format = "bare" 335 disk_format = "qcow2" 336 visibility = "private" 337 }` 338 339 var testAccImagesImageV2_visibility_2 = ` 340 resource "openstack_images_image_v2" "image_1" { 341 name = "Rancher TerraformAccTest" 342 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 343 container_format = "bare" 344 disk_format = "qcow2" 345 visibility = "public" 346 }` 347 348 var testAccImagesImageV2_timeout = ` 349 resource "openstack_images_image_v2" "image_1" { 350 name = "Rancher TerraformAccTest" 351 image_source_url = "https://releases.rancher.com/os/latest/rancheros-openstack.img" 352 container_format = "bare" 353 disk_format = "qcow2" 354 355 timeouts { 356 create = "10m" 357 } 358 }`