Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify magician to support optional envvars #12599

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading