github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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/acctest" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccDataSourceAwsSecurityGroup(t *testing.T) { 13 rInt := acctest.RandInt() 14 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 Steps: []resource.TestStep{ 18 { 19 Config: testAccDataSourceAwsSecurityGroupConfig(rInt), 20 Check: resource.ComposeTestCheckFunc( 21 testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_id"), 22 testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_tag"), 23 testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_filter"), 24 testAccDataSourceAwsSecurityGroupCheck("data.aws_security_group.by_name"), 25 testAccDataSourceAwsSecurityGroupCheckDefault("data.aws_security_group.default_by_name"), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func testAccDataSourceAwsSecurityGroupCheck(name string) resource.TestCheckFunc { 33 return func(s *terraform.State) error { 34 rs, ok := s.RootModule().Resources[name] 35 if !ok { 36 return fmt.Errorf("root module has no resource called %s", name) 37 } 38 39 SGRs, ok := s.RootModule().Resources["aws_security_group.test"] 40 if !ok { 41 return fmt.Errorf("can't find aws_security_group.test in state") 42 } 43 vpcRs, ok := s.RootModule().Resources["aws_vpc.test"] 44 if !ok { 45 return fmt.Errorf("can't find aws_vpc.test in state") 46 } 47 attr := rs.Primary.Attributes 48 49 if attr["id"] != SGRs.Primary.Attributes["id"] { 50 return fmt.Errorf( 51 "id is %s; want %s", 52 attr["id"], 53 SGRs.Primary.Attributes["id"], 54 ) 55 } 56 57 if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] { 58 return fmt.Errorf( 59 "vpc_id is %s; want %s", 60 attr["vpc_id"], 61 vpcRs.Primary.Attributes["id"], 62 ) 63 } 64 65 if attr["tags.Name"] != "tf-acctest" { 66 return fmt.Errorf("bad Name tag %s", attr["tags.Name"]) 67 } 68 69 return nil 70 } 71 } 72 73 func testAccDataSourceAwsSecurityGroupCheckDefault(name string) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 rs, ok := s.RootModule().Resources[name] 76 if !ok { 77 return fmt.Errorf("root module has no resource called %s", name) 78 } 79 80 vpcRs, ok := s.RootModule().Resources["aws_vpc.test"] 81 if !ok { 82 return fmt.Errorf("can't find aws_vpc.test in state") 83 } 84 attr := rs.Primary.Attributes 85 86 if attr["id"] != vpcRs.Primary.Attributes["default_security_group_id"] { 87 return fmt.Errorf( 88 "id is %s; want %s", 89 attr["id"], 90 vpcRs.Primary.Attributes["default_security_group_id"], 91 ) 92 } 93 94 return nil 95 } 96 } 97 98 func testAccDataSourceAwsSecurityGroupConfig(rInt int) string { 99 return fmt.Sprintf(` 100 provider "aws" { 101 region = "eu-west-1" 102 } 103 resource "aws_vpc" "test" { 104 cidr_block = "172.16.0.0/16" 105 106 tags { 107 Name = "terraform-testacc-subnet-data-source" 108 } 109 } 110 111 resource "aws_security_group" "test" { 112 vpc_id = "${aws_vpc.test.id}" 113 name = "test-%d" 114 tags { 115 Name = "tf-acctest" 116 Seed = "%d" 117 } 118 } 119 120 data "aws_security_group" "by_id" { 121 id = "${aws_security_group.test.id}" 122 } 123 124 data "aws_security_group" "by_name" { 125 name = "${aws_security_group.test.name}" 126 } 127 128 data "aws_security_group" "default_by_name" { 129 vpc_id = "${aws_vpc.test.id}" 130 name = "default" 131 } 132 133 data "aws_security_group" "by_tag" { 134 tags { 135 Seed = "${aws_security_group.test.tags["Seed"]}" 136 } 137 } 138 139 data "aws_security_group" "by_filter" { 140 filter { 141 name = "group-name" 142 values = ["${aws_security_group.test.name}"] 143 } 144 }`, rInt, rInt) 145 }