-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* 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 cai | ||
|
||
import ( | ||
"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 | ||
assetType string // asset type to retrieve. default: all | ||
} | ||
|
||
type cmdOption func(*CmdCfg) | ||
|
||
// newCmdConfig sets defaults and options | ||
func newCmdConfig(opts ...cmdOption) (*CmdCfg) { | ||
caiOpts := &CmdCfg{ | ||
sleep: 2, | ||
assetType: "", | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(caiOpts) | ||
} | ||
|
||
return caiOpts | ||
} | ||
|
||
// Set custom sleep minutes | ||
func WithSleep(sleep int) cmdOption { | ||
return func(f *CmdCfg) { | ||
f.sleep = sleep | ||
} | ||
} | ||
|
||
// Set asset type | ||
func WithAssetType(assetType string) cmdOption { | ||
return func(f *CmdCfg) { | ||
f.assetType = assetType | ||
} | ||
} | ||
|
||
// 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...) | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* 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 test | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"testing" | ||
|
||
"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/tft" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetProjectResources(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
assetType string | ||
wantKeyPath string | ||
wantVal string | ||
}{ | ||
{name: "all", assetType: "", wantKeyPath: "resource.data.nodeConfig.imageType", wantVal: "COS_CONTAINERD"}, | ||
{name: "cluster", assetType: "container.googleapis.com/Cluster", wantKeyPath: "resource.data.nodeConfig.imageType", wantVal: "COS_CONTAINERD"}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert := assert.New(t) | ||
|
||
tfBlueprint := tft.NewTFBlueprintTest(t, | ||
tft.WithTFDir("setup"), | ||
) | ||
|
||
clusterResourceName := fmt.Sprintf("//container.googleapis.com/projects/%s/locations/%s/clusters/%s", | ||
tfBlueprint.GetStringOutput("project_id"), | ||
tfBlueprint.GetStringOutput("cluster_region"), | ||
tfBlueprint.GetStringOutput("cluster_name"), | ||
) | ||
|
||
credDec, _ := base64.StdEncoding.DecodeString(tfBlueprint.GetStringOutput("sa_key")) | ||
gcloud.ActivateCredsAndEnvVars(t, string(credDec)) | ||
|
||
cai := cai.GetProjectResources(t, tfBlueprint.GetStringOutput("project_id"), cai.WithAssetType(tt.assetType)) | ||
assert.Equal(tt.wantVal, cai.Get("#(name=\"" + clusterResourceName + "\")." + tt.wantKeyPath).String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters