github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_network_interface_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSENI_basic(t *testing.T) {
    15  	var conf ec2.NetworkInterface
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:      func() { testAccPreCheck(t) },
    19  		IDRefreshName: "aws_network_interface.bar",
    20  		Providers:     testAccProviders,
    21  		CheckDestroy:  testAccCheckAWSENIDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSENIConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    27  					testAccCheckAWSENIAttributes(&conf),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_network_interface.bar", "private_ips.#", "1"),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_network_interface.bar", "tags.Name", "bar_interface"),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_network_interface.bar", "description", "Managed by Terraform"),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccAWSENI_updatedDescription(t *testing.T) {
    41  	var conf ec2.NetworkInterface
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:      func() { testAccPreCheck(t) },
    45  		IDRefreshName: "aws_network_interface.bar",
    46  		Providers:     testAccProviders,
    47  		CheckDestroy:  testAccCheckAWSENIDestroy,
    48  		Steps: []resource.TestStep{
    49  			resource.TestStep{
    50  				Config: testAccAWSENIConfig,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    53  					resource.TestCheckResourceAttr(
    54  						"aws_network_interface.bar", "description", "Managed by Terraform"),
    55  				),
    56  			},
    57  
    58  			resource.TestStep{
    59  				Config: testAccAWSENIConfigUpdatedDescription,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    62  					resource.TestCheckResourceAttr(
    63  						"aws_network_interface.bar", "description", "Updated ENI Description"),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccAWSENI_attached(t *testing.T) {
    71  	var conf ec2.NetworkInterface
    72  
    73  	resource.Test(t, resource.TestCase{
    74  		PreCheck:      func() { testAccPreCheck(t) },
    75  		IDRefreshName: "aws_network_interface.bar",
    76  		Providers:     testAccProviders,
    77  		CheckDestroy:  testAccCheckAWSENIDestroy,
    78  		Steps: []resource.TestStep{
    79  			resource.TestStep{
    80  				Config: testAccAWSENIConfigWithAttachment,
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    83  					testAccCheckAWSENIAttributesWithAttachment(&conf),
    84  					resource.TestCheckResourceAttr(
    85  						"aws_network_interface.bar", "private_ips.#", "1"),
    86  					resource.TestCheckResourceAttr(
    87  						"aws_network_interface.bar", "tags.Name", "bar_interface"),
    88  				),
    89  			},
    90  		},
    91  	})
    92  }
    93  
    94  func TestAccAWSENI_ignoreExternalAttachment(t *testing.T) {
    95  	var conf ec2.NetworkInterface
    96  
    97  	resource.Test(t, resource.TestCase{
    98  		PreCheck:      func() { testAccPreCheck(t) },
    99  		IDRefreshName: "aws_network_interface.bar",
   100  		Providers:     testAccProviders,
   101  		CheckDestroy:  testAccCheckAWSENIDestroy,
   102  		Steps: []resource.TestStep{
   103  			resource.TestStep{
   104  				Config: testAccAWSENIConfigExternalAttachment,
   105  				Check: resource.ComposeTestCheckFunc(
   106  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
   107  					testAccCheckAWSENIAttributes(&conf),
   108  					testAccCheckAWSENIMakeExternalAttachment("aws_instance.foo", &conf),
   109  				),
   110  			},
   111  		},
   112  	})
   113  }
   114  
   115  func TestAccAWSENI_sourceDestCheck(t *testing.T) {
   116  	var conf ec2.NetworkInterface
   117  
   118  	resource.Test(t, resource.TestCase{
   119  		PreCheck:      func() { testAccPreCheck(t) },
   120  		IDRefreshName: "aws_network_interface.bar",
   121  		Providers:     testAccProviders,
   122  		CheckDestroy:  testAccCheckAWSENIDestroy,
   123  		Steps: []resource.TestStep{
   124  			resource.TestStep{
   125  				Config: testAccAWSENIConfigWithSourceDestCheck,
   126  				Check: resource.ComposeTestCheckFunc(
   127  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
   128  					resource.TestCheckResourceAttr(
   129  						"aws_network_interface.bar", "source_dest_check", "false"),
   130  				),
   131  			},
   132  		},
   133  	})
   134  }
   135  
   136  func TestAccAWSENI_computedIPs(t *testing.T) {
   137  	var conf ec2.NetworkInterface
   138  
   139  	resource.Test(t, resource.TestCase{
   140  		PreCheck:      func() { testAccPreCheck(t) },
   141  		IDRefreshName: "aws_network_interface.bar",
   142  		Providers:     testAccProviders,
   143  		CheckDestroy:  testAccCheckAWSENIDestroy,
   144  		Steps: []resource.TestStep{
   145  			resource.TestStep{
   146  				Config: testAccAWSENIConfigWithNoPrivateIPs,
   147  				Check: resource.ComposeTestCheckFunc(
   148  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
   149  					resource.TestCheckResourceAttr(
   150  						"aws_network_interface.bar", "private_ips.#", "1"),
   151  				),
   152  			},
   153  		},
   154  	})
   155  }
   156  
   157  func testAccCheckAWSENIExists(n string, res *ec2.NetworkInterface) resource.TestCheckFunc {
   158  	return func(s *terraform.State) error {
   159  		rs, ok := s.RootModule().Resources[n]
   160  		if !ok {
   161  			return fmt.Errorf("Not found: %s", n)
   162  		}
   163  
   164  		if rs.Primary.ID == "" {
   165  			return fmt.Errorf("No ENI ID is set")
   166  		}
   167  
   168  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   169  		describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
   170  			NetworkInterfaceIds: []*string{aws.String(rs.Primary.ID)},
   171  		}
   172  		describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
   173  
   174  		if err != nil {
   175  			return err
   176  		}
   177  
   178  		if len(describeResp.NetworkInterfaces) != 1 ||
   179  			*describeResp.NetworkInterfaces[0].NetworkInterfaceId != rs.Primary.ID {
   180  			return fmt.Errorf("ENI not found")
   181  		}
   182  
   183  		*res = *describeResp.NetworkInterfaces[0]
   184  
   185  		return nil
   186  	}
   187  }
   188  
   189  func testAccCheckAWSENIAttributes(conf *ec2.NetworkInterface) resource.TestCheckFunc {
   190  	return func(s *terraform.State) error {
   191  
   192  		if conf.Attachment != nil {
   193  			return fmt.Errorf("expected attachment to be nil")
   194  		}
   195  
   196  		if *conf.AvailabilityZone != "us-west-2a" {
   197  			return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone)
   198  		}
   199  
   200  		if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" {
   201  			return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups)
   202  		}
   203  
   204  		if *conf.PrivateIpAddress != "172.16.10.100" {
   205  			return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIpAddress)
   206  		}
   207  
   208  		if *conf.SourceDestCheck != true {
   209  			return fmt.Errorf("expected source_dest_check to be true, but was %t", *conf.SourceDestCheck)
   210  		}
   211  
   212  		if len(conf.TagSet) == 0 {
   213  			return fmt.Errorf("expected tags")
   214  		}
   215  
   216  		return nil
   217  	}
   218  }
   219  
   220  func testAccCheckAWSENIAttributesWithAttachment(conf *ec2.NetworkInterface) resource.TestCheckFunc {
   221  	return func(s *terraform.State) error {
   222  
   223  		if conf.Attachment == nil {
   224  			return fmt.Errorf("expected attachment to be set, but was nil")
   225  		}
   226  
   227  		if *conf.Attachment.DeviceIndex != 1 {
   228  			return fmt.Errorf("expected attachment device index to be 1, but was %d", *conf.Attachment.DeviceIndex)
   229  		}
   230  
   231  		if *conf.AvailabilityZone != "us-west-2a" {
   232  			return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone)
   233  		}
   234  
   235  		if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" {
   236  			return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups)
   237  		}
   238  
   239  		if *conf.PrivateIpAddress != "172.16.10.100" {
   240  			return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIpAddress)
   241  		}
   242  
   243  		return nil
   244  	}
   245  }
   246  
   247  func testAccCheckAWSENIDestroy(s *terraform.State) error {
   248  	for _, rs := range s.RootModule().Resources {
   249  		if rs.Type != "aws_network_interface" {
   250  			continue
   251  		}
   252  
   253  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   254  		describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
   255  			NetworkInterfaceIds: []*string{aws.String(rs.Primary.ID)},
   256  		}
   257  		_, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
   258  
   259  		if err != nil {
   260  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" {
   261  				return nil
   262  			}
   263  
   264  			return err
   265  		}
   266  	}
   267  
   268  	return nil
   269  }
   270  
   271  func testAccCheckAWSENIMakeExternalAttachment(n string, conf *ec2.NetworkInterface) resource.TestCheckFunc {
   272  	return func(s *terraform.State) error {
   273  		rs, ok := s.RootModule().Resources[n]
   274  		if !ok || rs.Primary.ID == "" {
   275  			return fmt.Errorf("Not found: %s", n)
   276  		}
   277  		attach_request := &ec2.AttachNetworkInterfaceInput{
   278  			DeviceIndex:        aws.Int64(2),
   279  			InstanceId:         aws.String(rs.Primary.ID),
   280  			NetworkInterfaceId: conf.NetworkInterfaceId,
   281  		}
   282  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   283  		_, attach_err := conn.AttachNetworkInterface(attach_request)
   284  		if attach_err != nil {
   285  			return fmt.Errorf("Error attaching ENI: %s", attach_err)
   286  		}
   287  		return nil
   288  	}
   289  }
   290  
   291  const testAccAWSENIConfig = `
   292  resource "aws_vpc" "foo" {
   293      cidr_block = "172.16.0.0/16"
   294  }
   295  
   296  resource "aws_subnet" "foo" {
   297      vpc_id = "${aws_vpc.foo.id}"
   298      cidr_block = "172.16.10.0/24"
   299      availability_zone = "us-west-2a"
   300  }
   301  
   302  resource "aws_security_group" "foo" {
   303    vpc_id = "${aws_vpc.foo.id}"
   304    description = "foo"
   305    name = "foo"
   306  
   307          egress {
   308                  from_port = 0
   309                  to_port = 0
   310                  protocol = "tcp"
   311                  cidr_blocks = ["10.0.0.0/16"]
   312          }
   313  }
   314  
   315  resource "aws_network_interface" "bar" {
   316      subnet_id = "${aws_subnet.foo.id}"
   317      private_ips = ["172.16.10.100"]
   318      security_groups = ["${aws_security_group.foo.id}"]
   319      description = "Managed by Terraform"
   320      tags {
   321          Name = "bar_interface"
   322      }
   323  }
   324  `
   325  
   326  const testAccAWSENIConfigUpdatedDescription = `
   327  resource "aws_vpc" "foo" {
   328      cidr_block = "172.16.0.0/16"
   329  }
   330  
   331  resource "aws_subnet" "foo" {
   332      vpc_id = "${aws_vpc.foo.id}"
   333      cidr_block = "172.16.10.0/24"
   334      availability_zone = "us-west-2a"
   335  }
   336  
   337  resource "aws_security_group" "foo" {
   338    vpc_id = "${aws_vpc.foo.id}"
   339    description = "foo"
   340    name = "foo"
   341  
   342          egress {
   343                  from_port = 0
   344                  to_port = 0
   345                  protocol = "tcp"
   346                  cidr_blocks = ["10.0.0.0/16"]
   347          }
   348  }
   349  
   350  resource "aws_network_interface" "bar" {
   351      subnet_id = "${aws_subnet.foo.id}"
   352      private_ips = ["172.16.10.100"]
   353      security_groups = ["${aws_security_group.foo.id}"]
   354      description = "Updated ENI Description"
   355      tags {
   356          Name = "bar_interface"
   357      }
   358  }
   359  `
   360  
   361  const testAccAWSENIConfigWithSourceDestCheck = `
   362  resource "aws_vpc" "foo" {
   363      cidr_block = "172.16.0.0/16"
   364  }
   365  
   366  resource "aws_subnet" "foo" {
   367      vpc_id = "${aws_vpc.foo.id}"
   368      cidr_block = "172.16.10.0/24"
   369      availability_zone = "us-west-2a"
   370  }
   371  
   372  resource "aws_network_interface" "bar" {
   373      subnet_id = "${aws_subnet.foo.id}"
   374          source_dest_check = false
   375      private_ips = ["172.16.10.100"]
   376  }
   377  `
   378  
   379  const testAccAWSENIConfigWithNoPrivateIPs = `
   380  resource "aws_vpc" "foo" {
   381      cidr_block = "172.16.0.0/16"
   382  }
   383  
   384  resource "aws_subnet" "foo" {
   385      vpc_id = "${aws_vpc.foo.id}"
   386      cidr_block = "172.16.10.0/24"
   387      availability_zone = "us-west-2a"
   388  }
   389  
   390  resource "aws_network_interface" "bar" {
   391      subnet_id = "${aws_subnet.foo.id}"
   392          source_dest_check = false
   393  }
   394  `
   395  
   396  const testAccAWSENIConfigWithAttachment = `
   397  resource "aws_vpc" "foo" {
   398      cidr_block = "172.16.0.0/16"
   399          tags {
   400              Name = "tf-eni-test"
   401          }
   402  }
   403  
   404  resource "aws_subnet" "foo" {
   405      vpc_id = "${aws_vpc.foo.id}"
   406      cidr_block = "172.16.10.0/24"
   407      availability_zone = "us-west-2a"
   408          tags {
   409              Name = "tf-foo-eni-test"
   410          }
   411  }
   412  
   413  resource "aws_subnet" "bar" {
   414      vpc_id = "${aws_vpc.foo.id}"
   415      cidr_block = "172.16.11.0/24"
   416      availability_zone = "us-west-2a"
   417          tags {
   418              Name = "tf-bar-eni-test"
   419          }
   420  }
   421  
   422  resource "aws_security_group" "foo" {
   423    vpc_id = "${aws_vpc.foo.id}"
   424    description = "foo"
   425    name = "foo"
   426  }
   427  
   428  resource "aws_instance" "foo" {
   429      ami = "ami-c5eabbf5"
   430      instance_type = "t2.micro"
   431      subnet_id = "${aws_subnet.bar.id}"
   432      associate_public_ip_address = false
   433      private_ip = "172.16.11.50"
   434      tags {
   435          Name = "foo-tf-eni-test"
   436      }
   437  }
   438  
   439  resource "aws_network_interface" "bar" {
   440      subnet_id = "${aws_subnet.foo.id}"
   441      private_ips = ["172.16.10.100"]
   442      security_groups = ["${aws_security_group.foo.id}"]
   443      attachment {
   444          instance = "${aws_instance.foo.id}"
   445          device_index = 1
   446      }
   447      tags {
   448          Name = "bar_interface"
   449      }
   450  }
   451  `
   452  
   453  const testAccAWSENIConfigExternalAttachment = `
   454  resource "aws_vpc" "foo" {
   455      cidr_block = "172.16.0.0/16"
   456          tags {
   457              Name = "tf-eni-test"
   458          }
   459  }
   460  
   461  resource "aws_subnet" "foo" {
   462      vpc_id = "${aws_vpc.foo.id}"
   463      cidr_block = "172.16.10.0/24"
   464      availability_zone = "us-west-2a"
   465          tags {
   466              Name = "tf-eni-test"
   467          }
   468  }
   469  
   470  resource "aws_subnet" "bar" {
   471      vpc_id = "${aws_vpc.foo.id}"
   472      cidr_block = "172.16.11.0/24"
   473      availability_zone = "us-west-2a"
   474          tags {
   475              Name = "tf-eni-test"
   476          }
   477  }
   478  
   479  resource "aws_security_group" "foo" {
   480    vpc_id = "${aws_vpc.foo.id}"
   481    description = "foo"
   482    name = "foo"
   483  }
   484  
   485  resource "aws_instance" "foo" {
   486      ami = "ami-c5eabbf5"
   487      instance_type = "t2.micro"
   488      subnet_id = "${aws_subnet.bar.id}"
   489      associate_public_ip_address = false
   490      private_ip = "172.16.11.50"
   491      tags {
   492          Name = "tf-eni-test"
   493      }
   494  }
   495  
   496  resource "aws_network_interface" "bar" {
   497      subnet_id = "${aws_subnet.foo.id}"
   498      private_ips = ["172.16.10.100"]
   499      security_groups = ["${aws_security_group.foo.id}"]
   500      tags {
   501          Name = "bar_interface"
   502      }
   503  }
   504  `