mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
![Joe Selman](/assets/img/avatar_default.png)
This reverts commit 8ab9d96d25
and revives
the original commit that adds t.Parallel to all acceptance tests. It
turns out test failures were unrelated to this change (rather, they were
related to quota issues).
209 lines
5.1 KiB
Go
209 lines
5.1 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccComputeRouter_basic(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
resourceRegion := "europe-west1"
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckComputeRouterDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccComputeRouterBasic(resourceRegion),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckComputeRouterExists(
|
|
"google_compute_router.foobar"),
|
|
resource.TestCheckResourceAttr(
|
|
"google_compute_router.foobar", "region", resourceRegion),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccComputeRouter_noRegion(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
providerRegion := "us-central1"
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckComputeRouterDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccComputeRouterNoRegion(providerRegion),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckComputeRouterExists(
|
|
"google_compute_router.foobar"),
|
|
resource.TestCheckResourceAttr(
|
|
"google_compute_router.foobar", "region", providerRegion),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccComputeRouter_networkLink(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckComputeRouterDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testAccComputeRouterNetworkLink(),
|
|
Check: testAccCheckComputeRouterExists(
|
|
"google_compute_router.foobar"),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckComputeRouterDestroy(s *terraform.State) error {
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
routersService := config.clientCompute.Routers
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "google_compute_router" {
|
|
continue
|
|
}
|
|
|
|
project, err := getTestProject(rs.Primary, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
region, err := getTestRegion(rs.Primary, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
_, err = routersService.Get(project, region, name).Do()
|
|
|
|
if err == nil {
|
|
return fmt.Errorf("Error, Router %s in region %s still exists",
|
|
name, region)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testAccCheckComputeRouterExists(n string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[n]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", n)
|
|
}
|
|
|
|
if rs.Primary.ID == "" {
|
|
return fmt.Errorf("No ID is set")
|
|
}
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
project, err := getTestProject(rs.Primary, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
region, err := getTestRegion(rs.Primary, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
name := rs.Primary.Attributes["name"]
|
|
|
|
routersService := config.clientCompute.Routers
|
|
_, err = routersService.Get(project, region, name).Do()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("Error Reading Router %s: %s", name, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccComputeRouterBasic(resourceRegion string) string {
|
|
testId := acctest.RandString(10)
|
|
return fmt.Sprintf(`
|
|
resource "google_compute_network" "foobar" {
|
|
name = "router-test-%s"
|
|
}
|
|
resource "google_compute_subnetwork" "foobar" {
|
|
name = "router-test-subnetwork-%s"
|
|
network = "${google_compute_network.foobar.self_link}"
|
|
ip_cidr_range = "10.0.0.0/16"
|
|
region = "%s"
|
|
}
|
|
resource "google_compute_router" "foobar" {
|
|
name = "router-test-%s"
|
|
region = "${google_compute_subnetwork.foobar.region}"
|
|
network = "${google_compute_network.foobar.name}"
|
|
bgp {
|
|
asn = 64514
|
|
}
|
|
}
|
|
`, testId, testId, resourceRegion, testId)
|
|
}
|
|
|
|
func testAccComputeRouterNoRegion(providerRegion string) string {
|
|
testId := acctest.RandString(10)
|
|
return fmt.Sprintf(`
|
|
resource "google_compute_network" "foobar" {
|
|
name = "router-test-%s"
|
|
}
|
|
resource "google_compute_subnetwork" "foobar" {
|
|
name = "router-test-subnetwork-%s"
|
|
network = "${google_compute_network.foobar.name}"
|
|
ip_cidr_range = "10.0.0.0/16"
|
|
region = "%s"
|
|
}
|
|
resource "google_compute_router" "foobar" {
|
|
name = "router-test-%s"
|
|
network = "${google_compute_network.foobar.name}"
|
|
bgp {
|
|
asn = 64514
|
|
}
|
|
}
|
|
`, testId, testId, providerRegion, testId)
|
|
}
|
|
|
|
func testAccComputeRouterNetworkLink() string {
|
|
testId := acctest.RandString(10)
|
|
return fmt.Sprintf(`
|
|
resource "google_compute_network" "foobar" {
|
|
name = "router-test-%s"
|
|
}
|
|
resource "google_compute_subnetwork" "foobar" {
|
|
name = "router-test-subnetwork-%s"
|
|
network = "${google_compute_network.foobar.self_link}"
|
|
ip_cidr_range = "10.0.0.0/16"
|
|
region = "europe-west1"
|
|
}
|
|
resource "google_compute_router" "foobar" {
|
|
name = "router-test-%s"
|
|
region = "${google_compute_subnetwork.foobar.region}"
|
|
network = "${google_compute_network.foobar.self_link}"
|
|
bgp {
|
|
asn = 64514
|
|
}
|
|
}
|
|
`, testId, testId, testId)
|
|
}
|