github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_region_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 TestAccDataSourceAwsRegion(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: testAccDataSourceAwsRegionConfig,
    18  				Check: resource.ComposeTestCheckFunc(
    19  					testAccDataSourceAwsRegionCheck("data.aws_region.by_name_current", "us-west-2", "true"),
    20  					testAccDataSourceAwsRegionCheck("data.aws_region.by_name_other", "us-west-1", "false"),
    21  					testAccDataSourceAwsRegionCheck("data.aws_region.by_current", "us-west-2", "true"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func testAccDataSourceAwsRegionCheck(name, region, current string) resource.TestCheckFunc {
    29  	return func(s *terraform.State) error {
    30  		rs, ok := s.RootModule().Resources[name]
    31  		if !ok {
    32  			return fmt.Errorf("root module has no resource called %s", name)
    33  		}
    34  
    35  		attr := rs.Primary.Attributes
    36  
    37  		if attr["name"] != region {
    38  			return fmt.Errorf("bad name %s", attr["name"])
    39  		}
    40  		if attr["current"] != current {
    41  			return fmt.Errorf("bad current %s; want %s", attr["current"], current)
    42  		}
    43  
    44  		return nil
    45  	}
    46  }
    47  
    48  const testAccDataSourceAwsRegionConfig = `
    49  provider "aws" {
    50    region = "us-west-2"
    51  }
    52  
    53  data "aws_region" "by_name_current" {
    54    name = "us-west-2"
    55  }
    56  
    57  data "aws_region" "by_name_other" {
    58    name = "us-west-1"
    59  }
    60  
    61  data "aws_region" "by_current" {
    62    current = true
    63  }
    64  `