|
| 1 | +// Copyright 2022-2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package node_pool |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud" |
| 22 | + "github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/testutils" |
| 25 | + gkeutils "github.com/terraform-google-modules/terraform-google-kubernetes-engine/test/integration/utils" |
| 26 | +) |
| 27 | + |
| 28 | +func TestNodePool(t *testing.T) { |
| 29 | + bpt := tft.NewTFBlueprintTest(t, |
| 30 | + tft.WithRetryableTerraformErrors(testutils.RetryableTransientErrors, 3, 2*time.Minute), |
| 31 | + ) |
| 32 | + |
| 33 | + bpt.DefineVerify(func(assert *assert.Assertions) { |
| 34 | + //Skipping Default Verify as the Verify Stage fails due to change in Client Cert Token |
| 35 | + // bpt.DefaultVerify(assert) |
| 36 | + gkeutils.TGKEVerify(t, bpt, assert) // Verify Resources |
| 37 | + |
| 38 | + projectId := bpt.GetStringOutput("project_id") |
| 39 | + location := bpt.GetStringOutput("location") |
| 40 | + clusterName := bpt.GetStringOutput("cluster_name") |
| 41 | + |
| 42 | + op := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId) |
| 43 | + |
| 44 | + // Cluster |
| 45 | + assert.Contains([]string{"RUNNING", "RECONCILING"}, op.Get("status").String(), "Cluster is Running") |
| 46 | + assert.Equal("COS_CONTAINERD", op.Get("autoscaling.autoprovisioningNodePoolDefaults.imageType").String(), "has the expected image type") |
| 47 | + assert.Equal("https://www.googleapis.com/auth/cloud-platform", op.Get("autoscaling.autoprovisioningNodePoolDefaults.oauthScopes").String(), "has the expected oauth scopes") |
| 48 | + assert.Equal("default", op.Get("autoscaling.autoprovisioningNodePoolDefaults.serviceAccount").String(), "has the expected service account") |
| 49 | + assert.Equal("OPTIMIZE_UTILIZATION", op.Get("autoscaling.autoprovisioningNodePoolDefaults.autoscalingProfile").String(), "has the expected autoscaling profile") |
| 50 | + assert.True(op.Get("autoscaling.autoprovisioningNodePoolDefaults.enableNodeAutoprovisioning").Bool(), "has the expected node autoprovisioning") |
| 51 | + assert.JSONEq(`[ |
| 52 | + { |
| 53 | + "maximum" => "20", |
| 54 | + "minimum" => "5", |
| 55 | + "resourceType" => "cpu" |
| 56 | + }, |
| 57 | + { |
| 58 | + "maximum" => "30", |
| 59 | + "minimum" => "10", |
| 60 | + "resourceType" => "memory" |
| 61 | + } |
| 62 | + ]`, |
| 63 | + op.Get("autoscaling.autoprovisioningNodePoolDefaults.resourceLimits").String(), "has the expected resource limits") |
| 64 | + |
| 65 | + // Pool-01 |
| 66 | + assert.Equal("pool-01", op.Get("nodePools.#(name==\"pool-01\").name").String(), "pool-1 exists") |
| 67 | + assert.Equal("e2-medium", op.Get("nodePools.#(name==\"pool-01\").config.machineType").String(), "is the expected machine type") |
| 68 | + assert.Equal("COS_CONTAINERD", op.Get("nodePools.#(name==\"pool-01\").config.imageType").String(), "has the expected image") |
| 69 | + assert.True(op.Get("nodePools.#(name==\"pool-01\").autoscaling.enabled").Bool(), "has autoscaling enabled") |
| 70 | + assert.Equal(int64(1), op.Get("nodePools.#(name==\"pool-01\").minNodeCount").Int(), "has the expected minimum node count") |
| 71 | + assert.True(op.Get("nodePools.#(name==\"pool-01\").management.autoRepair").Bool(), "has autorepair enabled") |
| 72 | + assert.True(op.Get("nodePools.#(name==\"pool-01\").management.autoUpgrade").Bool(), "has automatic upgrades enabled") |
| 73 | + assert.Equal("kubectl --kubeconfig=/var/lib/kubelet/kubeconfig drain --force=true --ignore-daemonsets=true --delete-local-data \"$HOSTNAME\"", op.Get("nodePools.#(name==\"pool-01\").config.metadata.shutdown-script").String(), "pool-2 exists") |
| 74 | + assert.Equal("false", op.Get("nodePools.#(name==\"pool-01\").config.metadata.disable-legacy-endpoints").String(), "pool-2 exists") |
| 75 | + assert.JSONEq(fmt.Sprintf(`{"all-pools-example": "true", "cluster_name": "%s", "node_pool": "pool-01"}`, clusterName), |
| 76 | + op.Get("nodePools.#(name==\"pool-01\").config.labels").String(), "has the expected labels") |
| 77 | + assert.ElementsMatch([]string{"all-node-example", "pool-01-example", fmt.Sprintf("gke-%s", clusterName), fmt.Sprintf("gke-%s-pool-01", clusterName)}, |
| 78 | + op.Get("nodePools.#(name==\"pool-01\").config.tags").Value().([]interface{}), "has the expected network tags") |
| 79 | + assert.Equal(int64(10000), op.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") |
| 80 | + assert.Equal(int64(10000), op.Get("nodePools.#(name==\"pool-01\").config.linuxNodeConfig.sysctls.net\\.core\\.rmem_max").Int(), "has the expected linux node config net.core.rmem_max sysctl") |
| 81 | + |
| 82 | + |
| 83 | + // Pool-02 |
| 84 | + assert.Equal("pool-02", op.Get("nodePools.#(name==\"pool-02\").name").String(), "pool-2 exists") |
| 85 | + assert.Equal("n1-standard-2", op.Get("nodePools.#(name==\"pool-02\").config.machineType").String(), "is the expected machine type") |
| 86 | + assert.False(op.Get("nodePools.#(name==\"pool-02\").autoscaling.enabled").Bool(), "has autoscaling enabled") |
| 87 | + assert.Equal(int64(1), op.Get("nodePools.#(name==\"pool-02\").minNodeCount").Int(), "has the expected minimum node count") |
| 88 | + assert.Equal(int64(2), op.Get("nodePools.#(name==\"pool-02\").maxNodeCount").Int(), "has the expected maximum node count") |
| 89 | + assert.Equal(int64(30), op.Get("nodePools.#(name==\"pool-02\").config.diskSizeGb").Int(), "has the expected disk size") |
| 90 | + assert.Equal("pd-standard", op.Get("nodePools.#(name==\"pool-02\").config.diskType").String(), "has the expected disk type") |
| 91 | + assert.Equal("COS_CONTAINERD", op.Get("nodePools.#(name==\"pool-02\").config.imageType").String(), "has the expected image") |
| 92 | + assert.JSONEq(fmt.Sprintf(`{"all-pools-example": "true", "cluster_name": "%s", "node_pool": "pool-02"}`, clusterName), |
| 93 | + op.Get("nodePools.#(name==\"pool-02\").config.labels").String(), "has the expected labels") |
| 94 | + assert.ElementsMatch([]string{"all-node-example", fmt.Sprintf("gke-%s", clusterName), fmt.Sprintf("gke-%s-pool-02", clusterName)}, |
| 95 | + op.Get("nodePools.#(name==\"pool-02\").config.tags").Value().([]interface{}), "has the expected network tags") |
| 96 | + assert.Equal(int64(10000), op.Get("nodePools.#(name==\"pool-02\").config.linuxNodeConfig.sysctls.net\\.core\\.netdev_max_backlog").Int(), "has the expected linux node config sysctls") |
| 97 | + |
| 98 | + // Pool-03 |
| 99 | + assert.Equal("pool-03", op.Get("nodePools.#(name==\"pool-03\").name").String(), "pool-3 exists") |
| 100 | + assert.JSONEq(fmt.Sprintf(`["%s-b", "%s-c"]`, location, location), op.Get("nodePools.#(name==\"pool-03\").locations").String(), "has nodes in correct locations") |
| 101 | + assert.Equal("n1-standard-2", op.Get("nodePools.#(name==\"pool-03\").config.machineType").String(), "is the expected machine type") |
| 102 | + assert.False(op.Get("nodePools.#(name==\"pool-03\").autoscaling.enabled").Bool(), "has autoscaling enabled") |
| 103 | + assert.Equal(int64(2), op.Get("nodePools.#(name==\"pool-03\").initialNodeCount").Int(), "has the expected inital node count") |
| 104 | + assert.True(op.Get("nodePools.#(name==\"pool-03\").management.autoRepair").Bool(), "has autorepair enabled") |
| 105 | + assert.True(op.Get("nodePools.#(name==\"pool-03\").management.autoUpgrade").Bool(), "has automatic upgrades enabled") |
| 106 | + assert.JSONEq(fmt.Sprintf(`{"all-pools-example": "true", "cluster_name": "%s", "node_pool": "pool-03", "sandbox.gke.io/runtime": "gvisor"}`, clusterName), |
| 107 | + op.Get("nodePools.#(name==\"pool-03\").config.labels").String(), "has the expected labels") |
| 108 | + assert.ElementsMatch([]string{"all-node-example", fmt.Sprintf("gke-%s", clusterName), fmt.Sprintf("gke-%s-pool-03", clusterName)}, |
| 109 | + op.Get("nodePools.#(name==\"pool-03\").config.tags").Value().([]interface{}), "has the expected network tags") |
| 110 | + assert.Equal("172.16.0.0/18", op.Get("nodePools.#(name==\"pool-03\").networkConfig.podIpv4CidrBlock").String(), "has the expected pod range") |
| 111 | + assert.Equal("test", op.Get("nodePools.#(name==\"pool-03\").networkConfig.podRange").String(), "has the expected pod range") |
| 112 | + assert.Equal("COS_CONTAINERD", op.Get("nodePools.#(name==\"pool-03\").config.imageType").String(), "has the expected image") |
| 113 | + assert.Equal("static", op.Get("nodePools.#(name==\"pool-03\").config.kubeletConfig.cpuManagerPolicy").String(), "has the expected cpuManagerPolicy kubelet config") |
| 114 | + assert.True(op.Get("nodePools.#(name==\"pool-03\").config.kubeletConfig.cpuCfsQuota").Bool(), "has the expected cpuCfsQuota kubelet config") |
| 115 | + assert.Equal(int64(20000), op.Get("nodePools.#(name==\"pool-03\").config.linuxNodeConfig.sysctls.net\\.core\\.netdev_max_backlog").Int(), "has the expected linux node config sysctls") |
| 116 | + |
| 117 | + // Pool-04 |
| 118 | + assert.Equal("pool-04", op.Get("nodePools.#(name==\"pool-04\").name").String(), "pool-4 exists") |
| 119 | + assert.False(true, op.Get("nodePools.#(name==\"pool-04\").queued_provisioning.enabled").Bool(), "has queued provisioning enabled") |
| 120 | + |
| 121 | + // Pool-05 |
| 122 | + assert.Equal("pool-05", op.Get("nodePools.#(name==\"pool-05\").name").String(), "pool-5 exists") |
| 123 | + assert.False(op.Get("nodePools.#(name==\"pool-05\").node_config.advanced_machine_features.enable_nested_virtualization").Bool(), "has enable_nested_virtualization enabled") |
| 124 | + |
| 125 | + }) |
| 126 | + |
| 127 | + bpt.Test() |
| 128 | +} |
0 commit comments