From 1be62ba30c9098b0681df5b1bb84028b0267ccb3 Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Thu, 18 Mar 2021 14:01:32 -0700 Subject: [PATCH] Copied tag key tests and adjusted for values --- mmv1/products/tags/terraform.yaml | 1 + .../tests/resource_tags_tag_key_test.go.erb | 150 ++++++++++++++++++ 2 files changed, 151 insertions(+) diff --git a/mmv1/products/tags/terraform.yaml b/mmv1/products/tags/terraform.yaml index 7d13508ee5f5..d32a52a62159 100644 --- a/mmv1/products/tags/terraform.yaml +++ b/mmv1/products/tags/terraform.yaml @@ -43,6 +43,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides - !ruby/object:Provider::Terraform::Examples name: "tag_value_basic" min_version: 'beta' + skip_test: true primary_resource_id: "value" vars: short_name: "foo" diff --git a/mmv1/third_party/terraform/tests/resource_tags_tag_key_test.go.erb b/mmv1/third_party/terraform/tests/resource_tags_tag_key_test.go.erb index d113ba9467c1..7eb540792138 100644 --- a/mmv1/third_party/terraform/tests/resource_tags_tag_key_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_tags_tag_key_test.go.erb @@ -18,6 +18,8 @@ func TestAccTags(t *testing.T) { testCases := map[string]func(t *testing.T){ "basic": testAccTagsTagKey_tagKeyBasic, "update": testAccTagsTagKey_update, + "basic": testAccTagsTagValue_tagValueBasic, + "update": testAccTagsTagValue_update, } for name, tc := range testCases { @@ -156,4 +158,152 @@ func testAccCheckTagsTagKeyDestroyProducer(t *testing.T) func(s *terraform.State } } +func testAccTagsTagValue_tagValueBasic(t *testing.T) { + context := map[string]interface{}{ + "org_id": getTestOrgFromEnv(t), + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProvidersOiCS, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + CheckDestroy: testAccCheckTagsTagValueDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccTagsTagValue_tagValueBasicExample(context), + }, + }, + }) +} + +func testAccTagsTagValue_tagValueBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_tags_tag_key" "key" { + provider = google-beta + + parent = "organizations/%{org_id}" + short_name = "foobarbaz%{random_suffix}" + description = "For foo/bar/baz resources." +} + +resource "google_tags_tag_value" "value" { + provider = google-beta + + parent = "tagKeys/${google_tags_tag_key.key.name}" + short_name = "foo%{random_suffix}" + description = "For foo resources." +} +`, context) +} + +func testAccTagsTagValue_update(t *testing.T) { + context := map[string]interface{}{ + "org_id": getTestOrgFromEnv(t), + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProvidersOiCS, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + CheckDestroy: testAccCheckTagsTagValueDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccTagsTagValue_basic(context), + }, + { + ResourceName: "google_tags_tag_key.key", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccTagsTagValue_basicUpdated(context), + }, + { + ResourceName: "google_tags_tag_key.key", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccTagsTagValue_basic(context map[string]interface{}) string { + return Nprintf(` +resource "google_tags_tag_key" "key" { + provider = google-beta + + parent = "organizations/%{org_id}" + short_name = "foobarbaz%{random_suffix}" + description = "For foo/bar/baz resources." +} + +resource "google_tags_tag_value" "value" { + provider = google-beta + + parent = "tagKeys/${google_tags_tag_key.key.name}" + short_name = "foo%{random_suffix}" + description = "For foo resources." +} +`, context) +} + +func testAccTagsTagValue_basicUpdated(context map[string]interface{}) string { + return Nprintf(` +resource "google_tags_tag_key" "key" { + provider = google-beta + + parent = "organizations/%{org_id}" + short_name = "foobarbaz%{random_suffix}" + description = "For foo/bar/baz resources." +} + +resource "google_tags_tag_value" "value" { + provider = google-beta + + parent = "tagKeys/${google_tags_tag_key.key.name}" + short_name = "foo%{random_suffix}" + description = "For any foo resources." +} +`, context) +} + +func testAccCheckTagsTagValueDestroyProducer(t *testing.T) func(s *terraform.State) error { + return func(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_tags_tag_key" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := googleProviderConfig(t) + + url, err := replaceVarsForTest(config, rs, "{{TagsBasePath}}tagValues/{{name}}") + if err != nil { + return err + } + + billingProject := "" + + if config.BillingProject != "" { + billingProject = config.BillingProject + } + + _, err = sendRequest(config, "GET", billingProject, url, config.userAgent, nil) + if err == nil { + return fmt.Errorf("TagsTagValue still exists at %s", url) + } + } + + return nil + } +} + <% end %> \ No newline at end of file