github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/docker_cli_save_load_unix_test.go (about)

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