github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/builtin/providers/aws/data_source_aws_instance_test.go (about)

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