github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/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  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func testAccDataSourceAwsRouteTableCheck(name string) resource.TestCheckFunc {
    29  	return func(s *terraform.State) error {
    30  		rs, ok := s.RootModule().Resources[name]
    31  
    32  		if !ok {
    33  			return fmt.Errorf("root module has no resource called %s", name)
    34  		}
    35  
    36  		rts, ok := s.RootModule().Resources["aws_route_table.test"]
    37  		if !ok {
    38  			return fmt.Errorf("can't find aws_route_table.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  		subnetRs, ok := s.RootModule().Resources["aws_subnet.test"]
    45  		if !ok {
    46  			return fmt.Errorf("can't find aws_subnet.test in state")
    47  		}
    48  		attr := rs.Primary.Attributes
    49  
    50  		if attr["id"] != rts.Primary.Attributes["id"] {
    51  			return fmt.Errorf(
    52  				"id is %s; want %s",
    53  				attr["id"],
    54  				rts.Primary.Attributes["id"],
    55  			)
    56  		}
    57  
    58  		if attr["vpc_id"] != vpcRs.Primary.Attributes["id"] {
    59  			return fmt.Errorf(
    60  				"vpc_id is %s; want %s",
    61  				attr["vpc_id"],
    62  				vpcRs.Primary.Attributes["id"],
    63  			)
    64  		}
    65  
    66  		if attr["tags.Name"] != "terraform-testacc-routetable-data-source" {
    67  			return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
    68  		}
    69  		if attr["associations.0.subnet_id"] != subnetRs.Primary.Attributes["id"] {
    70  			return fmt.Errorf(
    71  				"subnet_id  is %v; want %s",
    72  				attr["associations.0.subnet_id"],
    73  				subnetRs.Primary.Attributes["id"],
    74  			)
    75  		}
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  const testAccDataSourceAwsRouteTableGroupConfig = `
    82  provider "aws" {
    83    region = "eu-central-1"
    84  }
    85  resource "aws_vpc" "test" {
    86    cidr_block = "172.16.0.0/16"
    87  
    88    tags {
    89      Name = "terraform-testacc-data-source"
    90    }
    91  }
    92  
    93  resource "aws_subnet" "test" {
    94    cidr_block = "172.16.0.0/24"
    95    vpc_id     = "${aws_vpc.test.id}"
    96    tags {
    97      Name = "terraform-testacc-data-source"
    98    }
    99  }
   100  
   101  resource "aws_route_table" "test" {
   102    vpc_id = "${aws_vpc.test.id}"
   103    tags {
   104      Name = "terraform-testacc-routetable-data-source"
   105    }
   106  }
   107  
   108  resource "aws_route_table_association" "a" {
   109      subnet_id = "${aws_subnet.test.id}"
   110      route_table_id = "${aws_route_table.test.id}"
   111  }
   112  
   113  data "aws_route_table" "by_filter" {
   114    filter {
   115      name = "association.route-table-association-id"
   116      values = ["${aws_route_table_association.a.id}"]
   117    }
   118    depends_on = ["aws_route_table_association.a"]
   119  }
   120  
   121  data "aws_route_table" "by_tag" {
   122    tags {
   123      Name = "${aws_route_table.test.tags["Name"]}"
   124    }
   125    depends_on = ["aws_route_table_association.a"]
   126  }
   127  data "aws_route_table" "by_subnet" {
   128    subnet_id = "${aws_subnet.test.id}"
   129    depends_on = ["aws_route_table_association.a"]
   130  }
   131  
   132  `