Skip to content

Commit

Permalink
fix(sdk): wing test fails if unable to clean up files (#6320)
Browse files Browse the repository at this point in the history
If a container mounts a volume to the state directory as described in #6295, it can sometimes result in permission errors when the CLI is cleaning up files after `wing test` because the mounted directory may have different permissions.

```
EACCES: permission denied, scandir 'target/test/mount.test.wsim/.state/c892787d9270cdccc06c34c95f7f9738be92b19134'
```

This error only seems to happen on CI for me. I'm not sure of the cause but I'm guessing that in CI, docker runs in a different (rootless maybe?) mode than on macOS.

Unblocks winglang/winglibs#207

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
Chriscbr authored Apr 25, 2024
1 parent 263831b commit e188127
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
6 changes: 5 additions & 1 deletion apps/wing/src/commands/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,11 @@ async function testSimulator(synthDir: string, options: TestOptions) {
}

if (clean) {
rmSync(synthDir, { recursive: true, force: true });
try {
rmSync(synthDir, { recursive: true, force: true });
} catch (err) {
console.warn(`Warning: unable to clean up test directory: ${err}`);
}
} else {
noCleanUp(synthDir);
}
Expand Down
21 changes: 21 additions & 0 deletions examples/tests/sdk_tests/container/mount.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
bring sim;
bring util;

// This test was added to check that "wing test" still works when sim.Container is mounted to the state directory

// only relevant in simulator
if util.env("WING_TARGET") == "sim" {
let container = new sim.Container(
name: "postgres",
image: "postgres:15",
env: {
POSTGRES_PASSWORD: "password"
},
containerPort: 5432,
volumes: ["$WING_STATE_DIR:/var/lib/postgresql/data"],
);

test "my test" {
log("dummy test");
}
}
2 changes: 1 addition & 1 deletion libs/wingsdk/src/simulator/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { access, constants } from "node:fs";
import { promisify } from "node:util";

/**
* Check if a file exists for an specific path
* Check if a file exists for a specific path
* @param filePath
* @Returns Return `true` if the file exists, `false` otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# [mount.test.w](../../../../../../examples/tests/sdk_tests/container/mount.test.w) | compile | tf-aws

## main.tf.json
```json
{
"//": {
"metadata": {
"backend": "local",
"stackName": "root",
"version": "0.20.3"
},
"outputs": {}
},
"provider": {
"aws": [
{}
]
}
}
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# [mount.test.w](../../../../../../examples/tests/sdk_tests/container/mount.test.w) | test | sim

## stderr.log
```log
Warning: unable to clean up test directory: Error: EACCES: permission denied, scandir 'target/test/mount.test.wsim/.state/<STATE_FILE>'
```

## stdout.log
```log
[INFO] my test | dummy test
pass ─ mount.test.wsim » root/env0/test:my test
Tests 1 passed (1)
Snapshots 1 skipped
Test Files 1 passed (1)
Duration <DURATION>
```

0 comments on commit e188127

Please sign in to comment.