github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/aws/resource_aws_db_subnet_group_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  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/rds"
    13  )
    14  
    15  func TestAccAWSDBSubnetGroup_basic(t *testing.T) {
    16  	var v rds.DBSubnetGroup
    17  
    18  	testCheck := func(*terraform.State) error {
    19  		return nil
    20  	}
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccDBSubnetGroupConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckDBSubnetGroupExists(
    31  						"aws_db_subnet_group.foo", &v),
    32  					testCheck,
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  // Regression test for https://github.com/hashicorp/terraform/issues/2603 and
    40  // https://github.com/hashicorp/terraform/issues/2664
    41  func TestAccAWSDBSubnetGroup_withUndocumentedCharacters(t *testing.T) {
    42  	var v rds.DBSubnetGroup
    43  
    44  	testCheck := func(*terraform.State) error {
    45  		return nil
    46  	}
    47  
    48  	resource.Test(t, resource.TestCase{
    49  		PreCheck:     func() { testAccPreCheck(t) },
    50  		Providers:    testAccProviders,
    51  		CheckDestroy: testAccCheckDBSubnetGroupDestroy,
    52  		Steps: []resource.TestStep{
    53  			resource.TestStep{
    54  				Config: testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckDBSubnetGroupExists(
    57  						"aws_db_subnet_group.underscores", &v),
    58  					testAccCheckDBSubnetGroupExists(
    59  						"aws_db_subnet_group.periods", &v),
    60  					testAccCheckDBSubnetGroupExists(
    61  						"aws_db_subnet_group.spaces", &v),
    62  					testCheck,
    63  				),
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func TestResourceAWSDBSubnetGroupNameValidation(t *testing.T) {
    70  	cases := []struct {
    71  		Value    string
    72  		ErrCount int
    73  	}{
    74  		{
    75  			Value:    "tEsting",
    76  			ErrCount: 1,
    77  		},
    78  		{
    79  			Value:    "testing?",
    80  			ErrCount: 1,
    81  		},
    82  		{
    83  			Value:    "default",
    84  			ErrCount: 1,
    85  		},
    86  		{
    87  			Value:    randomString(300),
    88  			ErrCount: 1,
    89  		},
    90  	}
    91  
    92  	for _, tc := range cases {
    93  		_, errors := validateSubnetGroupName(tc.Value, "aws_db_subnet_group")
    94  
    95  		if len(errors) != tc.ErrCount {
    96  			t.Fatalf("Expected the DB Subnet Group name to trigger a validation error")
    97  		}
    98  	}
    99  }
   100  
   101  func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error {
   102  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   103  
   104  	for _, rs := range s.RootModule().Resources {
   105  		if rs.Type != "aws_db_subnet_group" {
   106  			continue
   107  		}
   108  
   109  		// Try to find the resource
   110  		resp, err := conn.DescribeDBSubnetGroups(
   111  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
   112  		if err == nil {
   113  			if len(resp.DBSubnetGroups) > 0 {
   114  				return fmt.Errorf("still exist.")
   115  			}
   116  
   117  			return nil
   118  		}
   119  
   120  		// Verify the error is what we want
   121  		rdserr, ok := err.(awserr.Error)
   122  		if !ok {
   123  			return err
   124  		}
   125  		if rdserr.Code() != "DBSubnetGroupNotFoundFault" {
   126  			return err
   127  		}
   128  	}
   129  
   130  	return nil
   131  }
   132  
   133  func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc {
   134  	return func(s *terraform.State) error {
   135  		rs, ok := s.RootModule().Resources[n]
   136  		if !ok {
   137  			return fmt.Errorf("Not found: %s", n)
   138  		}
   139  
   140  		if rs.Primary.ID == "" {
   141  			return fmt.Errorf("No ID is set")
   142  		}
   143  
   144  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   145  		resp, err := conn.DescribeDBSubnetGroups(
   146  			&rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)})
   147  		if err != nil {
   148  			return err
   149  		}
   150  		if len(resp.DBSubnetGroups) == 0 {
   151  			return fmt.Errorf("DbSubnetGroup not found")
   152  		}
   153  
   154  		*v = *resp.DBSubnetGroups[0]
   155  
   156  		return nil
   157  	}
   158  }
   159  
   160  const testAccDBSubnetGroupConfig = `
   161  resource "aws_vpc" "foo" {
   162  	cidr_block = "10.1.0.0/16"
   163  }
   164  
   165  resource "aws_subnet" "foo" {
   166  	cidr_block = "10.1.1.0/24"
   167  	availability_zone = "us-west-2a"
   168  	vpc_id = "${aws_vpc.foo.id}"
   169  	tags {
   170  		Name = "tf-dbsubnet-test-1"
   171  	}
   172  }
   173  
   174  resource "aws_subnet" "bar" {
   175  	cidr_block = "10.1.2.0/24"
   176  	availability_zone = "us-west-2b"
   177  	vpc_id = "${aws_vpc.foo.id}"
   178  	tags {
   179  		Name = "tf-dbsubnet-test-2"
   180  	}
   181  }
   182  
   183  resource "aws_db_subnet_group" "foo" {
   184  	name = "foo"
   185  	description = "foo description"
   186  	subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"]
   187  	tags {
   188  		Name = "tf-dbsubnet-group-test"
   189  	}
   190  }
   191  `
   192  
   193  const testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces = `
   194  resource "aws_vpc" "main" {
   195      cidr_block = "192.168.0.0/16"
   196  }
   197  
   198  resource "aws_subnet" "frontend" {
   199      vpc_id = "${aws_vpc.main.id}"
   200      availability_zone = "us-west-2b"
   201      cidr_block = "192.168.1.0/24"
   202  }
   203  
   204  resource "aws_subnet" "backend" {
   205      vpc_id = "${aws_vpc.main.id}"
   206      availability_zone = "us-west-2c"
   207      cidr_block = "192.168.2.0/24"
   208  }
   209  
   210  resource "aws_db_subnet_group" "underscores" {
   211      name = "with_underscores"
   212      description = "Our main group of subnets"
   213      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   214  }
   215  
   216  resource "aws_db_subnet_group" "periods" {
   217      name = "with.periods"
   218      description = "Our main group of subnets"
   219      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   220  }
   221  
   222  resource "aws_db_subnet_group" "spaces" {
   223      name = "with spaces"
   224      description = "Our main group of subnets"
   225      subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"]
   226  }
   227  `