github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/data_source_aws_route_table_test.go (about)

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