github.com/acm1/terraform@v0.6.2-0.20150729164239-1f314444f45c/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  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSENIDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSENIConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    26  					testAccCheckAWSENIAttributes(&conf),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_network_interface.bar", "private_ips.#", "1"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_network_interface.bar", "tags.Name", "bar_interface"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccAWSENI_attached(t *testing.T) {
    38  	var conf ec2.NetworkInterface
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckAWSENIDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccAWSENIConfigWithAttachment,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    49  					testAccCheckAWSENIAttributesWithAttachment(&conf),
    50  					resource.TestCheckResourceAttr(
    51  						"aws_network_interface.bar", "private_ips.#", "1"),
    52  					resource.TestCheckResourceAttr(
    53  						"aws_network_interface.bar", "tags.Name", "bar_interface"),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccAWSENI_sourceDestCheck(t *testing.T) {
    61  	var conf ec2.NetworkInterface
    62  
    63  	resource.Test(t, resource.TestCase{
    64  		PreCheck:     func() { testAccPreCheck(t) },
    65  		Providers:    testAccProviders,
    66  		CheckDestroy: testAccCheckAWSENIDestroy,
    67  		Steps: []resource.TestStep{
    68  			resource.TestStep{
    69  				Config: testAccAWSENIConfigWithSourceDestCheck,
    70  				Check: resource.ComposeTestCheckFunc(
    71  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    72  					resource.TestCheckResourceAttr(
    73  						"aws_network_interface.bar", "source_dest_check", "false"),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccAWSENI_computedIPs(t *testing.T) {
    81  	var conf ec2.NetworkInterface
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		PreCheck:     func() { testAccPreCheck(t) },
    85  		Providers:    testAccProviders,
    86  		CheckDestroy: testAccCheckAWSENIDestroy,
    87  		Steps: []resource.TestStep{
    88  			resource.TestStep{
    89  				Config: testAccAWSENIConfigWithNoPrivateIPs,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckAWSENIExists("aws_network_interface.bar", &conf),
    92  					resource.TestCheckResourceAttr(
    93  						"aws_network_interface.bar", "private_ips.#", "1"),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func testAccCheckAWSENIExists(n string, res *ec2.NetworkInterface) resource.TestCheckFunc {
   101  	return func(s *terraform.State) error {
   102  		rs, ok := s.RootModule().Resources[n]
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", n)
   105  		}
   106  
   107  		if rs.Primary.ID == "" {
   108  			return fmt.Errorf("No ENI ID is set")
   109  		}
   110  
   111  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   112  		describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
   113  			NetworkInterfaceIDs: []*string{aws.String(rs.Primary.ID)},
   114  		}
   115  		describeResp, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
   116  
   117  		if err != nil {
   118  			return err
   119  		}
   120  
   121  		if len(describeResp.NetworkInterfaces) != 1 ||
   122  			*describeResp.NetworkInterfaces[0].NetworkInterfaceID != rs.Primary.ID {
   123  			return fmt.Errorf("ENI not found")
   124  		}
   125  
   126  		*res = *describeResp.NetworkInterfaces[0]
   127  
   128  		return nil
   129  	}
   130  }
   131  
   132  func testAccCheckAWSENIAttributes(conf *ec2.NetworkInterface) resource.TestCheckFunc {
   133  	return func(s *terraform.State) error {
   134  
   135  		if conf.Attachment != nil {
   136  			return fmt.Errorf("expected attachment to be nil")
   137  		}
   138  
   139  		if *conf.AvailabilityZone != "us-west-2a" {
   140  			return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone)
   141  		}
   142  
   143  		if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" {
   144  			return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups)
   145  		}
   146  
   147  		if *conf.PrivateIPAddress != "172.16.10.100" {
   148  			return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIPAddress)
   149  		}
   150  
   151  		if *conf.SourceDestCheck != true {
   152  			return fmt.Errorf("expected source_dest_check to be true, but was %t", *conf.SourceDestCheck)
   153  		}
   154  
   155  		if len(conf.TagSet) == 0 {
   156  			return fmt.Errorf("expected tags")
   157  		}
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  func testAccCheckAWSENIAttributesWithAttachment(conf *ec2.NetworkInterface) resource.TestCheckFunc {
   164  	return func(s *terraform.State) error {
   165  
   166  		if conf.Attachment == nil {
   167  			return fmt.Errorf("expected attachment to be set, but was nil")
   168  		}
   169  
   170  		if *conf.Attachment.DeviceIndex != 1 {
   171  			return fmt.Errorf("expected attachment device index to be 1, but was %d", *conf.Attachment.DeviceIndex)
   172  		}
   173  
   174  		if *conf.AvailabilityZone != "us-west-2a" {
   175  			return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone)
   176  		}
   177  
   178  		if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" {
   179  			return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups)
   180  		}
   181  
   182  		if *conf.PrivateIPAddress != "172.16.10.100" {
   183  			return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIPAddress)
   184  		}
   185  
   186  		return nil
   187  	}
   188  }
   189  
   190  func testAccCheckAWSENIDestroy(s *terraform.State) error {
   191  	for _, rs := range s.RootModule().Resources {
   192  		if rs.Type != "aws_network_interface" {
   193  			continue
   194  		}
   195  
   196  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   197  		describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesInput{
   198  			NetworkInterfaceIDs: []*string{aws.String(rs.Primary.ID)},
   199  		}
   200  		_, err := conn.DescribeNetworkInterfaces(describe_network_interfaces_request)
   201  
   202  		if err != nil {
   203  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidNetworkInterfaceID.NotFound" {
   204  				return nil
   205  			}
   206  
   207  			return err
   208  		}
   209  	}
   210  
   211  	return nil
   212  }
   213  
   214  const testAccAWSENIConfig = `
   215  resource "aws_vpc" "foo" {
   216      cidr_block = "172.16.0.0/16"    
   217  }
   218  
   219  resource "aws_subnet" "foo" {
   220      vpc_id = "${aws_vpc.foo.id}"
   221      cidr_block = "172.16.10.0/24"
   222      availability_zone = "us-west-2a"
   223  }
   224  
   225  resource "aws_security_group" "foo" {
   226    vpc_id = "${aws_vpc.foo.id}"
   227    description = "foo"
   228    name = "foo"  
   229  
   230          egress {
   231                  from_port = 0
   232                  to_port = 0
   233                  protocol = "tcp"
   234                  cidr_blocks = ["10.0.0.0/16"]
   235          }
   236  }
   237  
   238  resource "aws_network_interface" "bar" {
   239      subnet_id = "${aws_subnet.foo.id}"   
   240      private_ips = ["172.16.10.100"]
   241      security_groups = ["${aws_security_group.foo.id}"]    
   242      tags {
   243          Name = "bar_interface"
   244      }
   245  }
   246  `
   247  
   248  const testAccAWSENIConfigWithSourceDestCheck = `
   249  resource "aws_vpc" "foo" {
   250      cidr_block = "172.16.0.0/16"
   251  }
   252  
   253  resource "aws_subnet" "foo" {
   254      vpc_id = "${aws_vpc.foo.id}"
   255      cidr_block = "172.16.10.0/24"
   256      availability_zone = "us-west-2a"
   257  }
   258  
   259  resource "aws_network_interface" "bar" {
   260      subnet_id = "${aws_subnet.foo.id}"
   261  		source_dest_check = false
   262      private_ips = ["172.16.10.100"]
   263  }
   264  `
   265  
   266  const testAccAWSENIConfigWithNoPrivateIPs = `
   267  resource "aws_vpc" "foo" {
   268      cidr_block = "172.16.0.0/16"
   269  }
   270  
   271  resource "aws_subnet" "foo" {
   272      vpc_id = "${aws_vpc.foo.id}"
   273      cidr_block = "172.16.10.0/24"
   274      availability_zone = "us-west-2a"
   275  }
   276  
   277  resource "aws_network_interface" "bar" {
   278      subnet_id = "${aws_subnet.foo.id}"
   279  		source_dest_check = false
   280  }
   281  `
   282  
   283  const testAccAWSENIConfigWithAttachment = `
   284  resource "aws_vpc" "foo" {
   285      cidr_block = "172.16.0.0/16"    
   286  		tags {
   287  			Name = "tf-eni-test"
   288  		}
   289  }
   290  
   291  resource "aws_subnet" "foo" {
   292      vpc_id = "${aws_vpc.foo.id}"
   293      cidr_block = "172.16.10.0/24"
   294      availability_zone = "us-west-2a"
   295  		tags {
   296  			Name = "tf-eni-test"
   297  		}
   298  }
   299  
   300  resource "aws_subnet" "bar" {
   301      vpc_id = "${aws_vpc.foo.id}"
   302      cidr_block = "172.16.11.0/24"
   303      availability_zone = "us-west-2a"
   304  		tags {
   305  			Name = "tf-eni-test"
   306  		}
   307  }
   308  
   309  resource "aws_security_group" "foo" {
   310    vpc_id = "${aws_vpc.foo.id}"
   311    description = "foo"
   312    name = "foo"  
   313  }
   314  
   315  resource "aws_instance" "foo" {
   316  	ami = "ami-c5eabbf5"
   317  	instance_type = "t2.micro"
   318  	subnet_id = "${aws_subnet.bar.id}"
   319  	associate_public_ip_address = false
   320  	private_ip = "172.16.11.50"
   321  	tags {
   322  		Name = "tf-eni-test"
   323  	}
   324  }
   325  
   326  resource "aws_network_interface" "bar" {
   327      subnet_id = "${aws_subnet.foo.id}"   
   328      private_ips = ["172.16.10.100"]
   329      security_groups = ["${aws_security_group.foo.id}"]    
   330      attachment {
   331      	instance = "${aws_instance.foo.id}"
   332      	device_index = 1
   333      }
   334      tags {
   335          Name = "bar_interface"
   336      }
   337  }
   338  `