github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  			{
    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  			{
    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  			{
    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  			{
   133  				Config: testAccCheckAwsAmiDataSourceOwnersConfig,
   134  				Check: resource.ComposeTestCheckFunc(
   135  					testAccCheckAwsAmiDataSourceID("data.aws_ami.amazon_ami"),
   136  				),
   137  			},
   138  		},
   139  	})
   140  }
   141  
   142  // Acceptance test for: https://github.com/hashicorp/terraform/issues/10758
   143  func TestAccAWSAmiDataSource_ownersEmpty(t *testing.T) {
   144  	resource.Test(t, resource.TestCase{
   145  		PreCheck:  func() { testAccPreCheck(t) },
   146  		Providers: testAccProviders,
   147  		Steps: []resource.TestStep{
   148  			{
   149  				Config: testAccCheckAwsAmiDataSourceEmptyOwnersConfig,
   150  				Check: resource.ComposeTestCheckFunc(
   151  					testAccCheckAwsAmiDataSourceID("data.aws_ami.amazon_ami"),
   152  				),
   153  			},
   154  		},
   155  	})
   156  }
   157  
   158  func TestAccAWSAmiDataSource_localNameFilter(t *testing.T) {
   159  	resource.Test(t, resource.TestCase{
   160  		PreCheck:  func() { testAccPreCheck(t) },
   161  		Providers: testAccProviders,
   162  		Steps: []resource.TestStep{
   163  			{
   164  				Config: testAccCheckAwsAmiDataSourceNameRegexConfig,
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckAwsAmiDataSourceID("data.aws_ami.name_regex_filtered_ami"),
   167  					resource.TestMatchResourceAttr("data.aws_ami.name_regex_filtered_ami", "image_id", regexp.MustCompile("^ami-")),
   168  				),
   169  			},
   170  		},
   171  	})
   172  }
   173  
   174  func TestResourceValidateNameRegex(t *testing.T) {
   175  	type testCases struct {
   176  		Value    string
   177  		ErrCount int
   178  	}
   179  
   180  	invalidCases := []testCases{
   181  		{
   182  			Value:    `\`,
   183  			ErrCount: 1,
   184  		},
   185  		{
   186  			Value:    `**`,
   187  			ErrCount: 1,
   188  		},
   189  		{
   190  			Value:    `(.+`,
   191  			ErrCount: 1,
   192  		},
   193  	}
   194  
   195  	for _, tc := range invalidCases {
   196  		_, errors := validateNameRegex(tc.Value, "name_regex")
   197  		if len(errors) != tc.ErrCount {
   198  			t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
   199  		}
   200  	}
   201  
   202  	validCases := []testCases{
   203  		{
   204  			Value:    `\/`,
   205  			ErrCount: 0,
   206  		},
   207  		{
   208  			Value:    `.*`,
   209  			ErrCount: 0,
   210  		},
   211  		{
   212  			Value:    `\b(?:\d{1,3}\.){3}\d{1,3}\b`,
   213  			ErrCount: 0,
   214  		},
   215  	}
   216  
   217  	for _, tc := range validCases {
   218  		_, errors := validateNameRegex(tc.Value, "name_regex")
   219  		if len(errors) != tc.ErrCount {
   220  			t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
   221  		}
   222  	}
   223  }
   224  
   225  func testAccCheckAwsAmiDataSourceDestroy(s *terraform.State) error {
   226  	return nil
   227  }
   228  
   229  func testAccCheckAwsAmiDataSourceID(n string) resource.TestCheckFunc {
   230  	// Wait for IAM role
   231  	return func(s *terraform.State) error {
   232  		rs, ok := s.RootModule().Resources[n]
   233  		if !ok {
   234  			return fmt.Errorf("Can't find AMI data source: %s", n)
   235  		}
   236  
   237  		if rs.Primary.ID == "" {
   238  			return fmt.Errorf("AMI data source ID not set")
   239  		}
   240  		return nil
   241  	}
   242  }
   243  
   244  // Using NAT AMIs for testing - I would expect with NAT gateways now a thing,
   245  // that this will possibly be deprecated at some point in time. Other candidates
   246  // for testing this after that may be Ubuntu's AMI's, or Amazon's regular
   247  // Amazon Linux AMIs.
   248  const testAccCheckAwsAmiDataSourceConfig = `
   249  data "aws_ami" "nat_ami" {
   250  	most_recent = true
   251  	filter {
   252  		name = "owner-alias"
   253  		values = ["amazon"]
   254  	}
   255  	filter {
   256  		name = "name"
   257  		values = ["amzn-ami-vpc-nat*"]
   258  	}
   259  	filter {
   260  		name = "virtualization-type"
   261  		values = ["hvm"]
   262  	}
   263  	filter {
   264  		name = "root-device-type"
   265  		values = ["ebs"]
   266  	}
   267  	filter {
   268  		name = "block-device-mapping.volume-type"
   269  		values = ["standard"]
   270  	}
   271  }
   272  `
   273  
   274  // Windows image test.
   275  const testAccCheckAwsAmiDataSourceWindowsConfig = `
   276  data "aws_ami" "windows_ami" {
   277  	most_recent = true
   278  	filter {
   279  		name = "owner-alias"
   280  		values = ["amazon"]
   281  	}
   282  	filter {
   283  		name = "name"
   284  		values = ["Windows_Server-2012-R2*"]
   285  	}
   286  	filter {
   287  		name = "virtualization-type"
   288  		values = ["hvm"]
   289  	}
   290  	filter {
   291  		name = "root-device-type"
   292  		values = ["ebs"]
   293  	}
   294  	filter {
   295  		name = "block-device-mapping.volume-type"
   296  		values = ["gp2"]
   297  	}
   298  }
   299  `
   300  
   301  // Instance store test - using Ubuntu images
   302  const testAccCheckAwsAmiDataSourceInstanceStoreConfig = `
   303  data "aws_ami" "instance_store_ami" {
   304  	most_recent = true
   305  	filter {
   306  		name = "owner-id"
   307  		values = ["099720109477"]
   308  	}
   309  	filter {
   310  		name = "name"
   311  		values = ["ubuntu/images/hvm-instance/ubuntu-trusty-14.04-amd64-server*"]
   312  	}
   313  	filter {
   314  		name = "virtualization-type"
   315  		values = ["hvm"]
   316  	}
   317  	filter {
   318  		name = "root-device-type"
   319  		values = ["instance-store"]
   320  	}
   321  }
   322  `
   323  
   324  // Testing owner parameter
   325  const testAccCheckAwsAmiDataSourceOwnersConfig = `
   326  data "aws_ami" "amazon_ami" {
   327  	most_recent = true
   328  	owners = ["amazon"]
   329  }
   330  `
   331  
   332  const testAccCheckAwsAmiDataSourceEmptyOwnersConfig = `
   333  data "aws_ami" "amazon_ami" {
   334  	most_recent = true
   335  	owners = [""]
   336  }
   337  `
   338  
   339  // Testing name_regex parameter
   340  const testAccCheckAwsAmiDataSourceNameRegexConfig = `
   341  data "aws_ami" "name_regex_filtered_ami" {
   342  	most_recent = true
   343  	owners = ["amazon"]
   344  	filter {
   345  		name = "name"
   346  		values = ["amzn-ami-*"]
   347  	}
   348  	name_regex = "^amzn-ami-\\d{3}[5].*-ecs-optimized"
   349  }
   350  `