From c3ba0a7f6dee7ae800891c71c8833c51535a141d Mon Sep 17 00:00:00 2001 From: Peter Beams Date: Fri, 6 Mar 2015 15:49:56 +0000 Subject: [PATCH] adding acceptance tests for network interface --- .../aws/resource_aws_network_interface.go | 41 ++- .../resource_aws_network_interface_test.go | 240 ++++++++++++++++++ grep | 200 +++++++++++++++ 3 files changed, 478 insertions(+), 3 deletions(-) create mode 100644 builtin/providers/aws/resource_aws_network_interface_test.go create mode 100644 grep diff --git a/builtin/providers/aws/resource_aws_network_interface.go b/builtin/providers/aws/resource_aws_network_interface.go index 3c7aa74a4..4241f8bde 100644 --- a/builtin/providers/aws/resource_aws_network_interface.go +++ b/builtin/providers/aws/resource_aws_network_interface.go @@ -4,9 +4,12 @@ import ( "bytes" "fmt" "log" + "strconv" + "time" "github.com/hashicorp/aws-sdk-go/aws" "github.com/hashicorp/terraform/helper/hashcode" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/aws-sdk-go/gen/ec2" ) @@ -131,7 +134,27 @@ func resourceAwsNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e return nil } -func resourceAwsNetworkInterfaceDetach(oa *schema.Set, meta interface{}) error { +func networkInterfaceAttachmentRefreshFunc(ec2conn *ec2.EC2, id string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + + describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesRequest{ + NetworkInterfaceIDs: []string{id}, + } + describeResp, err := ec2conn.DescribeNetworkInterfaces(describe_network_interfaces_request) + + if err != nil { + log.Printf("[ERROR] Could not find network interface %s. %s", id, err) + return nil, "", err + } + + eni := describeResp.NetworkInterfaces[0] + hasAttachment := strconv.FormatBool(eni.Attachment != nil) + log.Printf("[DEBUG] ENI %s has attachment state %s", id, hasAttachment) + return eni, hasAttachment, nil + } +} + +func resourceAwsNetworkInterfaceDetach(oa *schema.Set, meta interface{}, eniId string) error { // if there was an old attachment, remove it if oa != nil && len(oa.List()) > 0 { old_attachment := oa.List()[0].(map[string]interface{}) @@ -144,6 +167,18 @@ func resourceAwsNetworkInterfaceDetach(oa *schema.Set, meta interface{}) error { if detach_err != nil { return fmt.Errorf("Error detaching ENI: %s", detach_err) } + + log.Printf("[DEBUG] Waiting for ENI (%s) to become dettached", eniId) + stateConf := &resource.StateChangeConf{ + Pending: []string{"true"}, + Target: "false", + Refresh: networkInterfaceAttachmentRefreshFunc(ec2conn, eniId), + Timeout: 10 * time.Minute, + } + if _, err := stateConf.WaitForState(); err != nil { + return fmt.Errorf( + "Error waiting for ENI (%s) to become dettached: %s", eniId, err) + } } return nil @@ -157,7 +192,7 @@ func resourceAwsNetworkInterfaceUpdate(d *schema.ResourceData, meta interface{}) ec2conn := meta.(*AWSClient).ec2conn2 oa, na := d.GetChange("attachment") - detach_err := resourceAwsNetworkInterfaceDetach(oa.(*schema.Set), meta) + detach_err := resourceAwsNetworkInterfaceDetach(oa.(*schema.Set), meta, d.Id()) if detach_err != nil { return detach_err } @@ -204,7 +239,7 @@ func resourceAwsNetworkInterfaceDelete(d *schema.ResourceData, meta interface{}) log.Printf("[INFO] Deleting ENI: %s", d.Id()) - detach_err := resourceAwsNetworkInterfaceDetach(d.Get("attachment").(*schema.Set), meta) + detach_err := resourceAwsNetworkInterfaceDetach(d.Get("attachment").(*schema.Set), meta, d.Id()) if detach_err != nil { return detach_err } diff --git a/builtin/providers/aws/resource_aws_network_interface_test.go b/builtin/providers/aws/resource_aws_network_interface_test.go new file mode 100644 index 000000000..27eedba45 --- /dev/null +++ b/builtin/providers/aws/resource_aws_network_interface_test.go @@ -0,0 +1,240 @@ +package aws + +import ( + //"log" + "fmt" + //"os" + //"reflect" + //"sort" + "testing" + + "github.com/hashicorp/aws-sdk-go/aws" + "github.com/hashicorp/aws-sdk-go/gen/ec2" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSENI_basic(t *testing.T) { + var conf ec2.NetworkInterface + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSENIDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSENIConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSENIExists("aws_network_interface.bar", &conf), + testAccCheckAWSENIAttributes(&conf), + resource.TestCheckResourceAttr( + "aws_network_interface.bar", "private_ips.#", "1"), + resource.TestCheckResourceAttr( + "aws_network_interface.bar", "tags.Name", "bar_interface"), + ), + }, + }, + }) +} + +func TestAccAWSENI_attached(t *testing.T) { + var conf ec2.NetworkInterface + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSENIDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSENIConfigWithAttachment, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSENIExists("aws_network_interface.bar", &conf), + testAccCheckAWSENIAttributesWithAttachment(&conf), + resource.TestCheckResourceAttr( + "aws_network_interface.bar", "private_ips.#", "1"), + resource.TestCheckResourceAttr( + "aws_network_interface.bar", "tags.Name", "bar_interface"), + ), + }, + }, + }) +} + +func testAccCheckAWSENIExists(n string, res *ec2.NetworkInterface) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ENI ID is set") + } + + ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn2 + describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesRequest{ + NetworkInterfaceIDs: []string{rs.Primary.ID}, + } + describeResp, err := ec2conn.DescribeNetworkInterfaces(describe_network_interfaces_request) + + if err != nil { + return err + } + + if len(describeResp.NetworkInterfaces) != 1 || + *describeResp.NetworkInterfaces[0].NetworkInterfaceID != rs.Primary.ID { + return fmt.Errorf("ENI not found") + } + + *res = describeResp.NetworkInterfaces[0] + + return nil + } +} + +func testAccCheckAWSENIAttributes(conf *ec2.NetworkInterface) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if conf.Attachment != nil { + return fmt.Errorf("expected attachment to be nil") + } + + if *conf.AvailabilityZone != "us-west-2a" { + return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone) + } + + if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" { + return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups) + } + + if *conf.PrivateIPAddress != "172.16.10.100" { + return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIPAddress) + } + + return nil + } +} + +func testAccCheckAWSENIAttributesWithAttachment(conf *ec2.NetworkInterface) resource.TestCheckFunc { + return func(s *terraform.State) error { + + if conf.Attachment == nil { + return fmt.Errorf("expected attachment to be set, but was nil") + } + + if *conf.Attachment.DeviceIndex != 1 { + return fmt.Errorf("expected attachment device index to be 1, but was %d", *conf.Attachment.DeviceIndex) + } + + if *conf.AvailabilityZone != "us-west-2a" { + return fmt.Errorf("expected availability_zone to be us-west-2a, but was %s", *conf.AvailabilityZone) + } + + if len(conf.Groups) != 1 && *conf.Groups[0].GroupName != "foo" { + return fmt.Errorf("expected security group to be foo, but was %#v", conf.Groups) + } + + if *conf.PrivateIPAddress != "172.16.10.100" { + return fmt.Errorf("expected private ip to be 172.16.10.100, but was %s", *conf.PrivateIPAddress) + } + + return nil + } +} + +func testAccCheckAWSENIDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_network_interface" { + continue + } + + ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn2 + describe_network_interfaces_request := &ec2.DescribeNetworkInterfacesRequest{ + NetworkInterfaceIDs: []string{rs.Primary.ID}, + } + _, err := ec2conn.DescribeNetworkInterfaces(describe_network_interfaces_request) + + if err != nil { + if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidNetworkInterfaceID.NotFound" { + return nil + } + + return err + } + } + + return nil +} + + +const testAccAWSENIConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "172.16.0.0/16" +} + +resource "aws_subnet" "foo" { + vpc_id = "${aws_vpc.foo.id}" + cidr_block = "172.16.10.0/24" + availability_zone = "us-west-2a" +} + +resource "aws_security_group" "foo" { + vpc_id = "${aws_vpc.foo.id}" + description = "foo" + name = "foo" +} + +resource "aws_network_interface" "bar" { + subnet_id = "${aws_subnet.foo.id}" + private_ips = ["172.16.10.100"] + security_groups = ["${aws_security_group.foo.id}"] + tags { + Name = "bar_interface" + } +} +` + +const testAccAWSENIConfigWithAttachment = ` +resource "aws_vpc" "foo" { + cidr_block = "172.16.0.0/16" +} + +resource "aws_subnet" "foo" { + vpc_id = "${aws_vpc.foo.id}" + cidr_block = "172.16.10.0/24" + availability_zone = "us-west-2a" +} + +resource "aws_subnet" "bar" { + vpc_id = "${aws_vpc.foo.id}" + cidr_block = "172.16.11.0/24" + availability_zone = "us-west-2a" +} + +resource "aws_security_group" "foo" { + vpc_id = "${aws_vpc.foo.id}" + description = "foo" + name = "foo" +} + +resource "aws_instance" "foo" { + ami = "ami-c5eabbf5" + instance_type = "t2.micro" + subnet_id = "${aws_subnet.bar.id}" + associate_public_ip_address = false + private_ip = "172.16.11.50" +} + +resource "aws_network_interface" "bar" { + subnet_id = "${aws_subnet.foo.id}" + private_ips = ["172.16.10.100"] + security_groups = ["${aws_security_group.foo.id}"] + attachment { + instance = "${aws_instance.foo.id}" + device_index = 1 + } + tags { + Name = "bar_interface" + } +} +` \ No newline at end of file diff --git a/grep b/grep new file mode 100644 index 000000000..2ba075b08 --- /dev/null +++ b/grep @@ -0,0 +1,200 @@ +go generate ./... +TF_ACC= go test ./builtin/providers/aws/ -v -timeout=30s -parallel=4 +=== RUN Test_expandNetworkAclEntry +--- PASS: Test_expandNetworkAclEntry (0.00s) +=== RUN Test_flattenNetworkAclEntry +--- PASS: Test_flattenNetworkAclEntry (0.00s) +=== RUN TestProvider +--- PASS: TestProvider (0.00s) +=== RUN TestProvider_impl +--- PASS: TestProvider_impl (0.00s) +=== RUN TestAccAWSAutoScalingGroup_basic +--- SKIP: TestAccAWSAutoScalingGroup_basic (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSAutoScalingGroupWithLoadBalancer +--- SKIP: TestAccAWSAutoScalingGroupWithLoadBalancer (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSDBInstance +--- SKIP: TestAccAWSDBInstance (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSDBParameterGroup +--- SKIP: TestAccAWSDBParameterGroup (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSDBParameterGroupOnly +--- SKIP: TestAccAWSDBParameterGroupOnly (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSDBSecurityGroup +--- SKIP: TestAccAWSDBSecurityGroup (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSDBSubnetGroup +--- SKIP: TestAccAWSDBSubnetGroup (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSEIP_normal +--- SKIP: TestAccAWSEIP_normal (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSEIP_instance +--- SKIP: TestAccAWSEIP_instance (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSELB_basic +--- SKIP: TestAccAWSELB_basic (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSELB_InstanceAttaching +--- SKIP: TestAccAWSELB_InstanceAttaching (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSELB_HealthCheck +--- SKIP: TestAccAWSELB_HealthCheck (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSELBUpdate_HealthCheck +--- SKIP: TestAccAWSELBUpdate_HealthCheck (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSInstance_normal +--- SKIP: TestAccAWSInstance_normal (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSInstance_blockDevices +--- SKIP: TestAccAWSInstance_blockDevices (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSInstance_sourceDestCheck +--- SKIP: TestAccAWSInstance_sourceDestCheck (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSInstance_vpc +--- SKIP: TestAccAWSInstance_vpc (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccInstance_tags +--- SKIP: TestAccInstance_tags (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccInstance_privateIP +--- SKIP: TestAccInstance_privateIP (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccInstance_associatePublicIPAndPrivateIP +--- SKIP: TestAccInstance_associatePublicIPAndPrivateIP (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestInstanceTenancySchema +--- PASS: TestInstanceTenancySchema (0.00s) +=== RUN TestAccAWSInternetGateway +--- SKIP: TestAccAWSInternetGateway (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSInternetGateway_delete +--- SKIP: TestAccAWSInternetGateway_delete (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccInternetGateway_tags +--- SKIP: TestAccInternetGateway_tags (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSKeyPair_normal +--- SKIP: TestAccAWSKeyPair_normal (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSLaunchConfiguration +--- SKIP: TestAccAWSLaunchConfiguration (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSMainRouteTableAssociation +--- SKIP: TestAccAWSMainRouteTableAssociation (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSNetworkAclsWithEgressAndIngressRules +--- SKIP: TestAccAWSNetworkAclsWithEgressAndIngressRules (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSNetworkAclsOnlyIngressRules +--- SKIP: TestAccAWSNetworkAclsOnlyIngressRules (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSNetworkAclsOnlyIngressRulesChange +--- SKIP: TestAccAWSNetworkAclsOnlyIngressRulesChange (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSNetworkAclsOnlyEgressRules +--- SKIP: TestAccAWSNetworkAclsOnlyEgressRules (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccNetworkAcl_SubnetChange +--- SKIP: TestAccNetworkAcl_SubnetChange (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSENI_basic +--- SKIP: TestAccAWSENI_basic (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccRoute53Record +--- SKIP: TestAccRoute53Record (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccRoute53Record_generatesSuffix +--- SKIP: TestAccRoute53Record_generatesSuffix (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestCleanPrefix +--- PASS: TestCleanPrefix (0.00s) +=== RUN TestCleanZoneID +--- PASS: TestCleanZoneID (0.00s) +=== RUN TestCleanChangeID +--- PASS: TestCleanChangeID (0.00s) +=== RUN TestAccRoute53Zone +--- SKIP: TestAccRoute53Zone (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSRouteTableAssociation +--- SKIP: TestAccAWSRouteTableAssociation (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSRouteTable_normal +--- SKIP: TestAccAWSRouteTable_normal (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSRouteTable_instance +--- SKIP: TestAccAWSRouteTable_instance (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSRouteTable_tags +--- SKIP: TestAccAWSRouteTable_tags (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSRouteTable_vpcPeering +--- SKIP: TestAccAWSRouteTable_vpcPeering (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSS3Bucket +--- SKIP: TestAccAWSS3Bucket (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSSecurityGroup_normal +--- SKIP: TestAccAWSSecurityGroup_normal (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSSecurityGroup_self +--- SKIP: TestAccAWSSecurityGroup_self (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSSecurityGroup_vpc +--- SKIP: TestAccAWSSecurityGroup_vpc (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSSecurityGroup_MultiIngress +--- SKIP: TestAccAWSSecurityGroup_MultiIngress (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSSecurityGroup_Change +--- SKIP: TestAccAWSSecurityGroup_Change (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSSecurityGroup_tags +--- SKIP: TestAccAWSSecurityGroup_tags (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSSubnet +--- SKIP: TestAccAWSSubnet (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccAWSVPCPeeringConnection_normal +--- SKIP: TestAccAWSVPCPeeringConnection_normal (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccVpc_basic +--- SKIP: TestAccVpc_basic (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccVpc_dedicatedTenancy +--- SKIP: TestAccVpc_dedicatedTenancy (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccVpc_tags +--- SKIP: TestAccVpc_tags (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestAccVpcUpdate +--- SKIP: TestAccVpcUpdate (0.00s) + testing.go:89: Acceptance tests skipped unless env 'TF_ACC' set +=== RUN TestExpandIPPerms +--- PASS: TestExpandIPPerms (0.00s) +=== RUN TestFlattenIPPerms +--- PASS: TestFlattenIPPerms (0.00s) +=== RUN TestExpandListeners +--- PASS: TestExpandListeners (0.00s) +=== RUN TestFlattenHealthCheck +--- PASS: TestFlattenHealthCheck (0.00s) +=== RUN TestExpandStringList +--- PASS: TestExpandStringList (0.00s) +=== RUN TestExpandParameters +--- PASS: TestExpandParameters (0.00s) +=== RUN TestFlattenParameters +--- PASS: TestFlattenParameters (0.00s) +=== RUN TestExpandInstanceString +--- PASS: TestExpandInstanceString (0.00s) +=== RUN TestDiffTags +--- PASS: TestDiffTags (0.00s) +PASS +ok github.com/hashicorp/terraform/builtin/providers/aws 0.042s +make[1]: Entering directory `/home/peter/go/src/github.com/hashicorp/terraform' +go tool vet -asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr . +make[1]: Leaving directory `/home/peter/go/src/github.com/hashicorp/terraform'