terraform/builtin/providers/aws/data_source_aws_vpc_test.go

146 lines
3.3 KiB
Go
Raw Normal View History

2016-05-22 17:25:30 +02:00
package aws
import (
"fmt"
"math/rand"
2016-05-22 17:25:30 +02:00
"testing"
"time"
2016-05-22 17:25:30 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
2017-01-31 21:50:21 +01:00
func TestAccDataSourceAwsVpc_basic(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
rInt := rand.Intn(16)
cidr := fmt.Sprintf("172.%d.0.0/16", rInt)
tag := fmt.Sprintf("terraform-testacc-vpc-data-source-%d", rInt)
2016-05-22 17:25:30 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsVpcConfig(cidr, tag),
2016-05-22 17:25:30 +02:00
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_id", cidr, tag),
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_cidr", cidr, tag),
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_tag", cidr, tag),
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_filter", cidr, tag),
2016-05-22 17:25:30 +02:00
),
},
},
})
}
func TestAccDataSourceAwsVpc_ipv6Associated(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
rInt := rand.Intn(16)
cidr := fmt.Sprintf("172.%d.0.0/16", rInt)
tag := fmt.Sprintf("terraform-testacc-vpc-data-source-%d", rInt)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsVpcConfigIpv6(cidr, tag),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsVpcCheck("data.aws_vpc.by_id", cidr, tag),
resource.TestCheckResourceAttrSet(
"data.aws_vpc.by_id", "ipv6_association_id"),
resource.TestCheckResourceAttrSet(
"data.aws_vpc.by_id", "ipv6_cidr_block"),
),
},
},
})
}
func testAccDataSourceAwsVpcCheck(name, cidr, tag string) resource.TestCheckFunc {
2016-05-22 17:25:30 +02:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("root module has no resource called %s", name)
}
vpcRs, ok := s.RootModule().Resources["aws_vpc.test"]
if !ok {
return fmt.Errorf("can't find aws_vpc.test in state")
}
attr := rs.Primary.Attributes
if attr["id"] != vpcRs.Primary.Attributes["id"] {
return fmt.Errorf(
"id is %s; want %s",
attr["id"],
vpcRs.Primary.Attributes["id"],
)
}
if attr["cidr_block"] != cidr {
return fmt.Errorf("bad cidr_block %s, expected: %s", attr["cidr_block"], cidr)
2016-05-22 17:25:30 +02:00
}
if attr["tags.Name"] != tag {
2016-05-22 17:25:30 +02:00
return fmt.Errorf("bad Name tag %s", attr["tags.Name"])
}
return nil
}
}
func testAccDataSourceAwsVpcConfigIpv6(cidr, tag string) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "test" {
cidr_block = "%s"
assign_generated_ipv6_cidr_block = true
tags {
Name = "terraform-testacc-vpc-data-source-%d"
}
}
data "aws_vpc" "by_id" {
id = "${aws_vpc.test.id}"
}`, cidr, tag)
}
func testAccDataSourceAwsVpcConfig(cidr, tag string) string {
return fmt.Sprintf(`
2016-05-22 17:25:30 +02:00
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "test" {
cidr_block = "%s"
2016-05-22 17:25:30 +02:00
tags {
Name = "%s"
2016-05-22 17:25:30 +02:00
}
}
data "aws_vpc" "by_id" {
id = "${aws_vpc.test.id}"
}
data "aws_vpc" "by_cidr" {
cidr_block = "${aws_vpc.test.cidr_block}"
}
data "aws_vpc" "by_tag" {
tags {
Name = "${aws_vpc.test.tags["Name"]}"
}
}
data "aws_vpc" "by_filter" {
filter {
name = "cidr"
values = ["${aws_vpc.test.cidr_block}"]
}
}`, cidr, tag)
2016-05-22 17:25:30 +02:00
}