Skip to content

Commit

Permalink
Merge branch 'GoogleCloudPlatform:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
anavada authored Sep 30, 2024
2 parents 7b1d819 + b750180 commit dd0a446
Show file tree
Hide file tree
Showing 79 changed files with 3,239 additions and 296 deletions.
2 changes: 1 addition & 1 deletion .ci/gcb-pr-downstream-generation-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ steps:
- $COMMIT_SHA
- $BUILD_ID
- $PROJECT_ID
- "22" # Build step
- "23" # Build step

- name: 'gcr.io/graphite-docker-images/go-plus'
entrypoint: '/workspace/.ci/scripts/go-plus/magician/exec.sh'
Expand Down
2 changes: 1 addition & 1 deletion .ci/magician/cmd/check_cassettes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var checkCassettesCmd = &cobra.Command{

ctlr := source.NewController(env["GOPATH"], "modular-magician", githubToken, rnr)

vt, err := vcr.NewTester(env, "vcr-check-cassettes", "ci-vcr-cassettes", rnr)
vt, err := vcr.NewTester(env, "ci-vcr-cassettes", "vcr-check-cassettes", rnr)
if err != nil {
return fmt.Errorf("error creating VCR tester: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/generate_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ func detectMissingTests(diffProcessorPath, tpgbLocalPath string, rnr ExecRunner)
}

func formatDiffComment(data diffCommentData) (string, error) {
tmpl, err := template.New("DIFF_COMMENT.md").Parse(diffComment)
tmpl, err := template.New("DIFF_COMMENT.md.tmpl").Parse(diffComment)
if err != nil {
panic(fmt.Sprintf("Unable to parse DIFF_COMMENT.md: %s", err))
return "", fmt.Errorf("unable to parse template DIFF_COMMENT.md.tmpl: %s", err)
}
sb := new(strings.Builder)
err = tmpl.Execute(sb, data)
Expand Down
23 changes: 23 additions & 0 deletions .ci/magician/cmd/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"
)

func color(color, text string) string {
if color == "" || text == "" {
return text
}
var emoji string
switch color {
case "red":
emoji = "🔴"
case "yellow":
emoji = "🟡"
case "green":
emoji = "🟢"
default:
return text
}
return fmt.Sprintf("%s %s", emoji, text)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{ if lt .SinceDays 30 -}}
@{{.User}}, this PR is waiting for action from you. Please address any comments or change requests, or [re-request review](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/requesting-a-pull-request-review) from a core reviewer if no action is required.

![Image showing the re-request review button](https://docs.github.com/assets/cb-28785/mw-1440/images/help/pull_requests/request-re-review.webp)

If no action is taken, this PR will be closed in
@{{.User}}, this PR is waiting for action from you. If no action is taken, this PR will be closed in
{{- if eq .SinceDays 10 }} 28 days.{{end}}
{{- if eq .SinceDays 20 }} 14 days.{{end}}
{{- if eq .SinceDays 28 }} 2 weekdays.{{end}}

Please address any comments or change requests, or [re-request review](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/requesting-a-pull-request-review) from a core reviewer if no action is required.

![Image showing the re-request review button](https://docs.github.com/assets/cb-28785/mw-1440/images/help/pull_requests/request-re-review.webp)

This notification can be disabled with the `disable-automatic-closure` label.
{{ else -}}
@{{.User}}, this PR is being closed due to inactivity.
Expand Down
14 changes: 7 additions & 7 deletions .ci/magician/cmd/templates/vcr/record_replay.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{- if gt (len .RecordingResult.PassedTests) 0 -}}
$\textcolor{green}{\textsf{Tests passed during RECORDING mode:}}$
{{color "green" "Tests passed during RECORDING mode:"}}
{{range .RecordingResult.PassedTests}}`{{.}}`[[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
{{end}}

{{- if gt (len .ReplayingAfterRecordingResult.FailedTests ) 0 -}}
$\textcolor{red}{\textsf{Tests failed when rerunning REPLAYING mode:}}$
{{color "red" "Tests failed when rerunning REPLAYING mode:"}}
{{range .ReplayingAfterRecordingResult.FailedTests}}`{{.}}`[[Error message](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/build-log/replaying_build_after_recording/{{.}}_replaying_test.log)] [[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/replaying_after_recording/{{.}}.log)]
{{end}}

Expand All @@ -13,19 +13,19 @@ Tests failed due to non-determinism or randomness when the VCR replayed the resp
Please fix these to complete your PR. If you believe these test failures to be incorrect or unrelated to your change, or if you have any questions, please raise the concern with your reviewer.

{{else}}
$\textcolor{green}{\textsf{No issues found for passed tests after REPLAYING rerun.}}$
{{color "green" "No issues found for passed tests after REPLAYING rerun."}}
{{end}}{{/* end of if gt (len .ReplayingAfterRecordingResult.FailedTests ) 0 */}}
---
{{end}}{{/* end of if gt (len .RecordingResult.PassedTests) 0 */}}

{{if gt (len .RecordingResult.FailedTests) 0 -}}
$\textcolor{red}{\textsf{Tests failed during RECORDING mode:}}$
{{color "red" "Tests failed during RECORDING mode:"}}
{{range .RecordingResult.FailedTests}}`{{.}}`[[Error message](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/build-log/recording_build/{{.}}_recording_test.log)] [[Debug log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{$.PRNumber}}/artifacts/{{$.BuildID}}/recording/{{.}}.log)]
{{end}}
{{end}} {{- /* end of if gt (len .RecordingResult.FailedTests) 0 */ -}}

{{if .HasTerminatedTests}}$\textcolor{red}{\textsf{Several tests got terminated during RECORDING mode.}}${{end}}
{{if .RecordingErr}}$\textcolor{red}{\textsf{Errors occurred during RECORDING mode. Please fix them to complete your PR.}}${{end}}
{{if .AllRecordingPassed}}$\textcolor{green}{\textsf{All tests passed!}}${{end}}
{{if .HasTerminatedTests}}{{color "red" "Several tests got terminated during RECORDING mode."}}{{end}}
{{if .RecordingErr}}{{color "red" "Errors occurred during RECORDING mode. Please fix them to complete your PR."}}{{end}}
{{if .AllRecordingPassed}}{{color "green" "All tests passed!"}}{{end}}

View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/build-log/recording_test.log) or the [debug log](https://console.cloud.google.com/storage/browser/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/recording) for each test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- if .ReplayingErr -}}
$\textcolor{red}{\textsf{Errors occurred during REPLAYING mode. Please fix them to complete your PR.}}$
{{color "red" "Errors occurred during REPLAYING mode. Please fix them to complete your PR."}}
{{- else -}}
$\textcolor{green}{\textsf{All tests passed!}}$
{{color "green" "All tests passed!"}}
{{- end}}

View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-{{.PRNumber}}/artifacts/{{.BuildID}}/build-log/replaying_test.log)
58 changes: 58 additions & 0 deletions .ci/magician/cmd/templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"testing"
)

func TestColor(t *testing.T) {
cases := []struct {
name string
color string
text string
want string
}{
{
name: "red",
color: "red",
text: "Test text",
want: "🔴 Test text",
},
{
name: "yellow",
color: "yellow",
text: "Test text",
want: "🟡 Test text",
},
{
name: "green",
color: "green",
text: "Test text",
want: "🟢 Test text",
},
{
name: "unsupported color",
color: "mauve",
text: "Test text",
want: "Test text",
},
{
name: "empty color",
text: "Test text",
want: "Test text",
},
{
name: "empty text",
color: "green",
want: "",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := color(tc.color, tc.text)
if got != tc.want {
t.Errorf("color(%s, %s) got %s; want %s", tc.color, tc.text, got, tc.want)
}
})
}
}
39 changes: 20 additions & 19 deletions .ci/magician/cmd/test_terraform_vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ var testTerraformVCRCmd = &cobra.Command{
}
ctlr := source.NewController(env["GOPATH"], "modular-magician", env["GITHUB_TOKEN_DOWNSTREAMS"], rnr)

vt, err := vcr.NewTester(env, "ci-vcr-logs", "ci-vcr-cassettes", rnr)
vt, err := vcr.NewTester(env, "ci-vcr-cassettes", "ci-vcr-logs", rnr)
if err != nil {
return fmt.Errorf("error creating VCR tester: %w", err)
}
Expand Down Expand Up @@ -180,7 +180,7 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
}
fmt.Println("Running tests: Go files or test fixtures changed")

if err := vt.FetchCassettes(provider.Beta, baseBranch, prNumber); err != nil {
if err := vt.FetchCassettes(provider.Beta, baseBranch, newBranch); err != nil {
return fmt.Errorf("error fetching cassettes: %w", err)
}

Expand All @@ -196,10 +196,10 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
}

if err := vt.UploadLogs(vcr.UploadLogsOptions{
PRNumber: prNumber,
BuildID: buildID,
Mode: vcr.Replaying,
Version: provider.Beta,
Head: newBranch,
BuildID: buildID,
Mode: vcr.Replaying,
Version: provider.Beta,
}); err != nil {
return fmt.Errorf("error uploading replaying logs: %w", err)
}
Expand Down Expand Up @@ -261,12 +261,12 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
testState = "success"
}

if err := vt.UploadCassettes(prNumber, provider.Beta); err != nil {
if err := vt.UploadCassettes(newBranch, provider.Beta); err != nil {
return fmt.Errorf("error uploading cassettes: %w", err)
}

if err := vt.UploadLogs(vcr.UploadLogsOptions{
PRNumber: prNumber,
Head: newBranch,
BuildID: buildID,
Parallel: true,
Mode: vcr.Recording,
Expand Down Expand Up @@ -295,10 +295,10 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
}

if err := vt.UploadLogs(vcr.UploadLogsOptions{
PRNumber: prNumber,
Head: newBranch,
BuildID: buildID,
Parallel: true,
AfterRecording: true,
Parallel: true,
Mode: vcr.Replaying,
Version: provider.Beta,
}); err != nil {
Expand Down Expand Up @@ -350,7 +350,7 @@ func execTestTerraformVCR(prNumber, mmCommitSha, buildID, projectID, buildStep,
return nil
}

var addedTestsRegexp = regexp.MustCompile(`(?m)^\+func (Test\w+)\(t \*testing.T\) {`)
var addedTestsRegexp = regexp.MustCompile(`(?m)^\+func (TestAcc\w+)\(t \*testing.T\) {`)

func notRunTests(gaDiff, betaDiff string, result vcr.Result) ([]string, []string) {
fmt.Println("Checking for new acceptance tests that were not run")
Expand Down Expand Up @@ -455,9 +455,9 @@ func runReplaying(runFullVCR bool, services map[string]struct{}, vt *vcr.Tester)

func handlePanics(prNumber, buildID, buildStatusTargetURL, mmCommitSha string, result vcr.Result, mode vcr.Mode, gh GithubClient) (bool, error) {
if len(result.Panics) > 0 {
comment := fmt.Sprintf(`$\textcolor{red}{\textsf{The provider crashed while running the VCR tests in %s mode}}$
$\textcolor{red}{\textsf{Please fix it to complete your PR}}$
View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-%s/artifacts/%s/build-log/%s_test.log)`, mode.Upper(), prNumber, buildID, mode.Lower())
comment := color("red", fmt.Sprintf("The provider crashed while running the VCR tests in %s mode\n", mode.Upper()))
comment += fmt.Sprintf(`Please fix it to complete your PR.
View the [build log](https://storage.cloud.google.com/ci-vcr-logs/beta/refs/heads/auto-pr-%s/artifacts/%s/build-log/%s_test.log)`, prNumber, buildID, mode.Lower())
if err := gh.PostComment(prNumber, comment); err != nil {
return true, fmt.Errorf("error posting comment: %v", err)
}
Expand All @@ -477,6 +477,7 @@ func formatComment(fileName string, tmplText string, data any) (string, error) {
funcs := template.FuncMap{
"join": strings.Join,
"add": func(i, j int) int { return i + j },
"color": color,
}
tmpl, err := template.New(fileName).Funcs(funcs).Parse(tmplText)
if err != nil {
Expand All @@ -491,21 +492,21 @@ func formatComment(fileName string, tmplText string, data any) (string, error) {
}

func formatTestsAnalytics(data analytics) (string, error) {
return formatComment("test_terraform_vcr_test_analytics.tmpl", testsAnalyticsTmplText, data)
return formatComment("test_analytics.tmpl", testsAnalyticsTmplText, data)
}

func formatNonExercisedTests(data nonExercisedTests) (string, error) {
return formatComment("test_terraform_vcr_recording_mode_results.tmpl", nonExercisedTestsTmplText, data)
return formatComment("non_exercised_tests.tmpl", nonExercisedTestsTmplText, data)
}

func formatWithReplayFailedTests(data withReplayFailedTests) (string, error) {
return formatComment("test_terraform_vcr_with_replay_failed_tests.tmpl", withReplayFailedTestsTmplText, data)
return formatComment("with_replay_failed_tests.tmpl", withReplayFailedTestsTmplText, data)
}

func formatWithoutReplayFailedTests(data withoutReplayFailedTests) (string, error) {
return formatComment("test_terraform_vcr_without_replay_failed_tests.tmpl", withoutReplayFailedTestsTmplText, data)
return formatComment("without_replay_failed_tests.tmpl", withoutReplayFailedTestsTmplText, data)
}

func formatRecordReplay(data recordReplay) (string, error) {
return formatComment("test_terraform_vcr_record_replay.tmpl", recordReplayTmplText, data)
return formatComment("record_replay.tmpl", recordReplayTmplText, data)
}
Loading

0 comments on commit dd0a446

Please sign in to comment.