Skip to content

Commit

Permalink
spanner: updated tests to allow VCR (GoogleCloudPlatform#12280)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyardley authored and zeleena committed Nov 18, 2024
1 parent 80a3deb commit e048917
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
)

func TestAccDataSourceSpannerInstance_basic(t *testing.T) {
// Randomness from spanner instance
acctest.SkipIfVcr(t)
t.Parallel()

context := map[string]interface{}{
Expand All @@ -34,16 +32,18 @@ func TestAccDataSourceSpannerInstance_basic(t *testing.T) {
func testAccDataSourceSpannerInstanceBasic(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_spanner_instance" "bar" {
config = "regional-us-central1"
display_name = "Test Spanner Instance"
num_nodes = 2
labels = {
"foo" = "bar"
}
name = "tf-test-%{random_suffix}"
display_name = "Test Spanner Instance"
config = "regional-us-central1"
processing_units = 100
labels = {
"foo" = "bar"
}
}
data "google_spanner_instance" "foo" {
name = google_spanner_instance.bar.name
name = google_spanner_instance.bar.name
}
`, context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ func TestAccSpannerDatabase_versionRetentionPeriod(t *testing.T) {
// Test that when the above config is reapplied:
// - changes the value (reverts to set value of `version_retention_period`, 2h)
// - is stable; no further conflict
Config: testAccSpannerDatabase_versionRetentionPeriodUpdate3(instanceName, databaseName), //same as previous step
ExpectNonEmptyPlan: false, // is stable
Config: testAccSpannerDatabase_versionRetentionPeriodUpdate3(instanceName, databaseName), // same as previous step
ExpectNonEmptyPlan: false, // is stable
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("google_spanner_database.basic", "state"),
resource.TestCheckResourceAttr("google_spanner_database.basic", "version_retention_period", "2h"),
Expand Down Expand Up @@ -465,7 +465,6 @@ resource "google_spanner_database" "basic" {
}

func TestAccSpannerDatabase_deletionProtection(t *testing.T) {
acctest.SkipIfVcr(t)
t.Parallel()

context := map[string]interface{}{
Expand Down Expand Up @@ -501,8 +500,9 @@ func TestAccSpannerDatabase_deletionProtection(t *testing.T) {
func testAccSpannerDatabase_deletionProtection(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_spanner_instance" "main" {
config = "regional-europe-west1"
name = "tf-test-%{random_suffix}"
display_name = "main-instance"
config = "regional-europe-west1"
num_nodes = 1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestAccSpannerInstance_basic(t *testing.T) {
t.Parallel()

idName := fmt.Sprintf("spanner-test-%s", acctest.RandString(t, 10))
idName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Expand All @@ -39,7 +39,7 @@ func TestAccSpannerInstance_basic(t *testing.T) {
func TestAccSpannerInstance_noNodeCountSpecified(t *testing.T) {
t.Parallel()

idName := fmt.Sprintf("spanner-test-%s", acctest.RandString(t, 10))
idName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Expand All @@ -54,11 +54,12 @@ func TestAccSpannerInstance_noNodeCountSpecified(t *testing.T) {
}

func TestAccSpannerInstance_basicWithAutogenName(t *testing.T) {
// Randomness
// Since we're testing the autogenerated name specifically here, we can't use VCR. This shouldn't be copy /
// pasted to other configs, though.
acctest.SkipIfVcr(t)
t.Parallel()

displayName := fmt.Sprintf("spanner-test-%s-dname", acctest.RandString(t, 10))
displayName := fmt.Sprintf("tf-test-%s-dname", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Expand All @@ -80,19 +81,21 @@ func TestAccSpannerInstance_basicWithAutogenName(t *testing.T) {
}

func TestAccSpannerInstance_update(t *testing.T) {
// Randomness
acctest.SkipIfVcr(t)
t.Parallel()

dName1 := fmt.Sprintf("spanner-dname1-%s", acctest.RandString(t, 10))
dName2 := fmt.Sprintf("spanner-dname2-%s", acctest.RandString(t, 10))
// Update display name, but keep real name consistent, as it cannot be
// updated after creation.
name := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
dName1 := fmt.Sprintf("tf-test-dname1-%s", acctest.RandString(t, 10))
dName2 := fmt.Sprintf("tf-test-dname2-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckSpannerInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccSpannerInstance_update(dName1, 1, false),
Config: testAccSpannerInstance_update(name, dName1, 1, false),
},
{
ResourceName: "google_spanner_instance.updater",
Expand All @@ -101,7 +104,7 @@ func TestAccSpannerInstance_update(t *testing.T) {
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccSpannerInstance_update(dName2, 2, true),
Config: testAccSpannerInstance_update(name, dName2, 2, true),
},
{
ResourceName: "google_spanner_instance.updater",
Expand All @@ -114,11 +117,9 @@ func TestAccSpannerInstance_update(t *testing.T) {
}

func TestAccSpannerInstance_virtualUpdate(t *testing.T) {
// Randomness
acctest.SkipIfVcr(t)
t.Parallel()

dName := fmt.Sprintf("spanner-dname1-%s", acctest.RandString(t, 10))
dName := fmt.Sprintf("tf-test-dname1-%s", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Expand All @@ -145,7 +146,7 @@ func TestAccSpannerInstance_virtualUpdate(t *testing.T) {
func TestAccSpannerInstance_basicWithAutoscalingUsingProcessingUnitConfig(t *testing.T) {
t.Parallel()

displayName := fmt.Sprintf("spanner-test-%s-dname", acctest.RandString(t, 10))
displayName := fmt.Sprintf("tf-test-%s-dname", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Expand Down Expand Up @@ -230,7 +231,7 @@ func TestAccSpannerInstance_basicWithAutoscalingUsingProcessingUnitConfigUpdate(
func TestAccSpannerInstance_basicWithAutoscalingUsingNodeConfig(t *testing.T) {
t.Parallel()

displayName := fmt.Sprintf("spanner-test-%s-dname", acctest.RandString(t, 10))
displayName := fmt.Sprintf("tf-test-%s-dname", acctest.RandString(t, 10))
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Expand Down Expand Up @@ -365,8 +366,9 @@ resource "google_spanner_instance" "basic" {
name = "%s"
config = "regional-us-central1"
display_name = "%s-dname"
num_nodes = 1
edition = "ENTERPRISE"
processing_units = 100
edition = "ENTERPRISE"
default_backup_schedule_type = "NONE"
}
`, name, name)
Expand All @@ -387,19 +389,21 @@ func testAccSpannerInstance_basicWithAutogenName(name string) string {
resource "google_spanner_instance" "basic" {
config = "regional-us-central1"
display_name = "%s"
num_nodes = 1
processing_units = 100
}
`, name)
}

func testAccSpannerInstance_update(name string, nodes int, addLabel bool) string {
func testAccSpannerInstance_update(name, dname string, nodes int, addLabel bool) string {
extraLabel := ""
if addLabel {
extraLabel = "\"key2\" = \"value2\""
}
return fmt.Sprintf(`
resource "google_spanner_instance" "updater" {
config = "regional-us-central1"
name = "%s"
display_name = "%s"
num_nodes = %d
Expand All @@ -408,15 +412,16 @@ resource "google_spanner_instance" "updater" {
%s
}
}
`, name, nodes, extraLabel)
`, name, dname, nodes, extraLabel)
}

func testAccSpannerInstance_virtualUpdate(name, virtual string) string {
return fmt.Sprintf(`
resource "google_spanner_instance" "basic" {
name = "%s"
config = "regional-us-central1"
display_name = "%s"
config = "regional-us-central1"
processing_units = 100
force_destroy = "%s"
}
Expand Down

0 comments on commit e048917

Please sign in to comment.