github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_eip_association_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/service/ec2"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSEIPAssociation_basic(t *testing.T) {
    14  	var a ec2.Address
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSEIPAssociationDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSEIPAssociationConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSEIPExists(
    25  						"aws_eip.bar.0", &a),
    26  					testAccCheckAWSEIPAssociationExists(
    27  						"aws_eip_association.by_allocation_id", &a),
    28  					testAccCheckAWSEIPExists(
    29  						"aws_eip.bar.1", &a),
    30  					testAccCheckAWSEIPAssociationExists(
    31  						"aws_eip_association.by_public_ip", &a),
    32  					testAccCheckAWSEIPExists(
    33  						"aws_eip.bar.2", &a),
    34  					testAccCheckAWSEIPAssociationExists(
    35  						"aws_eip_association.to_eni", &a),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAWSEIPAssociation_disappears(t *testing.T) {
    43  	var a ec2.Address
    44  
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:     func() { testAccPreCheck(t) },
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccCheckAWSEIPAssociationDestroy,
    49  		Steps: []resource.TestStep{
    50  			resource.TestStep{
    51  				Config: testAccAWSEIPAssociationConfigDisappears,
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckAWSEIPExists(
    54  						"aws_eip.bar", &a),
    55  					testAccCheckAWSEIPAssociationExists(
    56  						"aws_eip_association.by_allocation_id", &a),
    57  					testAccCheckEIPAssociationDisappears(&a),
    58  				),
    59  				ExpectNonEmptyPlan: true,
    60  			},
    61  		},
    62  	})
    63  }
    64  
    65  func testAccCheckEIPAssociationDisappears(address *ec2.Address) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    68  		opts := &ec2.DisassociateAddressInput{
    69  			AssociationId: address.AssociationId,
    70  		}
    71  		if _, err := conn.DisassociateAddress(opts); err != nil {
    72  			return err
    73  		}
    74  		return nil
    75  	}
    76  }
    77  
    78  func testAccCheckAWSEIPAssociationExists(name string, res *ec2.Address) resource.TestCheckFunc {
    79  	return func(s *terraform.State) error {
    80  		rs, ok := s.RootModule().Resources[name]
    81  		if !ok {
    82  			return fmt.Errorf("Not found: %s", name)
    83  		}
    84  
    85  		if rs.Primary.ID == "" {
    86  			return fmt.Errorf("No EIP Association ID is set")
    87  		}
    88  
    89  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    90  
    91  		request := &ec2.DescribeAddressesInput{
    92  			Filters: []*ec2.Filter{
    93  				&ec2.Filter{
    94  					Name:   aws.String("association-id"),
    95  					Values: []*string{res.AssociationId},
    96  				},
    97  			},
    98  		}
    99  		describe, err := conn.DescribeAddresses(request)
   100  		if err != nil {
   101  			return err
   102  		}
   103  
   104  		if len(describe.Addresses) != 1 ||
   105  			*describe.Addresses[0].AssociationId != *res.AssociationId {
   106  			return fmt.Errorf("EIP Association not found")
   107  		}
   108  
   109  		return nil
   110  	}
   111  }
   112  
   113  func testAccCheckAWSEIPAssociationDestroy(s *terraform.State) error {
   114  	for _, rs := range s.RootModule().Resources {
   115  		if rs.Type != "aws_eip_association" {
   116  			continue
   117  		}
   118  
   119  		if rs.Primary.ID == "" {
   120  			return fmt.Errorf("No EIP Association ID is set")
   121  		}
   122  
   123  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   124  
   125  		request := &ec2.DescribeAddressesInput{
   126  			Filters: []*ec2.Filter{
   127  				&ec2.Filter{
   128  					Name:   aws.String("association-id"),
   129  					Values: []*string{aws.String(rs.Primary.ID)},
   130  				},
   131  			},
   132  		}
   133  		describe, err := conn.DescribeAddresses(request)
   134  		if err != nil {
   135  			return err
   136  		}
   137  
   138  		if len(describe.Addresses) > 0 {
   139  			return fmt.Errorf("EIP Association still exists")
   140  		}
   141  	}
   142  	return nil
   143  }
   144  
   145  const testAccAWSEIPAssociationConfig = `
   146  resource "aws_vpc" "main" {
   147  	cidr_block = "192.168.0.0/24"
   148  }
   149  resource "aws_subnet" "sub" {
   150  	vpc_id = "${aws_vpc.main.id}"
   151  	cidr_block = "192.168.0.0/25"
   152  	availability_zone = "us-west-2a"
   153  }
   154  resource "aws_internet_gateway" "igw" {
   155  	vpc_id = "${aws_vpc.main.id}"
   156  }
   157  resource "aws_instance" "foo" {
   158  	count = 2
   159  	ami = "ami-21f78e11"
   160  	availability_zone = "us-west-2a"
   161  	instance_type = "t1.micro"
   162  	subnet_id = "${aws_subnet.sub.id}"
   163  }
   164  resource "aws_eip" "bar" {
   165  	count = 3
   166  	vpc = true
   167  }
   168  resource "aws_eip_association" "by_allocation_id" {
   169  	allocation_id = "${aws_eip.bar.0.id}"
   170  	instance_id = "${aws_instance.foo.0.id}"
   171  }
   172  resource "aws_eip_association" "by_public_ip" {
   173  	public_ip = "${aws_eip.bar.1.public_ip}"
   174  	instance_id = "${aws_instance.foo.1.id}"
   175  }
   176  resource "aws_eip_association" "to_eni" {
   177  	allocation_id = "${aws_eip.bar.2.id}"
   178  	network_interface_id = "${aws_network_interface.baz.id}"
   179  }
   180  resource "aws_network_interface" "baz" {
   181  	subnet_id = "${aws_subnet.sub.id}"
   182  	private_ips = ["192.168.0.10"]
   183  	attachment {
   184  		instance = "${aws_instance.foo.0.id}"
   185  		device_index = 1
   186  	}
   187  }
   188  `
   189  
   190  const testAccAWSEIPAssociationConfigDisappears = `
   191  resource "aws_vpc" "main" {
   192  	cidr_block = "192.168.0.0/24"
   193  }
   194  resource "aws_subnet" "sub" {
   195  	vpc_id = "${aws_vpc.main.id}"
   196  	cidr_block = "192.168.0.0/25"
   197  	availability_zone = "us-west-2a"
   198  }
   199  resource "aws_internet_gateway" "igw" {
   200  	vpc_id = "${aws_vpc.main.id}"
   201  }
   202  resource "aws_instance" "foo" {
   203  	ami = "ami-21f78e11"
   204  	availability_zone = "us-west-2a"
   205  	instance_type = "t1.micro"
   206  	subnet_id = "${aws_subnet.sub.id}"
   207  }
   208  resource "aws_eip" "bar" {
   209  	vpc = true
   210  }
   211  resource "aws_eip_association" "by_allocation_id" {
   212  	allocation_id = "${aws_eip.bar.id}"
   213  	instance_id = "${aws_instance.foo.id}"
   214  }`