github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 v ec2.Subnet
    49  
    50  	testCheck := func(*terraform.State) error {
    51  		if v.Ipv6CidrBlockAssociationSet == nil {
    52  			return fmt.Errorf("Expected IPV6 CIDR Block Association")
    53  		}
    54  
    55  		if *v.AssignIpv6AddressOnCreation != true {
    56  			return fmt.Errorf("bad AssignIpv6AddressOnCreation: %t", *v.AssignIpv6AddressOnCreation)
    57  		}
    58  
    59  		return nil
    60  	}
    61  
    62  	testCheckUpdated := func(*terraform.State) error {
    63  		if *v.AssignIpv6AddressOnCreation != false {
    64  			return fmt.Errorf("bad AssignIpv6AddressOnCreation: %t", *v.AssignIpv6AddressOnCreation)
    65  		}
    66  
    67  		return nil
    68  	}
    69  
    70  	resource.Test(t, resource.TestCase{
    71  		PreCheck:      func() { testAccPreCheck(t) },
    72  		IDRefreshName: "aws_subnet.foo",
    73  		Providers:     testAccProviders,
    74  		CheckDestroy:  testAccCheckSubnetDestroy,
    75  		Steps: []resource.TestStep{
    76  			{
    77  				Config: testAccSubnetConfigIpv6,
    78  				Check: resource.ComposeTestCheckFunc(
    79  					testAccCheckSubnetExists(
    80  						"aws_subnet.foo", &v),
    81  					testCheck,
    82  				),
    83  			},
    84  			{
    85  				Config: testAccSubnetConfigIpv6Updated,
    86  				Check: resource.ComposeTestCheckFunc(
    87  					testAccCheckSubnetExists(
    88  						"aws_subnet.foo", &v),
    89  					testCheckUpdated,
    90  				),
    91  			},
    92  		},
    93  	})
    94  }
    95  
    96  func testAccCheckSubnetDestroy(s *terraform.State) error {
    97  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    98  
    99  	for _, rs := range s.RootModule().Resources {
   100  		if rs.Type != "aws_subnet" {
   101  			continue
   102  		}
   103  
   104  		// Try to find the resource
   105  		resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
   106  			SubnetIds: []*string{aws.String(rs.Primary.ID)},
   107  		})
   108  		if err == nil {
   109  			if len(resp.Subnets) > 0 {
   110  				return fmt.Errorf("still exist.")
   111  			}
   112  
   113  			return nil
   114  		}
   115  
   116  		// Verify the error is what we want
   117  		ec2err, ok := err.(awserr.Error)
   118  		if !ok {
   119  			return err
   120  		}
   121  		if ec2err.Code() != "InvalidSubnetID.NotFound" {
   122  			return err
   123  		}
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  		rs, ok := s.RootModule().Resources[n]
   132  		if !ok {
   133  			return fmt.Errorf("Not found: %s", n)
   134  		}
   135  
   136  		if rs.Primary.ID == "" {
   137  			return fmt.Errorf("No ID is set")
   138  		}
   139  
   140  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
   141  		resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
   142  			SubnetIds: []*string{aws.String(rs.Primary.ID)},
   143  		})
   144  		if err != nil {
   145  			return err
   146  		}
   147  		if len(resp.Subnets) == 0 {
   148  			return fmt.Errorf("Subnet not found")
   149  		}
   150  
   151  		*v = *resp.Subnets[0]
   152  
   153  		return nil
   154  	}
   155  }
   156  
   157  const testAccSubnetConfig = `
   158  resource "aws_vpc" "foo" {
   159  	cidr_block = "10.1.0.0/16"
   160  }
   161  
   162  resource "aws_subnet" "foo" {
   163  	cidr_block = "10.1.1.0/24"
   164  	vpc_id = "${aws_vpc.foo.id}"
   165  	map_public_ip_on_launch = true
   166  	tags {
   167  		Name = "tf-subnet-acc-test"
   168  	}
   169  }
   170  `
   171  
   172  const testAccSubnetConfigIpv6 = `
   173  resource "aws_vpc" "foo" {
   174  	cidr_block = "10.10.0.0/16"
   175  	assign_generated_ipv6_cidr_block = true
   176  }
   177  
   178  resource "aws_subnet" "foo" {
   179  	cidr_block = "10.10.1.0/24"
   180  	vpc_id = "${aws_vpc.foo.id}"
   181  	ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}"
   182  	map_public_ip_on_launch = true
   183  	assign_ipv6_address_on_creation = true
   184  	tags {
   185  		Name = "tf-subnet-acc-test"
   186  	}
   187  }
   188  `
   189  
   190  const testAccSubnetConfigIpv6Updated = `
   191  resource "aws_vpc" "foo" {
   192  	cidr_block = "10.10.0.0/16"
   193  	assign_generated_ipv6_cidr_block = true
   194  }
   195  
   196  resource "aws_subnet" "foo" {
   197  	cidr_block = "10.10.1.0/24"
   198  	vpc_id = "${aws_vpc.foo.id}"
   199  	ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 3)}"
   200  	map_public_ip_on_launch = true
   201  	assign_ipv6_address_on_creation = false
   202  	tags {
   203  		Name = "tf-subnet-acc-test"
   204  	}
   205  }
   206  `