github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/aws/data_source_aws_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"testing"
     5  
     6  	"fmt"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  )
    11  
    12  func TestAccAWSInstanceDataSource_basic(t *testing.T) {
    13  	resource.Test(t, resource.TestCase{
    14  		PreCheck:  func() { testAccPreCheck(t) },
    15  		Providers: testAccProviders,
    16  		Steps: []resource.TestStep{
    17  			{
    18  				Config: testAccInstanceDataSourceConfig,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					resource.TestCheckResourceAttr(
    21  						"data.aws_instance.web-instance", "ami", "ami-4fccb37f"),
    22  					resource.TestCheckResourceAttr(
    23  						"data.aws_instance.web-instance", "tags.#", "1"),
    24  					resource.TestCheckResourceAttr(
    25  						"data.aws_instance.web-instance", "instance_type", "m1.small"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSInstanceDataSource_tags(t *testing.T) {
    33  	rInt := acctest.RandInt()
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:  func() { testAccPreCheck(t) },
    36  		Providers: testAccProviders,
    37  		Steps: []resource.TestStep{
    38  			{
    39  				Config: testAccInstanceDataSourceConfig_Tags(rInt),
    40  				Check: resource.ComposeTestCheckFunc(
    41  					resource.TestCheckResourceAttr(
    42  						"data.aws_instance.web-instance", "ami", "ami-4fccb37f"),
    43  					resource.TestCheckResourceAttr(
    44  						"data.aws_instance.web-instance", "tags.#", "2"),
    45  					resource.TestCheckResourceAttr(
    46  						"data.aws_instance.web-instance", "instance_type", "m1.small"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) {
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:  func() { testAccPreCheck(t) },
    56  		Providers: testAccProviders,
    57  		Steps: []resource.TestStep{
    58  			{
    59  				Config: testAccInstanceDataSourceConfig_AzUserData,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					resource.TestCheckResourceAttr(
    62  						"data.aws_instance.foo", "ami", "ami-4fccb37f"),
    63  					resource.TestCheckResourceAttr(
    64  						"data.aws_instance.foo", "tags.#", "1"),
    65  					resource.TestCheckResourceAttr(
    66  						"data.aws_instance.foo", "instance_type", "m1.small"),
    67  					resource.TestCheckResourceAttr(
    68  						"data.aws_instance.foo", "availability_zone", "us-west-2a"),
    69  					resource.TestCheckResourceAttr(
    70  						"data.aws_instance.foo", "user_data", "3dc39dda39be1205215e776bad998da361a5955d"),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  
    77  func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) {
    78  	resource.Test(t, resource.TestCase{
    79  		PreCheck:  func() { testAccPreCheck(t) },
    80  		Providers: testAccProviders,
    81  		Steps: []resource.TestStep{
    82  			{
    83  				Config: testAccInstanceDataSourceConfig_gp2IopsDevice,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					resource.TestCheckResourceAttr(
    86  						"data.aws_instance.foo", "ami", "ami-55a7ea65"),
    87  					resource.TestCheckResourceAttr(
    88  						"data.aws_instance.foo", "instance_type", "m3.medium"),
    89  					resource.TestCheckResourceAttr(
    90  						"aws_instance.foo", "root_block_device.#", "1"),
    91  					resource.TestCheckResourceAttr(
    92  						"aws_instance.foo", "root_block_device.0.volume_size", "11"),
    93  					resource.TestCheckResourceAttr(
    94  						"aws_instance.foo", "root_block_device.0.volume_type", "gp2"),
    95  					resource.TestCheckResourceAttr(
    96  						"aws_instance.foo", "root_block_device.0.iops", "100"),
    97  				),
    98  			},
    99  		},
   100  	})
   101  }
   102  
   103  func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) {
   104  	resource.Test(t, resource.TestCase{
   105  		PreCheck:  func() { testAccPreCheck(t) },
   106  		Providers: testAccProviders,
   107  		Steps: []resource.TestStep{
   108  			{
   109  				Config: testAccInstanceDataSourceConfig_blockDevices,
   110  				Check: resource.ComposeTestCheckFunc(
   111  					resource.TestCheckResourceAttr(
   112  						"data.aws_instance.foo", "ami", "ami-55a7ea65"),
   113  					resource.TestCheckResourceAttr(
   114  						"data.aws_instance.foo", "instance_type", "m3.medium"),
   115  					resource.TestCheckResourceAttr(
   116  						"aws_instance.foo", "root_block_device.#", "1"),
   117  					resource.TestCheckResourceAttr(
   118  						"aws_instance.foo", "root_block_device.0.volume_size", "11"),
   119  					resource.TestCheckResourceAttr(
   120  						"aws_instance.foo", "root_block_device.0.volume_type", "gp2"),
   121  					resource.TestCheckResourceAttr(
   122  						"aws_instance.foo", "ebs_block_device.#", "3"),
   123  					resource.TestCheckResourceAttr(
   124  						"aws_instance.foo", "ephemeral_block_device.#", "1"),
   125  				),
   126  			},
   127  		},
   128  	})
   129  }
   130  
   131  func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) {
   132  	resource.Test(t, resource.TestCase{
   133  		PreCheck:  func() { testAccPreCheck(t) },
   134  		Providers: testAccProviders,
   135  		Steps: []resource.TestStep{
   136  			{
   137  				Config: testAccInstanceDataSourceConfig_rootInstanceStore,
   138  				Check: resource.ComposeTestCheckFunc(
   139  					resource.TestCheckResourceAttr(
   140  						"data.aws_instance.foo", "ami", "ami-44c36524"),
   141  					resource.TestCheckResourceAttr(
   142  						"data.aws_instance.foo", "instance_type", "m3.medium"),
   143  					resource.TestCheckResourceAttr(
   144  						"aws_instance.foo", "ebs_block_device.#", "0"),
   145  					resource.TestCheckResourceAttr(
   146  						"aws_instance.foo", "ebs_optimized", "false"),
   147  					resource.TestCheckResourceAttr(
   148  						"aws_instance.foo", "root_block_device.#", "0"),
   149  				),
   150  			},
   151  		},
   152  	})
   153  }
   154  
   155  func TestAccAWSInstanceDataSource_privateIP(t *testing.T) {
   156  	resource.Test(t, resource.TestCase{
   157  		PreCheck:  func() { testAccPreCheck(t) },
   158  		Providers: testAccProviders,
   159  		Steps: []resource.TestStep{
   160  			{
   161  				Config: testAccInstanceDataSourceConfig_privateIP,
   162  				Check: resource.ComposeTestCheckFunc(
   163  					resource.TestCheckResourceAttr(
   164  						"data.aws_instance.foo", "ami", "ami-c5eabbf5"),
   165  					resource.TestCheckResourceAttr(
   166  						"data.aws_instance.foo", "instance_type", "t2.micro"),
   167  					resource.TestCheckResourceAttr(
   168  						"data.aws_instance.foo", "private_ip", "10.1.1.42"),
   169  				),
   170  			},
   171  		},
   172  	})
   173  }
   174  
   175  func TestAccAWSInstanceDataSource_keyPair(t *testing.T) {
   176  	rName := fmt.Sprintf("tf-test-key-%d", acctest.RandInt())
   177  	resource.Test(t, resource.TestCase{
   178  		PreCheck:  func() { testAccPreCheck(t) },
   179  		Providers: testAccProviders,
   180  		Steps: []resource.TestStep{
   181  			{
   182  				Config: testAccInstanceDataSourceConfig_keyPair(rName),
   183  				Check: resource.ComposeTestCheckFunc(
   184  					resource.TestCheckResourceAttr(
   185  						"data.aws_instance.foo", "ami", "ami-408c7f28"),
   186  					resource.TestCheckResourceAttr(
   187  						"data.aws_instance.foo", "instance_type", "t1.micro"),
   188  					resource.TestCheckResourceAttr(
   189  						"data.aws_instance.foo", "tags.#", "1"),
   190  					resource.TestCheckResourceAttr(
   191  						"data.aws_instance.foo", "key_name", rName),
   192  				),
   193  			},
   194  		},
   195  	})
   196  }
   197  
   198  func TestAccAWSInstanceDataSource_VPC(t *testing.T) {
   199  	resource.Test(t, resource.TestCase{
   200  		PreCheck:  func() { testAccPreCheck(t) },
   201  		Providers: testAccProviders,
   202  		Steps: []resource.TestStep{
   203  			{
   204  				Config: testAccInstanceDataSourceConfig_VPC,
   205  				Check: resource.ComposeTestCheckFunc(
   206  					resource.TestCheckResourceAttr(
   207  						"data.aws_instance.foo", "ami", "ami-4fccb37f"),
   208  					resource.TestCheckResourceAttr(
   209  						"data.aws_instance.foo", "instance_type", "m1.small"),
   210  					resource.TestCheckResourceAttr(
   211  						"data.aws_instance.foo", "user_data", "562a3e32810edf6ff09994f050f12e799452379d"),
   212  					resource.TestCheckResourceAttr(
   213  						"data.aws_instance.foo", "associate_public_ip_address", "true"),
   214  					resource.TestCheckResourceAttr(
   215  						"data.aws_instance.foo", "tenancy", "dedicated"),
   216  				),
   217  			},
   218  		},
   219  	})
   220  }
   221  
   222  func TestAccAWSInstanceDataSource_SecurityGroups(t *testing.T) {
   223  	rInt := acctest.RandInt()
   224  	resource.Test(t, resource.TestCase{
   225  		PreCheck:  func() { testAccPreCheck(t) },
   226  		Providers: testAccProviders,
   227  		Steps: []resource.TestStep{
   228  			{
   229  				Config: testAccInstanceDataSourceConfig_SecurityGroups(rInt),
   230  				Check: resource.ComposeTestCheckFunc(
   231  					resource.TestCheckResourceAttr(
   232  						"data.aws_instance.foo", "ami", "ami-408c7f28"),
   233  					resource.TestCheckResourceAttr(
   234  						"data.aws_instance.foo", "instance_type", "m1.small"),
   235  					resource.TestCheckResourceAttr(
   236  						"data.aws_instance.foo", "vpc_security_group_ids.#", "0"),
   237  					resource.TestCheckResourceAttr(
   238  						"data.aws_instance.foo", "security_groups.#", "1"),
   239  					resource.TestCheckResourceAttr(
   240  						"data.aws_instance.foo", "user_data", "3dc39dda39be1205215e776bad998da361a5955d"),
   241  				),
   242  			},
   243  		},
   244  	})
   245  }
   246  
   247  func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) {
   248  	resource.Test(t, resource.TestCase{
   249  		PreCheck:  func() { testAccPreCheck(t) },
   250  		Providers: testAccProviders,
   251  		Steps: []resource.TestStep{
   252  			{
   253  				Config: testAccInstanceDataSourceConfig_VPCSecurityGroups,
   254  				Check: resource.ComposeTestCheckFunc(
   255  					resource.TestCheckResourceAttr(
   256  						"data.aws_instance.foo", "ami", "ami-21f78e11"),
   257  					resource.TestCheckResourceAttr(
   258  						"data.aws_instance.foo", "instance_type", "t1.micro"),
   259  					resource.TestCheckResourceAttr(
   260  						"data.aws_instance.foo", "security_groups.#", "0"),
   261  					resource.TestCheckResourceAttr(
   262  						"data.aws_instance.foo", "vpc_security_group_ids.#", "1"),
   263  				),
   264  			},
   265  		},
   266  	})
   267  }
   268  
   269  // Lookup based on InstanceID
   270  const testAccInstanceDataSourceConfig = `
   271  resource "aws_instance" "web" {
   272  	# us-west-2
   273    ami = "ami-4fccb37f"
   274    instance_type = "m1.small"
   275    tags {
   276      Name = "HelloWorld"
   277    }
   278  }
   279  
   280  data "aws_instance" "web-instance" {
   281    filter {
   282      name = "instance-id"
   283      values = ["${aws_instance.web.id}"]
   284    }
   285  }
   286  `
   287  
   288  // Use the tags attribute to filter
   289  func testAccInstanceDataSourceConfig_Tags(rInt int) string {
   290  	return fmt.Sprintf(`
   291  resource "aws_instance" "web" {
   292  	# us-west-2
   293    ami = "ami-4fccb37f"
   294    instance_type = "m1.small"
   295    tags {
   296      Name = "HelloWorld"
   297      TestSeed = "%d"
   298    }
   299  }
   300  
   301  data "aws_instance" "web-instance" {
   302    instance_tags {
   303      Name = "${aws_instance.web.tags["Name"]}"
   304      TestSeed = "%d"
   305    }
   306  }
   307  `, rInt, rInt)
   308  }
   309  
   310  // filter on tag, populate more attributes
   311  const testAccInstanceDataSourceConfig_AzUserData = `
   312  resource "aws_instance" "foo" {
   313  	# us-west-2
   314  	ami = "ami-4fccb37f"
   315  	availability_zone = "us-west-2a"
   316  
   317  	instance_type = "m1.small"
   318  	user_data = "foo:-with-character's"
   319  	tags {
   320  	  TFAccTest = "YesThisIsATest"
   321  	}
   322  }
   323  
   324  data "aws_instance" "foo" {
   325    instance_id = "${aws_instance.foo.id}"
   326  }
   327  `
   328  
   329  // GP2IopsDevice
   330  const testAccInstanceDataSourceConfig_gp2IopsDevice = `
   331  resource "aws_instance" "foo" {
   332  	# us-west-2
   333  	ami = "ami-55a7ea65"
   334  	instance_type = "m3.medium"
   335  	root_block_device {
   336  		volume_type = "gp2"
   337  		volume_size = 11
   338  	}
   339  }
   340  
   341  data "aws_instance" "foo" {
   342    instance_id = "${aws_instance.foo.id}"
   343  }
   344  `
   345  
   346  // Block Device
   347  const testAccInstanceDataSourceConfig_blockDevices = `
   348  resource "aws_instance" "foo" {
   349  	# us-west-2
   350  	ami = "ami-55a7ea65"
   351  	instance_type = "m3.medium"
   352  
   353  	root_block_device {
   354  		volume_type = "gp2"
   355  		volume_size = 11
   356  	}
   357  	ebs_block_device {
   358  		device_name = "/dev/sdb"
   359  		volume_size = 9
   360  	}
   361  	ebs_block_device {
   362  		device_name = "/dev/sdc"
   363  		volume_size = 10
   364  		volume_type = "io1"
   365  		iops = 100
   366  	}
   367  
   368  	# Encrypted ebs block device
   369  	ebs_block_device {
   370  		device_name = "/dev/sdd"
   371  		volume_size = 12
   372  		encrypted = true
   373  	}
   374  
   375  	ephemeral_block_device {
   376  		device_name = "/dev/sde"
   377  		virtual_name = "ephemeral0"
   378  	}
   379  }
   380  
   381  data "aws_instance" "foo" {
   382    instance_id = "${aws_instance.foo.id}"
   383  }
   384  `
   385  
   386  const testAccInstanceDataSourceConfig_rootInstanceStore = `
   387  resource "aws_instance" "foo" {
   388    ami = "ami-44c36524"
   389    instance_type = "m3.medium"
   390  }
   391  data "aws_instance" "foo" {
   392    instance_id = "${aws_instance.foo.id}"
   393  }
   394  `
   395  
   396  const testAccInstanceDataSourceConfig_privateIP = `
   397  resource "aws_vpc" "foo" {
   398  	cidr_block = "10.1.0.0/16"
   399  	tags {
   400  		Name = "testAccInstanceDataSourceConfig_privateIP"
   401  	}
   402  }
   403  
   404  resource "aws_subnet" "foo" {
   405  	cidr_block = "10.1.1.0/24"
   406  	vpc_id = "${aws_vpc.foo.id}"
   407  }
   408  
   409  resource "aws_instance" "foo" {
   410  	ami = "ami-c5eabbf5"
   411  	instance_type = "t2.micro"
   412  	subnet_id = "${aws_subnet.foo.id}"
   413  	private_ip = "10.1.1.42"
   414  }
   415  
   416  data "aws_instance" "foo" {
   417    instance_id = "${aws_instance.foo.id}"
   418  }
   419  `
   420  
   421  func testAccInstanceDataSourceConfig_keyPair(rName string) string {
   422  	return fmt.Sprintf(`
   423  provider "aws" {
   424  	region = "us-east-1"
   425  }
   426  
   427  resource "aws_key_pair" "debugging" {
   428  	key_name = "%s"
   429  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   430  }
   431  
   432  resource "aws_instance" "foo" {
   433    ami = "ami-408c7f28"
   434    instance_type = "t1.micro"
   435    key_name = "${aws_key_pair.debugging.key_name}"
   436  	tags {
   437  		Name = "testAccInstanceDataSourceConfigKeyPair_TestAMI"
   438  	}
   439  }
   440  
   441  data "aws_instance" "foo" {
   442    filter {
   443      name = "tag:Name"
   444      values = ["testAccInstanceDataSourceConfigKeyPair_TestAMI"]
   445    }
   446    filter {
   447      name = "key-name"
   448      values = ["${aws_instance.foo.key_name}"]
   449    }
   450  }`, rName)
   451  }
   452  
   453  const testAccInstanceDataSourceConfig_VPC = `
   454  resource "aws_vpc" "foo" {
   455  	cidr_block = "10.1.0.0/16"
   456  	tags {
   457  		Name = "testAccInstanceDataSourceConfig_VPC"
   458  	}
   459  }
   460  
   461  resource "aws_subnet" "foo" {
   462  	cidr_block = "10.1.1.0/24"
   463  	vpc_id = "${aws_vpc.foo.id}"
   464  }
   465  
   466  resource "aws_instance" "foo" {
   467  	# us-west-2
   468  	ami = "ami-4fccb37f"
   469  	instance_type = "m1.small"
   470  	subnet_id = "${aws_subnet.foo.id}"
   471  	associate_public_ip_address = true
   472  	tenancy = "dedicated"
   473  	# pre-encoded base64 data
   474  	user_data = "3dc39dda39be1205215e776bad998da361a5955d"
   475  }
   476  
   477  data "aws_instance" "foo" {
   478    instance_id = "${aws_instance.foo.id}"
   479  }
   480  `
   481  
   482  func testAccInstanceDataSourceConfig_SecurityGroups(rInt int) string {
   483  	return fmt.Sprintf(`
   484  provider "aws" {
   485  	region = "us-east-1"
   486  }
   487  
   488  resource "aws_security_group" "tf_test_foo" {
   489  	name = "tf_test_foo-%d"
   490  	description = "foo"
   491  
   492  	ingress {
   493  		protocol = "icmp"
   494  		from_port = -1
   495  		to_port = -1
   496  		cidr_blocks = ["0.0.0.0/0"]
   497  	}
   498  }
   499  
   500  resource "aws_instance" "foo" {
   501  	ami = "ami-408c7f28"
   502  	instance_type = "m1.small"
   503  	security_groups = ["${aws_security_group.tf_test_foo.name}"]
   504  	user_data = "foo:-with-character's"
   505  }
   506  
   507  data "aws_instance" "foo" {
   508    instance_id = "${aws_instance.foo.id}"
   509  }
   510  `, rInt)
   511  }
   512  
   513  const testAccInstanceDataSourceConfig_VPCSecurityGroups = `
   514  resource "aws_internet_gateway" "gw" {
   515    vpc_id = "${aws_vpc.foo.id}"
   516  }
   517  
   518  resource "aws_vpc" "foo" {
   519    cidr_block = "10.1.0.0/16"
   520  	tags {
   521  		Name = "tf-network-test"
   522  	}
   523  }
   524  
   525  resource "aws_security_group" "tf_test_foo" {
   526    name = "tf_test_foo"
   527    description = "foo"
   528    vpc_id="${aws_vpc.foo.id}"
   529  
   530    ingress {
   531      protocol = "icmp"
   532      from_port = -1
   533      to_port = -1
   534      cidr_blocks = ["0.0.0.0/0"]
   535    }
   536  }
   537  
   538  resource "aws_subnet" "foo" {
   539    cidr_block = "10.1.1.0/24"
   540    vpc_id = "${aws_vpc.foo.id}"
   541  }
   542  
   543  resource "aws_instance" "foo_instance" {
   544    ami = "ami-21f78e11"
   545    instance_type = "t1.micro"
   546    vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"]
   547    subnet_id = "${aws_subnet.foo.id}"
   548  	depends_on = ["aws_internet_gateway.gw"]
   549  }
   550  
   551  data "aws_instance" "foo" {
   552    instance_id = "${aws_instance.foo_instance.id}"
   553  }
   554  `