github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/data_source_aws_ami_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccAWSAmiDataSource_natInstance(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: testAccCheckAwsAmiDataSourceConfig, 19 Check: resource.ComposeTestCheckFunc( 20 testAccCheckAwsAmiDataSourceID("data.aws_ami.nat_ami"), 21 // Check attributes. Some attributes are tough to test - any not contained here should not be considered 22 // stable and should not be used in interpolation. Exception to block_device_mappings which should both 23 // show up consistently and break if certain references are not available. However modification of the 24 // snapshot ID which is bound to happen on the NAT AMIs will cause testing to break consistently, so 25 // deep inspection is not included, simply the count is checked. 26 // Tags and product codes may need more testing, but I'm having a hard time finding images with 27 // these attributes set. 28 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "architecture", "x86_64"), 29 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "block_device_mappings.#", "1"), 30 resource.TestMatchResourceAttr("data.aws_ami.nat_ami", "creation_date", regexp.MustCompile("^20[0-9]{2}-")), 31 resource.TestMatchResourceAttr("data.aws_ami.nat_ami", "description", regexp.MustCompile("^Amazon Linux AMI")), 32 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "hypervisor", "xen"), 33 resource.TestMatchResourceAttr("data.aws_ami.nat_ami", "image_id", regexp.MustCompile("^ami-")), 34 resource.TestMatchResourceAttr("data.aws_ami.nat_ami", "image_location", regexp.MustCompile("^amazon/")), 35 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "image_owner_alias", "amazon"), 36 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "image_type", "machine"), 37 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "most_recent", "true"), 38 resource.TestMatchResourceAttr("data.aws_ami.nat_ami", "name", regexp.MustCompile("^amzn-ami-vpc-nat")), 39 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "owner_id", "137112412989"), 40 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "public", "true"), 41 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "product_codes.#", "0"), 42 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "root_device_name", "/dev/xvda"), 43 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "root_device_type", "ebs"), 44 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "sriov_net_support", "simple"), 45 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "state", "available"), 46 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "state_reason.code", "UNSET"), 47 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "state_reason.message", "UNSET"), 48 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "tags.#", "0"), 49 resource.TestCheckResourceAttr("data.aws_ami.nat_ami", "virtualization_type", "hvm"), 50 ), 51 }, 52 }, 53 }) 54 } 55 func TestAccAWSAmiDataSource_windowsInstance(t *testing.T) { 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 Steps: []resource.TestStep{ 60 resource.TestStep{ 61 Config: testAccCheckAwsAmiDataSourceWindowsConfig, 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckAwsAmiDataSourceID("data.aws_ami.windows_ami"), 64 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "architecture", "x86_64"), 65 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "block_device_mappings.#", "27"), 66 resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "creation_date", regexp.MustCompile("^20[0-9]{2}-")), 67 resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "description", regexp.MustCompile("^Microsoft Windows Server 2012")), 68 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "hypervisor", "xen"), 69 resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "image_id", regexp.MustCompile("^ami-")), 70 resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "image_location", regexp.MustCompile("^amazon/")), 71 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "image_owner_alias", "amazon"), 72 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "image_type", "machine"), 73 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "most_recent", "true"), 74 resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "name", regexp.MustCompile("^Windows_Server-2012-R2")), 75 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "owner_id", "801119661308"), 76 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "platform", "windows"), 77 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "public", "true"), 78 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "product_codes.#", "0"), 79 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "root_device_name", "/dev/sda1"), 80 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "root_device_type", "ebs"), 81 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "sriov_net_support", "simple"), 82 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "state", "available"), 83 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "state_reason.code", "UNSET"), 84 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "state_reason.message", "UNSET"), 85 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "tags.#", "0"), 86 resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "virtualization_type", "hvm"), 87 ), 88 }, 89 }, 90 }) 91 } 92 93 func TestAccAWSAmiDataSource_instanceStore(t *testing.T) { 94 resource.Test(t, resource.TestCase{ 95 PreCheck: func() { testAccPreCheck(t) }, 96 Providers: testAccProviders, 97 Steps: []resource.TestStep{ 98 resource.TestStep{ 99 Config: testAccCheckAwsAmiDataSourceInstanceStoreConfig, 100 Check: resource.ComposeTestCheckFunc( 101 testAccCheckAwsAmiDataSourceID("data.aws_ami.instance_store_ami"), 102 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "architecture", "x86_64"), 103 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "block_device_mappings.#", "0"), 104 resource.TestMatchResourceAttr("data.aws_ami.instance_store_ami", "creation_date", regexp.MustCompile("^20[0-9]{2}-")), 105 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "hypervisor", "xen"), 106 resource.TestMatchResourceAttr("data.aws_ami.instance_store_ami", "image_id", regexp.MustCompile("^ami-")), 107 resource.TestMatchResourceAttr("data.aws_ami.instance_store_ami", "image_location", regexp.MustCompile("images/hvm-instance/ubuntu-trusty-14.04-amd64-server")), 108 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "image_type", "machine"), 109 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "most_recent", "true"), 110 resource.TestMatchResourceAttr("data.aws_ami.instance_store_ami", "name", regexp.MustCompile("^ubuntu/images/hvm-instance/ubuntu-trusty-14.04-amd64-server")), 111 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "owner_id", "099720109477"), 112 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "public", "true"), 113 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "product_codes.#", "0"), 114 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "root_device_type", "instance-store"), 115 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "sriov_net_support", "simple"), 116 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "state", "available"), 117 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "state_reason.code", "UNSET"), 118 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "state_reason.message", "UNSET"), 119 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "tags.#", "0"), 120 resource.TestCheckResourceAttr("data.aws_ami.instance_store_ami", "virtualization_type", "hvm"), 121 ), 122 }, 123 }, 124 }) 125 } 126 127 func TestAccAWSAmiDataSource_owners(t *testing.T) { 128 resource.Test(t, resource.TestCase{ 129 PreCheck: func() { testAccPreCheck(t) }, 130 Providers: testAccProviders, 131 Steps: []resource.TestStep{ 132 resource.TestStep{ 133 Config: testAccCheckAwsAmiDataSourceOwnersConfig, 134 Check: resource.ComposeTestCheckFunc( 135 testAccCheckAwsAmiDataSourceID("data.aws_ami.amazon_ami"), 136 ), 137 }, 138 }, 139 }) 140 } 141 142 func TestAccAWSAmiDataSource_localNameFilter(t *testing.T) { 143 resource.Test(t, resource.TestCase{ 144 PreCheck: func() { testAccPreCheck(t) }, 145 Providers: testAccProviders, 146 Steps: []resource.TestStep{ 147 resource.TestStep{ 148 Config: testAccCheckAwsAmiDataSourceNameRegexConfig, 149 Check: resource.ComposeTestCheckFunc( 150 testAccCheckAwsAmiDataSourceID("data.aws_ami.name_regex_filtered_ami"), 151 resource.TestMatchResourceAttr("data.aws_ami.name_regex_filtered_ami", "image_id", regexp.MustCompile("^ami-")), 152 ), 153 }, 154 }, 155 }) 156 } 157 158 func TestResourceValidateNameRegex(t *testing.T) { 159 type testCases struct { 160 Value string 161 ErrCount int 162 } 163 164 invalidCases := []testCases{ 165 { 166 Value: `\`, 167 ErrCount: 1, 168 }, 169 { 170 Value: `**`, 171 ErrCount: 1, 172 }, 173 { 174 Value: `(.+`, 175 ErrCount: 1, 176 }, 177 } 178 179 for _, tc := range invalidCases { 180 _, errors := validateNameRegex(tc.Value, "name_regex") 181 if len(errors) != tc.ErrCount { 182 t.Fatalf("Expected %q to trigger a validation error.", tc.Value) 183 } 184 } 185 186 validCases := []testCases{ 187 { 188 Value: `\/`, 189 ErrCount: 0, 190 }, 191 { 192 Value: `.*`, 193 ErrCount: 0, 194 }, 195 { 196 Value: `\b(?:\d{1,3}\.){3}\d{1,3}\b`, 197 ErrCount: 0, 198 }, 199 } 200 201 for _, tc := range validCases { 202 _, errors := validateNameRegex(tc.Value, "name_regex") 203 if len(errors) != tc.ErrCount { 204 t.Fatalf("Expected %q not to trigger a validation error.", tc.Value) 205 } 206 } 207 } 208 209 func testAccCheckAwsAmiDataSourceDestroy(s *terraform.State) error { 210 return nil 211 } 212 213 func testAccCheckAwsAmiDataSourceID(n string) resource.TestCheckFunc { 214 // Wait for IAM role 215 return func(s *terraform.State) error { 216 rs, ok := s.RootModule().Resources[n] 217 if !ok { 218 return fmt.Errorf("Can't find AMI data source: %s", n) 219 } 220 221 if rs.Primary.ID == "" { 222 return fmt.Errorf("AMI data source ID not set") 223 } 224 return nil 225 } 226 } 227 228 // Using NAT AMIs for testing - I would expect with NAT gateways now a thing, 229 // that this will possibly be deprecated at some point in time. Other candidates 230 // for testing this after that may be Ubuntu's AMI's, or Amazon's regular 231 // Amazon Linux AMIs. 232 const testAccCheckAwsAmiDataSourceConfig = ` 233 data "aws_ami" "nat_ami" { 234 most_recent = true 235 filter { 236 name = "owner-alias" 237 values = ["amazon"] 238 } 239 filter { 240 name = "name" 241 values = ["amzn-ami-vpc-nat*"] 242 } 243 filter { 244 name = "virtualization-type" 245 values = ["hvm"] 246 } 247 filter { 248 name = "root-device-type" 249 values = ["ebs"] 250 } 251 filter { 252 name = "block-device-mapping.volume-type" 253 values = ["standard"] 254 } 255 } 256 ` 257 258 // Windows image test. 259 const testAccCheckAwsAmiDataSourceWindowsConfig = ` 260 data "aws_ami" "windows_ami" { 261 most_recent = true 262 filter { 263 name = "owner-alias" 264 values = ["amazon"] 265 } 266 filter { 267 name = "name" 268 values = ["Windows_Server-2012-R2*"] 269 } 270 filter { 271 name = "virtualization-type" 272 values = ["hvm"] 273 } 274 filter { 275 name = "root-device-type" 276 values = ["ebs"] 277 } 278 filter { 279 name = "block-device-mapping.volume-type" 280 values = ["gp2"] 281 } 282 } 283 ` 284 285 // Instance store test - using Ubuntu images 286 const testAccCheckAwsAmiDataSourceInstanceStoreConfig = ` 287 data "aws_ami" "instance_store_ami" { 288 most_recent = true 289 filter { 290 name = "owner-id" 291 values = ["099720109477"] 292 } 293 filter { 294 name = "name" 295 values = ["ubuntu/images/hvm-instance/ubuntu-trusty-14.04-amd64-server*"] 296 } 297 filter { 298 name = "virtualization-type" 299 values = ["hvm"] 300 } 301 filter { 302 name = "root-device-type" 303 values = ["instance-store"] 304 } 305 } 306 ` 307 308 // Testing owner parameter 309 const testAccCheckAwsAmiDataSourceOwnersConfig = ` 310 data "aws_ami" "amazon_ami" { 311 most_recent = true 312 owners = ["amazon"] 313 } 314 ` 315 316 // Testing name_regex parameter 317 const testAccCheckAwsAmiDataSourceNameRegexConfig = ` 318 data "aws_ami" "name_regex_filtered_ami" { 319 most_recent = true 320 owners = ["amazon"] 321 filter { 322 name = "name" 323 values = ["amzn-ami-*"] 324 } 325 name_regex = "^amzn-ami-\\d{3}[5].*-ecs-optimized" 326 } 327 `