github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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  }
   400  
   401  resource "aws_subnet" "foo" {
   402  	cidr_block = "10.1.1.0/24"
   403  	vpc_id = "${aws_vpc.foo.id}"
   404  }
   405  
   406  resource "aws_instance" "foo" {
   407  	ami = "ami-c5eabbf5"
   408  	instance_type = "t2.micro"
   409  	subnet_id = "${aws_subnet.foo.id}"
   410  	private_ip = "10.1.1.42"
   411  }
   412  
   413  data "aws_instance" "foo" {
   414    instance_id = "${aws_instance.foo.id}"
   415  }
   416  `
   417  
   418  func testAccInstanceDataSourceConfig_keyPair(rName string) string {
   419  	return fmt.Sprintf(`
   420  provider "aws" {
   421  	region = "us-east-1"
   422  }
   423  
   424  resource "aws_key_pair" "debugging" {
   425  	key_name = "%s"
   426  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   427  }
   428  
   429  resource "aws_instance" "foo" {
   430    ami = "ami-408c7f28"
   431    instance_type = "t1.micro"
   432    key_name = "${aws_key_pair.debugging.key_name}"
   433  	tags {
   434  		Name = "testAccInstanceDataSourceConfigKeyPair_TestAMI"
   435  	}
   436  }
   437  
   438  data "aws_instance" "foo" {
   439    filter {
   440      name = "tag:Name"
   441      values = ["testAccInstanceDataSourceConfigKeyPair_TestAMI"]
   442    }
   443    filter {
   444      name = "key-name"
   445      values = ["${aws_instance.foo.key_name}"]
   446    }
   447  }`, rName)
   448  }
   449  
   450  const testAccInstanceDataSourceConfig_VPC = `
   451  resource "aws_vpc" "foo" {
   452  	cidr_block = "10.1.0.0/16"
   453  }
   454  
   455  resource "aws_subnet" "foo" {
   456  	cidr_block = "10.1.1.0/24"
   457  	vpc_id = "${aws_vpc.foo.id}"
   458  }
   459  
   460  resource "aws_instance" "foo" {
   461  	# us-west-2
   462  	ami = "ami-4fccb37f"
   463  	instance_type = "m1.small"
   464  	subnet_id = "${aws_subnet.foo.id}"
   465  	associate_public_ip_address = true
   466  	tenancy = "dedicated"
   467  	# pre-encoded base64 data
   468  	user_data = "3dc39dda39be1205215e776bad998da361a5955d"
   469  }
   470  
   471  data "aws_instance" "foo" {
   472    instance_id = "${aws_instance.foo.id}"
   473  }
   474  `
   475  
   476  func testAccInstanceDataSourceConfig_SecurityGroups(rInt int) string {
   477  	return fmt.Sprintf(`
   478  provider "aws" {
   479  	region = "us-east-1"
   480  }
   481  
   482  resource "aws_security_group" "tf_test_foo" {
   483  	name = "tf_test_foo-%d"
   484  	description = "foo"
   485  
   486  	ingress {
   487  		protocol = "icmp"
   488  		from_port = -1
   489  		to_port = -1
   490  		cidr_blocks = ["0.0.0.0/0"]
   491  	}
   492  }
   493  
   494  resource "aws_instance" "foo" {
   495  	ami = "ami-408c7f28"
   496  	instance_type = "m1.small"
   497  	security_groups = ["${aws_security_group.tf_test_foo.name}"]
   498  	user_data = "foo:-with-character's"
   499  }
   500  
   501  data "aws_instance" "foo" {
   502    instance_id = "${aws_instance.foo.id}"
   503  }
   504  `, rInt)
   505  }
   506  
   507  const testAccInstanceDataSourceConfig_VPCSecurityGroups = `
   508  resource "aws_internet_gateway" "gw" {
   509    vpc_id = "${aws_vpc.foo.id}"
   510  }
   511  
   512  resource "aws_vpc" "foo" {
   513    cidr_block = "10.1.0.0/16"
   514  	tags {
   515  		Name = "tf-network-test"
   516  	}
   517  }
   518  
   519  resource "aws_security_group" "tf_test_foo" {
   520    name = "tf_test_foo"
   521    description = "foo"
   522    vpc_id="${aws_vpc.foo.id}"
   523  
   524    ingress {
   525      protocol = "icmp"
   526      from_port = -1
   527      to_port = -1
   528      cidr_blocks = ["0.0.0.0/0"]
   529    }
   530  }
   531  
   532  resource "aws_subnet" "foo" {
   533    cidr_block = "10.1.1.0/24"
   534    vpc_id = "${aws_vpc.foo.id}"
   535  }
   536  
   537  resource "aws_instance" "foo_instance" {
   538    ami = "ami-21f78e11"
   539    instance_type = "t1.micro"
   540    vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"]
   541    subnet_id = "${aws_subnet.foo.id}"
   542  	depends_on = ["aws_internet_gateway.gw"]
   543  }
   544  
   545  data "aws_instance" "foo" {
   546    instance_id = "${aws_instance.foo_instance.id}"
   547  }
   548  `