@@ -15,16 +15,20 @@ package node_pool
1515
1616import (
1717 "fmt"
18+ "slices"
19+ "strings"
1820 "testing"
1921 "time"
2022
23+ "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/cai"
2124 "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
2225 "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/golden"
2326 "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
27+ "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2428 "github.com/gruntwork-io/terratest/modules/k8s"
2529 "github.com/stretchr/testify/assert"
2630 "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/testutils"
27- gkeutils "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/utils "
31+ "golang.org/x/sync/errgroup "
2832)
2933
3034func TestNodePool (t * testing.T ) {
@@ -35,15 +39,18 @@ func TestNodePool(t *testing.T) {
3539 bpt .DefineVerify (func (assert * assert.Assertions ) {
3640 // Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token
3741 // bpt.DefaultVerify(assert)
38- gkeutils .TGKEVerify (t , bpt , assert ) // Verify Resources
42+ testutils .TGKEVerify (t , bpt , assert ) // Verify Resources
3943
4044 projectId := bpt .GetStringOutput ("project_id" )
4145 location := bpt .GetStringOutput ("location" )
4246 clusterName := bpt .GetStringOutput ("cluster_name" )
47+ randomString := bpt .GetStringOutput ("random_string" )
48+ kubernetesEndpoint := bpt .GetStringOutput ("kubernetes_endpoint" )
49+ //serviceAccount := bpt.GetStringOutput("service_account")
4350
4451 // CAI
4552 clusterResourceName := fmt .Sprintf ("//container.googleapis.com/projects/%s/locations/%s/clusters/%s" , projectId , location , clusterName )
46- cluster := gkeutils .GetProjectResources (t , projectId , gkeutils .WithAssetTypes ([]string {"container.googleapis.com/Cluster" })).Get ("#(name=\" " + clusterResourceName + "\" ).resource.data" )
53+ cluster := cai .GetProjectResources (t , projectId , cai .WithAssetTypes ([]string {"container.googleapis.com/Cluster" })).Get ("#(name=\" " + clusterResourceName + "\" ).resource.data" )
4754
4855 // Equivalent gcloud describe command
4956 // cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
@@ -73,7 +80,10 @@ func TestNodePool(t *testing.T) {
7380 g := golden .NewOrUpdate (t , cluster .String (),
7481 golden .WithSanitizer (golden .StringSanitizer (projectId , "PROJECT_ID" )),
7582 golden .WithSanitizer (golden .StringSanitizer (location , "LOCATION" )),
76- golden .WithSanitizer (golden .StringSanitizer (clusterName , "CLUSTER_NAME" )),
83+ //golden.WithSanitizer(golden.StringSanitizer(clusterName, "CLUSTER_NAME")),
84+ golden .WithSanitizer (golden .StringSanitizer (randomString , "RANDOM_STRING" )),
85+ golden .WithSanitizer (golden .StringSanitizer (kubernetesEndpoint , "KUBERNETES_ENDPOINT" )),
86+ //golden.WithSanitizer(golden.StringSanitizer(serviceAccount, "SERVICE_ACCOUNT")),
7787 )
7888 validateJSONPaths := []string {
7989 "autoscaling.autoprovisioningNodePoolDefaults.imageType" ,
@@ -92,6 +102,56 @@ func TestNodePool(t *testing.T) {
92102 g .JSONEq (assert , cluster , pth )
93103 }
94104
105+ fmt .Println ("START one path" )
106+ g .JSONPathEqs (assert , cluster , []string {"autoscaling.autoprovisioningNodePoolDefaults.imageType" })
107+ fmt .Println ("END one path" )
108+
109+ fmt .Println ("START multi path" )
110+ g .JSONPathEqs (assert , cluster , validateJSONPaths )
111+ fmt .Println ("END multi path" )
112+
113+ fmt .Println ("START all paths 1" )
114+ // Test validating all paths in golden image
115+ jsonPaths := utils .GetTerminalJSONPaths (g .GetJSON ())
116+
117+ // List of paths exempt from validation
118+ exemptJSONPathPrefixes := []string {
119+ "nodePools" , // nodePools are unordered
120+ }
121+
122+ // Remove exempt paths by prefix
123+ jsonPaths = slices .DeleteFunc (jsonPaths , func (s string ) bool {
124+ for _ , path := range exemptJSONPathPrefixes {
125+ if strings .HasPrefix (s , path ) {
126+ // prefix match
127+ return true
128+ }
129+ }
130+ // no prefix match
131+ return false
132+ })
133+
134+ syncGroup := new (errgroup.Group )
135+ syncGroup .SetLimit (24 )
136+ t .Logf ("Checking %d JSON paths with max %d goroutines" , len (jsonPaths ), 24 )
137+ for _ , jsonPath := range jsonPaths {
138+ jsonPath := jsonPath
139+ syncGroup .Go (func () error {
140+ g .JSONEq (assert , cluster , jsonPath )
141+ return nil
142+ })
143+ }
144+ if err := syncGroup .Wait (); err != nil {
145+ t .Fatal (err )
146+ }
147+ fmt .Println ("END all paths 1" )
148+
149+ //fmt.Println("all paths 2")
150+ // Test validating all Paths
151+ //evalPaths := utils.GetJSONPaths(cluster)
152+ //fmt.Println(evalPaths)
153+ //g.JSONPathEqs(assert, cluster, evalPaths)
154+
95155 // Pool-01
96156 assert .Equal ("pool-01" , cluster .Get ("nodePools.#(name==\" pool-01\" ).name" ).String (), "pool-1 exists" )
97157 assert .Equal ("e2-medium" , cluster .Get ("nodePools.#(name==\" pool-01\" ).config.machineType" ).String (), "is the expected machine type" )
@@ -156,7 +216,7 @@ func TestNodePool(t *testing.T) {
156216 k8sOpts := k8s.KubectlOptions {}
157217 clusterNodesOp , err := k8s .RunKubectlAndGetOutputE (t , & k8sOpts , "get" , "nodes" , "-o" , "json" )
158218 assert .NoError (err )
159- clusterNodes := testutils .ParseKubectlJSONResult (t , clusterNodesOp )
219+ clusterNodes := utils .ParseKubectlJSONResult (t , clusterNodesOp )
160220 assert .JSONEq (`[
161221 {
162222 "effect": "PreferNoSchedule",
0 commit comments