Skip to content

Commit d8e04a1

Browse files
committed
validate all paths
1 parent e33b3ea commit d8e04a1

File tree

7 files changed

+111
-150
lines changed

7 files changed

+111
-150
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: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import (
1818
"testing"
1919
"time"
2020

21+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/cai"
2122
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
2223
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/golden"
2324
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
25+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2426
"github.com/gruntwork-io/terratest/modules/k8s"
2527
"github.com/stretchr/testify/assert"
2628
"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"
29+
"golang.org/x/sync/errgroup"
2830
)
2931

3032
func TestNodePool(t *testing.T) {
@@ -35,15 +37,18 @@ func TestNodePool(t *testing.T) {
3537
bpt.DefineVerify(func(assert *assert.Assertions) {
3638
// Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token
3739
// bpt.DefaultVerify(assert)
38-
gkeutils.TGKEVerify(t, bpt, assert) // Verify Resources
40+
testutils.TGKEVerify(t, bpt, assert) // Verify Resources
3941

4042
projectId := bpt.GetStringOutput("project_id")
4143
location := bpt.GetStringOutput("location")
4244
clusterName := bpt.GetStringOutput("cluster_name")
45+
randomString := bpt.GetStringOutput("random_string")
46+
kubernetesEndpoint := bpt.GetStringOutput("kubernetes_endpoint")
47+
serviceAccount := bpt.GetStringOutput("service_account")
4348

4449
// CAI
4550
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")
51+
cluster := cai.GetProjectResources(t, projectId, cai.WithAssetTypes([]string{"container.googleapis.com/Cluster"})).Get("#(name=\"" + clusterResourceName + "\").resource.data")
4752

4853
// Equivalent gcloud describe command
4954
// cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
@@ -74,6 +79,9 @@ func TestNodePool(t *testing.T) {
7479
golden.WithSanitizer(golden.StringSanitizer(projectId, "PROJECT_ID")),
7580
golden.WithSanitizer(golden.StringSanitizer(location, "LOCATION")),
7681
golden.WithSanitizer(golden.StringSanitizer(clusterName, "CLUSTER_NAME")),
82+
golden.WithSanitizer(golden.StringSanitizer(randomString, "RANDOM_STRING")),
83+
golden.WithSanitizer(golden.StringSanitizer(kubernetesEndpoint, "KUBERNETES_ENDPOINT")),
84+
golden.WithSanitizer(golden.StringSanitizer(serviceAccount, "SERVICE_ACCOUNT")),
7785
)
7886
validateJSONPaths := []string{
7987
"autoscaling.autoprovisioningNodePoolDefaults.imageType",
@@ -92,6 +100,61 @@ func TestNodePool(t *testing.T) {
92100
g.JSONEq(assert, cluster, pth)
93101
}
94102

103+
fmt.Println("START one path")
104+
g.JSONPathEqs(assert, cluster, []string{"autoscaling.autoprovisioningNodePoolDefaults.imageType"})
105+
fmt.Println("END one path")
106+
107+
fmt.Println("START multi path")
108+
g.JSONPathEqs(assert, cluster, validateJSONPaths)
109+
fmt.Println("END multi path")
110+
111+
fmt.Println("START all paths 1")
112+
// Test validating all paths in golden image
113+
jsonPaths := utils.GetTerminalJSONPaths(g.GetJSON())
114+
115+
// List of paths exempt from validation
116+
//exemptJSONPaths := []string{
117+
//"createTime",
118+
//"id",
119+
//"etag",
120+
//"controlPlaneEndpointsConfig.dnsEndpointConfig.endpoint",
121+
//"instanceGroupUrls.0",
122+
//"instanceGroupUrls.1",
123+
//"instanceGroupUrls.2",
124+
//"instanceGroupUrls.3",
125+
//"instanceGroupUrls.4",
126+
//"instanceGroupUrls.5",
127+
//"instanceGroupUrls.6",
128+
//"instanceGroupUrls.7",
129+
//"masterAuth.clusterCaCertificate",
130+
//}
131+
132+
// Remove exempt tags
133+
//jsonPaths = slices.DeleteFunc(jsonPaths, func(s string) bool {
134+
// return slices.Contains(exemptJSONPaths, s)
135+
//})
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)