github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/data_source_openstack_images_image_v2_test.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestAccOpenStackImagesV2ImageDataSource_basic(t *testing.T) { 12 resource.Test(t, resource.TestCase{ 13 PreCheck: func() { testAccPreCheck(t) }, 14 Providers: testAccProviders, 15 Steps: []resource.TestStep{ 16 resource.TestStep{ 17 Config: testAccOpenStackImagesV2ImageDataSource_cirros, 18 }, 19 resource.TestStep{ 20 Config: testAccOpenStackImagesV2ImageDataSource_basic, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckImagesV2DataSourceID("data.openstack_images_image_v2.image_1"), 23 resource.TestCheckResourceAttr( 24 "data.openstack_images_image_v2.image_1", "name", "CirrOS-tf"), 25 resource.TestCheckResourceAttr( 26 "data.openstack_images_image_v2.image_1", "container_format", "bare"), 27 resource.TestCheckResourceAttr( 28 "data.openstack_images_image_v2.image_1", "disk_format", "qcow2"), 29 resource.TestCheckResourceAttr( 30 "data.openstack_images_image_v2.image_1", "min_disk_gb", "0"), 31 resource.TestCheckResourceAttr( 32 "data.openstack_images_image_v2.image_1", "min_ram_mb", "0"), 33 resource.TestCheckResourceAttr( 34 "data.openstack_images_image_v2.image_1", "protected", "false"), 35 resource.TestCheckResourceAttr( 36 "data.openstack_images_image_v2.image_1", "visibility", "private"), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccOpenStackImagesV2ImageDataSource_testQueries(t *testing.T) { 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 Steps: []resource.TestStep{ 48 resource.TestStep{ 49 Config: testAccOpenStackImagesV2ImageDataSource_cirros, 50 }, 51 resource.TestStep{ 52 Config: testAccOpenStackImagesV2ImageDataSource_queryTag, 53 Check: resource.ComposeTestCheckFunc( 54 testAccCheckImagesV2DataSourceID("data.openstack_images_image_v2.image_1"), 55 ), 56 }, 57 resource.TestStep{ 58 Config: testAccOpenStackImagesV2ImageDataSource_querySizeMin, 59 Check: resource.ComposeTestCheckFunc( 60 testAccCheckImagesV2DataSourceID("data.openstack_images_image_v2.image_1"), 61 ), 62 }, 63 resource.TestStep{ 64 Config: testAccOpenStackImagesV2ImageDataSource_querySizeMax, 65 Check: resource.ComposeTestCheckFunc( 66 testAccCheckImagesV2DataSourceID("data.openstack_images_image_v2.image_1"), 67 ), 68 }, 69 }, 70 }) 71 } 72 73 func testAccCheckImagesV2DataSourceID(n string) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 rs, ok := s.RootModule().Resources[n] 76 if !ok { 77 return fmt.Errorf("Can't find image data source: %s", n) 78 } 79 80 if rs.Primary.ID == "" { 81 return fmt.Errorf("Image data source ID not set") 82 } 83 84 return nil 85 } 86 } 87 88 // Standard CirrOS image 89 const testAccOpenStackImagesV2ImageDataSource_cirros = ` 90 resource "openstack_images_image_v2" "image_1" { 91 name = "CirrOS-tf" 92 container_format = "bare" 93 disk_format = "qcow2" 94 image_source_url = "http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img" 95 tags = ["cirros-tf"] 96 } 97 ` 98 99 var testAccOpenStackImagesV2ImageDataSource_basic = fmt.Sprintf(` 100 %s 101 102 data "openstack_images_image_v2" "image_1" { 103 most_recent = true 104 name = "${openstack_images_image_v2.image_1.name}" 105 } 106 `, testAccOpenStackImagesV2ImageDataSource_cirros) 107 108 var testAccOpenStackImagesV2ImageDataSource_queryTag = fmt.Sprintf(` 109 %s 110 111 data "openstack_images_image_v2" "image_1" { 112 most_recent = true 113 visibility = "private" 114 tag = "cirros-tf" 115 } 116 `, testAccOpenStackImagesV2ImageDataSource_cirros) 117 118 var testAccOpenStackImagesV2ImageDataSource_querySizeMin = fmt.Sprintf(` 119 %s 120 121 data "openstack_images_image_v2" "image_1" { 122 most_recent = true 123 visibility = "private" 124 size_min = "13000000" 125 } 126 `, testAccOpenStackImagesV2ImageDataSource_cirros) 127 128 var testAccOpenStackImagesV2ImageDataSource_querySizeMax = fmt.Sprintf(` 129 %s 130 131 data "openstack_images_image_v2" "image_1" { 132 most_recent = true 133 visibility = "private" 134 size_max = "23000000" 135 } 136 `, testAccOpenStackImagesV2ImageDataSource_cirros)