github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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 if conf.PrivateIpAddress != "" { 97 return fmt.Errorf("should not have private_ip for non-vpc") 98 } 99 100 return nil 101 } 102 } 103 104 func testAccCheckAWSEIPExists(n string, res *ec2.Address) resource.TestCheckFunc { 105 return func(s *terraform.State) error { 106 rs, ok := s.RootModule().Resources[n] 107 if !ok { 108 return fmt.Errorf("Not found: %s", n) 109 } 110 111 if rs.Primary.ID == "" { 112 return fmt.Errorf("No EIP ID is set") 113 } 114 115 conn := testAccProvider.Meta().(*AWSClient).ec2conn 116 117 if strings.Contains(rs.Primary.ID, "eipalloc") { 118 describe, err := conn.Addresses([]string{}, []string{rs.Primary.ID}, nil) 119 if err != nil { 120 return err 121 } 122 123 if len(describe.Addresses) != 1 || 124 describe.Addresses[0].AllocationId != rs.Primary.ID { 125 return fmt.Errorf("EIP not found") 126 } 127 *res = describe.Addresses[0] 128 129 } else { 130 describe, err := conn.Addresses([]string{rs.Primary.ID}, []string{}, nil) 131 if err != nil { 132 return err 133 } 134 135 if len(describe.Addresses) != 1 || 136 describe.Addresses[0].PublicIp != rs.Primary.ID { 137 return fmt.Errorf("EIP not found") 138 } 139 *res = describe.Addresses[0] 140 } 141 142 return nil 143 } 144 } 145 146 const testAccAWSEIPConfig = ` 147 resource "aws_eip" "bar" { 148 } 149 ` 150 151 const testAccAWSEIPInstanceConfig = ` 152 resource "aws_instance" "foo" { 153 # us-west-2 154 ami = "ami-4fccb37f" 155 instance_type = "m1.small" 156 } 157 158 resource "aws_eip" "bar" { 159 instance = "${aws_instance.foo.id}" 160 } 161 ` 162 163 const testAccAWSEIPInstanceConfig2 = ` 164 resource "aws_instance" "bar" { 165 # us-west-2 166 ami = "ami-4fccb37f" 167 instance_type = "m1.small" 168 } 169 170 resource "aws_eip" "bar" { 171 instance = "${aws_instance.bar.id}" 172 } 173 `