github.com/lacework-dev/go-moby@v20.10.12+incompatible/integration-cli/docker_cli_save_load_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"os"
    10  	"os/exec"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/creack/pty"
    16  	"github.com/docker/docker/integration-cli/cli/build"
    17  	"gotest.tools/v3/assert"
    18  	"gotest.tools/v3/icmd"
    19  )
    20  
    21  // save a repo and try to load it using stdout
    22  func (s *DockerSuite) TestSaveAndLoadRepoStdout(c *testing.T) {
    23  	name := "test-save-and-load-repo-stdout"
    24  	dockerCmd(c, "run", "--name", name, "busybox", "true")
    25  
    26  	repoName := "foobar-save-load-test"
    27  	before, _ := dockerCmd(c, "commit", name, repoName)
    28  	before = strings.TrimRight(before, "\n")
    29  
    30  	tmpFile, err := ioutil.TempFile("", "foobar-save-load-test.tar")
    31  	assert.NilError(c, err)
    32  	defer os.Remove(tmpFile.Name())
    33  
    34  	icmd.RunCmd(icmd.Cmd{
    35  		Command: []string{dockerBinary, "save", repoName},
    36  		Stdout:  tmpFile,
    37  	}).Assert(c, icmd.Success)
    38  
    39  	tmpFile, err = os.Open(tmpFile.Name())
    40  	assert.NilError(c, err)
    41  	defer tmpFile.Close()
    42  
    43  	deleteImages(repoName)
    44  
    45  	icmd.RunCmd(icmd.Cmd{
    46  		Command: []string{dockerBinary, "load"},
    47  		Stdin:   tmpFile,
    48  	}).Assert(c, icmd.Success)
    49  
    50  	after := inspectField(c, repoName, "Id")
    51  	after = strings.TrimRight(after, "\n")
    52  
    53  	assert.Equal(c, after, before, "inspect is not the same after a save / load")
    54  
    55  	deleteImages(repoName)
    56  
    57  	pty, tty, err := pty.Open()
    58  	assert.NilError(c, err)
    59  	cmd := exec.Command(dockerBinary, "save", repoName)
    60  	cmd.Stdin = tty
    61  	cmd.Stdout = tty
    62  	cmd.Stderr = tty
    63  	assert.NilError(c, cmd.Start())
    64  	assert.ErrorContains(c, cmd.Wait(), "", "did not break writing to a TTY")
    65  
    66  	buf := make([]byte, 1024)
    67  
    68  	n, err := pty.Read(buf)
    69  	assert.NilError(c, err, "could not read tty output")
    70  	assert.Assert(c, strings.Contains(string(buf[:n]), "cowardly refusing"), "help output is not being yielded")
    71  }
    72  
    73  func (s *DockerSuite) TestSaveAndLoadWithProgressBar(c *testing.T) {
    74  	name := "test-load"
    75  	buildImageSuccessfully(c, name, build.WithDockerfile(`FROM busybox
    76  	RUN touch aa
    77  	`))
    78  
    79  	tmptar := name + ".tar"
    80  	dockerCmd(c, "save", "-o", tmptar, name)
    81  	defer os.Remove(tmptar)
    82  
    83  	dockerCmd(c, "rmi", name)
    84  	dockerCmd(c, "tag", "busybox", name)
    85  	out, _ := dockerCmd(c, "load", "-i", tmptar)
    86  	expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name)
    87  	assert.Assert(c, strings.Contains(out, expected))
    88  }
    89  
    90  // fail because load didn't receive data from stdin
    91  func (s *DockerSuite) TestLoadNoStdinFail(c *testing.T) {
    92  	pty, tty, err := pty.Open()
    93  	assert.NilError(c, err)
    94  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    95  	defer cancel()
    96  	cmd := exec.CommandContext(ctx, dockerBinary, "load")
    97  	cmd.Stdin = tty
    98  	cmd.Stdout = tty
    99  	cmd.Stderr = tty
   100  	assert.ErrorContains(c, cmd.Run(), "", "docker-load should fail")
   101  
   102  	buf := make([]byte, 1024)
   103  
   104  	n, err := pty.Read(buf)
   105  	assert.NilError(c, err) //could not read tty output
   106  	assert.Assert(c, strings.Contains(string(buf[:n]), "requested load from stdin, but stdin is empty"))
   107  }