Skip to content

Commit

Permalink
Merge pull request #9949 from terraform-providers/rfd-at002-subnet
Browse files Browse the repository at this point in the history
Import test refactor for subnets
  • Loading branch information
ryndaniels authored Sep 5, 2019
2 parents 5cf37a3 + d02fa08 commit 781af7c
Showing 1 changed file with 63 additions and 60 deletions.
123 changes: 63 additions & 60 deletions aws/resource_aws_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,9 @@ func testSweepSubnets(region string) error {
return nil
}

func TestAccAWSSubnet_importBasic(t *testing.T) {
resourceName := "aws_subnet.foo"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccSubnetConfig,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSSubnet_basic(t *testing.T) {
var v ec2.Subnet
resourceName := "aws_subnet.test"

testCheck := func(*terraform.State) error {
if aws.StringValue(v.CidrBlock) != "10.1.1.0/24" {
Expand All @@ -145,64 +125,75 @@ func TestAccAWSSubnet_basic(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_subnet.foo",
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccSubnetConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(
"aws_subnet.foo", &v),
resourceName, &v),
testCheck,
// ipv6 should be empty if disabled so we can still use the property in conditionals
resource.TestCheckResourceAttr(
"aws_subnet.foo", "ipv6_cidr_block", ""),
resourceName, "ipv6_cidr_block", ""),
resource.TestMatchResourceAttr(
"aws_subnet.foo",
resourceName,
"arn",
regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:subnet/subnet-.+`)),
testAccCheckResourceAttrAccountID("aws_subnet.foo", "owner_id"),
testAccCheckResourceAttrAccountID(resourceName, "owner_id"),
resource.TestCheckResourceAttrSet(
"aws_subnet.foo", "availability_zone"),
resourceName, "availability_zone"),
resource.TestCheckResourceAttrSet(
"aws_subnet.foo", "availability_zone_id"),
resourceName, "availability_zone_id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSSubnet_ipv6(t *testing.T) {
var before, after ec2.Subnet
resourceName := "aws_subnet.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_subnet.foo",
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccSubnetConfigIpv6,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(
"aws_subnet.foo", &before),
resourceName, &before),
testAccCheckAwsSubnetIpv6BeforeUpdate(t, &before),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccSubnetConfigIpv6UpdateAssignIpv6OnCreation,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(
"aws_subnet.foo", &after),
resourceName, &after),
testAccCheckAwsSubnetIpv6AfterUpdate(t, &after),
),
},
{
Config: testAccSubnetConfigIpv6UpdateIpv6Cidr,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(
"aws_subnet.foo", &after),
resourceName, &after),

testAccCheckAwsSubnetNotRecreated(t, &before, &after),
),
Expand All @@ -213,25 +204,31 @@ func TestAccAWSSubnet_ipv6(t *testing.T) {

func TestAccAWSSubnet_enableIpv6(t *testing.T) {
var subnet ec2.Subnet
resourceName := "aws_subnet.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_subnet.foo",
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccSubnetConfigPreIpv6,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(
"aws_subnet.foo", &subnet),
resourceName, &subnet),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccSubnetConfigIpv6,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(
"aws_subnet.foo", &subnet),
resourceName, &subnet),
),
},
},
Expand All @@ -240,24 +237,30 @@ func TestAccAWSSubnet_enableIpv6(t *testing.T) {

func TestAccAWSSubnet_availabilityZoneId(t *testing.T) {
var v ec2.Subnet
resourceName := "aws_subnet.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_subnet.foo",
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccSubnetConfigAvailabilityZoneId,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists(
"aws_subnet.foo", &v),
resourceName, &v),
resource.TestCheckResourceAttrSet(
"aws_subnet.foo", "availability_zone"),
resourceName, "availability_zone"),
resource.TestCheckResourceAttr(
"aws_subnet.foo", "availability_zone_id", "usw2-az3"),
resourceName, "availability_zone_id", "usw2-az3"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -358,16 +361,16 @@ func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc {
}

const testAccSubnetConfig = `
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-subnet"
}
}
resource "aws_subnet" "foo" {
resource "aws_subnet" "test" {
cidr_block = "10.1.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
vpc_id = "${aws_vpc.test.id}"
map_public_ip_on_launch = true
tags = {
Name = "tf-acc-subnet"
Expand All @@ -376,17 +379,17 @@ resource "aws_subnet" "foo" {
`

const testAccSubnetConfigPreIpv6 = `
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.10.0.0/16"
assign_generated_ipv6_cidr_block = true
tags = {
Name = "terraform-testacc-subnet-ipv6"
}
}
resource "aws_subnet" "foo" {
resource "aws_subnet" "test" {
cidr_block = "10.10.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
vpc_id = "${aws_vpc.test.id}"
map_public_ip_on_launch = true
tags = {
Name = "tf-acc-subnet-ipv6"
Expand All @@ -395,18 +398,18 @@ resource "aws_subnet" "foo" {
`

const testAccSubnetConfigIpv6 = `
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.10.0.0/16"
assign_generated_ipv6_cidr_block = true
tags = {
Name = "terraform-testacc-subnet-ipv6"
}
}
resource "aws_subnet" "foo" {
resource "aws_subnet" "test" {
cidr_block = "10.10.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}"
vpc_id = "${aws_vpc.test.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 1)}"
map_public_ip_on_launch = true
assign_ipv6_address_on_creation = true
tags = {
Expand All @@ -416,18 +419,18 @@ resource "aws_subnet" "foo" {
`

const testAccSubnetConfigIpv6UpdateAssignIpv6OnCreation = `
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.10.0.0/16"
assign_generated_ipv6_cidr_block = true
tags = {
Name = "terraform-testacc-subnet-assign-ipv6-on-creation"
}
}
resource "aws_subnet" "foo" {
resource "aws_subnet" "test" {
cidr_block = "10.10.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}"
vpc_id = "${aws_vpc.test.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 1)}"
map_public_ip_on_launch = true
assign_ipv6_address_on_creation = false
tags = {
Expand All @@ -437,18 +440,18 @@ resource "aws_subnet" "foo" {
`

const testAccSubnetConfigIpv6UpdateIpv6Cidr = `
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.10.0.0/16"
assign_generated_ipv6_cidr_block = true
tags = {
Name = "terraform-testacc-subnet-ipv6-update-cidr"
}
}
resource "aws_subnet" "foo" {
resource "aws_subnet" "test" {
cidr_block = "10.10.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 3)}"
vpc_id = "${aws_vpc.test.id}"
ipv6_cidr_block = "${cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, 3)}"
map_public_ip_on_launch = true
assign_ipv6_address_on_creation = false
tags = {
Expand All @@ -458,16 +461,16 @@ resource "aws_subnet" "foo" {
`

const testAccSubnetConfigAvailabilityZoneId = `
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
tags = {
Name = "terraform-testacc-subnet"
}
}
resource "aws_subnet" "foo" {
resource "aws_subnet" "test" {
cidr_block = "10.1.1.0/24"
vpc_id = "${aws_vpc.foo.id}"
vpc_id = "${aws_vpc.test.id}"
availability_zone_id = "usw2-az3"
tags = {
Name = "tf-acc-subnet"
Expand Down

0 comments on commit 781af7c

Please sign in to comment.