Skip to content

Forward host environment variables to the emulator container#161

Open
gtsiolis wants to merge 1 commit intomainfrom
george/des-184
Open

Forward host environment variables to the emulator container#161
gtsiolis wants to merge 1 commit intomainfrom
george/des-184

Conversation

@gtsiolis
Copy link
Member

Forwards two sets of host environment variables into the LocalStack container at startup, matching the behaviour of the legacy CLI:

  • CI: ensures LocalStack behaves correctly in CI environments.
  • LOCALSTACK_*: forwards all LocalStack configuration variables set on the host. LOCALSTACK_AUTH_TOKEN is excluded since it is already explicitly injected.

@coderabbitai
Copy link

coderabbitai bot commented Mar 23, 2026

Warning

Rate limit exceeded

@gtsiolis has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 14 minutes and 32 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: b2fca232-5648-49b5-9927-bf1caaf8cf83

📥 Commits

Reviewing files that changed from the base of the PR and between 1384ec6 and 2d8e0ec.

📒 Files selected for processing (3)
  • internal/container/start.go
  • internal/container/start_test.go
  • test/integration/start_test.go
📝 Walkthrough

Walkthrough

Host environment variables with prefix CI= or LOCALSTACK_ (except LOCALSTACK_AUTH_TOKEN=) are collected from os.Environ() and appended to each container's environment during Start. Tests added/updated to verify CI forwarding and ensure response bodies are closed via a deferred noop close.

Changes

Cohort / File(s) Summary
Environment Variable Forwarding
internal/container/start.go, internal/container/start_test.go
Collects host env entries matching CI= and LOCALSTACK_ (excluding LOCALSTACK_AUTH_TOKEN=) and appends them to each container's computed env during Start. Adds TestForwardCIEnvVariable to validate forwarding behavior.
Integration test cleanup
test/integration/start_test.go
Replaced direct defer resp.Body.Close() with defer func() { _ = resp.Body.Close() }() in HTTP/HTTPS subtests to explicitly ignore Close errors after assertions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • carole-lavillonniere
  • anisaoshafi
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: forwarding host environment variables to the container, which matches the primary purpose of the changeset.
Description check ✅ Passed The description accurately explains the changeset: it documents the forwarding of CI and LOCALSTACK_* variables to match legacy CLI behavior, which directly relates to the code changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch george/des-184

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/container/start_test.go`:
- Line 72: Replace the unchecked defer os.Setenv("CI", originalCI) with a
t.Cleanup that restores the env and handles errors: inside the test (in
start_test.go) register t.Cleanup(func() { if err := os.Setenv("CI",
originalCI); err != nil { t.Fatalf("failed to restore CI env: %v", err) } }) so
the environment is restored reliably and the error from os.Setenv is not
ignored; alternatively, if available use t.Setenv to manage the variable
automatically.
- Around line 85-103: The test is rebuilding the env-forwarding logic instead of
exercising the real implementation and also ignores the os.Setenv error; extract
the host environment collection into a testable function (e.g., collectHostEnv
in start.go) that iterates os.Environ() and filters entries with
strings.HasPrefix(e, "CI=") or the LOCALSTACK_ rules, update Start to call
collectHostEnv, then rewrite the test to call collectHostEnv directly (in
start_test.go) and assert presence/absence of "CI=" using strings.HasPrefix
checks; also handle os.Setenv/os.Unsetenv return values (check and/or use
t.Cleanup and os.LookupEnv to restore original state) so no env-setting errors
are ignored.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 892049f8-b49a-4036-8d20-0d575e08580c

📥 Commits

Reviewing files that changed from the base of the PR and between 87a0335 and d784aba.

📒 Files selected for processing (2)
  • internal/container/start.go
  • internal/container/start_test.go

defer func() { _ = resp.Body.Close() }()
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: can we cover the new functionality with an integration test? It could set CI and another LOCALSTACK_* env var, and make sure it's passed to the container. In case we change the value of CI, let's reset it to its original value after the test is done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made an attempt to resolve in 2d8e0ec.

require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: why are we removing all these subtests?

  • docker socket mount
  • service port range
  • main port
  • https port
  • volume mount
  • http health endpoint
  • https health endpoint

assert.Equal(t, tt.expected, got)
})
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I don't see the point of these unit tests. They are duplicating the logic of the code under test. I think we can delete and rely solely on the new integration tests.

Comment on lines +171 to +173
t.Setenv("CI", "true")
t.Setenv("LOCALSTACK_DISABLE_EVENTS", "1")
t.Setenv("LOCALSTACK_AUTH_TOKEN", "host-token")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: we should not be setting env vars like this, instead when running the lstk command:

  _, stderr, err := runLstk(t, ctx, "",
      env.With(env.APIEndpoint, mockServer.URL).
          With(env.CI, "true").
          With(env.DisableEvents, "1").
          With(env.AuthToken, "host-token"),
      "start")

With env.With we inject the env vars explicitly into the subprocess env without mutating the test process env, it is cleaner and consistent with how other tests work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants