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