github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/data_source_alicloud_images_test.go (about) 1 package alicloud 2 3 import ( 4 "regexp" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 ) 9 10 func TestAccAlicloudImagesDataSource_images(t *testing.T) { 11 resource.Test(t, resource.TestCase{ 12 PreCheck: func() { testAccPreCheck(t) }, 13 Providers: testAccProviders, 14 Steps: []resource.TestStep{ 15 { 16 Config: testAccCheckAlicloudImagesDataSourceImagesConfig, 17 Check: resource.ComposeTestCheckFunc( 18 testAccCheckAlicloudDataSourceID("data.alicloud_images.multi_image"), 19 20 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.#", "2"), 21 22 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.architecture", "x86_64"), 23 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.disk_device_mappings.#", "0"), 24 resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.0.creation_time", regexp.MustCompile("^20[0-9]{2}-")), 25 resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.0.image_id", regexp.MustCompile("^centos_6\\w{1,5}[64]{1}.")), 26 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.image_owner_alias", "system"), 27 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.os_type", "linux"), 28 resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.0.name", regexp.MustCompile("^centos_6[a-zA-Z0-9_]{1,5}[64]{1}.")), 29 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.progress", "100%"), 30 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.state", "Available"), 31 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.status", "Available"), 32 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.usage", "instance"), 33 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.0.tags.%", "0"), 34 35 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.architecture", "i386"), 36 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.disk_device_mappings.#", "0"), 37 resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.1.creation_time", regexp.MustCompile("^20[0-9]{2}-")), 38 resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.1.image_id", regexp.MustCompile("^centos_6[a-zA-Z0-9_]{1,5}[32]{1}.")), 39 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.image_owner_alias", "system"), 40 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.os_type", "linux"), 41 resource.TestMatchResourceAttr("data.alicloud_images.multi_image", "images.1.name", regexp.MustCompile("^centos_6\\w{1,5}[32]{1}.")), 42 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.progress", "100%"), 43 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.state", "Available"), 44 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.status", "Available"), 45 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.usage", "instance"), 46 resource.TestCheckResourceAttr("data.alicloud_images.multi_image", "images.1.tags.%", "0"), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func TestAccAlicloudImagesDataSource_owners(t *testing.T) { 54 resource.Test(t, resource.TestCase{ 55 PreCheck: func() { testAccPreCheck(t) }, 56 Providers: testAccProviders, 57 Steps: []resource.TestStep{ 58 { 59 Config: testAccCheckAlicloudImagesDataSourceOwnersConfig, 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckAlicloudDataSourceID("data.alicloud_images.owners_filtered_image"), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func TestAccAlicloudImagesDataSource_ownersEmpty(t *testing.T) { 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 Steps: []resource.TestStep{ 73 { 74 Config: testAccCheckAlicloudImagesDataSourceEmptyOwnersConfig, 75 Check: resource.ComposeTestCheckFunc( 76 testAccCheckAlicloudDataSourceID("data.alicloud_images.empty_owners_filtered_image"), 77 resource.TestCheckResourceAttr("data.alicloud_images.empty_owners_filtered_image", "most_recent", "true"), 78 ), 79 }, 80 }, 81 }) 82 } 83 84 func TestAccAlicloudImagesDataSource_nameRegexFilter(t *testing.T) { 85 resource.Test(t, resource.TestCase{ 86 PreCheck: func() { testAccPreCheck(t) }, 87 Providers: testAccProviders, 88 Steps: []resource.TestStep{ 89 { 90 Config: testAccCheckAlicloudImagesDataSourceNameRegexConfig, 91 Check: resource.ComposeTestCheckFunc( 92 testAccCheckAlicloudDataSourceID("data.alicloud_images.name_regex_filtered_image"), 93 resource.TestMatchResourceAttr("data.alicloud_images.name_regex_filtered_image", "images.0.image_id", regexp.MustCompile("^centos_")), 94 ), 95 }, 96 }, 97 }) 98 } 99 100 func TestAccAlicloudImagesDataSource_imageNotInFirstPage(t *testing.T) { 101 resource.Test(t, resource.TestCase{ 102 PreCheck: func() { testAccPreCheck(t) }, 103 Providers: testAccProviders, 104 Steps: []resource.TestStep{ 105 { 106 Config: testAccCheckAlicloudImagesDataSourceImageNotInFirstPageConfig, 107 Check: resource.ComposeTestCheckFunc( 108 testAccCheckAlicloudDataSourceID("data.alicloud_images.name_regex_filtered_image"), 109 resource.TestMatchResourceAttr("data.alicloud_images.name_regex_filtered_image", "images.0.image_id", regexp.MustCompile("^ubuntu_14")), 110 ), 111 }, 112 }, 113 }) 114 } 115 116 // Instance store test - using centos images 117 const testAccCheckAlicloudImagesDataSourceImagesConfig = ` 118 data "alicloud_images" "multi_image" { 119 owners = "system" 120 name_regex = "^centos_6" 121 } 122 ` 123 124 // Testing owner parameter 125 const testAccCheckAlicloudImagesDataSourceOwnersConfig = ` 126 data "alicloud_images" "owners_filtered_image" { 127 most_recent = true 128 owners = "system" 129 } 130 ` 131 132 const testAccCheckAlicloudImagesDataSourceEmptyOwnersConfig = ` 133 data "alicloud_images" "empty_owners_filtered_image" { 134 most_recent = true 135 owners = "" 136 } 137 ` 138 139 // Testing name_regex parameter 140 const testAccCheckAlicloudImagesDataSourceNameRegexConfig = ` 141 data "alicloud_images" "name_regex_filtered_image" { 142 most_recent = true 143 owners = "system" 144 name_regex = "^centos_6\\w{1,5}[64]{1}.*" 145 } 146 ` 147 148 // Testing image not in first page response 149 const testAccCheckAlicloudImagesDataSourceImageNotInFirstPageConfig = ` 150 data "alicloud_images" "name_regex_filtered_image" { 151 most_recent = true 152 owners = "system" 153 name_regex = "^ubuntu_14.*_64" 154 } 155 `