Skip to content

Commit 96435ca

Browse files
committed
multi asset types
validate all paths
1 parent c02c7ba commit 96435ca

File tree

9 files changed

+275
-99
lines changed

9 files changed

+275
-99
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 --command='curl -H \"Authorization: Bearer $(gcloud auth print-access-token)\" -H \"Content-Type: application/json\" -sS https://${module.example.endpoint_dns}/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 -H 'Authorization: Bearer $(gcloud auth print-access-token)' -H 'Content-Type: application/json' -sS https://${module.example.endpoint_dns}/version -k\""
1919
}
2020

2121
module "example" {

test/integration/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.7
55
toolchain go1.23.4
66

77
require (
8-
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.17.0
8+
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.17.2
99
github.com/gruntwork-io/terratest v0.47.2
1010
github.com/hashicorp/terraform-json v0.23.0
1111
github.com/stretchr/testify v1.10.0
@@ -51,7 +51,7 @@ require (
5151
github.com/hashicorp/go-version v1.7.0 // indirect
5252
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f // indirect
5353
github.com/hashicorp/hcl/v2 v2.20.1 // indirect
54-
github.com/hashicorp/terraform-config-inspect v0.0.0-20241107133921-3adb156ecfe2 // indirect
54+
github.com/hashicorp/terraform-config-inspect v0.0.0-20241129133400-c404f8227ea6 // indirect
5555
github.com/imdario/mergo v0.3.16 // indirect
5656
github.com/jinzhu/copier v0.4.0 // indirect
5757
github.com/jmespath/go-jmespath v0.4.0 // indirect
@@ -85,7 +85,7 @@ require (
8585
golang.org/x/mod v0.22.0 // indirect
8686
golang.org/x/net v0.23.0 // indirect
8787
golang.org/x/oauth2 v0.12.0 // indirect
88-
golang.org/x/sync v0.4.0 // indirect
88+
golang.org/x/sync v0.10.0 // indirect
8989
golang.org/x/sys v0.21.0 // indirect
9090
golang.org/x/term v0.18.0 // indirect
9191
golang.org/x/text v0.14.0 // indirect

test/integration/go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoIS
187187
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
188188
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
189189
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
190-
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.17.0 h1:EcxvASC45eOFrkk+cw9vP8Wztms3ZMWAQyBNEaI5MdU=
191-
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.17.0/go.mod h1:homk/qeh6p4f7onHkLc5Vy8nG31ZmDK2pamBdHrc1OM=
190+
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.17.2 h1:lvXlLBFvpZvuch2PKgkrUaJ9RVez7JP0LEMVVD5Bvbo=
191+
github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.17.2/go.mod h1:gEV+whUF+NHOV6f6Hwrn64VEijQ2nOwNgpdotVm3rvM=
192192
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
193193
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
194194
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
@@ -400,8 +400,8 @@ github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+Db
400400
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
401401
github.com/hashicorp/hcl/v2 v2.20.1 h1:M6hgdyz7HYt1UN9e61j+qKJBqR3orTWbI1HKBJEdxtc=
402402
github.com/hashicorp/hcl/v2 v2.20.1/go.mod h1:TZDqQ4kNKCbh1iJp99FdPiUaVDDUPivbqxZulxDYqL4=
403-
github.com/hashicorp/terraform-config-inspect v0.0.0-20241107133921-3adb156ecfe2 h1:h+wcDC5wTiM+5YKjuLtUSWJtdtMVJNCRCPHoWieLpM4=
404-
github.com/hashicorp/terraform-config-inspect v0.0.0-20241107133921-3adb156ecfe2/go.mod h1:Gz/z9Hbn+4KSp8A2FBtNszfLSdT2Tn/uAKGuVqqWmDI=
403+
github.com/hashicorp/terraform-config-inspect v0.0.0-20241129133400-c404f8227ea6 h1:146llE+6P/9YO8RcHRehzGNiS9+OoirKW9/aML6/JIA=
404+
github.com/hashicorp/terraform-config-inspect v0.0.0-20241129133400-c404f8227ea6/go.mod h1:Gz/z9Hbn+4KSp8A2FBtNszfLSdT2Tn/uAKGuVqqWmDI=
405405
github.com/hashicorp/terraform-json v0.23.0 h1:sniCkExU4iKtTADReHzACkk8fnpQXrdD2xoR+lppBkI=
406406
github.com/hashicorp/terraform-json v0.23.0/go.mod h1:MHdXbBAbSg0GvzuWazEGKAn/cyNfIB7mN6y7KJN6y2c=
407407
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
@@ -664,8 +664,8 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ
664664
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
665665
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
666666
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
667-
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
668-
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
667+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
668+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
669669
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
670670
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
671671
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

test/integration/node_pool/node_pool_test.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ 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"
23+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/golden"
2224
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
25+
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
2326
"github.com/gruntwork-io/terratest/modules/k8s"
2427
"github.com/stretchr/testify/assert"
2528
"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"
2729
)
2830

2931
func TestNodePool(t *testing.T) {
@@ -34,20 +36,24 @@ func TestNodePool(t *testing.T) {
3436
bpt.DefineVerify(func(assert *assert.Assertions) {
3537
// Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token
3638
// bpt.DefaultVerify(assert)
37-
gkeutils.TGKEVerify(t, bpt, assert) // Verify Resources
39+
testutils.TGKEVerify(t, bpt, assert) // Verify Resources
3840

3941
projectId := bpt.GetStringOutput("project_id")
4042
location := bpt.GetStringOutput("location")
4143
clusterName := bpt.GetStringOutput("cluster_name")
44+
randomString := bpt.GetStringOutput("random_string")
45+
kubernetesEndpoint := bpt.GetStringOutput("kubernetes_endpoint")
4246

43-
//cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
47+
// Retrieve Cluster using CAI
4448
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")
49+
cluster := cai.GetProjectResources(t, projectId, cai.WithAssetTypes([]string{"container.googleapis.com/Cluster"})).Get("#(name=\"" + clusterResourceName + "\").resource.data")
50+
// Equivalent gcloud describe command (classic)
51+
// cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
4652

47-
// Cluster
53+
// Cluster Assertions (classic)
4854
assert.Contains([]string{"RUNNING", "RECONCILING"}, cluster.Get("status").String(), "Cluster is Running")
4955
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")
56+
assert.Equal("https://www.googleapis.com/auth/cloud-platform", cluster.Get("autoscaling.autoprovisioningNodePoolDefaults.oauthScopes.0").String(), "has the expected oauth scopes")
5157
assert.Equal("default", cluster.Get("autoscaling.autoprovisioningNodePoolDefaults.serviceAccount").String(), "has the expected service account")
5258
assert.Equal("OPTIMIZE_UTILIZATION", cluster.Get("autoscaling.autoscalingProfile").String(), "has the expected autoscaling profile")
5359
assert.True(cluster.Get("autoscaling.enableNodeAutoprovisioning").Bool(), "has the expected node autoprovisioning")
@@ -65,7 +71,15 @@ func TestNodePool(t *testing.T) {
6571
]`,
6672
cluster.Get("autoscaling.resourceLimits").String(), "has the expected resource limits")
6773

68-
// Pool-01
74+
// Cluster Assertions using golden image (TestNodePool.json) with sanitizer
75+
g := golden.NewOrUpdate(t, cluster.String(),
76+
golden.WithSanitizer(golden.StringSanitizer(projectId, "PROJECT_ID")),
77+
golden.WithSanitizer(golden.StringSanitizer(randomString, "RANDOM_STRING")),
78+
golden.WithSanitizer(golden.StringSanitizer(kubernetesEndpoint, "KUBERNETES_ENDPOINT")),
79+
)
80+
g.JSONPathEqs(assert, cluster, utils.GetTerminalJSONPaths(g.GetJSON()))
81+
82+
// nodePool-01 Assertions
6983
assert.Equal("pool-01", cluster.Get("nodePools.#(name==\"pool-01\").name").String(), "pool-1 exists")
7084
assert.Equal("e2-medium", cluster.Get("nodePools.#(name==\"pool-01\").config.machineType").String(), "is the expected machine type")
7185
assert.Equal("COS_CONTAINERD", cluster.Get("nodePools.#(name==\"pool-01\").config.imageType").String(), "has the expected image")
@@ -82,7 +96,7 @@ func TestNodePool(t *testing.T) {
8296
assert.Equal(int64(10000), cluster.Get("nodePools.#(name==\"pool-01\").config.linuxNodeConfig.sysctls.net\\.core\\.netdev_max_backlog").Int(), "has the expected linux node config net.core.netdev_max_backlog sysctl")
8397
assert.Equal(int64(10000), cluster.Get("nodePools.#(name==\"pool-01\").config.linuxNodeConfig.sysctls.net\\.core\\.rmem_max").Int(), "has the expected linux node config net.core.rmem_max sysctl")
8498

85-
// Pool-02
99+
// nodePool-02 Assertions
86100
assert.Equal("pool-02", cluster.Get("nodePools.#(name==\"pool-02\").name").String(), "pool-2 exists")
87101
assert.Equal("n1-standard-2", cluster.Get("nodePools.#(name==\"pool-02\").config.machineType").String(), "is the expected machine type")
88102
assert.True(cluster.Get("nodePools.#(name==\"pool-02\").autoscaling.enabled").Bool(), "has autoscaling enabled")
@@ -97,7 +111,7 @@ func TestNodePool(t *testing.T) {
97111
cluster.Get("nodePools.#(name==\"pool-02\").config.tags").Value().([]interface{}), "has the expected network tags")
98112
assert.Equal(int64(10000), cluster.Get("nodePools.#(name==\"pool-02\").config.linuxNodeConfig.sysctls.net\\.core\\.netdev_max_backlog").Int(), "has the expected linux node config sysctls")
99113

100-
// Pool-03
114+
// nodwPool-03 Assertions
101115
assert.Equal("pool-03", cluster.Get("nodePools.#(name==\"pool-03\").name").String(), "pool-3 exists")
102116
assert.JSONEq(fmt.Sprintf(`["%s-b", "%s-c"]`, location, location), cluster.Get("nodePools.#(name==\"pool-03\").locations").String(), "has nodes in correct locations")
103117
assert.Equal("n1-standard-2", cluster.Get("nodePools.#(name==\"pool-03\").config.machineType").String(), "is the expected machine type")
@@ -116,20 +130,20 @@ func TestNodePool(t *testing.T) {
116130
assert.True(cluster.Get("nodePools.#(name==\"pool-03\").config.kubeletConfig.cpuCfsQuota").Bool(), "has the expected cpuCfsQuota kubelet config")
117131
assert.Equal(int64(20000), cluster.Get("nodePools.#(name==\"pool-03\").config.linuxNodeConfig.sysctls.net\\.core\\.netdev_max_backlog").Int(), "has the expected linux node config sysctls")
118132

119-
// Pool-04
133+
// nodePool-04 Assertions
120134
assert.Equal("pool-04", cluster.Get("nodePools.#(name==\"pool-04\").name").String(), "pool-4 exists")
121135
assert.False(cluster.Get("nodePools.#(name==\"pool-04\").config.queuedProvisioning.enabled").Bool(), "has queued provisioning not enabled")
122136

123-
// Pool-05
137+
// nodePool-05 Assertions
124138
assert.Equal("pool-05", cluster.Get("nodePools.#(name==\"pool-05\").name").String(), "pool-5 exists")
125139
assert.True(cluster.Get("nodePools.#(name==\"pool-05\").config.advancedMachineFeatures.enableNestedVirtualization").Bool(), "has enable_nested_virtualization enabled")
126140

127-
// K8s
141+
// K8s Assertions
128142
gcloud.Runf(t, "container clusters get-credentials %s --region %s --project %s", clusterName, location, projectId)
129143
k8sOpts := k8s.KubectlOptions{}
130144
clusterNodesOp, err := k8s.RunKubectlAndGetOutputE(t, &k8sOpts, "get", "nodes", "-o", "json")
131145
assert.NoError(err)
132-
clusterNodes := testutils.ParseKubectlJSONResult(t, clusterNodesOp)
146+
clusterNodes := utils.ParseKubectlJSONResult(t, clusterNodesOp)
133147
assert.JSONEq(`[
134148
{
135149
"effect": "PreferNoSchedule",
@@ -148,6 +162,11 @@ func TestNodePool(t *testing.T) {
148162
"effect": "PreferNoSchedule",
149163
"key": "all-pools-example",
150164
"value": "true"
165+
},
166+
{
167+
"effect": "NoSchedule",
168+
"key": "nvidia.com/gpu",
169+
"value": "present"
151170
}
152171
]`,
153172
clusterNodes.Get("items.#(metadata.labels.node_pool==\"pool-02\").spec.taints").String(), "has the expected all-pools-example taint")
@@ -156,6 +175,11 @@ func TestNodePool(t *testing.T) {
156175
"effect": "PreferNoSchedule",
157176
"key": "all-pools-example",
158177
"value": "true"
178+
},
179+
{
180+
"effect": "NoSchedule",
181+
"key": "sandbox.gke.io/runtime",
182+
"value": "gvisor"
159183
}
160184
]`,
161185
clusterNodes.Get("items.#(metadata.labels.node_pool==\"pool-03\").spec.taints").String(), "has the expected all-pools-example taint")

0 commit comments

Comments
 (0)