-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests for aws_subnet
- Loading branch information
Showing
11 changed files
with
232 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package test_acceptance | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/cloudetc/awsweeper/command_wipe" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
func TestAccSubnet_deleteByTags(t *testing.T) { | ||
var subnet1, subnet2 ec2.Subnet | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSubnetConfig, | ||
ExpectNonEmptyPlan: true, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSubnetExists("aws_subnet.foo", &subnet1), | ||
testAccCheckSubnetExists("aws_subnet.bar", &subnet2), | ||
testMainTags(argsDryRun, testAccSubnetAWSweeperTagsConfig), | ||
testSubnetExists(&subnet1), | ||
testSubnetExists(&subnet2), | ||
testMainTags(argsForceDelete, testAccSubnetAWSweeperTagsConfig), | ||
testSubnetDeleted(&subnet1), | ||
testSubnetExists(&subnet2), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccSubnet_deleteByIds(t *testing.T) { | ||
var subnet1, subnet2 ec2.Subnet | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSubnetConfig, | ||
ExpectNonEmptyPlan: true, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSubnetExists("aws_subnet.foo", &subnet1), | ||
testAccCheckSubnetExists("aws_subnet.bar", &subnet2), | ||
testMainSubnetIds(argsDryRun, &subnet1), | ||
testSubnetExists(&subnet1), | ||
testSubnetExists(&subnet2), | ||
testMainSubnetIds(argsForceDelete, &subnet1), | ||
testSubnetDeleted(&subnet1), | ||
testSubnetExists(&subnet2), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckSubnetExists(n string, subnet *ec2.Subnet) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No subnet ID is set") | ||
} | ||
|
||
conn := client.ec2conn | ||
DescribeSubnetOpts := &ec2.DescribeSubnetsInput{ | ||
SubnetIds: []*string{aws.String(rs.Primary.ID)}, | ||
} | ||
resp, err := conn.DescribeSubnets(DescribeSubnetOpts) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.Subnets) == 0 { | ||
return fmt.Errorf("Subnet not found") | ||
} | ||
|
||
*subnet = *resp.Subnets[0] | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testSubnetExists(subnet *ec2.Subnet) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := client.ec2conn | ||
DescribeSubnetOpts := &ec2.DescribeSubnetsInput{ | ||
SubnetIds: []*string{subnet.SubnetId}, | ||
} | ||
resp, err := conn.DescribeSubnets(DescribeSubnetOpts) | ||
if err != nil { | ||
return err | ||
} | ||
if len(resp.Subnets) == 0 { | ||
return fmt.Errorf("Subnet has been deleted") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testSubnetDeleted(subnet *ec2.Subnet) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := client.ec2conn | ||
DescribeSubnetOpts := &ec2.DescribeSubnetsInput{ | ||
SubnetIds: []*string{subnet.SubnetId}, | ||
} | ||
resp, err := conn.DescribeSubnets(DescribeSubnetOpts) | ||
if err != nil { | ||
ec2err, ok := err.(awserr.Error) | ||
if !ok { | ||
return err | ||
} | ||
if ec2err.Code() == "InvalidSubnetID.NotFound" { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if len(resp.Subnets) != 0 { | ||
return fmt.Errorf("Subnet hasn't been deleted") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testMainSubnetIds(args []string, subnet *ec2.Subnet) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
command_wipe.OsFs = afero.NewMemMapFs() | ||
afero.WriteFile(command_wipe.OsFs, "config.yml", []byte(testAccSubnetAWSweeperIdsConfig(subnet)), 0644) | ||
os.Args = args | ||
|
||
command_wipe.WrappedMain() | ||
return nil | ||
} | ||
} | ||
|
||
const testAccSubnetConfig = ` | ||
resource "aws_vpc" "foo" { | ||
cidr_block = "10.1.0.0/16" | ||
tags { | ||
Name = "awsweeper-testacc" | ||
} | ||
} | ||
resource "aws_subnet" "foo" { | ||
vpc_id = "${aws_vpc.foo.id}" | ||
cidr_block = "10.1.1.0/24" | ||
tags { | ||
foo = "bar" | ||
Name = "awsweeper-testacc" | ||
} | ||
} | ||
resource "aws_subnet" "bar" { | ||
vpc_id = "${aws_vpc.foo.id}" | ||
cidr_block = "10.1.2.0/24" | ||
tags { | ||
bar = "baz" | ||
Name = "awsweeper-testacc" | ||
} | ||
} | ||
` | ||
|
||
const testAccSubnetAWSweeperTagsConfig = ` | ||
aws_subnet: | ||
tags: | ||
foo: bar | ||
` | ||
|
||
func testAccSubnetAWSweeperIdsConfig(subnet *ec2.Subnet) string { | ||
id := subnet.SubnetId | ||
return fmt.Sprintf(` | ||
aws_subnet: | ||
ids: | ||
- %s | ||
`, *id) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package test_acceptance | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
|