providers/aws: internet gateway acceptance test

This commit is contained in:
Mitchell Hashimoto 2014-07-10 14:11:45 -07:00
parent 63ef4cf28a
commit 2d34f8f209
1 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,98 @@
package aws
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2"
)
func TestAccAWSInternetGateway(t *testing.T) {
var v ec2.InternetGateway
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckInternetGatewayDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccInternetGatewayConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckInternetGatewayExists(
"aws_internet_gateway.foo", &v),
),
},
},
})
}
func testAccCheckInternetGatewayDestroy(s *terraform.State) error {
conn := testAccProvider.ec2conn
for _, rs := range s.Resources {
if rs.Type != "aws_internet_gateway" {
continue
}
// Try to find the resource
resp, err := conn.DescribeInternetGateways(
[]string{rs.ID}, ec2.NewFilter())
if err == nil {
if len(resp.InternetGateways) > 0 {
return fmt.Errorf("still exist.")
}
return nil
}
// Verify the error is what we want
ec2err, ok := err.(*ec2.Error)
if !ok {
return err
}
if ec2err.Code != "InvalidInternetGatewayID.NotFound" {
return err
}
}
return nil
}
func testAccCheckInternetGatewayExists(n string, ig *ec2.InternetGateway) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.ID == "" {
return fmt.Errorf("No ID is set")
}
conn := testAccProvider.ec2conn
resp, err := conn.DescribeInternetGateways(
[]string{rs.ID}, ec2.NewFilter())
if err != nil {
return err
}
if len(resp.InternetGateways) == 0 {
return fmt.Errorf("InternetGateway not found")
}
*ig = resp.InternetGateways[0]
return nil
}
}
const testAccInternetGatewayConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
}
resource "aws_internet_gateway" "foo" {
vpc_id = "${aws_vpc.foo.id}"
}
`