Skip to content

Commit

Permalink
Modify magician to support optional envvars (#12599)
Browse files Browse the repository at this point in the history
  • Loading branch information
roaks3 authored Dec 17, 2024
1 parent 90e4b55 commit 24ac835
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
24 changes: 17 additions & 7 deletions .ci/magician/cmd/check_cassettes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
)

var ccEnvironmentVariables = [...]string{
var ccRequiredEnvironmentVariables = [...]string{
"COMMIT_SHA",
"GOCACHE",
"GOPATH",
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions .ci/magician/cmd/test_terraform_vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
recordReplayTmplText string
)

var ttvEnvironmentVariables = [...]string{
var ttvRequiredEnvironmentVariables = [...]string{
"GOCACHE",
"GOPATH",
"GOOGLE_BILLING_ACCOUNT",
Expand All @@ -55,6 +55,8 @@ var ttvEnvironmentVariables = [...]string{
"USER",
}

var ttvOptionalEnvironmentVariables = [...]string{}

type analytics struct {
ReplayingResult vcr.Result
RunFullVCR bool
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions .ci/magician/cmd/vcr_cassette_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
_ "embed"
)

var vcuEnvironmentVariables = [...]string{
var vcuRequiredEnvironmentVariables = [...]string{
"GOCACHE",
"GOPATH",
"GOOGLE_BILLING_ACCOUNT",
Expand All @@ -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
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit 24ac835

Please sign in to comment.