terraform-provider-google/google/resource_compute_route_test.go

142 lines
3.5 KiB
Go
Raw Normal View History

2014-08-26 05:39:29 +00:00
package google
import (
"fmt"
2017-11-16 18:53:49 +00:00
"regexp"
2014-08-26 05:39:29 +00:00
"testing"
"github.com/hashicorp/terraform/helper/acctest"
2014-08-26 05:39:29 +00:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/compute/v1"
2014-08-26 05:39:29 +00:00
)
func TestAccComputeRoute_defaultInternetGateway(t *testing.T) {
t.Parallel()
var route compute.Route
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeRoute_defaultInternetGateway(),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRouteExists(
"google_compute_route.foobar", &route),
),
},
{
2017-12-19 23:33:20 +00:00
ResourceName: "google_compute_route.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccComputeRoute_hopInstance(t *testing.T) {
var route compute.Route
2017-10-20 16:48:00 +00:00
instanceName := "tf" + acctest.RandString(10)
zone := "us-central1-b"
2017-11-16 18:53:49 +00:00
instanceNameRegexp := regexp.MustCompile(fmt.Sprintf("projects/(.+)/zones/%s/instances/%s$", zone, instanceName))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeRoute_hopInstance(instanceName, zone),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeRouteExists(
"google_compute_route.foobar", &route),
2017-11-16 18:53:49 +00:00
resource.TestMatchResourceAttr("google_compute_route.foobar", "next_hop_instance", instanceNameRegexp),
resource.TestMatchResourceAttr("google_compute_route.foobar", "next_hop_instance", instanceNameRegexp),
),
},
{
2017-12-19 23:33:20 +00:00
ResourceName: "google_compute_route.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
2014-08-26 05:39:29 +00:00
func testAccCheckComputeRouteExists(n string, route *compute.Route) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
2014-08-26 05:39:29 +00:00
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
2014-08-26 05:39:29 +00:00
return fmt.Errorf("No ID is set")
}
config := testAccProvider.Meta().(*Config)
found, err := config.clientCompute.Routes.Get(
config.Project, rs.Primary.ID).Do()
2014-08-26 05:39:29 +00:00
if err != nil {
return err
}
if found.Name != rs.Primary.ID {
2014-08-26 05:39:29 +00:00
return fmt.Errorf("Route not found")
}
*route = *found
return nil
}
}
func testAccComputeRoute_defaultInternetGateway() string {
return fmt.Sprintf(`
resource "google_compute_route" "foobar" {
name = "route-test-%s"
dest_range = "0.0.0.0/0"
network = "default"
next_hop_gateway = "default-internet-gateway"
priority = 100
}`, acctest.RandString(10))
}
func testAccComputeRoute_hopInstance(instanceName, zone string) string {
return fmt.Sprintf(`
2018-08-13 23:20:33 +00:00
data "google_compute_image" "my_image" {
2018-08-15 20:23:31 +00:00
family = "debian-9"
project = "debian-cloud"
2018-08-13 23:20:33 +00:00
}
resource "google_compute_instance" "foo" {
name = "%s"
machine_type = "n1-standard-1"
zone = "%s"
boot_disk {
initialize_params{
2018-08-13 23:20:33 +00:00
image = "${data.google_compute_image.my_image.self_link}"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_route" "foobar" {
name = "route-test-%s"
dest_range = "0.0.0.0/0"
network = "default"
next_hop_instance = "${google_compute_instance.foo.name}"
next_hop_instance_zone = "${google_compute_instance.foo.zone}"
priority = 100
}`, instanceName, zone, acctest.RandString(10))
}