Skip to content

Fix nerdctl load stdin for pipes. #1405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/nerdctl/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func loadAction(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
if stdinStat.Size() == 0 {
if stdinStat.Size() == 0 && (stdinStat.Mode()&os.ModeNamedPipe) == 0 {
return errors.New("stdin is empty and input flag is not specified")
}
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func loadImage(in io.Reader, cmd *cobra.Command, platMC platforms.MatchComparer,

// TODO: Show unpack status
if !quiet {
fmt.Fprintf(cmd.OutOrStdout(), "unpacking %s (%s)...", img.Name, img.Target.Digest)
fmt.Fprintf(cmd.OutOrStdout(), "unpacking %s (%s)...\n", img.Name, img.Target.Digest)
}
err = image.Unpack(ctx, sn)
if err != nil {
Expand All @@ -132,7 +132,7 @@ func loadImage(in io.Reader, cmd *cobra.Command, platMC platforms.MatchComparer,
if quiet {
fmt.Fprintln(cmd.OutOrStdout(), img.Target.Digest)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "done\n")
fmt.Fprintf(cmd.OutOrStdout(), "Loaded image: %s", img.Name)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes the test docker compatible.

}
}

Expand Down
55 changes: 55 additions & 0 deletions cmd/nerdctl/load_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/containerd/nerdctl/pkg/testutil"
"gotest.tools/v3/assert"
)

func TestLoadStdinFromPipe(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)

tmp := t.TempDir()
base.Cmd("pull", testutil.CommonImage).AssertOK()
base.Cmd("save", testutil.CommonImage, "-o", filepath.Join(tmp, "common.tar")).AssertOK()

loadCmd := strings.Join(base.Cmd("load").Command, " ")
output := filepath.Join(tmp, "output")

combined, err := exec.Command("sh", "-euxc", fmt.Sprintf("`cat %s/common.tar | %s > %s`", tmp, loadCmd, output)).CombinedOutput()
assert.NilError(t, err, "failed with error %s and combined output is %s", err, string(combined))
fb, err := os.ReadFile(output)
assert.NilError(t, err)

assert.Assert(t, strings.Contains(string(fb), fmt.Sprintf("Loaded image: %s", testutil.CommonImage)))
base.Cmd("images").AssertOutContains(strings.Split(testutil.CommonImage, ":")[0])
}

func TestLoadStdinEmpty(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
base.Cmd("load").AssertFail()
}