Skip to content

Commit 3d8a12d

Browse files
committed
test create project on the fly
1 parent 87b0dc6 commit 3d8a12d

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

tfplan2cai/test/cli_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package test
1616

1717
import (
18+
"encoding/json"
1819
"fmt"
1920
"log"
2021
"os"
@@ -23,6 +24,7 @@ import (
2324

2425
"github.com/GoogleCloudPlatform/terraform-google-conversion/v2/caiasset"
2526
"github.com/google/go-cmp/cmp"
27+
terraformJSON "github.com/hashicorp/terraform-json"
2628
)
2729

2830
// TestCLI tests the "convert" and "validate" subcommand against a generated .tfplan file.
@@ -32,6 +34,19 @@ func TestCLI(t *testing.T) {
3234
return
3335
}
3436

37+
if os.Getenv("CREATE_TEST_PROJECT") != "" && os.Getenv("TEST_ORG_ID") != "" {
38+
orgID := os.Getenv("TEST_ORG_ID")
39+
dir, err := os.MkdirTemp(t.TempDir(), "terraform")
40+
if err != nil {
41+
t.Fatalf("os.MkdirTemp = %v", err)
42+
}
43+
// Do not use defer since it will execute before t.Parallel()
44+
t.Cleanup(func() {
45+
terraformDestroy(t, "terraform", dir, "")
46+
})
47+
createTestProject(t, dir, orgID)
48+
}
49+
3550
// Test cases for each type of resource is defined here.
3651
cases := []struct {
3752
name string
@@ -176,3 +191,34 @@ func compareMergedIamBindingOutput(t *testing.T, expected []caiasset.Asset, actu
176191
t.Errorf("%v diff(-want, +got):\n%s", t.Name(), diff)
177192
}
178193
}
194+
195+
func createTestProject(t *testing.T, dir string, orgID string) {
196+
generateTestFiles(t, "./", dir, "create_test_project.tf")
197+
// any terraform execute failure will trigger t.Fatal
198+
terraformInit(t, "terraform", dir)
199+
terraformPlan(t, "terraform", dir, "create_test_project.tfplan")
200+
terraformApply(t, "terraform", dir, "")
201+
// terraform show result contains format_version field which is required in unmarshal.
202+
b := terraformShow(t, "terraform", dir, "")
203+
var state terraformJSON.State
204+
err := json.Unmarshal(b, &state)
205+
if err != nil {
206+
t.Fatal(err)
207+
}
208+
209+
var ok bool
210+
for _, resource := range state.Values.RootModule.Resources {
211+
if resource.Type == "google_project" {
212+
data.FolderID, ok = resource.AttributeValues["folder_id"].(string)
213+
if !ok {
214+
t.Fatalf("Failed to get folder ID from value %v", resource.AttributeValues["folder_id"])
215+
}
216+
data.Project["project"], ok = resource.AttributeValues["project_id"].(string)
217+
if !ok {
218+
t.Fatalf("Failed to get project ID from value %v", resource.AttributeValues["project_id"])
219+
}
220+
}
221+
}
222+
data.Ancestry = fmt.Sprintf("organizations/%s/folders/%s", orgID, data.FolderID)
223+
t.Logf("Successfully created folder_id=%v, project_id=%v", data.FolderID, data.Project["project"])
224+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
terraform {
2+
required_providers {
3+
google = {
4+
source = "hashicorp/google-beta"
5+
version = "~> {{.Provider.version}}"
6+
}
7+
}
8+
}
9+
10+
provider "google" {
11+
{{if .Provider.credentials }}credentials = "{{.Provider.credentials}}"{{end}}
12+
}
13+
14+
resource "random_string" "suffix" {
15+
length = 10
16+
upper = false
17+
special = false
18+
}
19+
20+
resource "google_folder" "test-folder" {
21+
display_name = "test-folder-${random_string.suffix.result}"
22+
parent = "organizations/{{.OrgID}}"
23+
}
24+
25+
resource "google_project" "test-project" {
26+
folder_id = google_folder.test-folder.name
27+
name = "My Project ${random_string.suffix.result}"
28+
project_id = "test-project-${random_string.suffix.result}"
29+
}
30+

tfplan2cai/test/utils_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,28 @@ func terraformPlan(t *testing.T, executable, dir, tfplan string) {
6363
terraformExec(t, executable, dir, "plan", "-input=false", "-refresh=false", "-out", tfplan)
6464
}
6565

66+
func terraformApply(t *testing.T, executable, dir, statePath string) {
67+
if statePath != "" {
68+
terraformExec(t, executable, dir, "apply", "-auto-approve", "-state="+statePath)
69+
return
70+
}
71+
terraformExec(t, executable, dir, "apply", "-auto-approve")
72+
}
73+
74+
func terraformDestroy(t *testing.T, executable, dir string, statePath string) {
75+
if statePath != "" {
76+
terraformExec(t, executable, dir, "destroy", "-state="+statePath, "-auto-approve")
77+
return
78+
}
79+
terraformExec(t, executable, dir, "destroy", "-state="+statePath, "-auto-approve")
80+
}
81+
6682
func terraformShow(t *testing.T, executable, dir, tfplan string) []byte {
67-
return terraformExec(t, executable, dir, "show", "--json", tfplan)
83+
if tfplan != "" {
84+
return terraformExec(t, executable, dir, "show", "-json", tfplan)
85+
}
86+
// If no tfplan provided, default to show terraform.tfstate file.
87+
return terraformExec(t, executable, dir, "show", "-json")
6888
}
6989

7090
func terraformExec(t *testing.T, executable, dir string, args ...string) []byte {

0 commit comments

Comments
 (0)