github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 TestAccAWSSubnet_enableIpv6(t *testing.T) { 86 var subnet ec2.Subnet 87 88 resource.Test(t, resource.TestCase{ 89 PreCheck: func() { testAccPreCheck(t) }, 90 IDRefreshName: "aws_subnet.foo", 91 Providers: testAccProviders, 92 CheckDestroy: testAccCheckSubnetDestroy, 93 Steps: []resource.TestStep{ 94 { 95 Config: testAccSubnetConfigPreIpv6, 96 Check: resource.ComposeTestCheckFunc( 97 testAccCheckSubnetExists( 98 "aws_subnet.foo", &subnet), 99 ), 100 }, 101 { 102 Config: testAccSubnetConfigIpv6, 103 Check: resource.ComposeTestCheckFunc( 104 testAccCheckSubnetExists( 105 "aws_subnet.foo", &subnet), 106 ), 107 }, 108 }, 109 }) 110 } 111 112 func testAccCheckAwsSubnetIpv6BeforeUpdate(t *testing.T, subnet *ec2.Subnet) resource.TestCheckFunc { 113 return func(s *terraform.State) error { 114 if subnet.Ipv6CidrBlockAssociationSet == nil { 115 return fmt.Errorf("Expected IPV6 CIDR Block Association") 116 } 117 118 if *subnet.AssignIpv6AddressOnCreation != true { 119 return fmt.Errorf("bad AssignIpv6AddressOnCreation: %t", *subnet.AssignIpv6AddressOnCreation) 120 } 121 122 return nil 123 } 124 } 125 126 func testAccCheckAwsSubnetIpv6AfterUpdate(t *testing.T, subnet *ec2.Subnet) resource.TestCheckFunc { 127 return func(s *terraform.State) error { 128 if *subnet.AssignIpv6AddressOnCreation != false { 129 return fmt.Errorf("bad AssignIpv6AddressOnCreation: %t", *subnet.AssignIpv6AddressOnCreation) 130 } 131 132 return nil 133 } 134 } 135 136 func testAccCheckAwsSubnetNotRecreated(t *testing.T, 137 before, after *ec2.Subnet) resource.TestCheckFunc { 138 return func(s *terraform.State) error { 139 if *before.SubnetId != *after.SubnetId { 140 t.Fatalf("Expected SubnetIDs not to change, but both got before: %s and after: %s", *before.SubnetId, *after.SubnetId) 141 } 142 return nil 143 } 144 } 145 146 func testAccCheckSubnetDestroy(s *terraform.State) error { 147 conn := testAccProvider.Meta().(*AWSClient).ec2conn 148 149 for _, rs := range s.RootModule().Resources { 150 if rs.Type != "aws_subnet" { 151 continue 152 } 153 154 // Try to find the resource 155 resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{ 156 SubnetIds: []*string{aws.String(rs.Primary.ID)}, 157 }) 158 if err == nil { 159 if len(resp.Subnets) > 0 { 160 return fmt.Errorf("still exist.") 161 } 162 163 return nil 164 } 165 166 // Verify the error is what we want 167 ec2err, ok := err.(awserr.Error) 168 if !ok { 169 return err 170 } 171 if ec2err.Code() != "InvalidSubnetID.NotFound" { 172 return err 173 } 174 } 175 176 return nil 177 } 178 179 func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc { 180 return func(s *terraform.State) error { 181 rs, ok := s.RootModule().Resources[n] 182 if !ok { 183 return fmt.Errorf("Not found: %s", n) 184 } 185 186 if rs.Primary.ID == "" { 187 return fmt.Errorf("No ID is set") 188 } 189 190 conn := testAccProvider.Meta().(*AWSClient).ec2conn 191 resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{ 192 SubnetIds: []*string{aws.String(rs.Primary.ID)}, 193 }) 194 if err != nil { 195 return err 196 } 197 if len(resp.Subnets) == 0 { 198 return fmt.Errorf("Subnet not found") 199 } 200 201 *v = *resp.Subnets[0] 202 203 return nil 204 } 205 } 206 207 const testAccSubnetConfig = ` 208 resource "aws_vpc" "foo" { 209 cidr_block = "10.1.0.0/16" 210 } 211 212 resource "aws_subnet" "foo" { 213 cidr_block = "10.1.1.0/24" 214 vpc_id = "${aws_vpc.foo.id}" 215 map_public_ip_on_launch = true 216 tags { 217 Name = "tf-subnet-acc-test" 218 } 219 } 220 ` 221 222 const testAccSubnetConfigPreIpv6 = ` 223 resource "aws_vpc" "foo" { 224 cidr_block = "10.10.0.0/16" 225 assign_generated_ipv6_cidr_block = true 226 } 227 228 resource "aws_subnet" "foo" { 229 cidr_block = "10.10.1.0/24" 230 vpc_id = "${aws_vpc.foo.id}" 231 map_public_ip_on_launch = true 232 tags { 233 Name = "tf-subnet-acc-test" 234 } 235 } 236 ` 237 238 const testAccSubnetConfigIpv6 = ` 239 resource "aws_vpc" "foo" { 240 cidr_block = "10.10.0.0/16" 241 assign_generated_ipv6_cidr_block = true 242 } 243 244 resource "aws_subnet" "foo" { 245 cidr_block = "10.10.1.0/24" 246 vpc_id = "${aws_vpc.foo.id}" 247 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 248 map_public_ip_on_launch = true 249 assign_ipv6_address_on_creation = true 250 tags { 251 Name = "tf-subnet-acc-test" 252 } 253 } 254 ` 255 256 const testAccSubnetConfigIpv6UpdateAssignIpv6OnCreation = ` 257 resource "aws_vpc" "foo" { 258 cidr_block = "10.10.0.0/16" 259 assign_generated_ipv6_cidr_block = true 260 } 261 262 resource "aws_subnet" "foo" { 263 cidr_block = "10.10.1.0/24" 264 vpc_id = "${aws_vpc.foo.id}" 265 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 266 map_public_ip_on_launch = true 267 assign_ipv6_address_on_creation = false 268 tags { 269 Name = "tf-subnet-acc-test" 270 } 271 } 272 ` 273 274 const testAccSubnetConfigIpv6UpdateIpv6Cidr = ` 275 resource "aws_vpc" "foo" { 276 cidr_block = "10.10.0.0/16" 277 assign_generated_ipv6_cidr_block = true 278 } 279 280 resource "aws_subnet" "foo" { 281 cidr_block = "10.10.1.0/24" 282 vpc_id = "${aws_vpc.foo.id}" 283 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 3)}" 284 map_public_ip_on_launch = true 285 assign_ipv6_address_on_creation = false 286 tags { 287 Name = "tf-subnet-acc-test" 288 } 289 } 290 `