Skip to content

Commit

Permalink
Use bootstrapped service networking networks for redis, memcache tests (
Browse files Browse the repository at this point in the history
#4513)

* Use bootstrapped service networking networks for redis, memcache tests

* Fix sprintf args

* Correct address name

* Ignore other memcache tests
  • Loading branch information
slevenick authored Feb 25, 2021
1 parent bdfae12 commit ccd41ff
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 31 deletions.
6 changes: 6 additions & 0 deletions mmv1/products/memcache/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ overrides: !ruby/object:Overrides::ResourceOverrides
examples:
- !ruby/object:Provider::Terraform::Examples
min_version: beta
# Temporary as CI has used up servicenetworking quota
skip_vcr: true
name: "memcache_instance_basic"
primary_resource_id: "instance"
vars:
instance_name: "test-instance"
network_name: "test-network"
address_name: "address"
test_vars_overrides:
network_name: 'BootstrapSharedTestNetwork(t, "memcache-private")'
properties:
name: !ruby/object:Overrides::Terraform::PropertyOverride
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'
Expand Down
8 changes: 8 additions & 0 deletions mmv1/products/redis/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ overrides: !ruby/object:Overrides::ResourceOverrides
network_name: 'BootstrapSharedTestNetwork(t, "redis-full")'
- !ruby/object:Provider::Terraform::Examples
name: "redis_instance_private_service"
# Temporary for servicenetworking problems
skip_vcr: true
primary_resource_id: "cache"
vars:
instance_name: "private-cache"
address_name: "address"
network_name: "redis-test-network"
test_vars_overrides:
network_name: 'BootstrapSharedTestNetwork(t, "redis-private")'
properties:
alternativeLocationId: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
Expand Down
19 changes: 13 additions & 6 deletions mmv1/templates/terraform/examples/memcache_instance_basic.tf.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
resource "google_compute_network" "network" {
provider = google-beta
name = "tf-test%{random_suffix}"
// This example assumes this network already exists.
// The API creates a tenant network per network authorized for a
// Redis instance and that network is not deleted when the user-created
// network (authorized_network) is deleted, so this prevents issues
// with tenant network quota.
// If this network hasn't been created and you are using this example in your
// config, add an additional network resource or change
// this from "data"to "resource"
data "google_compute_network" "memcache_network" {
name = "<%= ctx[:vars]['network_name'] %>"
}

resource "google_compute_global_address" "service_range" {
provider = google-beta
name = "tf-test%{random_suffix}"
name = "<%= ctx[:vars]['address_name'] %>"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.network.id
network = data.google_compute_network.memcache_network.id
}

resource "google_service_networking_connection" "private_service_connection" {
provider = google-beta
network = google_compute_network.network.id
network = data.google_compute_network.memcache_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.service_range.name]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
resource "google_compute_network" "network" {
name = "tf-test%{random_suffix}"
// This example assumes this network already exists.
// The API creates a tenant network per network authorized for a
// Redis instance and that network is not deleted when the user-created
// network (authorized_network) is deleted, so this prevents issues
// with tenant network quota.
// If this network hasn't been created and you are using this example in your
// config, add an additional network resource or change
// this from "data"to "resource"
data "google_compute_network" "redis-network" {
name = "<%= ctx[:vars]['network_name'] %>"
}

resource "google_compute_global_address" "service_range" {
name = "tf-test%{random_suffix}"
name = "<%= ctx[:vars]['address_name'] %>"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.network.id
network = data.google_compute_network.redis-network.id
}

resource "google_service_networking_connection" "private_service_connection" {
network = google_compute_network.network.id
network = data.google_compute_network.redis-network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.service_range.name]
}

resource "google_redis_instance" "<%= ctx[:primary_resource_id] %>" {
name = "tf-test%{random_suffix}"
name = "<%= ctx[:vars]['instance_name'] %>"
tier = "STANDARD_HA"
memory_size_gb = 1

location_id = "us-central1-a"
alternative_location_id = "us-central1-f"

authorized_network = google_compute_network.network.id
authorized_network = data.google_compute_network.redis-network.id
connect_mode = "PRIVATE_SERVICE_ACCESS"

redis_version = "REDIS_4_0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@ import (

func TestAccMemcacheInstance_update(t *testing.T) {
t.Parallel()
// Temporary as CI has used up servicenetworking quota
skipIfVcr(t)

prefix := fmt.Sprintf("%d", randInt(t))
name := fmt.Sprintf("tf-test-%s", prefix)
network := BootstrapSharedTestNetwork(t, "memcache-update")

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMemcacheInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccMemcacheInstance_update(prefix, name),
Config: testAccMemcacheInstance_update(prefix, name, network),
},
{
ResourceName: "google_memcache_instance.test",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccMemcacheInstance_update2(prefix, name),
Config: testAccMemcacheInstance_update2(prefix, name, network),
},
{
ResourceName: "google_memcache_instance.test",
Expand All @@ -40,22 +43,18 @@ func TestAccMemcacheInstance_update(t *testing.T) {
})
}

func testAccMemcacheInstance_update(prefix, name string) string {
func testAccMemcacheInstance_update(prefix, name, network string) string {
return fmt.Sprintf(`
resource "google_compute_network" "network" {
name = "tf-test%s"
}

resource "google_compute_global_address" "service_range" {
name = "tf-test%s"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.network.id
network = data.google_compute_network.memcache_network.id
}

resource "google_service_networking_connection" "private_service_connection" {
network = google_compute_network.network.id
network = data.google_compute_network.memcache_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.service_range.name]
}
Expand All @@ -78,25 +77,25 @@ resource "google_memcache_instance" "test" {
}
}
}
`, prefix, prefix, name)
}

func testAccMemcacheInstance_update2(prefix, name string) string {
return fmt.Sprintf(`
resource "google_compute_network" "network" {
name = "tf-test%s"
data "google_compute_network" "memcache_network" {
name = "%s"
}
`, prefix, name, network)
}

func testAccMemcacheInstance_update2(prefix, name, network string) string {
return fmt.Sprintf(`
resource "google_compute_global_address" "service_range" {
name = "tf-test%s"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.network.id
network = data.google_compute_network.memcache_network.id
}

resource "google_service_networking_connection" "private_service_connection" {
network = google_compute_network.network.id
network = data.google_compute_network.memcache_network.id
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.service_range.name]
}
Expand All @@ -119,6 +118,10 @@ resource "google_memcache_instance" "test" {
}
}
}
`, prefix, prefix, name)

data "google_compute_network" "memcache_network" {
name = "%s"
}
`, prefix, name, network)
}
<% end -%>

0 comments on commit ccd41ff

Please sign in to comment.