Refactor with Acceptance Tests

This commit is contained in:
Rahul Menon 2015-03-02 14:21:13 +05:30
parent 06b2d0bbf6
commit 89d01b49ab
3 changed files with 52 additions and 45 deletions

View File

@ -81,7 +81,7 @@ func (c *Config) Client() (interface{}, error) {
//Check about using us-east-1 for all //Check about using us-east-1 for all
log.Println("[INFO] Initializing AWS-GO EC2 Connection") log.Println("[INFO] Initializing AWS-GO EC2 Connection")
client.awsEc2conn = awsEc2.New(creds, "us-east-1", nil) client.awsEc2conn = awsEc2.New(creds, c.Region, nil)
} }
if len(errs) > 0 { if len(errs) > 0 {

View File

@ -67,7 +67,7 @@ func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).awsEc2conn ec2conn := meta.(*AWSClient).awsEc2conn
cidr := d.Get("cidr_block").(string) cidr := d.Get("cidr_block").(string)
instance_tenancy := "default" instance_tenancy := "default"
if v := d.Get("instance_tenancy"); v != nil { if v, ok := d.GetOk("instance_tenancy"); ok {
instance_tenancy = v.(string) instance_tenancy = v.(string)
} }
// Create the VPC // Create the VPC
@ -75,10 +75,10 @@ func resourceAwsVpcCreate(d *schema.ResourceData, meta interface{}) error {
CIDRBlock: &cidr, CIDRBlock: &cidr,
InstanceTenancy: &instance_tenancy, InstanceTenancy: &instance_tenancy,
} }
log.Printf("[DEBUG] VPC create config: %#v", createOpts) log.Printf("[DEBUG] VPC create config: %#v", *createOpts)
vpcResp, err := ec2conn.CreateVPC(createOpts) vpcResp, err := ec2conn.CreateVPC(createOpts)
if err != nil { if err != nil {
return fmt.Errorf("Error creating VPC: %s", err) return fmt.Errorf("Error creating VPC: %s : %s", err)
} }
// Get the ID and store it // Get the ID and store it
@ -160,7 +160,7 @@ func resourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
Values: []string{("true")}, Values: []string{("true")},
} }
filter2 := &ec2.Filter{ filter2 := &ec2.Filter{
Name: awsGo.String("VPCID"), Name: awsGo.String("vpc-id"),
Values: []string{(d.Id())}, Values: []string{(d.Id())},
} }
DescribeRouteOpts := &ec2.DescribeRouteTablesRequest{ DescribeRouteOpts := &ec2.DescribeRouteTablesRequest{

View File

@ -2,11 +2,11 @@ package aws
import ( import (
"fmt" "fmt"
"testing" awsGo "github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2" "testing"
) )
func TestAccVpc_basic(t *testing.T) { func TestAccVpc_basic(t *testing.T) {
@ -50,36 +50,36 @@ func TestAccVpc_dedicatedTenancy(t *testing.T) {
}) })
} }
func TestAccVpc_tags(t *testing.T) { //func TestAccVpc_tags(t *testing.T) {
var vpc ec2.VPC // var vpc ec2.VPC
//
resource.Test(t, resource.TestCase{ // resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, // PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, // Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy, // CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{ // Steps: []resource.TestStep{
resource.TestStep{ // resource.TestStep{
Config: testAccVpcConfigTags, // Config: testAccVpcConfigTags,
Check: resource.ComposeTestCheckFunc( // Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc), // testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckVpcCidr(&vpc, "10.1.0.0/16"), // testAccCheckVpcCidr(&vpc, "10.1.0.0/16"),
resource.TestCheckResourceAttr( // resource.TestCheckResourceAttr(
"aws_vpc.foo", "cidr_block", "10.1.0.0/16"), // "aws_vpc.foo", "cidr_block", "10.1.0.0/16"),
testAccCheckTags(&vpc.Tags, "foo", "bar"), // testAccCheckTags(&vpc.Tags, "foo", "bar"),
), // ),
}, // },
//
resource.TestStep{ // resource.TestStep{
Config: testAccVpcConfigTagsUpdate, // Config: testAccVpcConfigTagsUpdate,
Check: resource.ComposeTestCheckFunc( // Check: resource.ComposeTestCheckFunc(
testAccCheckVpcExists("aws_vpc.foo", &vpc), // testAccCheckVpcExists("aws_vpc.foo", &vpc),
testAccCheckTags(&vpc.Tags, "foo", ""), // testAccCheckTags(&vpc.Tags, "foo", ""),
testAccCheckTags(&vpc.Tags, "bar", "baz"), // testAccCheckTags(&vpc.Tags, "bar", "baz"),
), // ),
}, // },
}, // },
}) // })
} //}
func TestAccVpcUpdate(t *testing.T) { func TestAccVpcUpdate(t *testing.T) {
var vpc ec2.VPC var vpc ec2.VPC
@ -111,7 +111,7 @@ func TestAccVpcUpdate(t *testing.T) {
} }
func testAccCheckVpcDestroy(s *terraform.State) error { func testAccCheckVpcDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).awsEc2conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_vpc" { if rs.Type != "aws_vpc" {
@ -119,7 +119,10 @@ func testAccCheckVpcDestroy(s *terraform.State) error {
} }
// Try to find the VPC // Try to find the VPC
resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter()) DescribeVpcOpts := &ec2.DescribeVPCsRequest{
VPCIDs: []string{rs.Primary.ID},
}
resp, err := conn.DescribeVPCs(DescribeVpcOpts)
if err == nil { if err == nil {
if len(resp.VPCs) > 0 { if len(resp.VPCs) > 0 {
return fmt.Errorf("VPCs still exist.") return fmt.Errorf("VPCs still exist.")
@ -129,7 +132,7 @@ func testAccCheckVpcDestroy(s *terraform.State) error {
} }
// Verify the error is what we want // Verify the error is what we want
ec2err, ok := err.(*ec2.Error) ec2err, ok := err.(*awsGo.APIError)
if !ok { if !ok {
return err return err
} }
@ -143,8 +146,9 @@ func testAccCheckVpcDestroy(s *terraform.State) error {
func testAccCheckVpcCidr(vpc *ec2.VPC, expected string) resource.TestCheckFunc { func testAccCheckVpcCidr(vpc *ec2.VPC, expected string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if vpc.CidrBlock != expected { CIDRBlock := vpc.CIDRBlock
return fmt.Errorf("Bad cidr: %s", vpc.CidrBlock) if *CIDRBlock != expected {
return fmt.Errorf("Bad cidr: %s", *vpc.CIDRBlock)
} }
return nil return nil
@ -162,8 +166,11 @@ func testAccCheckVpcExists(n string, vpc *ec2.VPC) resource.TestCheckFunc {
return fmt.Errorf("No VPC ID is set") return fmt.Errorf("No VPC ID is set")
} }
conn := testAccProvider.Meta().(*AWSClient).ec2conn conn := testAccProvider.Meta().(*AWSClient).awsEc2conn
resp, err := conn.DescribeVpcs([]string{rs.Primary.ID}, ec2.NewFilter()) DescribeVpcOpts := &ec2.DescribeVPCsRequest{
VPCIDs: []string{rs.Primary.ID},
}
resp, err := conn.DescribeVPCs(DescribeVpcOpts)
if err != nil { if err != nil {
return err return err
} }