github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/data_source_aws_security_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  
    11  func TestAccDataSourceAwsSecurityGroup(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:  func() { testAccPreCheck(t) },
    14  		Providers: testAccProviders,
    15  		Steps: []resource.TestStep{
    16  			resource.TestStep{
    17  				Config: testAccDataSourceAwsSecurityGroupConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_id"),
    20  					testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_tag"),
    21  					testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_filter"),
    22  					testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_name"),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccDataSourceAwsSecurityGroupCheck(name string) resource.TestCheckFunc {
    30  	return func(s *terraform.State) error {
    31  		rs, ok := s.RootModule().Resources[name]
    32  		if !ok {
    33  			return fmt.Errorf("root module has no resource called %s", name)
    34  		}
    35  
    36  		SGRs, ok := s.RootModule().Resources["aws_security_group.test"]
    37  		if !ok {
    38  			return fmt.Errorf("can't find aws_security_group.test in state")
    39  		}
    40  		vpcRs, ok := s.RootModule().Resources["aws_vpc.test"]
    41  		if !ok {
    42  			return fmt.Errorf("can't find aws_vpc.test in state")
    43  		}
    44  		attr := rs.Primary.Attributes
    45  
    46  		if attr["id"] != SGRs.Primary.Attributes["id"] {
    47  			return fmt.Errorf(
    48  				"id is %s; want %s",
    49  				attr["id"],
    50  				SGRs.Primary.Attributes["id"],
    51  			)
    52  		}
    53  
    54  		if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] {
    55  			return fmt.Errorf(
    56  				"vpc_id is %s; want %s",
    57  				attr["vpc_id"],
    58  				vpcRs.Primary.Attributes["id"],
    59  			)
    60  		}
    61  
    62  		if attr["tags.Name"] != "terraform-testacc-security-group-data-source" {
    63  			return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
    64  		}
    65  
    66  		return nil
    67  	}
    68  }
    69  
    70  const testAccDataSourceAwsSecurityGroupConfig = `
    71  provider "aws" {
    72    region = "eu-west-1"
    73  }
    74  resource "aws_vpc" "test" {
    75    cidr_block = "172.16.0.0/16"
    76  
    77    tags {
    78      Name = "terraform-testacc-subnet-data-source"
    79    }
    80  }
    81  
    82  resource "aws_security_group" "test" {
    83    vpc_id = "${aws_vpc.test.id}"
    84    name = "security-groupe-name-test"
    85    tags {
    86      Name = "terraform-testacc-security-group-data-source"
    87    }
    88  }
    89  
    90  data "aws_security_group" "by_id" {
    91    id = "${aws_security_group.test.id}"
    92  }
    93  
    94  data "aws_security_group" "by_name" {
    95    name = "${aws_security_group.test.name}"
    96  }
    97  data "aws_security_group" "by_tag" {
    98    tags {
    99      Name = "${aws_security_group.test.tags["Name"]}"
   100    }
   101  }
   102  
   103  data "aws_security_group" "by_filter" {
   104    filter {
   105      name = "group-name"
   106      values = ["${aws_security_group.test.name}"]
   107    }
   108  }
   109  `