github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_vpc_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccDataSourceAwsVpc_basic(t *testing.T) {
    14  	rand.Seed(time.Now().UTC().UnixNano())
    15  	rInt := rand.Intn(16)
    16  	cidr := fmt.Sprintf("172.%d.0.0/16", rInt)
    17  	tag := fmt.Sprintf("terraform-testacc-vpc-data-source-%d", rInt)
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:  func() { testAccPreCheck(t) },
    20  		Providers: testAccProviders,
    21  		Steps: []resource.TestStep{
    22  			{
    23  				Config: testAccDataSourceAwsVpcConfig(cidr, tag),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_id", cidr, tag),
    26  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_cidr", cidr, tag),
    27  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_tag", cidr, tag),
    28  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_filter", cidr, tag),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccDataSourceAwsVpc_ipv6Associated(t *testing.T) {
    36  	rand.Seed(time.Now().UTC().UnixNano())
    37  	rInt := rand.Intn(16)
    38  	cidr := fmt.Sprintf("172.%d.0.0/16", rInt)
    39  	tag := fmt.Sprintf("terraform-testacc-vpc-data-source-%d", rInt)
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:  func() { testAccPreCheck(t) },
    42  		Providers: testAccProviders,
    43  		Steps: []resource.TestStep{
    44  			{
    45  				Config: testAccDataSourceAwsVpcConfigIpv6(cidr, tag),
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccDataSourceAwsVpcCheck("data.aws_vpc.by_id", cidr, tag),
    48  					resource.TestCheckResourceAttrSet(
    49  						"data.aws_vpc.by_id", "ipv6_association_id"),
    50  					resource.TestCheckResourceAttrSet(
    51  						"data.aws_vpc.by_id", "ipv6_cidr_block"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func testAccDataSourceAwsVpcCheck(name, cidr, tag string) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[name]
    61  		if !ok {
    62  			return fmt.Errorf("root module has no resource called %s", name)
    63  		}
    64  
    65  		vpcRs, ok := s.RootModule().Resources["aws_vpc.test"]
    66  		if !ok {
    67  			return fmt.Errorf("can't find aws_vpc.test in state")
    68  		}
    69  
    70  		attr := rs.Primary.Attributes
    71  
    72  		if attr["id"] != vpcRs.Primary.Attributes["id"] {
    73  			return fmt.Errorf(
    74  				"id is %s; want %s",
    75  				attr["id"],
    76  				vpcRs.Primary.Attributes["id"],
    77  			)
    78  		}
    79  
    80  		if attr["cidr_block"] != cidr {
    81  			return fmt.Errorf("bad cidr_block %s, expected: %s", attr["cidr_block"], cidr)
    82  		}
    83  		if attr["tags.Name"] != tag {
    84  			return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
    85  		}
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  func testAccDataSourceAwsVpcConfigIpv6(cidr, tag string) string {
    92  	return fmt.Sprintf(`
    93  provider "aws" {
    94    region = "us-west-2"
    95  }
    96  
    97  resource "aws_vpc" "test" {
    98    cidr_block = "%s"
    99    assign_generated_ipv6_cidr_block = true
   100  
   101    tags {
   102      Name = "%s"
   103    }
   104  }
   105  
   106  data "aws_vpc" "by_id" {
   107    id = "${aws_vpc.test.id}"
   108  }`, cidr, tag)
   109  }
   110  
   111  func testAccDataSourceAwsVpcConfig(cidr, tag string) string {
   112  	return fmt.Sprintf(`
   113  provider "aws" {
   114    region = "us-west-2"
   115  }
   116  
   117  resource "aws_vpc" "test" {
   118    cidr_block = "%s"
   119  
   120    tags {
   121      Name = "%s"
   122    }
   123  }
   124  
   125  data "aws_vpc" "by_id" {
   126    id = "${aws_vpc.test.id}"
   127  }
   128  
   129  data "aws_vpc" "by_cidr" {
   130    cidr_block = "${aws_vpc.test.cidr_block}"
   131  }
   132  
   133  data "aws_vpc" "by_tag" {
   134    tags {
   135      Name = "${aws_vpc.test.tags["Name"]}"
   136    }
   137  }
   138  
   139  data "aws_vpc" "by_filter" {
   140    filter {
   141      name = "cidr"
   142      values = ["${aws_vpc.test.cidr_block}"]
   143    }
   144  }`, cidr, tag)
   145  }