diff --git a/.ci/magician/cmd/generate_comment.go b/.ci/magician/cmd/generate_comment.go index 2359fff51983..35d5b0e3af0e 100644 --- a/.ci/magician/cmd/generate_comment.go +++ b/.ci/magician/cmd/generate_comment.go @@ -151,7 +151,7 @@ func listGCEnvironmentVariables() string { return result } -func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep, projectId, commitSha string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller) error { +func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep, projectId, commitSha string, gh GithubClient, rnr exec.ExecRunner, ctlr *source.Controller) error { errors := map[string][]string{"Other": []string{}} pullRequest, err := gh.GetPullRequest(strconv.Itoa(prNumber)) @@ -414,7 +414,7 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep, } // Build the diff processor for tpg or tpgb -func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[string]string, rnr ExecRunner) error { +func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[string]string, rnr exec.ExecRunner) error { for _, path := range []string{"old", "new", "bin"} { if err := rnr.RemoveAll(filepath.Join(diffProcessorPath, path)); err != nil { return err @@ -434,7 +434,7 @@ func buildDiffProcessor(diffProcessorPath, providerLocalPath string, env map[str return rnr.PopDir() } -func computeBreakingChanges(diffProcessorPath string, rnr ExecRunner) ([]BreakingChange, error) { +func computeBreakingChanges(diffProcessorPath string, rnr exec.ExecRunner) ([]BreakingChange, error) { if err := rnr.PushDir(diffProcessorPath); err != nil { return nil, err } @@ -454,7 +454,7 @@ func computeBreakingChanges(diffProcessorPath string, rnr ExecRunner) ([]Breakin return changes, rnr.PopDir() } -func changedSchemaResources(diffProcessorPath string, rnr ExecRunner) ([]string, error) { +func changedSchemaResources(diffProcessorPath string, rnr exec.ExecRunner) ([]string, error) { if err := rnr.PushDir(diffProcessorPath); err != nil { return nil, err } @@ -480,7 +480,7 @@ func changedSchemaResources(diffProcessorPath string, rnr ExecRunner) ([]string, // Run the missing test detector and return the results. // Returns an empty string unless there are missing tests. // Error will be nil unless an error occurs during setup. -func detectMissingTests(diffProcessorPath, tpgbLocalPath string, rnr ExecRunner) (map[string]*MissingTestInfo, error) { +func detectMissingTests(diffProcessorPath, tpgbLocalPath string, rnr exec.ExecRunner) (map[string]*MissingTestInfo, error) { if err := rnr.PushDir(diffProcessorPath); err != nil { return nil, err } diff --git a/.ci/magician/cmd/generate_downstream.go b/.ci/magician/cmd/generate_downstream.go index 64ef6857f538..a58269e396f0 100644 --- a/.ci/magician/cmd/generate_downstream.go +++ b/.ci/magician/cmd/generate_downstream.go @@ -93,7 +93,7 @@ func listGDEnvironmentVariables() string { return result } -func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller) error { +func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh GithubClient, rnr exec.ExecRunner, ctlr *source.Controller) error { if baseBranch == "" { baseBranch = "main" } @@ -177,7 +177,7 @@ func execGenerateDownstream(baseBranch, command, repo, version, ref string, gh G return nil } -func cloneRepo(mmRepo *source.Repo, baseBranch, repo, version, command, ref string, rnr ExecRunner, ctlr *source.Controller) (*source.Repo, *source.Repo, string, error) { +func cloneRepo(mmRepo *source.Repo, baseBranch, repo, version, command, ref string, rnr exec.ExecRunner, ctlr *source.Controller) (*source.Repo, *source.Repo, string, error) { downstreamRepo := &source.Repo{ Title: repo, Branch: baseBranch, @@ -244,7 +244,7 @@ func cloneRepo(mmRepo *source.Repo, baseBranch, repo, version, command, ref stri return downstreamRepo, scratchRepo, commitMessage, nil } -func setGitConfig(rnr ExecRunner) error { +func setGitConfig(rnr exec.ExecRunner) error { if _, err := rnr.Run("git", []string{"config", "--local", "user.name", "Modular Magician"}, nil); err != nil { return err } @@ -254,7 +254,7 @@ func setGitConfig(rnr ExecRunner) error { return nil } -func runMake(downstreamRepo *source.Repo, command string, rnr ExecRunner) error { +func runMake(downstreamRepo *source.Repo, command string, rnr exec.ExecRunner) error { switch downstreamRepo.Title { case "terraform-google-conversion": if _, err := rnr.Run("make", []string{"clean-tgc", "OUTPUT_PATH=" + downstreamRepo.Path}, nil); err != nil { @@ -308,7 +308,7 @@ func getPullRequest(baseBranch, ref string, gh GithubClient) (*github.PullReques return nil, fmt.Errorf("no pr found with merge commit sha %s and base branch %s", ref, baseBranch) } -func createCommit(scratchRepo *source.Repo, commitMessage string, rnr ExecRunner) (string, error) { +func createCommit(scratchRepo *source.Repo, commitMessage string, rnr exec.ExecRunner) (string, error) { if err := rnr.PushDir(scratchRepo.Path); err != nil { return "", err } @@ -338,7 +338,7 @@ func createCommit(scratchRepo *source.Repo, commitMessage string, rnr ExecRunner return commitSha, err } -func addChangelogEntry(downstreamRepo *source.Repo, pullRequest *github.PullRequest, rnr ExecRunner) error { +func addChangelogEntry(downstreamRepo *source.Repo, pullRequest *github.PullRequest, rnr exec.ExecRunner) error { if err := rnr.PushDir(downstreamRepo.Path); err != nil { return err } @@ -349,7 +349,7 @@ func addChangelogEntry(downstreamRepo *source.Repo, pullRequest *github.PullRequ return rnr.PopDir() } -func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, scratchRepoSha string, pullRequest *github.PullRequest, rnr ExecRunner, gh GithubClient) error { +func mergePullRequest(downstreamRepo, scratchRepo *source.Repo, scratchRepoSha string, pullRequest *github.PullRequest, rnr exec.ExecRunner, gh GithubClient) error { fmt.Printf(`Base: %s:%s Head: %s:%s `, downstreamRepo.Owner, downstreamRepo.Branch, scratchRepo.Owner, scratchRepo.Branch) diff --git a/.ci/magician/cmd/interfaces.go b/.ci/magician/cmd/interfaces.go index 948ab794c907..13ec1b16375a 100644 --- a/.ci/magician/cmd/interfaces.go +++ b/.ci/magician/cmd/interfaces.go @@ -39,15 +39,3 @@ type CloudbuildClient interface { ApproveCommunityChecker(prNumber, commitSha string) error TriggerMMPresubmitRuns(commitSha string, substitutions map[string]string) error } - -type ExecRunner interface { - GetCWD() string - Copy(src, dest string) error - Mkdir(path string) error - RemoveAll(path string) error - PushDir(path string) error - PopDir() error - WriteFile(name, data string) error - Run(name string, args []string, env map[string]string) (string, error) - MustRun(name string, args []string, env map[string]string) string -} diff --git a/.ci/magician/cmd/mock_runner_test.go b/.ci/magician/cmd/mock_runner_test.go index c3a1abccbde3..ae354d7f488f 100644 --- a/.ci/magician/cmd/mock_runner_test.go +++ b/.ci/magician/cmd/mock_runner_test.go @@ -20,6 +20,7 @@ import ( "errors" "fmt" "log" + "magician/exec" "os" "path/filepath" "sort" @@ -31,7 +32,7 @@ import ( type ParameterList []any type MockRunner interface { - ExecRunner + exec.ExecRunner Calls(method string) ([]ParameterList, bool) } diff --git a/.ci/magician/cmd/test_terraform_vcr.go b/.ci/magician/cmd/test_terraform_vcr.go index 3b976fece0d4..18c3a2972c20 100644 --- a/.ci/magician/cmd/test_terraform_vcr.go +++ b/.ci/magician/cmd/test_terraform_vcr.go @@ -133,7 +133,7 @@ var testTerraformVCRCmd = &cobra.Command{ }, } -func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep, baseBranch string, gh GithubClient, rnr ExecRunner, ctlr *source.Controller, vt *vcr.Tester) error { +func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep, baseBranch string, gh GithubClient, rnr exec.ExecRunner, ctlr *source.Controller, vt *vcr.Tester) error { newBranch := "auto-pr-" + prNumber oldBranch := newBranch + "-old" diff --git a/.ci/magician/cmd/test_tgc_integration.go b/.ci/magician/cmd/test_tgc_integration.go index f5b6dfb68e0b..b518f81d1310 100644 --- a/.ci/magician/cmd/test_tgc_integration.go +++ b/.ci/magician/cmd/test_tgc_integration.go @@ -44,7 +44,7 @@ var testTGCIntegrationCmd = &cobra.Command{ }, } -func execTestTGCIntegration(prNumber, mmCommit, buildID, projectID, buildStep, ghRepo, githubUsername string, rnr ExecRunner, ctlr *source.Controller, gh GithubClient) error { +func execTestTGCIntegration(prNumber, mmCommit, buildID, projectID, buildStep, ghRepo, githubUsername string, rnr exec.ExecRunner, ctlr *source.Controller, gh GithubClient) error { newBranch := "auto-pr-" + prNumber repo := &source.Repo{ Name: ghRepo, diff --git a/.ci/magician/exec/interfaces.go b/.ci/magician/exec/interfaces.go new file mode 100644 index 000000000000..4f0b40d5e170 --- /dev/null +++ b/.ci/magician/exec/interfaces.go @@ -0,0 +1,17 @@ +package exec + +import "path/filepath" + +type ExecRunner interface { + GetCWD() string + Copy(src, dest string) error + Mkdir(path string) error + RemoveAll(path string) error + PushDir(path string) error + PopDir() error + WriteFile(name, data string) error + Run(name string, args []string, env map[string]string) (string, error) + MustRun(name string, args []string, env map[string]string) string + Walk(root string, fn filepath.WalkFunc) error + ReadFile(name string) (string, error) +} diff --git a/.ci/magician/vcr/tester.go b/.ci/magician/vcr/tester.go index c7b1343b4ef3..b838d62cad99 100644 --- a/.ci/magician/vcr/tester.go +++ b/.ci/magician/vcr/tester.go @@ -50,7 +50,7 @@ type logKey struct { type Tester struct { env map[string]string // shared environment variables for running tests - rnr *exec.Runner // for running commands and manipulating files + rnr exec.ExecRunner // for running commands and manipulating files baseDir string // the directory in which this tester was created saKeyPath string // where sa_key.json is relative to baseDir cassettePaths map[provider.Version]string // where cassettes are relative to baseDir by version @@ -68,7 +68,7 @@ var testResultsExpression = regexp.MustCompile(`(?m:^--- (PASS|FAIL|SKIP): (Test var testPanicExpression = regexp.MustCompile(`^panic: .*`) // Create a new tester in the current working directory and write the service account key file. -func NewTester(env map[string]string, rnr *exec.Runner) (*Tester, error) { +func NewTester(env map[string]string, rnr exec.ExecRunner) (*Tester, error) { saKeyPath := "sa_key.json" if err := rnr.WriteFile(saKeyPath, env["SA_KEY"]); err != nil { return nil, err @@ -91,11 +91,11 @@ func (vt *Tester) SetRepoPath(version provider.Version, repoPath string) { // Fetch the cassettes for the current version if not already fetched. // Should be run from the base dir. func (vt *Tester) FetchCassettes(version provider.Version, baseBranch, prNumber string) error { - cassettePath, ok := vt.cassettePaths[version] + _, ok := vt.cassettePaths[version] if ok { return nil } - cassettePath = filepath.Join(vt.baseDir, "cassettes", version.String()) + cassettePath := filepath.Join(vt.baseDir, "cassettes", version.String()) vt.rnr.Mkdir(cassettePath) if baseBranch != "FEATURE-BRANCH-major-release-6.0.0" { // pull main cassettes (major release uses branch specific casssettes as primary ones) @@ -240,7 +240,7 @@ func (vt *Tester) RunParallel(mode Mode, version provider.Version, testDirs, tes if err != nil { return nil, err } - if vt.rnr.Mkdir(filepath.Join(vt.baseDir, "testlogs", mode.Lower()+"_build")); err != nil { + if err := vt.rnr.Mkdir(filepath.Join(vt.baseDir, "testlogs", mode.Lower()+"_build")); err != nil { return nil, err } repoPath, ok := vt.repoPaths[version]