github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_subnet_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/ec2" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSSubnet_basic(t *testing.T) { 15 var v ec2.Subnet 16 17 testCheck := func(*terraform.State) error { 18 if *v.CidrBlock != "10.1.1.0/24" { 19 return fmt.Errorf("bad cidr: %s", *v.CidrBlock) 20 } 21 22 if *v.MapPublicIpOnLaunch != true { 23 return fmt.Errorf("bad MapPublicIpOnLaunch: %t", *v.MapPublicIpOnLaunch) 24 } 25 26 return nil 27 } 28 29 resource.Test(t, resource.TestCase{ 30 PreCheck: func() { testAccPreCheck(t) }, 31 IDRefreshName: "aws_subnet.foo", 32 Providers: testAccProviders, 33 CheckDestroy: testAccCheckSubnetDestroy, 34 Steps: []resource.TestStep{ 35 { 36 Config: testAccSubnetConfig, 37 Check: resource.ComposeTestCheckFunc( 38 testAccCheckSubnetExists( 39 "aws_subnet.foo", &v), 40 testCheck, 41 ), 42 }, 43 }, 44 }) 45 } 46 47 func TestAccAWSSubnet_ipv6(t *testing.T) { 48 var before, after ec2.Subnet 49 50 resource.Test(t, resource.TestCase{ 51 PreCheck: func() { testAccPreCheck(t) }, 52 IDRefreshName: "aws_subnet.foo", 53 Providers: testAccProviders, 54 CheckDestroy: testAccCheckSubnetDestroy, 55 Steps: []resource.TestStep{ 56 { 57 Config: testAccSubnetConfigIpv6, 58 Check: resource.ComposeTestCheckFunc( 59 testAccCheckSubnetExists( 60 "aws_subnet.foo", &before), 61 testAccCheckAwsSubnetIpv6BeforeUpdate(t, &before), 62 ), 63 }, 64 { 65 Config: testAccSubnetConfigIpv6UpdateAssignIpv6OnCreation, 66 Check: resource.ComposeTestCheckFunc( 67 testAccCheckSubnetExists( 68 "aws_subnet.foo", &after), 69 testAccCheckAwsSubnetIpv6AfterUpdate(t, &after), 70 ), 71 }, 72 { 73 Config: testAccSubnetConfigIpv6UpdateIpv6Cidr, 74 Check: resource.ComposeTestCheckFunc( 75 testAccCheckSubnetExists( 76 "aws_subnet.foo", &after), 77 78 testAccCheckAwsSubnetNotRecreated(t, &before, &after), 79 ), 80 }, 81 }, 82 }) 83 } 84 85 func testAccCheckAwsSubnetIpv6BeforeUpdate(t *testing.T, subnet *ec2.Subnet) resource.TestCheckFunc { 86 return func(s *terraform.State) error { 87 if subnet.Ipv6CidrBlockAssociationSet == nil { 88 return fmt.Errorf("Expected IPV6 CIDR Block Association") 89 } 90 91 if *subnet.AssignIpv6AddressOnCreation != true { 92 return fmt.Errorf("bad AssignIpv6AddressOnCreation: %t", *subnet.AssignIpv6AddressOnCreation) 93 } 94 95 return nil 96 } 97 } 98 99 func testAccCheckAwsSubnetIpv6AfterUpdate(t *testing.T, subnet *ec2.Subnet) resource.TestCheckFunc { 100 return func(s *terraform.State) error { 101 if *subnet.AssignIpv6AddressOnCreation != false { 102 return fmt.Errorf("bad AssignIpv6AddressOnCreation: %t", *subnet.AssignIpv6AddressOnCreation) 103 } 104 105 return nil 106 } 107 } 108 109 func testAccCheckAwsSubnetNotRecreated(t *testing.T, 110 before, after *ec2.Subnet) resource.TestCheckFunc { 111 return func(s *terraform.State) error { 112 if *before.SubnetId != *after.SubnetId { 113 t.Fatalf("Expected SubnetIDs not to change, but both got before: %s and after: %s", *before.SubnetId, *after.SubnetId) 114 } 115 return nil 116 } 117 } 118 119 func testAccCheckSubnetDestroy(s *terraform.State) error { 120 conn := testAccProvider.Meta().(*AWSClient).ec2conn 121 122 for _, rs := range s.RootModule().Resources { 123 if rs.Type != "aws_subnet" { 124 continue 125 } 126 127 // Try to find the resource 128 resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{ 129 SubnetIds: []*string{aws.String(rs.Primary.ID)}, 130 }) 131 if err == nil { 132 if len(resp.Subnets) > 0 { 133 return fmt.Errorf("still exist.") 134 } 135 136 return nil 137 } 138 139 // Verify the error is what we want 140 ec2err, ok := err.(awserr.Error) 141 if !ok { 142 return err 143 } 144 if ec2err.Code() != "InvalidSubnetID.NotFound" { 145 return err 146 } 147 } 148 149 return nil 150 } 151 152 func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc { 153 return func(s *terraform.State) error { 154 rs, ok := s.RootModule().Resources[n] 155 if !ok { 156 return fmt.Errorf("Not found: %s", n) 157 } 158 159 if rs.Primary.ID == "" { 160 return fmt.Errorf("No ID is set") 161 } 162 163 conn := testAccProvider.Meta().(*AWSClient).ec2conn 164 resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{ 165 SubnetIds: []*string{aws.String(rs.Primary.ID)}, 166 }) 167 if err != nil { 168 return err 169 } 170 if len(resp.Subnets) == 0 { 171 return fmt.Errorf("Subnet not found") 172 } 173 174 *v = *resp.Subnets[0] 175 176 return nil 177 } 178 } 179 180 const testAccSubnetConfig = ` 181 resource "aws_vpc" "foo" { 182 cidr_block = "10.1.0.0/16" 183 } 184 185 resource "aws_subnet" "foo" { 186 cidr_block = "10.1.1.0/24" 187 vpc_id = "${aws_vpc.foo.id}" 188 map_public_ip_on_launch = true 189 tags { 190 Name = "tf-subnet-acc-test" 191 } 192 } 193 ` 194 195 const testAccSubnetConfigIpv6 = ` 196 resource "aws_vpc" "foo" { 197 cidr_block = "10.10.0.0/16" 198 assign_generated_ipv6_cidr_block = true 199 } 200 201 resource "aws_subnet" "foo" { 202 cidr_block = "10.10.1.0/24" 203 vpc_id = "${aws_vpc.foo.id}" 204 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 205 map_public_ip_on_launch = true 206 assign_ipv6_address_on_creation = true 207 tags { 208 Name = "tf-subnet-acc-test" 209 } 210 } 211 ` 212 213 const testAccSubnetConfigIpv6UpdateAssignIpv6OnCreation = ` 214 resource "aws_vpc" "foo" { 215 cidr_block = "10.10.0.0/16" 216 assign_generated_ipv6_cidr_block = true 217 } 218 219 resource "aws_subnet" "foo" { 220 cidr_block = "10.10.1.0/24" 221 vpc_id = "${aws_vpc.foo.id}" 222 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 223 map_public_ip_on_launch = true 224 assign_ipv6_address_on_creation = false 225 tags { 226 Name = "tf-subnet-acc-test" 227 } 228 } 229 ` 230 231 const testAccSubnetConfigIpv6UpdateIpv6Cidr = ` 232 resource "aws_vpc" "foo" { 233 cidr_block = "10.10.0.0/16" 234 assign_generated_ipv6_cidr_block = true 235 } 236 237 resource "aws_subnet" "foo" { 238 cidr_block = "10.10.1.0/24" 239 vpc_id = "${aws_vpc.foo.id}" 240 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 3)}" 241 map_public_ip_on_launch = true 242 assign_ipv6_address_on_creation = false 243 tags { 244 Name = "tf-subnet-acc-test" 245 } 246 } 247 `