aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.61k stars 1.01k forks source link

Environment variables from per-lambda config doesn't correctly update environment across tests #2047

Open jwhite3 opened 1 year ago

jwhite3 commented 1 year ago

We've been experimenting with chalice and generally have been pretty pleased, but we've run into an issue while writing tests with pytest.

Our chalice app contains multiple lambda functions with per-lambda environment variables:

{
  "version": "2.0",
  "app_name": "my-app",
  "stages": {
    "dev": {
      "lambda_functions": {
        "func1": {
          "environment_variables": {
            "var1": "something",
            "var2": "something else",
          }
        },
        "func2": {
          "environment_variables": {
            "env1": "example",
            "env2": "other example"
          }
        }
      }
    }
  }
}

With our tests, we use a fixture for handling creating/tearing down a test client:

@pytest.fixture
def client() -> Client:
    with Client(app) as test_client:
        yield test_client

Which theoretically should configure the environment variables defined in the config file when we call invoke.

What seems to be happening is that if we run our lambda function tests individually, the correct environment variables seem to be assigned, but if we run both of our lambda function tests in the same test session, only the first set of environment variables are configured (from the test that was invoked first) in both.

I haven't quite figured out if something is not being reset properly between tests, or if there's a bug in setting up the environment from the config in the case of per-lambda environment variables with the test client.

The issue is resolved if we monkeypatch the environment with the necessary variables.