github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/builtin/providers/aws/resource_aws_eip_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/mitchellh/goamz/ec2"
    11  )
    12  
    13  func TestAccAWSEIP_normal(t *testing.T) {
    14  	var conf ec2.Address
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSEIPDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSEIPConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    25  					testAccCheckAWSEIPAttributes(&conf),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSEIP_instance(t *testing.T) {
    33  	var conf ec2.Address
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckAWSEIPDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccAWSEIPInstanceConfig,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    44  					testAccCheckAWSEIPAttributes(&conf),
    45  				),
    46  			},
    47  
    48  			resource.TestStep{
    49  				Config: testAccAWSEIPInstanceConfig2,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckAWSEIPExists("aws_eip.bar", &conf),
    52  					testAccCheckAWSEIPAttributes(&conf),
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func testAccCheckAWSEIPDestroy(s *terraform.State) error {
    60  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    61  
    62  	for _, rs := range s.RootModule().Resources {
    63  		if rs.Type != "aws_eip" {
    64  			continue
    65  		}
    66  
    67  		describe, err := conn.Addresses([]string{rs.Primary.ID}, []string{}, nil)
    68  
    69  		if err == nil {
    70  			if len(describe.Addresses) != 0 &&
    71  				describe.Addresses[0].PublicIp == rs.Primary.ID {
    72  				return fmt.Errorf("EIP still exists")
    73  			}
    74  		}
    75  
    76  		// Verify the error
    77  		providerErr, ok := err.(*ec2.Error)
    78  		if !ok {
    79  			return err
    80  		}
    81  
    82  		if providerErr.Code != "InvalidAllocationID.NotFound" {
    83  			return fmt.Errorf("Unexpected error: %s", err)
    84  		}
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  func testAccCheckAWSEIPAttributes(conf *ec2.Address) resource.TestCheckFunc {
    91  	return func(s *terraform.State) error {
    92  		if conf.PublicIp == "" {
    93  			return fmt.Errorf("empty public_ip")
    94  		}
    95  
    96  		return nil
    97  	}
    98  }
    99  
   100  func testAccCheckAWSEIPExists(n string, res *ec2.Address) 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 EIP ID is set")
   109  		}
   110  
   111  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   112  
   113  		if strings.Contains(rs.Primary.ID, "eipalloc") {
   114  			describe, err := conn.Addresses([]string{}, []string{rs.Primary.ID}, nil)
   115  			if err != nil {
   116  				return err
   117  			}
   118  
   119  			if len(describe.Addresses) != 1 ||
   120  				describe.Addresses[0].AllocationId != rs.Primary.ID {
   121  				return fmt.Errorf("EIP not found")
   122  			}
   123  			*res = describe.Addresses[0]
   124  
   125  		} else {
   126  			describe, err := conn.Addresses([]string{rs.Primary.ID}, []string{}, nil)
   127  			if err != nil {
   128  				return err
   129  			}
   130  
   131  			if len(describe.Addresses) != 1 ||
   132  				describe.Addresses[0].PublicIp != rs.Primary.ID {
   133  				return fmt.Errorf("EIP not found")
   134  			}
   135  			*res = describe.Addresses[0]
   136  		}
   137  
   138  		return nil
   139  	}
   140  }
   141  
   142  const testAccAWSEIPConfig = `
   143  resource "aws_eip" "bar" {
   144  }
   145  `
   146  
   147  const testAccAWSEIPInstanceConfig = `
   148  resource "aws_instance" "foo" {
   149  	# us-west-2
   150  	ami = "ami-4fccb37f"
   151  	instance_type = "m1.small"
   152  }
   153  
   154  resource "aws_eip" "bar" {
   155  	instance = "${aws_instance.foo.id}"
   156  }
   157  `
   158  
   159  const testAccAWSEIPInstanceConfig2 = `
   160  resource "aws_instance" "bar" {
   161  	# us-west-2
   162  	ami = "ami-4fccb37f"
   163  	instance_type = "m1.small"
   164  }
   165  
   166  resource "aws_eip" "bar" {
   167  	instance = "${aws_instance.bar.id}"
   168  }
   169  `