github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_elasticache_security_group_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/service/elasticache"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSElasticacheSecurityGroup_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckAWSElasticacheSecurityGroupDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccAWSElasticacheSecurityGroupConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccCheckAWSElasticacheSecurityGroupDestroy(s *terraform.State) error {
    30  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    31  
    32  	for _, rs := range s.RootModule().Resources {
    33  		if rs.Type != "aws_elasticache_security_group" {
    34  			continue
    35  		}
    36  		res, err := conn.DescribeCacheSecurityGroups(&elasticache.DescribeCacheSecurityGroupsInput{
    37  			CacheSecurityGroupName: aws.String(rs.Primary.ID),
    38  		})
    39  		if err != nil {
    40  			return err
    41  		}
    42  		if len(res.CacheSecurityGroups) > 0 {
    43  			return fmt.Errorf("still exist.")
    44  		}
    45  	}
    46  	return nil
    47  }
    48  
    49  func testAccCheckAWSElasticacheSecurityGroupExists(n string) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		rs, ok := s.RootModule().Resources[n]
    52  		if !ok {
    53  			return fmt.Errorf("Not found: %s", n)
    54  		}
    55  
    56  		if rs.Primary.ID == "" {
    57  			return fmt.Errorf("No cache security group ID is set")
    58  		}
    59  
    60  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    61  		_, err := conn.DescribeCacheSecurityGroups(&elasticache.DescribeCacheSecurityGroupsInput{
    62  			CacheSecurityGroupName: aws.String(rs.Primary.ID),
    63  		})
    64  		if err != nil {
    65  			return fmt.Errorf("CacheSecurityGroup error: %v", err)
    66  		}
    67  		return nil
    68  	}
    69  }
    70  
    71  var testAccAWSElasticacheSecurityGroupConfig = fmt.Sprintf(`
    72  resource "aws_security_group" "bar" {
    73      name = "tf-test-security-group-%03d"
    74      description = "tf-test-security-group-descr"
    75      ingress {
    76          from_port = -1
    77          to_port = -1
    78          protocol = "icmp"
    79          cidr_blocks = ["0.0.0.0/0"]
    80      }
    81  }
    82  
    83  resource "aws_elasticache_security_group" "bar" {
    84      name = "tf-test-security-group-%03d"
    85      description = "tf-test-security-group-descr"
    86      security_group_names = ["${aws_security_group.bar.name}"]
    87  }
    88  `, genRandInt(), genRandInt())