dashboard: set the correct module proxy for EC2 builders

This change sets the default module proxy address for EC2 builders.
EC2 builders should have the same proxy address set as reverse builders.

Fixes golang/go#40914

Change-Id: I4de2ed178170f60fdbe4e2f9aa6f024bb43ee020
Reviewed-on: https://go-review.googlesource.com/c/build/+/249577
Run-TryBot: Carlos Amedee <carlos@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/dashboard/builders.go b/dashboard/builders.go
index ceb63b5..6d9c39d 100644
--- a/dashboard/builders.go
+++ b/dashboard/builders.go
@@ -860,7 +860,9 @@
 // to append to this builder as a function of the repo being built
 // ("go", "oauth2", "net", etc).
 func (c *BuildConfig) ModulesEnv(repo string) (env []string) {
-	if c.IsReverse() && repo != "go" {
+	// EC2 and reverse builders should set the public module proxy
+	// address instead of the internal proxy.
+	if (c.HostConfig().isEC2 || c.IsReverse()) && repo != "go" {
 		env = append(env, "GOPROXY=https://proxy.golang.org")
 	}
 	switch repo {
diff --git a/dashboard/builders_test.go b/dashboard/builders_test.go
index 8c87c69..a747ab7 100644
--- a/dashboard/builders_test.go
+++ b/dashboard/builders_test.go
@@ -15,6 +15,8 @@
 	"strings"
 	"testing"
 	"time"
+
+	"github.com/google/go-cmp/cmp"
 )
 
 func TestOSARCHAccessors(t *testing.T) {
@@ -925,3 +927,110 @@
 		})
 	}
 }
+
+func TestModulesEnv(t *testing.T) {
+	testCases := []struct {
+		desc        string
+		buildConfig *BuildConfig
+		repo        string
+		want        []string
+	}{
+		{
+			desc: "ec2-builder-repo-non-go",
+			buildConfig: &BuildConfig{
+				testHostConf: &HostConfig{
+					IsReverse: false,
+					isEC2:     true,
+				},
+			},
+			repo: "bar",
+			want: []string{"GOPROXY=https://proxy.golang.org"},
+		},
+		{
+			desc: "reverse-builder-repo-non-go",
+			buildConfig: &BuildConfig{
+				testHostConf: &HostConfig{
+					IsReverse: true,
+					isEC2:     false,
+				},
+			},
+			repo: "bar",
+			want: []string{"GOPROXY=https://proxy.golang.org"},
+		},
+		{
+			desc: "reverse-builder-repo-go",
+			buildConfig: &BuildConfig{
+				testHostConf: &HostConfig{
+					IsReverse: true,
+					isEC2:     false,
+				},
+			},
+			repo: "go",
+			want: []string{"GOPROXY=off"},
+		},
+		{
+			desc: "builder-repo-go",
+			buildConfig: &BuildConfig{
+				testHostConf: &HostConfig{
+					IsReverse: false,
+					isEC2:     false,
+				},
+			},
+			repo: "go",
+			want: []string{"GOPROXY=off"},
+		},
+		{
+			desc: "builder-repo-go-outbound-network-allowed",
+			buildConfig: &BuildConfig{
+				Name: "test-longtest",
+				testHostConf: &HostConfig{
+					IsReverse: false,
+					isEC2:     false,
+				},
+			},
+			repo: "go",
+			want: nil,
+		},
+		{
+			desc: "builder-repo-special-case",
+			buildConfig: &BuildConfig{
+				testHostConf: &HostConfig{
+					IsReverse: false,
+					isEC2:     false,
+				},
+			},
+			repo: "build",
+			want: []string{"GO111MODULE=on"},
+		},
+		{
+			desc: "reverse-builder-repo-special-case",
+			buildConfig: &BuildConfig{
+				testHostConf: &HostConfig{
+					IsReverse: true,
+					isEC2:     false,
+				},
+			},
+			repo: "build",
+			want: []string{"GOPROXY=https://proxy.golang.org", "GO111MODULE=on"},
+		},
+		{
+			desc: "builder-repo-non-special-case",
+			buildConfig: &BuildConfig{
+				testHostConf: &HostConfig{
+					IsReverse: false,
+					isEC2:     false,
+				},
+			},
+			repo: "bar",
+			want: nil,
+		},
+	}
+	for _, tc := range testCases {
+		t.Run(tc.desc, func(t *testing.T) {
+			got := tc.buildConfig.ModulesEnv(tc.repo)
+			if diff := cmp.Diff(tc.want, got); diff != "" {
+				t.Errorf("BuildConfig.ModulesEnv(%q) mismatch (-want, +got)\n%s", tc.repo, diff)
+			}
+		})
+	}
+}