@@ -15,15 +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"
25+ "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/golden"
2226 "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
27+ "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2328 "github.com/gruntwork-io/terratest/modules/k8s"
2429 "github.com/stretchr/testify/assert"
2530 "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/testutils"
26- gkeutils "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/utils "
31+ "golang.org/x/sync/errgroup "
2732)
2833
2934func TestNodePool (t * testing.T ) {
@@ -34,20 +39,26 @@ func TestNodePool(t *testing.T) {
3439 bpt .DefineVerify (func (assert * assert.Assertions ) {
3540 // Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token
3641 // bpt.DefaultVerify(assert)
37- gkeutils .TGKEVerify (t , bpt , assert ) // Verify Resources
42+ testutils .TGKEVerify (t , bpt , assert ) // Verify Resources
3843
3944 projectId := bpt .GetStringOutput ("project_id" )
4045 location := bpt .GetStringOutput ("location" )
4146 clusterName := bpt .GetStringOutput ("cluster_name" )
47+ randomString := bpt .GetStringOutput ("random_string" )
48+ kubernetesEndpoint := bpt .GetStringOutput ("kubernetes_endpoint" )
49+ //serviceAccount := bpt.GetStringOutput("service_account")
4250
43- //cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
51+ // CAI
4452 clusterResourceName := fmt .Sprintf ("//container.googleapis.com/projects/%s/locations/%s/clusters/%s" , projectId , location , clusterName )
45- cluster := gkeutils .GetProjectResources (t , projectId , gkeutils .WithAssetType ("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" )
54+
55+ // Equivalent gcloud describe command
56+ // cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
4657
4758 // Cluster
4859 assert .Contains ([]string {"RUNNING" , "RECONCILING" }, cluster .Get ("status" ).String (), "Cluster is Running" )
4960 assert .Equal ("COS_CONTAINERD" , cluster .Get ("autoscaling.autoprovisioningNodePoolDefaults.imageType" ).String (), "has the expected image type" )
50- assert .Equal ("[ \n \" https://www.googleapis.com/auth/cloud-platform\" \n ]" , cluster .Get ("autoscaling.autoprovisioningNodePoolDefaults.oauthScopes" ).String (), "has the expected oauth scopes" )
61+ assert .Equal ("https://www.googleapis.com/auth/cloud-platform" , cluster .Get ("autoscaling.autoprovisioningNodePoolDefaults.oauthScopes.0 " ).String (), "has the expected oauth scopes" )
5162 assert .Equal ("default" , cluster .Get ("autoscaling.autoprovisioningNodePoolDefaults.serviceAccount" ).String (), "has the expected service account" )
5263 assert .Equal ("OPTIMIZE_UTILIZATION" , cluster .Get ("autoscaling.autoscalingProfile" ).String (), "has the expected autoscaling profile" )
5364 assert .True (cluster .Get ("autoscaling.enableNodeAutoprovisioning" ).Bool (), "has the expected node autoprovisioning" )
@@ -65,6 +76,85 @@ func TestNodePool(t *testing.T) {
6576 ]` ,
6677 cluster .Get ("autoscaling.resourceLimits" ).String (), "has the expected resource limits" )
6778
79+ // Cluster (using golden image with sanitizer)
80+ g := golden .NewOrUpdate (t , cluster .String (),
81+ //golden.WithSanitizer(golden.StringSanitizer(serviceAccount, "SERVICE_ACCOUNT")),
82+ golden .WithSanitizer (golden .StringSanitizer (projectId , "PROJECT_ID" )),
83+ golden .WithSanitizer (golden .StringSanitizer (location , "LOCATION" )),
84+ //golden.WithSanitizer(golden.StringSanitizer(clusterName, "CLUSTER_NAME")),
85+ golden .WithSanitizer (golden .StringSanitizer (randomString , "RANDOM_STRING" )),
86+ golden .WithSanitizer (golden .StringSanitizer (kubernetesEndpoint , "KUBERNETES_ENDPOINT" )),
87+ )
88+ validateJSONPaths := []string {
89+ "autoscaling.autoprovisioningNodePoolDefaults.imageType" ,
90+ "autoscaling.autoprovisioningNodePoolDefaults.oauthScopes.0" ,
91+ "autoscaling.autoprovisioningNodePoolDefaults.serviceAccount" ,
92+ "autoscaling.autoscalingProfile" ,
93+ "autoscaling.enableNodeAutoprovisioning" ,
94+ "autoscaling.resourceLimits[0].maximum" ,
95+ "autoscaling.resourceLimits[0].minimum" ,
96+ "autoscaling.resourceLimits[0].resourceType" ,
97+ "autoscaling.resourceLimits[1].maximum" ,
98+ "autoscaling.resourceLimits[1].minimum" ,
99+ "autoscaling.resourceLimits[1].resourceType" ,
100+ }
101+ for _ , pth := range validateJSONPaths {
102+ g .JSONEq (assert , cluster , pth )
103+ }
104+
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+ "monitoringConfig.componentConfig.enableComponents" ,
121+ }
122+
123+ // Remove exempt paths by prefix
124+ jsonPaths = slices .DeleteFunc (jsonPaths , func (s string ) bool {
125+ for _ , path := range exemptJSONPathPrefixes {
126+ if strings .HasPrefix (s , path ) {
127+ // prefix match
128+ return true
129+ }
130+ }
131+ // no prefix match
132+ return false
133+ })
134+
135+ jsonPaths = append (jsonPaths , "monitoringConfig.componentConfig.enableComponents" )
136+
137+ syncGroup := new (errgroup.Group )
138+ syncGroup .SetLimit (24 )
139+ t .Logf ("Checking %d JSON paths with max %d goroutines" , len (jsonPaths ), 24 )
140+ for _ , jsonPath := range jsonPaths {
141+ jsonPath := jsonPath
142+ syncGroup .Go (func () error {
143+ g .JSONEq (assert , cluster , jsonPath )
144+ return nil
145+ })
146+ }
147+ if err := syncGroup .Wait (); err != nil {
148+ t .Fatal (err )
149+ }
150+ fmt .Println ("END all paths 1" )
151+
152+ //fmt.Println("all paths 2")
153+ // Test validating all Paths
154+ //evalPaths := utils.GetJSONPaths(cluster)
155+ //fmt.Println(evalPaths)
156+ //g.JSONPathEqs(assert, cluster, evalPaths)
157+
68158 // Pool-01
69159 assert .Equal ("pool-01" , cluster .Get ("nodePools.#(name==\" pool-01\" ).name" ).String (), "pool-1 exists" )
70160 assert .Equal ("e2-medium" , cluster .Get ("nodePools.#(name==\" pool-01\" ).config.machineType" ).String (), "is the expected machine type" )
@@ -129,7 +219,7 @@ func TestNodePool(t *testing.T) {
129219 k8sOpts := k8s.KubectlOptions {}
130220 clusterNodesOp , err := k8s .RunKubectlAndGetOutputE (t , & k8sOpts , "get" , "nodes" , "-o" , "json" )
131221 assert .NoError (err )
132- clusterNodes := testutils .ParseKubectlJSONResult (t , clusterNodesOp )
222+ clusterNodes := utils .ParseKubectlJSONResult (t , clusterNodesOp )
133223 assert .JSONEq (`[
134224 {
135225 "effect": "PreferNoSchedule",
@@ -148,6 +238,11 @@ func TestNodePool(t *testing.T) {
148238 "effect": "PreferNoSchedule",
149239 "key": "all-pools-example",
150240 "value": "true"
241+ },
242+ {
243+ "effect": "NoSchedule",
244+ "key": "nvidia.com/gpu",
245+ "value": "present"
151246 }
152247 ]` ,
153248 clusterNodes .Get ("items.#(metadata.labels.node_pool==\" pool-02\" ).spec.taints" ).String (), "has the expected all-pools-example taint" )
@@ -156,6 +251,11 @@ func TestNodePool(t *testing.T) {
156251 "effect": "PreferNoSchedule",
157252 "key": "all-pools-example",
158253 "value": "true"
254+ },
255+ {
256+ "effect": "NoSchedule",
257+ "key": "sandbox.gke.io/runtime",
258+ "value": "gvisor"
159259 }
160260 ]` ,
161261 clusterNodes .Get ("items.#(metadata.labels.node_pool==\" pool-03\" ).spec.taints" ).String (), "has the expected all-pools-example taint" )
0 commit comments