Skip to content

Commit a01be1a

Browse files
authored
Add a test check function to get cai assets metadata (#13421)
1 parent 3f370a4 commit a01be1a

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package acctest
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"regexp"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-testing/terraform"
11+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
12+
)
13+
14+
// Hardcode the Terraform resource name -> API service name mapping temporarily.
15+
// TODO: [tgc] read the mapping from the resource metadata files.
16+
var ApiServiceNames = map[string]string{
17+
"google_compute_instance": "compute.googleapis.com",
18+
"google_project": "cloudresourcemanager.googleapis.com",
19+
}
20+
21+
// Gets the test metadata for tgc:
22+
// - test config
23+
// - cai asset name
24+
// For example: //compute.googleapis.com/projects/ci-test-188019/zones/us-central1-a/instances/tf-test-mi3fqaucf8
25+
func GetTestMetadataForTgc(service, address, config string) resource.TestCheckFunc {
26+
return func(s *terraform.State) error {
27+
splits := strings.Split(address, ".")
28+
if splits == nil || len(splits) < 2 {
29+
return fmt.Errorf("The resource address %s is invalid.", address)
30+
}
31+
resourceType := splits[0]
32+
resourceName := splits[1]
33+
34+
rState := s.RootModule().Resources[address]
35+
if rState == nil || rState.Primary == nil {
36+
return fmt.Errorf("The resource state is unavailable. Please check if the address %s.%s is correct.", resourceType, resourceName)
37+
}
38+
39+
// Convert the resource ID into CAI asset name
40+
// and then print out the CAI asset name in the logs
41+
if apiServiceName, ok := ApiServiceNames[resourceType]; !ok {
42+
return fmt.Errorf("The Cai product backend name for resource %s doesn't exist.", resourceType)
43+
} else {
44+
var rName string
45+
switch resourceType {
46+
case "google_project":
47+
rName = fmt.Sprintf("projects/%s", rState.Primary.Attributes["number"])
48+
default:
49+
rName = rState.Primary.ID
50+
}
51+
52+
caiAssetName := fmt.Sprintf("//%s/%s", apiServiceName, rName)
53+
log.Printf("[DEBUG]TGC CAI asset name: %s", caiAssetName)
54+
}
55+
56+
// The acceptance tests names will be also used for the tgc tests.
57+
// "service" is logged and will be used to put the tgc tests into specific service packages.
58+
log.Printf("[DEBUG]TGC Terraform service: %s", service)
59+
log.Printf("[DEBUG]TGC Terraform resource: %s", resourceType)
60+
61+
re := regexp.MustCompile(`\"(tf[-_]?test[-_]?.*?)([a-z0-9]+)\"`)
62+
config = re.ReplaceAllString(config, `"${1}tgc"`)
63+
64+
// Replace resource name with the resource's real name,
65+
// which is used to get the main resource object by checking the address after parsing raw config.
66+
// For example, replace `"google_compute_instance" "foobar"` with `"google_compute_instance" "tf-test-mi3fqaucf8"`
67+
n := tpgresource.GetResourceNameFromSelfLink(rState.Primary.ID)
68+
old := fmt.Sprintf(`"%s" "%s"`, resourceType, resourceName)
69+
new := fmt.Sprintf(`"%s" "%s"`, resourceType, n)
70+
config = strings.Replace(config, old, new, 1)
71+
72+
log.Printf("[DEBUG]TGC raw_config starts %sEnd of TGC raw_config", config)
73+
return nil
74+
}
75+
}

mmv1/third_party/terraform/services/resourcemanager/resource_google_project_test.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func TestAccProject_create(t *testing.T) {
6363
Config: testAccProject(pid, org),
6464
Check: resource.ComposeTestCheckFunc(
6565
testAccCheckGoogleProjectExists("google_project.acceptance", pid),
66+
acctest.GetTestMetadataForTgc("resourcemanager", "google_project.acceptance",
67+
testAccProject(pid, org)),
6668
),
6769
},
6870
},
@@ -155,6 +157,14 @@ func TestAccProject_labels(t *testing.T) {
155157
testAccCheckGoogleProjectHasNoLabels(t, "google_project.acceptance", pid),
156158
),
157159
},
160+
{
161+
Config: testAccProject_labels(pid, org, map[string]string{"label": "label-value"}),
162+
Check: resource.ComposeTestCheckFunc(
163+
testAccCheckGoogleProjectHasLabels(t, "google_project.acceptance", pid, map[string]string{"label": "label-value"}),
164+
acctest.GetTestMetadataForTgc("resourcemanager", "google_project.acceptance",
165+
testAccProject_labels(pid, org, map[string]string{"test": "that"})),
166+
),
167+
},
158168
},
159169
})
160170
}
@@ -188,6 +198,10 @@ func TestAccProject_parentFolder(t *testing.T) {
188198
Steps: []resource.TestStep{
189199
{
190200
Config: testAccProject_parentFolder(pid, folderDisplayName, org),
201+
Check: resource.ComposeTestCheckFunc(
202+
acctest.GetTestMetadataForTgc("resourcemanager", "google_project.acceptance",
203+
testAccProject_parentFolder(pid, folderDisplayName, org)),
204+
),
191205
},
192206
},
193207
})
@@ -611,7 +625,8 @@ resource "google_project" "acceptance" {
611625
}
612626

613627
func testAccProject_tagsAllowDestroy(pid, org string, tags map[string]string) string {
614-
r := fmt.Sprintf(`resource "google_project" "acceptance" {
628+
r := fmt.Sprintf(
629+
`resource "google_project" "acceptance" {
615630
project_id = "%s"
616631
name = "%s"
617632
org_id = "%s"

0 commit comments

Comments
 (0)