Skip to content

Commit

Permalink
multi asset types
Browse files Browse the repository at this point in the history
  • Loading branch information
apeabody committed Nov 15, 2024
1 parent 1c9487a commit 15d318a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
14 changes: 12 additions & 2 deletions test/integration/node_pool/node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func TestNodePool(t *testing.T) {

//cluster := gcloud.Runf(t, "container clusters describe %s --zone %s --project %s", clusterName, location, projectId)
clusterResourceName := fmt.Sprintf("//container.googleapis.com/projects/%s/locations/%s/clusters/%s", projectId, location, clusterName)
cluster := gkeutils.GetProjectResources(t, projectId, gkeutils.WithAssetType("container.googleapis.com/Cluster")).Get("#(name=\"" + clusterResourceName + "\").resource.data")
cluster := gkeutils.GetProjectResources(t, projectId, gkeutils.WithAssetTypes([]string{"container.googleapis.com/Cluster"})).Get("#(name=\"" + clusterResourceName + "\").resource.data")

// Cluster
assert.Contains([]string{"RUNNING", "RECONCILING"}, cluster.Get("status").String(), "Cluster is Running")
assert.Equal("COS_CONTAINERD", cluster.Get("autoscaling.autoprovisioningNodePoolDefaults.imageType").String(), "has the expected image type")
assert.Equal("[\n \"https://www.googleapis.com/auth/cloud-platform\"\n ]", cluster.Get("autoscaling.autoprovisioningNodePoolDefaults.oauthScopes").String(), "has the expected oauth scopes")
assert.Equal("https://www.googleapis.com/auth/cloud-platform", cluster.Get("autoscaling.autoprovisioningNodePoolDefaults.oauthScopes.1").String(), "has the expected oauth scopes")
assert.Equal("default", cluster.Get("autoscaling.autoprovisioningNodePoolDefaults.serviceAccount").String(), "has the expected service account")
assert.Equal("OPTIMIZE_UTILIZATION", cluster.Get("autoscaling.autoscalingProfile").String(), "has the expected autoscaling profile")
assert.True(cluster.Get("autoscaling.enableNodeAutoprovisioning").Bool(), "has the expected node autoprovisioning")
Expand Down Expand Up @@ -148,6 +148,11 @@ func TestNodePool(t *testing.T) {
"effect": "PreferNoSchedule",
"key": "all-pools-example",
"value": "true"
},
{
"effect": "NoSchedule",
"key": "nvidia.com/gpu",
"value": "present"
}
]`,
clusterNodes.Get("items.#(metadata.labels.node_pool==\"pool-02\").spec.taints").String(), "has the expected all-pools-example taint")
Expand All @@ -156,6 +161,11 @@ func TestNodePool(t *testing.T) {
"effect": "PreferNoSchedule",
"key": "all-pools-example",
"value": "true"
},
{
"effect": "NoSchedule",
"key": "sandbox.gke.io/runtime",
"value": "gvisor"
}
]`,
clusterNodes.Get("items.#(metadata.labels.node_pool==\"pool-03\").spec.taints").String(), "has the expected all-pools-example taint")
Expand Down
34 changes: 22 additions & 12 deletions test/integration/utils/cai.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package utils

import (
"fmt"
"strings"
"testing"
"time"

Expand All @@ -26,23 +28,30 @@ import (
)

type CmdCfg struct {
sleep int // minutes to sleep prior to CAI retreval. default: 2
assetType string // asset type to retrieve. default: all
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,
assetType: "",
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
}

Expand All @@ -53,20 +62,21 @@ func WithSleep(sleep int) cmdOption {
}
}

// Set asset type
func WithAssetType(assetType string) cmdOption {
// Set asset types
func WithAssetTypes(assetTypes []string) cmdOption {
return func(f *CmdCfg) {
f.assetType = assetType
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)
if caiOpts.assetType != "" {
return gcloud.Runf(t, "asset list --project=%s --asset-types=%s --content-type=resource", project, caiOpts.assetType)
} else {
return gcloud.Runf(t, "asset list --project=%s --content-type=resource", project)
}

cmd := fmt.Sprintf("asset list --project %s", project)
return gcloud.Runf(t, strings.Join(append([]string{cmd}, caiOpts.args...), " "))
}

0 comments on commit 15d318a

Please sign in to comment.