github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  		tags {
   295  			Name = "testAccAWSENIConfig"
   296  		}
   297  }
   298  
   299  resource "aws_subnet" "foo" {
   300      vpc_id = "${aws_vpc.foo.id}"
   301      cidr_block = "172.16.10.0/24"
   302      availability_zone = "us-west-2a"
   303  }
   304  
   305  resource "aws_security_group" "foo" {
   306    vpc_id = "${aws_vpc.foo.id}"
   307    description = "foo"
   308    name = "foo"
   309  
   310          egress {
   311                  from_port = 0
   312                  to_port = 0
   313                  protocol = "tcp"
   314                  cidr_blocks = ["10.0.0.0/16"]
   315          }
   316  }
   317  
   318  resource "aws_network_interface" "bar" {
   319      subnet_id = "${aws_subnet.foo.id}"
   320      private_ips = ["172.16.10.100"]
   321      security_groups = ["${aws_security_group.foo.id}"]
   322      description = "Managed by Terraform"
   323      tags {
   324          Name = "bar_interface"
   325      }
   326  }
   327  `
   328  
   329  const testAccAWSENIConfigUpdatedDescription = `
   330  resource "aws_vpc" "foo" {
   331      cidr_block = "172.16.0.0/16"
   332  		tags {
   333  			Name = "testAccAWSENIConfigUpdatedDescription"
   334  		}
   335  }
   336  
   337  resource "aws_subnet" "foo" {
   338      vpc_id = "${aws_vpc.foo.id}"
   339      cidr_block = "172.16.10.0/24"
   340      availability_zone = "us-west-2a"
   341  }
   342  
   343  resource "aws_security_group" "foo" {
   344    vpc_id = "${aws_vpc.foo.id}"
   345    description = "foo"
   346    name = "foo"
   347  
   348          egress {
   349                  from_port = 0
   350                  to_port = 0
   351                  protocol = "tcp"
   352                  cidr_blocks = ["10.0.0.0/16"]
   353          }
   354  }
   355  
   356  resource "aws_network_interface" "bar" {
   357      subnet_id = "${aws_subnet.foo.id}"
   358      private_ips = ["172.16.10.100"]
   359      security_groups = ["${aws_security_group.foo.id}"]
   360      description = "Updated ENI Description"
   361      tags {
   362          Name = "bar_interface"
   363      }
   364  }
   365  `
   366  
   367  const testAccAWSENIConfigWithSourceDestCheck = `
   368  resource "aws_vpc" "foo" {
   369      cidr_block = "172.16.0.0/16"
   370  		tags {
   371  			Name = "testAccAWSENIConfigWithSourceDestCheck"
   372  		}
   373  }
   374  
   375  resource "aws_subnet" "foo" {
   376      vpc_id = "${aws_vpc.foo.id}"
   377      cidr_block = "172.16.10.0/24"
   378      availability_zone = "us-west-2a"
   379  }
   380  
   381  resource "aws_network_interface" "bar" {
   382      subnet_id = "${aws_subnet.foo.id}"
   383          source_dest_check = false
   384      private_ips = ["172.16.10.100"]
   385  }
   386  `
   387  
   388  const testAccAWSENIConfigWithNoPrivateIPs = `
   389  resource "aws_vpc" "foo" {
   390      cidr_block = "172.16.0.0/16"
   391  		tags {
   392  			Name = "testAccAWSENIConfigWithNoPrivateIPs"
   393  		}
   394  }
   395  
   396  resource "aws_subnet" "foo" {
   397      vpc_id = "${aws_vpc.foo.id}"
   398      cidr_block = "172.16.10.0/24"
   399      availability_zone = "us-west-2a"
   400  }
   401  
   402  resource "aws_network_interface" "bar" {
   403      subnet_id = "${aws_subnet.foo.id}"
   404          source_dest_check = false
   405  }
   406  `
   407  
   408  const testAccAWSENIConfigWithAttachment = `
   409  resource "aws_vpc" "foo" {
   410      cidr_block = "172.16.0.0/16"
   411          tags {
   412              Name = "tf-eni-test"
   413          }
   414  }
   415  
   416  resource "aws_subnet" "foo" {
   417      vpc_id = "${aws_vpc.foo.id}"
   418      cidr_block = "172.16.10.0/24"
   419      availability_zone = "us-west-2a"
   420          tags {
   421              Name = "tf-foo-eni-test"
   422          }
   423  }
   424  
   425  resource "aws_subnet" "bar" {
   426      vpc_id = "${aws_vpc.foo.id}"
   427      cidr_block = "172.16.11.0/24"
   428      availability_zone = "us-west-2a"
   429          tags {
   430              Name = "tf-bar-eni-test"
   431          }
   432  }
   433  
   434  resource "aws_security_group" "foo" {
   435    vpc_id = "${aws_vpc.foo.id}"
   436    description = "foo"
   437    name = "foo"
   438  }
   439  
   440  resource "aws_instance" "foo" {
   441      ami = "ami-c5eabbf5"
   442      instance_type = "t2.micro"
   443      subnet_id = "${aws_subnet.bar.id}"
   444      associate_public_ip_address = false
   445      private_ip = "172.16.11.50"
   446      tags {
   447          Name = "foo-tf-eni-test"
   448      }
   449  }
   450  
   451  resource "aws_network_interface" "bar" {
   452      subnet_id = "${aws_subnet.foo.id}"
   453      private_ips = ["172.16.10.100"]
   454      security_groups = ["${aws_security_group.foo.id}"]
   455      attachment {
   456          instance = "${aws_instance.foo.id}"
   457          device_index = 1
   458      }
   459      tags {
   460          Name = "bar_interface"
   461      }
   462  }
   463  `
   464  
   465  const testAccAWSENIConfigExternalAttachment = `
   466  resource "aws_vpc" "foo" {
   467      cidr_block = "172.16.0.0/16"
   468          tags {
   469              Name = "tf-eni-test"
   470          }
   471  }
   472  
   473  resource "aws_subnet" "foo" {
   474      vpc_id = "${aws_vpc.foo.id}"
   475      cidr_block = "172.16.10.0/24"
   476      availability_zone = "us-west-2a"
   477          tags {
   478              Name = "tf-eni-test"
   479          }
   480  }
   481  
   482  resource "aws_subnet" "bar" {
   483      vpc_id = "${aws_vpc.foo.id}"
   484      cidr_block = "172.16.11.0/24"
   485      availability_zone = "us-west-2a"
   486          tags {
   487              Name = "tf-eni-test"
   488          }
   489  }
   490  
   491  resource "aws_security_group" "foo" {
   492    vpc_id = "${aws_vpc.foo.id}"
   493    description = "foo"
   494    name = "foo"
   495  }
   496  
   497  resource "aws_instance" "foo" {
   498      ami = "ami-c5eabbf5"
   499      instance_type = "t2.micro"
   500      subnet_id = "${aws_subnet.bar.id}"
   501      associate_public_ip_address = false
   502      private_ip = "172.16.11.50"
   503      tags {
   504          Name = "tf-eni-test"
   505      }
   506  }
   507  
   508  resource "aws_network_interface" "bar" {
   509      subnet_id = "${aws_subnet.foo.id}"
   510      private_ips = ["172.16.10.100"]
   511      security_groups = ["${aws_security_group.foo.id}"]
   512      tags {
   513          Name = "bar_interface"
   514      }
   515  }
   516  `