Skip to content

Commit 3cc5d62

Browse files
committed
run getent with a noop stdin
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 1f9eb9a commit 3cc5d62

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

integration/container/copy_test.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,34 @@ func TestCopyToContainerPathDoesNotExist(t *testing.T) {
6565
func TestCopyEmptyFile(t *testing.T) {
6666
defer setupTest(t)()
6767

68+
ctx := context.Background()
69+
apiclient := testEnv.APIClient()
70+
cid := container.Create(ctx, t, apiclient)
71+
72+
// empty content
73+
dstDir, _ := makeEmptyArchive(t)
74+
err := apiclient.CopyToContainer(ctx, cid, dstDir, bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
75+
assert.NilError(t, err)
76+
77+
// tar with empty file
78+
dstDir, preparedArchive := makeEmptyArchive(t)
79+
err = apiclient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{})
80+
assert.NilError(t, err)
81+
82+
// tar with empty file archive mode
83+
dstDir, preparedArchive = makeEmptyArchive(t)
84+
err = apiclient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{
85+
CopyUIDGID: true,
86+
})
87+
assert.NilError(t, err)
88+
89+
// copy from empty file
90+
rdr, _, err := apiclient.CopyFromContainer(ctx, cid, dstDir)
91+
assert.NilError(t, err)
92+
defer rdr.Close()
93+
}
94+
95+
func makeEmptyArchive(t *testing.T) (string, io.ReadCloser) {
6896
tmpDir := t.TempDir()
6997
srcPath := filepath.Join(tmpDir, "empty-file.txt")
7098
err := os.WriteFile(srcPath, []byte(""), 0400)
@@ -77,30 +105,18 @@ func TestCopyEmptyFile(t *testing.T) {
77105

78106
srcArchive, err := archive.TarResource(srcInfo)
79107
assert.NilError(t, err)
80-
defer srcArchive.Close()
108+
t.Cleanup(func() {
109+
srcArchive.Close()
110+
})
81111

82112
ctrPath := "/empty-file.txt"
83113
dstInfo := archive.CopyInfo{Path: ctrPath}
84114
dstDir, preparedArchive, err := archive.PrepareArchiveCopy(srcArchive, srcInfo, dstInfo)
85115
assert.NilError(t, err)
86-
defer preparedArchive.Close()
87-
88-
ctx := context.Background()
89-
apiclient := testEnv.APIClient()
90-
cid := container.Create(ctx, t, apiclient)
91-
92-
// empty content
93-
err = apiclient.CopyToContainer(ctx, cid, dstDir, bytes.NewReader([]byte("")), types.CopyToContainerOptions{})
94-
assert.NilError(t, err)
95-
96-
// tar with empty file
97-
err = apiclient.CopyToContainer(ctx, cid, dstDir, preparedArchive, types.CopyToContainerOptions{})
98-
assert.NilError(t, err)
99-
100-
// copy from empty file
101-
rdr, _, err := apiclient.CopyFromContainer(ctx, cid, dstDir)
102-
assert.NilError(t, err)
103-
defer rdr.Close()
116+
t.Cleanup(func() {
117+
preparedArchive.Close()
118+
})
119+
return dstDir, preparedArchive
104120
}
105121

106122
func TestCopyToContainerPathIsNotDir(t *testing.T) {

pkg/idtools/idtools_unix.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ func callGetent(database, key string) (io.Reader, error) {
166166
if getentCmd == "" {
167167
return nil, fmt.Errorf("unable to find getent command")
168168
}
169-
out, err := exec.Command(getentCmd, database, key).CombinedOutput()
169+
command := exec.Command(getentCmd, database, key)
170+
// we run getent within container filesystem, but without /dev so /dev/null is not available for exec to mock stdin
171+
command.Stdin = io.NopCloser(bytes.NewReader(nil))
172+
out, err := command.CombinedOutput()
170173
if err != nil {
171174
exitCode, errC := getExitCode(err)
172175
if errC != nil {

0 commit comments

Comments
 (0)