github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testAccDataSourceAwsSecurityGroupCheckDefault("data.aws_security_group.default_by_name"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func testAccDataSourceAwsSecurityGroupCheck(name string) resource.TestCheckFunc { 31 return func(s *terraform.State) error { 32 rs, ok := s.RootModule().Resources[name] 33 if !ok { 34 return fmt.Errorf("root module has no resource called %s", name) 35 } 36 37 SGRs, ok := s.RootModule().Resources["aws_security_group.test"] 38 if !ok { 39 return fmt.Errorf("can't find aws_security_group.test in state") 40 } 41 vpcRs, ok := s.RootModule().Resources["aws_vpc.test"] 42 if !ok { 43 return fmt.Errorf("can't find aws_vpc.test in state") 44 } 45 attr := rs.Primary.Attributes 46 47 if attr["id"] != SGRs.Primary.Attributes["id"] { 48 return fmt.Errorf( 49 "id is %s; want %s", 50 attr["id"], 51 SGRs.Primary.Attributes["id"], 52 ) 53 } 54 55 if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] { 56 return fmt.Errorf( 57 "vpc_id is %s; want %s", 58 attr["vpc_id"], 59 vpcRs.Primary.Attributes["id"], 60 ) 61 } 62 63 if attr["tags.Name"] != "terraform-testacc-security-group-data-source" { 64 return fmt.Errorf("bad Name tag %s", attr["tags.Name"]) 65 } 66 67 return nil 68 } 69 } 70 71 func testAccDataSourceAwsSecurityGroupCheckDefault(name string) resource.TestCheckFunc { 72 return func(s *terraform.State) error { 73 rs, ok := s.RootModule().Resources[name] 74 if !ok { 75 return fmt.Errorf("root module has no resource called %s", name) 76 } 77 78 vpcRs, ok := s.RootModule().Resources["aws_vpc.test"] 79 if !ok { 80 return fmt.Errorf("can't find aws_vpc.test in state") 81 } 82 attr := rs.Primary.Attributes 83 84 if attr["id"] != vpcRs.Primary.Attributes["default_security_group_id"] { 85 return fmt.Errorf( 86 "id is %s; want %s", 87 attr["id"], 88 vpcRs.Primary.Attributes["default_security_group_id"], 89 ) 90 } 91 92 return nil 93 } 94 } 95 96 const testAccDataSourceAwsSecurityGroupConfig = ` 97 provider "aws" { 98 region = "eu-west-1" 99 } 100 resource "aws_vpc" "test" { 101 cidr_block = "172.16.0.0/16" 102 103 tags { 104 Name = "terraform-testacc-subnet-data-source" 105 } 106 } 107 108 resource "aws_security_group" "test" { 109 vpc_id = "${aws_vpc.test.id}" 110 name = "security-groupe-name-test" 111 tags { 112 Name = "terraform-testacc-security-group-data-source" 113 } 114 } 115 116 data "aws_security_group" "by_id" { 117 id = "${aws_security_group.test.id}" 118 } 119 120 data "aws_security_group" "by_name" { 121 name = "${aws_security_group.test.name}" 122 } 123 124 data "aws_security_group" "default_by_name" { 125 vpc_id = "${aws_vpc.test.id}" 126 name = "default" 127 } 128 129 data "aws_security_group" "by_tag" { 130 tags { 131 Name = "${aws_security_group.test.tags["Name"]}" 132 } 133 } 134 135 data "aws_security_group" "by_filter" { 136 filter { 137 name = "group-name" 138 values = ["${aws_security_group.test.name}"] 139 } 140 } 141 `