Skip to content

Commit df15df8

Browse files
ekzhanggvisor-bot
authored andcommitted
Add test for workdir being a symlink to another folder
Should only pass after #11910 is merged in. Thanks @avagin for help here! #11910 (comment) FUTURE_COPYBARA_INTEGRATE_REVIEW=#11915 from ekzhang:add-test-symlink-workdir a8cf826 PiperOrigin-RevId: 783271203
1 parent 51ef193 commit df15df8

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

test/e2e/integration_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,70 @@ func TestNonSearchableWorkingDirectory(t *testing.T) {
10091009
}
10101010
}
10111011

1012+
// NOTE(gvisor.dev/issue/11910): Regression test. Make sure that working
1013+
// directory can be set to a symlinked path.
1014+
func TestSymlinkWorkingDirectory(t *testing.T) {
1015+
ctx := context.Background()
1016+
d := dockerutil.MakeContainer(ctx, t)
1017+
defer d.CleanUp(ctx)
1018+
1019+
dir, err := os.MkdirTemp(testutil.TmpDir(), "symlink-workdir")
1020+
if err != nil {
1021+
t.Fatalf("MkdirTemp() failed: %v", err)
1022+
}
1023+
defer os.RemoveAll(dir)
1024+
1025+
// Create the actual target directory
1026+
targetDir := filepath.Join(dir, "actual-dir")
1027+
if err := os.Mkdir(targetDir, 0755); err != nil {
1028+
t.Fatalf("Mkdir() failed: %v", err)
1029+
}
1030+
1031+
// Create a symlink pointing to the target directory
1032+
symlinkPath := filepath.Join(dir, "symlink-to-dir")
1033+
if err := os.Symlink("actual-dir", symlinkPath); err != nil {
1034+
t.Fatalf("Symlink() failed: %v", err)
1035+
}
1036+
1037+
// Mount the temp directory and set working directory to the symlink
1038+
opts := dockerutil.RunOpts{
1039+
Image: "basic/alpine",
1040+
WorkDir: "/test/symlink-to-dir",
1041+
Mounts: []mount.Mount{
1042+
{
1043+
Type: mount.TypeBind,
1044+
Source: dir,
1045+
Target: "/test",
1046+
},
1047+
},
1048+
}
1049+
1050+
// Test that the container can start with symlinked working directory
1051+
// and that pwd returns the symlinked path
1052+
got, err := d.Run(ctx, opts, "sh", "-c", "pwd")
1053+
if err != nil {
1054+
t.Fatalf("docker run failed: %v", err)
1055+
}
1056+
1057+
want := "/test/symlink-to-dir\n"
1058+
if got != want {
1059+
t.Errorf("working directory mismatch, want: %q, got: %q", want, got)
1060+
}
1061+
1062+
// Additional test: verify we can actually use the working directory
1063+
// by creating a file in it
1064+
_, err = d.Run(ctx, opts, "sh", "-c", "echo 'test content' > testfile && cat testfile")
1065+
if err != nil {
1066+
t.Fatalf("docker run failed: %v", err)
1067+
}
1068+
1069+
// Verify the file was created in the actual target directory
1070+
createdFile := filepath.Join(targetDir, "testfile")
1071+
if _, err := os.Stat(createdFile); err != nil {
1072+
t.Errorf("file was not created in target directory: %v", err)
1073+
}
1074+
}
1075+
10121076
func TestCharDevice(t *testing.T) {
10131077
if testutil.IsRunningWithOverlay() {
10141078
t.Skip("files are not available outside the sandbox with overlay.")

0 commit comments

Comments
 (0)