github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_route_table_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 TestAccDataSourceAwsRouteTable(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: testAccDataSourceAwsRouteTableGroupConfig, 18 Check: resource.ComposeTestCheckFunc( 19 testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_tag"), 20 testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_filter"), 21 testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_subnet"), 22 testAccDataSourceAwsRouteTableCheck("data.aws_route_table.by_id"), 23 ), 24 }, 25 }, 26 }) 27 } 28 29 func TestAccDataSourceAwsRouteTable_main(t *testing.T) { 30 resource.Test(t, resource.TestCase{ 31 PreCheck: func() { testAccPreCheck(t) }, 32 Providers: testAccProviders, 33 Steps: []resource.TestStep{ 34 resource.TestStep{ 35 Config: testAccDataSourceAwsRouteTableMainRoute, 36 Check: resource.ComposeTestCheckFunc( 37 testAccDataSourceAwsRouteTableCheckMain("data.aws_route_table.by_filter"), 38 ), 39 }, 40 }, 41 }) 42 } 43 44 func testAccDataSourceAwsRouteTableCheck(name string) resource.TestCheckFunc { 45 return func(s *terraform.State) error { 46 rs, ok := s.RootModule().Resources[name] 47 48 if !ok { 49 return fmt.Errorf("root module has no resource called %s", name) 50 } 51 52 rts, ok := s.RootModule().Resources["aws_route_table.test"] 53 if !ok { 54 return fmt.Errorf("can't find aws_route_table.test in state") 55 } 56 vpcRs, ok := s.RootModule().Resources["aws_vpc.test"] 57 if !ok { 58 return fmt.Errorf("can't find aws_vpc.test in state") 59 } 60 subnetRs, ok := s.RootModule().Resources["aws_subnet.test"] 61 if !ok { 62 return fmt.Errorf("can't find aws_subnet.test in state") 63 } 64 attr := rs.Primary.Attributes 65 66 if attr["id"] != rts.Primary.Attributes["id"] { 67 return fmt.Errorf( 68 "id is %s; want %s", 69 attr["id"], 70 rts.Primary.Attributes["id"], 71 ) 72 } 73 74 if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] { 75 return fmt.Errorf( 76 "vpc_id is %s; want %s", 77 attr["vpc_id"], 78 vpcRs.Primary.Attributes["id"], 79 ) 80 } 81 82 if attr["tags.Name"] != "terraform-testacc-routetable-data-source" { 83 return fmt.Errorf("bad Name tag %s", attr["tags.Name"]) 84 } 85 if attr["associations.0.subnet_id"] != subnetRs.Primary.Attributes["id"] { 86 return fmt.Errorf( 87 "subnet_id is %v; want %s", 88 attr["associations.0.subnet_id"], 89 subnetRs.Primary.Attributes["id"], 90 ) 91 } 92 93 return nil 94 } 95 } 96 97 func testAccDataSourceAwsRouteTableCheckMain(name string) resource.TestCheckFunc { 98 return func(s *terraform.State) error { 99 rs, ok := s.RootModule().Resources[name] 100 101 if !ok { 102 return fmt.Errorf("root module has no resource called %s", name) 103 } 104 105 attr := rs.Primary.Attributes 106 107 // Verify attributes are set 108 if _, ok := attr["id"]; !ok { 109 return fmt.Errorf("id not set for main route table") 110 } 111 if _, ok := attr["vpc_id"]; !ok { 112 return fmt.Errorf("vpc_id not set for main route table") 113 } 114 // Verify it's actually the main route table that's returned 115 if attr["associations.0.main"] != "true" { 116 return fmt.Errorf("main route table not found") 117 } 118 119 return nil 120 } 121 } 122 123 const testAccDataSourceAwsRouteTableGroupConfig = ` 124 provider "aws" { 125 region = "eu-central-1" 126 } 127 resource "aws_vpc" "test" { 128 cidr_block = "172.16.0.0/16" 129 130 tags { 131 Name = "terraform-testacc-data-source" 132 } 133 } 134 135 resource "aws_subnet" "test" { 136 cidr_block = "172.16.0.0/24" 137 vpc_id = "${aws_vpc.test.id}" 138 tags { 139 Name = "terraform-testacc-data-source" 140 } 141 } 142 143 resource "aws_route_table" "test" { 144 vpc_id = "${aws_vpc.test.id}" 145 tags { 146 Name = "terraform-testacc-routetable-data-source" 147 } 148 } 149 150 resource "aws_route_table_association" "a" { 151 subnet_id = "${aws_subnet.test.id}" 152 route_table_id = "${aws_route_table.test.id}" 153 } 154 155 data "aws_route_table" "by_filter" { 156 filter { 157 name = "association.route-table-association-id" 158 values = ["${aws_route_table_association.a.id}"] 159 } 160 depends_on = ["aws_route_table_association.a"] 161 } 162 163 data "aws_route_table" "by_tag" { 164 tags { 165 Name = "${aws_route_table.test.tags["Name"]}" 166 } 167 depends_on = ["aws_route_table_association.a"] 168 } 169 170 data "aws_route_table" "by_subnet" { 171 subnet_id = "${aws_subnet.test.id}" 172 depends_on = ["aws_route_table_association.a"] 173 } 174 175 data "aws_route_table" "by_id" { 176 route_table_id = "${aws_route_table.test.id}" 177 depends_on = ["aws_route_table_association.a"] 178 } 179 ` 180 181 // Uses us-east-2, as region only has a single main route table 182 const testAccDataSourceAwsRouteTableMainRoute = ` 183 provider "aws" { 184 region = "us-east-2" 185 } 186 187 data "aws_route_table" "by_filter" { 188 filter { 189 name = "association.main" 190 values = ["true"] 191 } 192 } 193 `