Skip to content

Commit b4ea021

Browse files
committed
Fix nerdctl load stdin for pipes.
If stdin is received from a pipe, the size is still zero and an error will not be not thrown now. If stdin is empty, error will still be thrown. Added tests Signed-off-by: Manu Gupta <[email protected]>
1 parent e83e18b commit b4ea021

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

cmd/nerdctl/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func loadAction(cmd *cobra.Command, _ []string) error {
7373
if err != nil {
7474
return err
7575
}
76-
if stdinStat.Size() == 0 {
76+
if stdinStat.Size() == 0 && (stdinStat.Mode()&os.ModeNamedPipe) == 0 {
7777
return errors.New("stdin is empty and input flag is not specified")
7878
}
7979
}

cmd/nerdctl/load_linux_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"os/exec"
23+
"path/filepath"
24+
"strings"
25+
"testing"
26+
27+
"github.com/containerd/nerdctl/pkg/testutil"
28+
"gotest.tools/v3/assert"
29+
)
30+
31+
func TestLoadStdinFromPipe(t *testing.T) {
32+
t.Parallel()
33+
base := testutil.NewBase(t)
34+
35+
tmp := t.TempDir()
36+
base.Cmd("pull", testutil.CommonImage).AssertOK()
37+
base.Cmd("save", testutil.CommonImage, "-o", filepath.Join(tmp, "common.tar")).AssertOK()
38+
39+
cmdStr := strings.Join(base.Cmd("load").Command, " ")
40+
41+
f, err := os.OpenFile(filepath.Join(tmp, "load.sh"), os.O_CREATE|os.O_RDWR, 0777)
42+
assert.NilError(t, err)
43+
44+
f.WriteString("#/bin/sh\n")
45+
f.WriteString("set -euo pipefail\n")
46+
f.WriteString(fmt.Sprintf("cat %s/common.tar | %s", tmp, cmdStr))
47+
err = f.Close()
48+
assert.NilError(t, err)
49+
50+
cmd := exec.Command("sh", "-euxc", filepath.Join(tmp, "load.sh"))
51+
combined, err := cmd.CombinedOutput()
52+
assert.NilError(t, err, "%s; %s", string(combined), cmd.String())
53+
assert.Assert(t, strings.Contains(string(combined), "unpacking"))
54+
base.Cmd("images").AssertOutContains(strings.Split(testutil.CommonImage, ":")[0])
55+
}
56+
57+
func TestLoadStdinEmpty(t *testing.T) {
58+
t.Parallel()
59+
base := testutil.NewBase(t)
60+
base.Cmd("load").AssertFail()
61+
}

0 commit comments

Comments
 (0)