From 24ac8352a6cd32f2eb3c36cfeaf3139a4e2b0c9d Mon Sep 17 00:00:00 2001 From: Ryan Oaks Date: Tue, 17 Dec 2024 15:55:03 -0500 Subject: [PATCH] Modify magician to support optional envvars (#12599) --- .ci/magician/cmd/check_cassettes.go | 24 +++++++++++++++++------- .ci/magician/cmd/test_terraform_vcr.go | 22 ++++++++++++++++------ .ci/magician/cmd/vcr_cassette_update.go | 16 +++++++++++++--- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/.ci/magician/cmd/check_cassettes.go b/.ci/magician/cmd/check_cassettes.go index e149dd370844..7d5c64414c61 100644 --- a/.ci/magician/cmd/check_cassettes.go +++ b/.ci/magician/cmd/check_cassettes.go @@ -11,7 +11,7 @@ import ( "github.com/spf13/cobra" ) -var ccEnvironmentVariables = [...]string{ +var ccRequiredEnvironmentVariables = [...]string{ "COMMIT_SHA", "GOCACHE", "GOPATH", @@ -32,25 +32,35 @@ var ccEnvironmentVariables = [...]string{ "SA_KEY", } +var ccOptionalEnvironmentVariables = [...]string{} + var checkCassettesCmd = &cobra.Command{ Use: "check-cassettes", Short: "Run VCR tests on downstream main branch", Long: `This command runs after downstream changes are merged and runs the most recent VCR cassettes using the newly built beta provider. - The following environment variables are expected: -` + listCCEnvironmentVariables() + ` + The following environment variables are required: +` + listCCRequiredEnvironmentVariables() + ` It prints a list of tests that failed in replaying mode along with all test output.`, RunE: func(cmd *cobra.Command, args []string) error { - env := make(map[string]string, len(ccEnvironmentVariables)) - for _, ev := range ccEnvironmentVariables { + env := make(map[string]string) + for _, ev := range ccRequiredEnvironmentVariables { val, ok := os.LookupEnv(ev) if !ok { return fmt.Errorf("did not provide %s environment variable", ev) } env[ev] = val } + for _, ev := range ccOptionalEnvironmentVariables { + val, ok := os.LookupEnv(ev) + if ok { + env[ev] = val + } else { + fmt.Printf("🟡 Did not provide %s environment variable\n", ev) + } + } githubToken, ok := lookupGithubTokenOrFallback("GITHUB_TOKEN_DOWNSTREAMS") if !ok { @@ -80,9 +90,9 @@ func lookupGithubTokenOrFallback(tokenName string) (string, bool) { return val, ok } -func listCCEnvironmentVariables() string { +func listCCRequiredEnvironmentVariables() string { var result string - for i, ev := range ccEnvironmentVariables { + for i, ev := range ccRequiredEnvironmentVariables { result += fmt.Sprintf("\t%2d. %s\n", i+1, ev) } return result diff --git a/.ci/magician/cmd/test_terraform_vcr.go b/.ci/magician/cmd/test_terraform_vcr.go index 641e43cbecc5..088cc98a1113 100644 --- a/.ci/magician/cmd/test_terraform_vcr.go +++ b/.ci/magician/cmd/test_terraform_vcr.go @@ -33,7 +33,7 @@ var ( recordReplayTmplText string ) -var ttvEnvironmentVariables = [...]string{ +var ttvRequiredEnvironmentVariables = [...]string{ "GOCACHE", "GOPATH", "GOOGLE_BILLING_ACCOUNT", @@ -55,6 +55,8 @@ var ttvEnvironmentVariables = [...]string{ "USER", } +var ttvOptionalEnvironmentVariables = [...]string{} + type analytics struct { ReplayingResult vcr.Result RunFullVCR bool @@ -103,16 +105,24 @@ It expects the following arguments: 5. Build step number The following environment variables are required: -` + listTTVEnvironmentVariables(), +` + listTTVRequiredEnvironmentVariables(), RunE: func(cmd *cobra.Command, args []string) error { - env := make(map[string]string, len(ttvEnvironmentVariables)) - for _, ev := range ttvEnvironmentVariables { + env := make(map[string]string) + for _, ev := range ttvRequiredEnvironmentVariables { val, ok := os.LookupEnv(ev) if !ok { return fmt.Errorf("did not provide %s environment variable", ev) } env[ev] = val } + for _, ev := range ttvOptionalEnvironmentVariables { + val, ok := os.LookupEnv(ev) + if ok { + env[ev] = val + } else { + fmt.Printf("🟡 Did not provide %s environment variable\n", ev) + } + } for _, tokenName := range []string{"GITHUB_TOKEN_DOWNSTREAMS", "GITHUB_TOKEN_MAGIC_MODULES"} { val, ok := lookupGithubTokenOrFallback(tokenName) @@ -147,9 +157,9 @@ The following environment variables are required: }, } -func listTTVEnvironmentVariables() string { +func listTTVRequiredEnvironmentVariables() string { var result string - for i, ev := range ttvEnvironmentVariables { + for i, ev := range ttvRequiredEnvironmentVariables { result += fmt.Sprintf("\t%2d. %s\n", i+1, ev) } return result diff --git a/.ci/magician/cmd/vcr_cassette_update.go b/.ci/magician/cmd/vcr_cassette_update.go index 91588e676195..27f45caf1662 100644 --- a/.ci/magician/cmd/vcr_cassette_update.go +++ b/.ci/magician/cmd/vcr_cassette_update.go @@ -15,7 +15,7 @@ import ( _ "embed" ) -var vcuEnvironmentVariables = [...]string{ +var vcuRequiredEnvironmentVariables = [...]string{ "GOCACHE", "GOPATH", "GOOGLE_BILLING_ACCOUNT", @@ -38,6 +38,8 @@ var vcuEnvironmentVariables = [...]string{ "GITHUB_TOKEN_CLASSIC", } +var vcuOptionalEnvironmentVariables = [...]string{} + var ( //go:embed templates/vcr/vcr_cassettes_update_replaying.tmpl replayingTmplText string @@ -73,14 +75,22 @@ var vcrCassetteUpdateCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - env := make(map[string]string, len(vcuEnvironmentVariables)) - for _, ev := range vcuEnvironmentVariables { + env := make(map[string]string) + for _, ev := range vcuRequiredEnvironmentVariables { val, ok := os.LookupEnv(ev) if !ok { return fmt.Errorf("did not provide %s environment variable", ev) } env[ev] = val } + for _, ev := range vcuOptionalEnvironmentVariables { + val, ok := os.LookupEnv(ev) + if ok { + env[ev] = val + } else { + fmt.Printf("🟡 Did not provide %s environment variable\n", ev) + } + } buildID := args[0]