diff --git a/infra/blueprint-test/pkg/utils/jsonresult.go b/infra/blueprint-test/pkg/utils/jsonresult.go index dc699e12568..7b9636dc491 100644 --- a/infra/blueprint-test/pkg/utils/jsonresult.go +++ b/infra/blueprint-test/pkg/utils/jsonresult.go @@ -18,6 +18,7 @@ package utils import ( "os" + "regexp" "github.com/mitchellh/go-testing-interface" "github.com/tidwall/gjson" @@ -43,3 +44,18 @@ func ParseJSONResult(t testing.TB, j string) gjson.Result { } return gjson.Parse(j) } + +// Kubectl transient errors +var ( + KubectlTransientErrors = []string{ + "E022[23] .* the server is currently unable to handle the request", + } +) + +// Filter transient errors from kubectl output +func ParseKubectlJSONResult(t testing.TB, str string) gjson.Result { + for _, error := range KubectlTransientErrors { + str = regexp.MustCompile(error).ReplaceAllString(str, "") + } + return ParseJSONResult(t, str) +} diff --git a/infra/blueprint-test/pkg/utils/jsonresult_test.go b/infra/blueprint-test/pkg/utils/jsonresult_test.go new file mode 100644 index 00000000000..fa041a9e6a7 --- /dev/null +++ b/infra/blueprint-test/pkg/utils/jsonresult_test.go @@ -0,0 +1,60 @@ +/** + * 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 utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestKubectlJSONResult(t *testing.T) { + tests := []struct { + name string + input string + output map[string]interface{} + }{ + { + name: "one error", + input: `E0223 error the server is currently unable to handle the request + { + "apiVersion": "v1" + }`, + output: map[string]interface{}{ + "apiVersion": "v1", + }, + }, + { + name: "two error", + input: `E0223 error the server is currently unable to handle the request + E0222 some other error so the server is currently unable to handle the request + { + "apiVersion": "v1" + }`, + output: map[string]interface{}{ + "apiVersion": "v1", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + funcOut := ParseKubectlJSONResult(t, tt.input) + assert.Equal(tt.output, funcOut.Value().(map[string]interface{})) + }) + } +} diff --git a/infra/blueprint-test/test/setup/main.tf b/infra/blueprint-test/test/setup/main.tf index ebeccc0eba6..a3baceec096 100644 --- a/infra/blueprint-test/test/setup/main.tf +++ b/infra/blueprint-test/test/setup/main.tf @@ -35,6 +35,8 @@ module "project" { folder_id = var.folder_id billing_account = var.billing_account + default_service_account = "DEPRIVILEGE" + activate_apis = [ "cloudresourcemanager.googleapis.com", "compute.googleapis.com",