Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test create project #1724

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tfplan2cai/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package test

import (
"encoding/json"
"fmt"
"log"
"os"
Expand All @@ -23,6 +24,7 @@ import (

"github.com/GoogleCloudPlatform/terraform-google-conversion/v2/caiasset"
"github.com/google/go-cmp/cmp"
terraformJSON "github.com/hashicorp/terraform-json"
)

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

if os.Getenv("CREATE_TEST_PROJECT") != "" && os.Getenv("TEST_ORG_ID") != "" {
orgID := os.Getenv("TEST_ORG_ID")
dir, err := os.MkdirTemp(t.TempDir(), "terraform")
if err != nil {
t.Fatalf("os.MkdirTemp = %v", err)
}
// Do not use defer since it will execute before t.Parallel()
t.Cleanup(func() {
terraformDestroy(t, "terraform", dir, "")
})
createTestProject(t, dir, orgID)
}

// Test cases for each type of resource is defined here.
cases := []struct {
name string
Expand Down Expand Up @@ -176,3 +191,34 @@ func compareMergedIamBindingOutput(t *testing.T, expected []caiasset.Asset, actu
t.Errorf("%v diff(-want, +got):\n%s", t.Name(), diff)
}
}

func createTestProject(t *testing.T, dir string, orgID string) {
generateTestFiles(t, "./", dir, "create_test_project.tf")
// any terraform execute failure will trigger t.Fatal
terraformInit(t, "terraform", dir)
terraformPlan(t, "terraform", dir, "create_test_project.tfplan")
terraformApply(t, "terraform", dir, "")
// terraform show result contains format_version field which is required in unmarshal.
b := terraformShow(t, "terraform", dir, "")
var state terraformJSON.State
err := json.Unmarshal(b, &state)
if err != nil {
t.Fatal(err)
}

var ok bool
for _, resource := range state.Values.RootModule.Resources {
if resource.Type == "google_project" {
data.FolderID, ok = resource.AttributeValues["folder_id"].(string)
if !ok {
t.Fatalf("Failed to get folder ID from value %v", resource.AttributeValues["folder_id"])
}
data.Project["project"], ok = resource.AttributeValues["project_id"].(string)
if !ok {
t.Fatalf("Failed to get project ID from value %v", resource.AttributeValues["project_id"])
}
}
}
data.Ancestry = fmt.Sprintf("organizations/%s/folders/%s", orgID, data.FolderID)
t.Logf("Successfully created folder_id=%v, project_id=%v", data.FolderID, data.Project["project"])
}
30 changes: 30 additions & 0 deletions tfplan2cai/test/create_test_project.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
terraform {
required_providers {
google = {
source = "hashicorp/google-beta"
version = "~> {{.Provider.version}}"
}
}
}

provider "google" {
{{if .Provider.credentials }}credentials = "{{.Provider.credentials}}"{{end}}
}

resource "random_string" "suffix" {
length = 10
upper = false
special = false
}

resource "google_folder" "test-folder" {
display_name = "test-folder-${random_string.suffix.result}"
parent = "organizations/{{.OrgID}}"
}

resource "google_project" "test-project" {
folder_id = google_folder.test-folder.name
name = "My Project ${random_string.suffix.result}"
project_id = "test-project-${random_string.suffix.result}"
}

22 changes: 21 additions & 1 deletion tfplan2cai/test/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,28 @@ func terraformPlan(t *testing.T, executable, dir, tfplan string) {
terraformExec(t, executable, dir, "plan", "-input=false", "-refresh=false", "-out", tfplan)
}

func terraformApply(t *testing.T, executable, dir, statePath string) {
if statePath != "" {
terraformExec(t, executable, dir, "apply", "-auto-approve", "-state="+statePath)
return
}
terraformExec(t, executable, dir, "apply", "-auto-approve")
}

func terraformDestroy(t *testing.T, executable, dir string, statePath string) {
if statePath != "" {
terraformExec(t, executable, dir, "destroy", "-state="+statePath, "-auto-approve")
return
}
terraformExec(t, executable, dir, "destroy", "-state="+statePath, "-auto-approve")
}

func terraformShow(t *testing.T, executable, dir, tfplan string) []byte {
return terraformExec(t, executable, dir, "show", "--json", tfplan)
if tfplan != "" {
return terraformExec(t, executable, dir, "show", "-json", tfplan)
}
// If no tfplan provided, default to show terraform.tfstate file.
return terraformExec(t, executable, dir, "show", "-json")
}

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