| @@ -13,7 +13,7 @@ func TestStepEnvDoesNotInherit(t *testing.T) { |
| 13 | 13 | t.Setenv("GITBAY_RUNNER_TOKEN", "a-secret-the-service-was-given") |
| 14 | 14 | t.Setenv("AWS_SECRET_ACCESS_KEY", "also-not-for-builds") |
| 15 | 15 | |
| 16 | | env := stepEnv(job{Repo: "alice/app", SHA: "abc", Ref: "main", Job: "test"}, "/tmp/buildhome") |
| 16 | env := stepEnv(job{Repo: "alice/app", SHA: "abc", Ref: "main", Job: "test"}, "/tmp/buildhome", "git@x.test") |
| 17 | 17 | |
| 18 | 18 | for _, e := range env { |
| 19 | 19 | if strings.HasPrefix(e, "GITBAY_RUNNER_TOKEN=") || strings.HasPrefix(e, "AWS_SECRET_ACCESS_KEY=") { |
| @@ -47,11 +47,11 @@ func TestStepEnvDoesNotInherit(t *testing.T) { |
| 47 | 47 | // Secrets are passed through when the server sent them, which it does |
| 48 | 48 | // only for a trusted build. |
| 49 | 49 | func TestStepEnvCarriesSecrets(t *testing.T) { |
| 50 | | env := stepEnv(job{Secrets: map[string]string{"TOKEN": "s3cret"}}, "/tmp/buildhome") |
| 50 | env := stepEnv(job{Secrets: map[string]string{"TOKEN": "s3cret"}}, "/tmp/buildhome", "git@x.test") |
| 51 | 51 | if !containsEnv(env, "TOKEN=s3cret") { |
| 52 | 52 | t.Error("a trusted build's secret did not reach the step") |
| 53 | 53 | } |
| 54 | | env = stepEnv(job{}, "/tmp/buildhome") |
| 54 | env = stepEnv(job{}, "/tmp/buildhome", "git@x.test") |
| 55 | 55 | for _, e := range env { |
| 56 | 56 | if strings.HasPrefix(e, "TOKEN=") { |
| 57 | 57 | t.Errorf("a secret appeared with none sent: %q", e) |
| @@ -64,7 +64,7 @@ func TestStepEnvPathFallback(t *testing.T) { |
| 64 | 64 | old := os.Getenv("PATH") |
| 65 | 65 | os.Unsetenv("PATH") |
| 66 | 66 | defer os.Setenv("PATH", old) |
| 67 | | if env := stepEnv(job{}, "/tmp/buildhome"); !containsEnv(env, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") { |
| 67 | if env := stepEnv(job{}, "/tmp/buildhome", "git@x.test"); !containsEnv(env, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") { |
| 68 | 68 | t.Errorf("no PATH fallback: %v", env) |
| 69 | 69 | } |
| 70 | 70 | } |
| @@ -82,7 +82,7 @@ func containsEnv(env []string, want string) bool { |
| 82 | 82 | // which run() removes when the build ends, so every build re-downloaded |
| 83 | 83 | // the Go module cache and the ~50MB sonar scanner. |
| 84 | 84 | func TestStepEnvHomeIsNotTheWorkspace(t *testing.T) { |
| 85 | | env := stepEnv(job{ID: 7}, "/var/lib/gitbay-runner/work/home") |
| 85 | env := stepEnv(job{ID: 7}, "/var/lib/gitbay-runner/work/home", "git@x.test") |
| 86 | 86 | for _, e := range env { |
| 87 | 87 | if strings.HasPrefix(e, "HOME=") && strings.Contains(e, "build-7") { |
| 88 | 88 | t.Errorf("HOME is the per-build workspace, which is deleted after the build: %q", e) |
| @@ -186,3 +186,27 @@ func TestSplitEnvKeepsMultilineOutOfTheFile(t *testing.T) { |
| 186 | 186 | } |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | |
| 190 | // A build that talks back to the instance — releases, comments — needs an |
| 191 | // address that works from where it runs. GITBAY_SSH carries the runner's |
| 192 | // remote; under podman a loopback remote is rewritten to the address at |
| 193 | // which pasta exposes the host, since the host's own addresses belong to |
| 194 | // the container inside it. |
| 195 | func TestStepEnvCarriesInstanceAddress(t *testing.T) { |
| 196 | env := stepEnv(job{}, "/tmp/buildhome", "git@gitbay.org") |
| 197 | if !containsEnv(env, "GITBAY_SSH=git@gitbay.org") { |
| 198 | t.Errorf("GITBAY_SSH missing: %q", env) |
| 199 | } |
| 200 | for _, tc := range []struct{ remote, isolation, want string }{ |
| 201 | {"git@127.0.0.1", isolationNone, "git@127.0.0.1"}, |
| 202 | {"git@127.0.0.1", isolationPodman, "git@169.254.1.2"}, |
| 203 | {"git@localhost", isolationPodman, "git@169.254.1.2"}, |
| 204 | {"git@gitbay.org", isolationPodman, "git@gitbay.org"}, |
| 205 | {"gitbay.org", isolationPodman, "gitbay.org"}, |
| 206 | } { |
| 207 | r := &runner{remote: tc.remote, isolation: tc.isolation} |
| 208 | if got := r.buildSSH(); got != tc.want { |
| 209 | t.Errorf("remote %s under %s: got %s want %s", tc.remote, tc.isolation, got, tc.want) |
| 210 | } |
| 211 | } |
| 212 | } |