|
| 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), fmt.Sprintf("Loaded image: %s", testutil.CommonImage))) |
| 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