From f1c5d65394f150524d51b30497e15aed01466a34 Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Mon, 25 Nov 2024 21:53:29 +0000 Subject: [PATCH] update for bpt 1.17 --- test/integration/node_pool/node_pool_test.go | 29 ++++--- .../safer_cluster_iap_bastion_test.go | 3 +- test/integration/utils/cai.go | 82 ------------------- .../workload_identity_test.go | 3 +- 4 files changed, 19 insertions(+), 98 deletions(-) delete mode 100644 test/integration/utils/cai.go diff --git a/test/integration/node_pool/node_pool_test.go b/test/integration/node_pool/node_pool_test.go index 7db9c7b23..f1af3451a 100644 --- a/test/integration/node_pool/node_pool_test.go +++ b/test/integration/node_pool/node_pool_test.go @@ -18,6 +18,7 @@ import ( "testing" "time" + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/cai" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/golden" "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft" @@ -25,7 +26,7 @@ import ( "github.com/gruntwork-io/terratest/modules/k8s" "github.com/stretchr/testify/assert" "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/testutils" - gkeutils "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/utils" + "golang.org/x/sync/errgroup" ) func TestNodePool(t *testing.T) { @@ -36,7 +37,7 @@ func TestNodePool(t *testing.T) { bpt.DefineVerify(func(assert *assert.Assertions) { // Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token // bpt.DefaultVerify(assert) - gkeutils.TGKEVerify(t, bpt, assert) // Verify Resources + testutils.TGKEVerify(t, bpt, assert) // Verify Resources projectId := bpt.GetStringOutput("project_id") location := bpt.GetStringOutput("location") @@ -44,7 +45,7 @@ func TestNodePool(t *testing.T) { // CAI clusterResourceName := fmt.Sprintf("//container.googleapis.com/projects/%s/locations/%s/clusters/%s", projectId, location, clusterName) - cluster := gkeutils.GetProjectResources(t, projectId, gkeutils.WithAssetTypes([]string{"container.googleapis.com/Cluster"})).Get("#(name=\"" + clusterResourceName + "\").resource.data") + cluster := cai.GetProjectResources(t, projectId, cai.WithAssetTypes([]string{"container.googleapis.com/Cluster"})).Get("#(name=\"" + clusterResourceName + "\").resource.data") // Equivalent gcloud describe command // cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId) @@ -102,18 +103,22 @@ func TestNodePool(t *testing.T) { fmt.Println("START all paths 1") // Test validating all Paths jsonPaths := utils.GetJSONPaths(cluster) - //var wg sync.WaitGroup + syncGroup := new(errgroup.Group) + syncGroup.SetLimit(12) numJSONPaths := len(jsonPaths) t.Logf("checking %d JSON paths", numJSONPaths) - //wg.Add(numJSONPaths) for _, jsonPath := range jsonPaths { - func(path string) { - //defer wg.Done() - t.Logf("checking JSON path: %s", path) - g.JSONEq(assert, cluster, path) - }(jsonPath) + jsonPath := jsonPath + syncGroup.Go(func() error { + t.Logf("checking JSON path: %s", jsonPath) + g.JSONEq(assert, cluster, jsonPath) + return nil + }) } - //wg.Wait() + if err := syncGroup.Wait(); err != nil { + t.Fatal(err) + } + fmt.Println("END all paths 1") //fmt.Println("all paths 2") @@ -186,7 +191,7 @@ func TestNodePool(t *testing.T) { k8sOpts := k8s.KubectlOptions{} clusterNodesOp, err := k8s.RunKubectlAndGetOutputE(t, &k8sOpts, "get", "nodes", "-o", "json") assert.NoError(err) - clusterNodes := testutils.ParseKubectlJSONResult(t, clusterNodesOp) + clusterNodes := utils.ParseKubectlJSONResult(t, clusterNodesOp) assert.JSONEq(`[ { "effect": "PreferNoSchedule", diff --git a/test/integration/safer_cluster_iap_bastion/safer_cluster_iap_bastion_test.go b/test/integration/safer_cluster_iap_bastion/safer_cluster_iap_bastion_test.go index 2e46f573f..aa912771d 100644 --- a/test/integration/safer_cluster_iap_bastion/safer_cluster_iap_bastion_test.go +++ b/test/integration/safer_cluster_iap_bastion/safer_cluster_iap_bastion_test.go @@ -23,7 +23,6 @@ import ( "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft" "github.com/stretchr/testify/assert" "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/testutils" - gkeutils "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/utils" ) func TestSaferClusterIapBastion(t *testing.T) { @@ -34,7 +33,7 @@ func TestSaferClusterIapBastion(t *testing.T) { bpt.DefineVerify(func(assert *assert.Assertions) { // Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token // bpt.DefaultVerify(assert) - gkeutils.TGKEVerify(t, bpt, assert) // Verify Resources + testutils.TGKEVerify(t, bpt, assert) // Verify Resources test_command, _ := strings.CutPrefix(bpt.GetStringOutput("test_command"), "gcloud ") diff --git a/test/integration/utils/cai.go b/test/integration/utils/cai.go deleted file mode 100644 index f219f6875..000000000 --- a/test/integration/utils/cai.go +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Copyright 2024 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package cai provides a set of helpers to interact with Cloud Asset Inventory -package utils - -import ( - "fmt" - "strings" - "testing" - "time" - - "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" - "github.com/tidwall/gjson" -) - -type CmdCfg struct { - sleep int // minutes to sleep prior to CAI retreval. default: 2 - assetTypes []string // asset types to retrieve. empty: all - args []string // arguments to pass to call -} - -type cmdOption func(*CmdCfg) - -// newCmdConfig sets defaults and options -func newCmdConfig(opts ...cmdOption) (*CmdCfg) { - caiOpts := &CmdCfg{ - sleep: 2, - assetTypes: nil, - args: nil, - } - - for _, opt := range opts { - opt(caiOpts) - } - - if caiOpts.assetTypes != nil { - caiOpts.args = []string{"--asset-types", strings.Join(caiOpts.assetTypes, ",")} - } - caiOpts.args = append(caiOpts.args, "--content-type", "resource") - - return caiOpts -} - -// Set custom sleep minutes -func WithSleep(sleep int) cmdOption { - return func(f *CmdCfg) { - f.sleep = sleep - } -} - -// Set asset types -func WithAssetTypes(assetTypes []string) cmdOption { - return func(f *CmdCfg) { - f.assetTypes = assetTypes - } -} - -// GetProjectResources returns the cloud asset inventory resources for a project as a gjson.Result -func GetProjectResources(t testing.TB, project string, opts ...cmdOption) gjson.Result { - caiOpts := newCmdConfig(opts...) - - // Cloud Asset Inventory offers best-effort data freshness. - t.Logf("Sleeping for %d minutes before retrieving Cloud Asset Inventory...", caiOpts.sleep) - time.Sleep(time.Duration(caiOpts.sleep) * time.Minute) - - cmd := fmt.Sprintf("asset list --project %s", project) - return gcloud.Runf(t, strings.Join(append([]string{cmd}, caiOpts.args...), " ")) -} diff --git a/test/integration/workload_identity/workload_identity_test.go b/test/integration/workload_identity/workload_identity_test.go index fe06e5321..92ebc5954 100644 --- a/test/integration/workload_identity/workload_identity_test.go +++ b/test/integration/workload_identity/workload_identity_test.go @@ -22,7 +22,6 @@ import ( "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft" "github.com/stretchr/testify/assert" "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/testutils" - gkeutils "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/utils" ) func TestWorkloadIdentity(t *testing.T) { @@ -33,7 +32,7 @@ func TestWorkloadIdentity(t *testing.T) { bpt.DefineVerify(func(assert *assert.Assertions) { // Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token // bpt.DefaultVerify(assert) - gkeutils.TGKEVerify(t, bpt, assert) // Verify Resources + testutils.TGKEVerify(t, bpt, assert) // Verify Resources projectId := bpt.GetStringOutput("project_id") location := bpt.GetStringOutput("location")