Skip to content

Commit

Permalink
move stringFromTextAndArgs to utils, added test
Browse files Browse the repository at this point in the history
  • Loading branch information
bradmiro committed Oct 27, 2023
1 parent 0cae7a3 commit b40e764
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 88 deletions.
38 changes: 2 additions & 36 deletions infra/blueprint-test/pkg/bq/bq.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func Run(t testing.TB, cmd string, opts ...cmdOption) gjson.Result {
//
// It fails the test if there are any errors executing the bq command or parsing the output value.
func RunWithCmdOptsf(t testing.TB, opts []cmdOption, cmd string, args ...interface{}) gjson.Result {
return Run(t, stringFromTextAndArgs(append([]interface{}{cmd}, args...)...), opts...)
return Run(t, utils.StringFromTextAndArgs(append([]interface{}{cmd}, args...)...), opts...)
}

// Runf executes a bq command and returns value as gjson.Result.
Expand All @@ -127,39 +127,5 @@ func RunWithCmdOptsf(t testing.TB, opts []cmdOption, cmd string, args ...interfa
//
// It fails the test if there are any errors executing the bq command or parsing the output value.
func Runf(t testing.TB, cmd string, args ...interface{}) gjson.Result {
return Run(t, stringFromTextAndArgs(append([]interface{}{cmd}, args...)...))
}

// stringFromTextAndArgs convert msg and args to formatted text
func stringFromTextAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
}

// ActivateCredsAndEnvVars activates credentials and exports auth related envvars.
func ActivateCredsAndEnvVars(t testing.TB, creds string) {
credsPath, err := utils.WriteTmpFile(creds)
if err != nil {
t.Fatal(err)
}
RunCmd(t, "auth activate-service-account", WithCommonArgs([]string{"--key-file", credsPath}))
// set auth related env vars
// TF provider auth
utils.SetEnv(t, "GOOGLE_CREDENTIALS", creds)
// bq SDK override
utils.SetEnv(t, "CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE", credsPath)
// ADC
utils.SetEnv(t, "GOOGLE_APPLICATION_CREDENTIALS", credsPath)
return Run(t, utils.StringFromTextAndArgs(append([]interface{}{cmd}, args...)...))
}
32 changes: 0 additions & 32 deletions infra/blueprint-test/pkg/bq/bq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestActivateCredsAndEnvVars(t *testing.T) {
tests := []struct {
name string
keyEnvVar string
user string
}{
{
name: "with sa key",
keyEnvVar: "TEST_KEY",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
creds, present := os.LookupEnv(tt.keyEnvVar)
if !present {
t.Logf("Skipping test, %s envvar not set", tt.keyEnvVar)
t.Skip()
}
ActivateCredsAndEnvVars(t, creds)
assert := assert.New(t)
assert.Equal(os.Getenv("GOOGLE_CREDENTIALS"), creds)
pathEnvVars := []string{"CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE", "GOOGLE_APPLICATION_CREDENTIALS"}
for _, v := range pathEnvVars {
c, err := os.ReadFile(os.Getenv(v))
assert.NoError(err)
assert.Equal(string(c), creds)
}

})
}
}

func TestRunf(t *testing.T) {
tests := []struct {
name string
Expand Down
22 changes: 2 additions & 20 deletions infra/blueprint-test/pkg/gcloud/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TFVet(t testing.TB, planFilePath string, policyLibraryPath, terraformVetPro
//
// It fails the test if there are any errors executing the gcloud command or parsing the output value.
func RunWithCmdOptsf(t testing.TB, opts []cmdOption, cmd string, args ...interface{}) gjson.Result {
return Run(t, stringFromTextAndArgs(append([]interface{}{cmd}, args...)...), opts...)
return Run(t, utils.StringFromTextAndArgs(append([]interface{}{cmd}, args...)...), opts...)
}

// Runf executes a gcloud command and returns value as gjson.Result.
Expand All @@ -139,25 +139,7 @@ func RunWithCmdOptsf(t testing.TB, opts []cmdOption, cmd string, args ...interfa
//
// It fails the test if there are any errors executing the gcloud command or parsing the output value.
func Runf(t testing.TB, cmd string, args ...interface{}) gjson.Result {
return Run(t, stringFromTextAndArgs(append([]interface{}{cmd}, args...)...))
}

// stringFromTextAndArgs convert msg and args to formatted text
func stringFromTextAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
return Run(t, utils.StringFromTextAndArgs(append([]interface{}{cmd}, args...)...))
}

// ActivateCredsAndEnvVars activates credentials and exports auth related envvars.
Expand Down
37 changes: 37 additions & 0 deletions infra/blueprint-test/pkg/utils/string_formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2023 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 "fmt"

// StringFromTextAndArgs converts msg and args to formatted text
func StringFromTextAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
}
46 changes: 46 additions & 0 deletions infra/blueprint-test/pkg/utils/string_formatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2022 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 TestStringFromTextAndArgs(t *testing.T) {
tests := []struct {
name string
cmd string
args []interface{}
output string
}{
{
name: "one arg",
cmd: "project list --filter=%s",
args: []interface{}{"TEST_PROJECT"},
output: "project list --filter=TEST_PROJECT",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
funcOut := StringFromTextAndArgs(append([]interface{}{tt.cmd}, tt.args...) ...)
assert.Equal(tt.output, funcOut)
})
}
}

0 comments on commit b40e764

Please sign in to comment.