github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_vpc_peering_connection_test.go (about) 1 // make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccDataSourceAwsVpcPeeringConnection_' 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 TestAccDataSourceAwsVpcPeeringConnection_basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 Steps: []resource.TestStep{ 17 resource.TestStep{ 18 Config: testAccDataSourceAwsVpcPeeringConnectionConfig, 19 Check: resource.ComposeTestCheckFunc( 20 testAccDataSourceAwsVpcPeeringConnectionCheck("data.aws_vpc_peering_connection.test_by_id"), 21 testAccDataSourceAwsVpcPeeringConnectionCheck("data.aws_vpc_peering_connection.test_by_requester_vpc_id"), 22 testAccDataSourceAwsVpcPeeringConnectionCheck("data.aws_vpc_peering_connection.test_by_accepter_vpc_id"), 23 testAccDataSourceAwsVpcPeeringConnectionCheck("data.aws_vpc_peering_connection.test_by_requester_cidr_block"), 24 testAccDataSourceAwsVpcPeeringConnectionCheck("data.aws_vpc_peering_connection.test_by_accepter_cidr_block"), 25 testAccDataSourceAwsVpcPeeringConnectionCheck("data.aws_vpc_peering_connection.test_by_owner_ids"), 26 ), 27 ExpectNonEmptyPlan: true, 28 }, 29 }, 30 }) 31 } 32 33 func testAccDataSourceAwsVpcPeeringConnectionCheck(name string) resource.TestCheckFunc { 34 return func(s *terraform.State) error { 35 rs, ok := s.RootModule().Resources[name] 36 if !ok { 37 return fmt.Errorf("root module has no resource called %s", name) 38 } 39 40 pcxRs, ok := s.RootModule().Resources["aws_vpc_peering_connection.test"] 41 if !ok { 42 return fmt.Errorf("can't find aws_vpc_peering_connection.test in state") 43 } 44 45 attr := rs.Primary.Attributes 46 47 if attr["id"] != pcxRs.Primary.Attributes["id"] { 48 return fmt.Errorf( 49 "id is %s; want %s", 50 attr["id"], 51 pcxRs.Primary.Attributes["id"], 52 ) 53 } 54 55 return nil 56 } 57 } 58 59 const testAccDataSourceAwsVpcPeeringConnectionConfig = ` 60 provider "aws" { 61 region = "us-west-2" 62 } 63 64 resource "aws_vpc" "foo" { 65 cidr_block = "10.1.0.0/16" 66 67 tags { 68 Name = "terraform-testacc-vpc-peering-connection-data-source-foo" 69 } 70 } 71 72 resource "aws_vpc" "bar" { 73 cidr_block = "10.2.0.0/16" 74 75 tags { 76 Name = "terraform-testacc-vpc-peering-connection-data-source-bar" 77 } 78 } 79 80 resource "aws_vpc_peering_connection" "test" { 81 vpc_id = "${aws_vpc.foo.id}" 82 peer_vpc_id = "${aws_vpc.bar.id}" 83 auto_accept = true 84 85 tags { 86 Name = "terraform-testacc-vpc-peering-connection-data-source-foo-to-bar" 87 } 88 } 89 90 data "aws_caller_identity" "current" {} 91 92 data "aws_vpc_peering_connection" "test_by_id" { 93 id = "${aws_vpc_peering_connection.test.id}" 94 } 95 96 data "aws_vpc_peering_connection" "test_by_requester_vpc_id" { 97 vpc_id = "${aws_vpc.foo.id}" 98 99 depends_on = ["aws_vpc_peering_connection.test"] 100 } 101 102 data "aws_vpc_peering_connection" "test_by_accepter_vpc_id" { 103 peer_vpc_id = "${aws_vpc.bar.id}" 104 105 depends_on = ["aws_vpc_peering_connection.test"] 106 } 107 108 data "aws_vpc_peering_connection" "test_by_requester_cidr_block" { 109 cidr_block = "10.1.0.0/16" 110 status = "active" 111 112 depends_on = ["aws_vpc_peering_connection.test"] 113 } 114 115 data "aws_vpc_peering_connection" "test_by_accepter_cidr_block" { 116 peer_cidr_block = "10.2.0.0/16" 117 status = "active" 118 119 depends_on = ["aws_vpc_peering_connection.test"] 120 } 121 122 data "aws_vpc_peering_connection" "test_by_owner_ids" { 123 owner_id = "${data.aws_caller_identity.current.account_id}" 124 peer_owner_id = "${data.aws_caller_identity.current.account_id}" 125 status = "active" 126 127 depends_on = ["aws_vpc_peering_connection.test"] 128 } 129 `