microsoft / microsoft/aspire

PostgreSQL persistent lifetime not working when setting --environment in Integration test

Open
#9,093 3 comments 0 reactions 0 assignees View on GitHub
area-app-testing
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

I've written integration tests using `DistributedApplicationTestingBuilder`. To shorten the test run, I use Persistent lifetime for my PostgreSQL container. I also set the environment to 'Testing' so I can detect that it's a test run in my AppHost:

```csharp
var appHost = await DistributedApplicationTestingBuilder.CreateAsync([
"--environment=Testing",
], cancellationToken);
```

When running the test, the PostgreSQL container is restarted each time.

If I remove the environment parameter, the PostgreSQL doesn't restart each time and works as expected:

```csharp
var appHost = await DistributedApplicationTestingBuilder.CreateAsync(cancellationToken);
```

I can also set other parameters without breaking the Persistent Lifetime functionality. It's only when settings the `--environment` parameter that the container restarts each time.

UPDATE: The PostgreSQL container doesn't restart when I explicitly pass in a password parameter.

### Expected Behavior

I expect the PostgreSQL container not to restart, independent of whether or not I'm setting the environment.

### Steps To Reproduce

- Create an Aspire Starter app (aspire-starter template)
- Add a test project (aspire-xunit template)
- Add the `Aspire.Hosting.PostgreSQL` package to the AppHost
- Add the PostgreSQL container:
```csharp
var postgres = builder.AddPostgres("postgres")
.WithLifetime(ContainerLifetime.Persistent);
```
- Add the environment parameter:
```csharp
var appHost = await DistributedApplicationTestingBuilder.CreateAsync([
"--environment=Testing",
], cancellationToken);
```
- Run the test a couple of times. Each time, the PostgreSQL container will restart (I monitor this in the Docker Desktop dashboard)
- Remove the `--environment` parameter
- Run the test a couple of times again. The PostgreSQL container will stay alive and not restart.

### Exceptions (if any)

_No response_

### .NET Version info

.NET version 9.0.100

### Anything else?

.NET Aspire version 9.2.1

Contributor guide

Open the contributing guide

Research direction

Start with DistributedApplicationTestingBuilder and the ContainerLifetime.Persistent configuration described in the reproduction. Compare repeated test runs with and without --environment=Testing, and verify that the PostgreSQL container remains running in both cases when the bug is fixed.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, docker, postgresql
Domain
databases, devops, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.