Skip to content

Commit 5f95f91

Browse files
committed
validate all paths
1 parent e33b3ea commit 5f95f91

File tree

7 files changed

+120
-181
lines changed

7 files changed

+120
-181
lines changed

test/fixtures/node_pool/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ output "service_account" {
8383
output "registry_project_ids" {
8484
value = var.registry_project_ids
8585
}
86+
87+
output "random_string" {
88+
value = random_string.suffix.result
89+
}

test/fixtures/safer_cluster_iap_bastion/example.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
locals {
18-
test_command = "gcloud beta compute ssh ${module.example.bastion_name} --tunnel-through-iap --verbosity=error --project ${var.project_ids[1]} --zone ${module.example.bastion_zone} -q -- curl -sS https://${module.example.endpoint}/version -k"
18+
test_command = "gcloud beta compute ssh ${module.example.bastion_name} --tunnel-through-iap --verbosity=error --project ${var.project_ids[1]} --zone ${module.example.bastion_zone} -q --command='curl -sS https://${module.example.endpoint}/version -k'"
1919
}
2020

2121
module "example" {

test/integration/node_pool/node_pool_test.go

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ package node_pool
1515

1616
import (
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

3034
func 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)
@@ -71,9 +78,12 @@ func TestNodePool(t *testing.T) {
7178

7279
// Cluster (using golden image with sanitizer)
7380
g := golden.NewOrUpdate(t, cluster.String(),
81+
//golden.WithSanitizer(golden.StringSanitizer(serviceAccount, "SERVICE_ACCOUNT")),
7482
golden.WithSanitizer(golden.StringSanitizer(projectId, "PROJECT_ID")),
7583
golden.WithSanitizer(golden.StringSanitizer(location, "LOCATION")),
76-
golden.WithSanitizer(golden.StringSanitizer(clusterName, "CLUSTER_NAME")),
84+
//golden.WithSanitizer(golden.StringSanitizer(clusterName, "CLUSTER_NAME")),
85+
golden.WithSanitizer(golden.StringSanitizer(randomString, "RANDOM_STRING")),
86+
golden.WithSanitizer(golden.StringSanitizer(kubernetesEndpoint, "KUBERNETES_ENDPOINT")),
7787
)
7888
validateJSONPaths := []string{
7989
"autoscaling.autoprovisioningNodePoolDefaults.imageType",
@@ -92,6 +102,59 @@ 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+
"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+
95158
// Pool-01
96159
assert.Equal("pool-01", cluster.Get("nodePools.#(name==\"pool-01\").name").String(), "pool-1 exists")
97160
assert.Equal("e2-medium", cluster.Get("nodePools.#(name==\"pool-01\").config.machineType").String(), "is the expected machine type")
@@ -156,7 +219,7 @@ func TestNodePool(t *testing.T) {
156219
k8sOpts := k8s.KubectlOptions{}
157220
clusterNodesOp, err := k8s.RunKubectlAndGetOutputE(t, &k8sOpts, "get", "nodes", "-o", "json")
158221
assert.NoError(err)
159-
clusterNodes := testutils.ParseKubectlJSONResult(t, clusterNodesOp)
222+
clusterNodes := utils.ParseKubectlJSONResult(t, clusterNodesOp)
160223
assert.JSONEq(`[
161224
{
162225
"effect": "PreferNoSchedule",

0 commit comments

Comments
 (0)