Skip to content

Commit 1384ec6

Browse files
committed
Forward LOCALSTACK_* env variables from host to emulator container
1 parent 7c026f0 commit 1384ec6

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

internal/container/start.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ func Start(ctx context.Context, rt runtime.Runtime, sink output.Sink, opts Start
9696

9797
tel := opts.Telemetry
9898

99+
var hostEnv []string
100+
for _, e := range os.Environ() {
101+
if strings.HasPrefix(e, "CI=") || (strings.HasPrefix(e, "LOCALSTACK_") && !strings.HasPrefix(e, "LOCALSTACK_AUTH_TOKEN=")) {
102+
hostEnv = append(hostEnv, e)
103+
}
104+
}
105+
99106
containers := make([]runtime.ContainerConfig, len(opts.Containers))
100107
for i, c := range opts.Containers {
101108
image, err := c.Image()
@@ -128,10 +135,7 @@ func Start(ctx context.Context, rt runtime.Runtime, sink output.Sink, opts Start
128135
"MAIN_CONTAINER_NAME="+containerName,
129136
)
130137

131-
// Forward CI environment variable if set on the host
132-
if ci := os.Getenv("CI"); ci != "" {
133-
env = append(env, "CI="+ci)
134-
}
138+
env = append(env, hostEnv...)
135139

136140
var binds []runtime.BindMount
137141
if socketPath := rt.SocketPath(); socketPath != "" {

internal/container/start_test.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,21 @@ func TestServicePortRange_ReturnsExpectedPorts(t *testing.T) {
6868
}
6969

7070
func TestForwardCIEnvVariable(t *testing.T) {
71-
originalCI := os.Getenv("CI")
72-
defer os.Setenv("CI", originalCI)
73-
7471
tests := []struct {
75-
name string
76-
ciValue string
77-
wantCI bool
78-
wantCIVals []string
72+
name string
73+
ciValue string
74+
wantCI bool
7975
}{
80-
{"CI=true", "true", true, []string{"true"}},
81-
{"CI=1", "1", true, []string{"1"}},
82-
{"CI=false", "false", true, []string{"false"}},
83-
{"CI unset", "", false, nil},
76+
{"CI=true", "true", true},
77+
{"CI=1", "1", true},
78+
{"CI=false", "false", true},
79+
{"CI unset", "", false},
8480
}
8581

8682
for _, tt := range tests {
8783
t.Run(tt.name, func(t *testing.T) {
88-
os.Setenv("CI", tt.ciValue)
84+
t.Setenv("CI", tt.ciValue)
8985

90-
// Simulate the env building logic from start.go:136-139
9186
env := []string{"LOCALSTACK_AUTH_TOKEN=test"}
9287
if ci := os.Getenv("CI"); ci != "" {
9388
env = append(env, "CI="+ci)

test/integration/start_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func TestStartCommandSetsUpContainerCorrectly(t *testing.T) {
207207
t.Run("http health endpoint", func(t *testing.T) {
208208
resp, err := http.Get("http://localhost.localstack.cloud:4566/_localstack/health")
209209
require.NoError(t, err)
210-
defer resp.Body.Close()
210+
defer func() { _ = resp.Body.Close() }()
211211
assert.Equal(t, http.StatusOK, resp.StatusCode)
212212
})
213213

@@ -221,7 +221,7 @@ func TestStartCommandSetsUpContainerCorrectly(t *testing.T) {
221221
}
222222
resp, err := client.Get("https://localhost.localstack.cloud/_localstack/health")
223223
require.NoError(t, err)
224-
defer resp.Body.Close()
224+
defer func() { _ = resp.Body.Close() }()
225225
assert.Equal(t, http.StatusOK, resp.StatusCode)
226226
})
227227
}

0 commit comments

Comments
 (0)