github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_subnet_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/ec2"
    10  )
    11  
    12  func TestAccAWSSubnet(t *testing.T) {
    13  	var v ec2.Subnet
    14  
    15  	testCheck := func(*terraform.State) error {
    16  		if v.CidrBlock != "10.1.1.0/24" {
    17  			return fmt.Errorf("bad cidr: %s", v.CidrBlock)
    18  		}
    19  
    20  		if v.MapPublicIpOnLaunch != true {
    21  			return fmt.Errorf("bad MapPublicIpOnLaunch: %t", v.MapPublicIpOnLaunch)
    22  		}
    23  
    24  		return nil
    25  	}
    26  
    27  	resource.Test(t, resource.TestCase{
    28  		PreCheck:     func() { testAccPreCheck(t) },
    29  		Providers:    testAccProviders,
    30  		CheckDestroy: testAccCheckSubnetDestroy,
    31  		Steps: []resource.TestStep{
    32  			resource.TestStep{
    33  				Config: testAccSubnetConfig,
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckSubnetExists(
    36  						"aws_subnet.foo", &v),
    37  					testCheck,
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func testAccCheckSubnetDestroy(s *terraform.State) error {
    45  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    46  
    47  	for _, rs := range s.RootModule().Resources {
    48  		if rs.Type != "aws_subnet" {
    49  			continue
    50  		}
    51  
    52  		// Try to find the resource
    53  		resp, err := conn.DescribeSubnets(
    54  			[]string{rs.Primary.ID}, ec2.NewFilter())
    55  		if err == nil {
    56  			if len(resp.Subnets) > 0 {
    57  				return fmt.Errorf("still exist.")
    58  			}
    59  
    60  			return nil
    61  		}
    62  
    63  		// Verify the error is what we want
    64  		ec2err, ok := err.(*ec2.Error)
    65  		if !ok {
    66  			return err
    67  		}
    68  		if ec2err.Code != "InvalidSubnetID.NotFound" {
    69  			return err
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func testAccCheckSubnetExists(n string, v *ec2.Subnet) resource.TestCheckFunc {
    77  	return func(s *terraform.State) error {
    78  		rs, ok := s.RootModule().Resources[n]
    79  		if !ok {
    80  			return fmt.Errorf("Not found: %s", n)
    81  		}
    82  
    83  		if rs.Primary.ID == "" {
    84  			return fmt.Errorf("No ID is set")
    85  		}
    86  
    87  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    88  		resp, err := conn.DescribeSubnets(
    89  			[]string{rs.Primary.ID}, ec2.NewFilter())
    90  		if err != nil {
    91  			return err
    92  		}
    93  		if len(resp.Subnets) == 0 {
    94  			return fmt.Errorf("Subnet not found")
    95  		}
    96  
    97  		*v = resp.Subnets[0]
    98  
    99  		return nil
   100  	}
   101  }
   102  
   103  const testAccSubnetConfig = `
   104  resource "aws_vpc" "foo" {
   105  	cidr_block = "10.1.0.0/16"
   106  }
   107  
   108  resource "aws_subnet" "foo" {
   109  	cidr_block = "10.1.1.0/24"
   110  	vpc_id = "${aws_vpc.foo.id}"
   111  	map_public_ip_on_launch = true
   112  }
   113  `