github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/builtin/providers/aws/resource_aws_internet_gateway_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/goamz/ec2" 10 ) 11 12 func TestAccAWSInternetGateway(t *testing.T) { 13 var v, v2 ec2.InternetGateway 14 15 testNotEqual := func(*terraform.State) error { 16 if len(v.Attachments) == 0 { 17 return fmt.Errorf("IG A is not attached") 18 } 19 if len(v2.Attachments) == 0 { 20 return fmt.Errorf("IG B is not attached") 21 } 22 23 id1 := v.Attachments[0].VpcId 24 id2 := v2.Attachments[0].VpcId 25 if id1 == id2 { 26 return fmt.Errorf("Both attachment IDs are the same") 27 } 28 29 return nil 30 } 31 32 resource.Test(t, resource.TestCase{ 33 PreCheck: func() { testAccPreCheck(t) }, 34 Providers: testAccProviders, 35 CheckDestroy: testAccCheckInternetGatewayDestroy, 36 Steps: []resource.TestStep{ 37 resource.TestStep{ 38 Config: testAccInternetGatewayConfig, 39 Check: resource.ComposeTestCheckFunc( 40 testAccCheckInternetGatewayExists( 41 "aws_internet_gateway.foo", &v), 42 ), 43 }, 44 45 resource.TestStep{ 46 Config: testAccInternetGatewayConfigChangeVPC, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckInternetGatewayExists( 49 "aws_internet_gateway.foo", &v2), 50 testNotEqual, 51 ), 52 }, 53 }, 54 }) 55 } 56 57 func testAccCheckInternetGatewayDestroy(s *terraform.State) error { 58 conn := testAccProvider.ec2conn 59 60 for _, rs := range s.RootModule().Resources { 61 if rs.Type != "aws_internet_gateway" { 62 continue 63 } 64 65 // Try to find the resource 66 resp, err := conn.DescribeInternetGateways( 67 []string{rs.Primary.ID}, ec2.NewFilter()) 68 if err == nil { 69 if len(resp.InternetGateways) > 0 { 70 return fmt.Errorf("still exist.") 71 } 72 73 return nil 74 } 75 76 // Verify the error is what we want 77 ec2err, ok := err.(*ec2.Error) 78 if !ok { 79 return err 80 } 81 if ec2err.Code != "InvalidInternetGatewayID.NotFound" { 82 return err 83 } 84 } 85 86 return nil 87 } 88 89 func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resource.TestCheckFunc { 90 return func(s *terraform.State) error { 91 rs, ok := s.RootModule().Resources[n] 92 if !ok { 93 return fmt.Errorf("Not found: %s", n) 94 } 95 96 if rs.Primary.ID == "" { 97 return fmt.Errorf("No ID is set") 98 } 99 100 conn := testAccProvider.ec2conn 101 resp, err := conn.DescribeInternetGateways( 102 []string{rs.Primary.ID}, ec2.NewFilter()) 103 if err != nil { 104 return err 105 } 106 if len(resp.InternetGateways) == 0 { 107 return fmt.Errorf("InternetGateway not found") 108 } 109 110 *ig = resp.InternetGateways[0] 111 112 return nil 113 } 114 } 115 116 const testAccInternetGatewayConfig = ` 117 resource "aws_vpc" "foo" { 118 cidr_block = "10.1.0.0/16" 119 } 120 121 resource "aws_internet_gateway" "foo" { 122 vpc_id = "${aws_vpc.foo.id}" 123 } 124 ` 125 126 const testAccInternetGatewayConfigChangeVPC = ` 127 resource "aws_vpc" "foo" { 128 cidr_block = "10.1.0.0/16" 129 } 130 131 resource "aws_vpc" "bar" { 132 cidr_block = "10.2.0.0/16" 133 } 134 135 resource "aws_internet_gateway" "foo" { 136 vpc_id = "${aws_vpc.bar.id}" 137 } 138 `