Skip to content

Commit

Permalink
switch to glcoud
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody committed Dec 18, 2024
1 parent cbfc3ab commit 79f9f1c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
2 changes: 1 addition & 1 deletion test/integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/gruntwork-io/terratest v0.48.1
github.com/hashicorp/terraform-json v0.24.0
github.com/stretchr/testify v1.10.0
github.com/tidwall/gjson v1.18.0
)

require (
Expand Down Expand Up @@ -103,7 +104,6 @@ require (
github.com/pquerna/otp v1.4.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
Expand Down
52 changes: 26 additions & 26 deletions test/integration/node_pool/node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ package node_pool

import (
"fmt"
"slices"
"testing"
"time"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/cai"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/golden"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
Expand All @@ -45,20 +43,20 @@ func TestNodePool(t *testing.T) {
randomString := bpt.GetStringOutput("random_string")
kubernetesEndpoint := bpt.GetStringOutput("kubernetes_endpoint")

// Retrieve Project CAI
projectCAI := cai.GetProjectResources(t, projectId, cai.WithAssetTypes([]string{"container.googleapis.com/Cluster", "k8s.io/Node"}))
t.Log(projectCAI.Raw)
// Retrieve Cluster from CAI
clusterResourceName := fmt.Sprintf("//container.googleapis.com/projects/%s/locations/%s/clusters/%s", projectId, location, clusterName)
// // Retrieve Project CAI
// projectCAI := cai.GetProjectResources(t, projectId, cai.WithAssetTypes([]string{"container.googleapis.com/Cluster", "k8s.io/Node"}))
// t.Log(projectCAI.Raw)
// // Retrieve Cluster from CAI
// clusterResourceName := fmt.Sprintf("//container.googleapis.com/projects/%s/locations/%s/clusters/%s", projectId, location, clusterName)

if !projectCAI.Get("#(name=\"" + clusterResourceName + "\").resource.data").Exists() {
t.Fatalf("Cluster not found: %s", clusterResourceName)
}
// if !projectCAI.Get("#(name=\"" + clusterResourceName + "\").resource.data").Exists() {
// t.Fatalf("Cluster not found: %s", clusterResourceName)
// }

cluster := projectCAI.Get("#(name=\"" + clusterResourceName + "\").resource.data")
t.Log(cluster.Raw)
// cluster := projectCAI.Get("#(name=\"" + clusterResourceName + "\").resource.data")
// t.Log(cluster.Raw)
// Equivalent gcloud describe command (classic)
// cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)

// Cluster Assertions (classic)
assert.Contains([]string{"RUNNING", "RECONCILING"}, cluster.Get("status").String(), "Cluster is Running")
Expand Down Expand Up @@ -87,19 +85,21 @@ func TestNodePool(t *testing.T) {
golden.WithSanitizer(golden.StringSanitizer(randomString, "RANDOM_STRING")),
golden.WithSanitizer(golden.StringSanitizer(kubernetesEndpoint, "KUBERNETES_ENDPOINT")),
)
checkPaths := utils.GetTerminalJSONPaths(g.GetJSON())

exemptPaths := []string{"nodePools"}
checkPaths = slices.DeleteFunc(checkPaths, func(s string) bool {
return slices.Contains(exemptPaths, s)
})
g.JSONPathEqs(assert, cluster, checkPaths)

// NodePool Assertions
nodePools := []string{"pool-01", "pool-02", "pool-03", "pool-04", "pool-05"}
for _, nodePool := range nodePools {
g.JSONPathEqs(assert, cluster.Get(fmt.Sprintf("nodePools.#(name==%s).name", nodePool)), utils.GetTerminalJSONPaths(g.GetJSON().Get(fmt.Sprintf("nodePools.#(name==%s).name", nodePool))))
}
// checkPaths := utils.GetTerminalJSONPaths(g.GetJSON())

// exemptPaths := []string{"nodePools"}
// checkPaths = slices.DeleteFunc(checkPaths, func(s string) bool {
// return slices.Contains(exemptPaths, s)
// })
// g.JSONPathEqs(assert, cluster, checkPaths)

// // NodePool Assertions
// nodePools := []string{"pool-01", "pool-02", "pool-03", "pool-04", "pool-05"}
// for _, nodePool := range nodePools {
// g.JSONPathEqs(assert, cluster.Get(fmt.Sprintf("nodePools.#(name==%s).name", nodePool)), utils.GetTerminalJSONPaths(g.GetJSON().Get(fmt.Sprintf("nodePools.#(name==%s).name", nodePool))))
// }

testutils.TGKEAssertGolden(t, assert, g, cluster, clusterName, location, projectId, []string{"pool-01", "pool-02", "pool-03", "pool-04", "pool-05"}, []string{})

// nodePool-01 Assertions
assert.Equal("pool-01", cluster.Get("nodePools.#(name==\"pool-01\").name").String(), "pool-1 exists")
Expand Down
28 changes: 28 additions & 0 deletions test/integration/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
package testutils

import (
"fmt"
"slices"
"strings"
"testing"
"time"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/golden"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
tfjson "github.com/hashicorp/terraform-json"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)

var (
Expand All @@ -36,6 +40,8 @@ var (
// API Rate limit exceeded errors can be retried.
".*rateLimitExceeded.*": "Rate limit exceeded.",
}

ClusterExemptPaths = []string{"nodePools"}
)

func GetTestProjectFromSetup(t *testing.T, idx int) string {
Expand Down Expand Up @@ -67,3 +73,25 @@ func TGKEVerifyExemptResources(t *testing.T, b *tft.TFBlueprintTest, assert *ass
assert.Equal(tfjson.Actions{tfjson.ActionNoop}, r.Change.Actions, "Plan must be no-op for resource: %s", r.Address)
}
}

// TGKEAssertGolden asserts a GKE cluster and listed node pools against paths in golden image
func TGKEAssertGolden(t *testing.T, assert *assert.Assertions, g *golden.GoldenFile, clusterJson gjson.Result, clusterName string, clusterLocation string, clusterProject string, nodePools []string, exemptPaths []string) {
// Retrieve Golden Paths and remove exempt GKE paths
checkPaths := utils.GetTerminalJSONPaths(g.GetJSON())
exemptPaths = slices.Concat(exemptPaths, ClusterExemptPaths)
exemptPaths = slices.Compact(exemptPaths)
checkPaths = slices.DeleteFunc(checkPaths, func(s string) bool {
return slices.Contains(exemptPaths, s)
})

// Cluster Assertions
g.JSONPathEqs(assert, clusterJson, checkPaths)

// NodePool Assertions
for _, nodePool := range nodePools {
if !clusterJson.Get(fmt.Sprintf("nodePools.#(name==%s).name", nodePool)).Exists() {
t.Fatalf("NodePool not found: %s", nodePool)
}
g.JSONPathEqs(assert, clusterJson.Get(fmt.Sprintf("nodePools.#(name==%s).name", nodePool)), utils.GetTerminalJSONPaths(g.GetJSON().Get(fmt.Sprintf("nodePools.#(name==%s).name", nodePool))))
}
}

0 comments on commit 79f9f1c

Please sign in to comment.