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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/Prakhar-Agarwal-byte/moby/api/types/versions"
     9  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/cli"
    10  	"gotest.tools/v3/assert"
    11  	"gotest.tools/v3/skip"
    12  )
    13  
    14  type DockerCLICommitSuite struct {
    15  	ds *DockerSuite
    16  }
    17  
    18  func (s *DockerCLICommitSuite) TearDownTest(ctx context.Context, c *testing.T) {
    19  	s.ds.TearDownTest(ctx, c)
    20  }
    21  
    22  func (s *DockerCLICommitSuite) OnTimeout(c *testing.T) {
    23  	s.ds.OnTimeout(c)
    24  }
    25  
    26  func (s *DockerCLICommitSuite) TestCommitAfterContainerIsDone(c *testing.T) {
    27  	skip.If(c, RuntimeIsWindowsContainerd(), "FIXME: Broken on Windows + containerd combination")
    28  	out := cli.DockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo").Combined()
    29  
    30  	cleanedContainerID := strings.TrimSpace(out)
    31  
    32  	cli.DockerCmd(c, "wait", cleanedContainerID)
    33  
    34  	out = cli.DockerCmd(c, "commit", cleanedContainerID).Combined()
    35  
    36  	cleanedImageID := strings.TrimSpace(out)
    37  
    38  	cli.DockerCmd(c, "inspect", cleanedImageID)
    39  }
    40  
    41  func (s *DockerCLICommitSuite) TestCommitWithoutPause(c *testing.T) {
    42  	testRequires(c, DaemonIsLinux)
    43  	out := cli.DockerCmd(c, "run", "-i", "-a", "stdin", "busybox", "echo", "foo").Combined()
    44  
    45  	cleanedContainerID := strings.TrimSpace(out)
    46  
    47  	cli.DockerCmd(c, "wait", cleanedContainerID)
    48  
    49  	out = cli.DockerCmd(c, "commit", "-p=false", cleanedContainerID).Combined()
    50  
    51  	cleanedImageID := strings.TrimSpace(out)
    52  
    53  	cli.DockerCmd(c, "inspect", cleanedImageID)
    54  }
    55  
    56  // TestCommitPausedContainer tests that a paused container is not unpaused after being committed
    57  func (s *DockerCLICommitSuite) TestCommitPausedContainer(c *testing.T) {
    58  	testRequires(c, DaemonIsLinux)
    59  	containerID := cli.DockerCmd(c, "run", "-i", "-d", "busybox").Stdout()
    60  	containerID = strings.TrimSpace(containerID)
    61  
    62  	cli.DockerCmd(c, "pause", containerID)
    63  	cli.DockerCmd(c, "commit", containerID)
    64  
    65  	out := inspectField(c, containerID, "State.Paused")
    66  	// commit should not unpause a paused container
    67  	assert.Assert(c, strings.Contains(out, "true"))
    68  }
    69  
    70  func (s *DockerCLICommitSuite) TestCommitNewFile(c *testing.T) {
    71  	cli.DockerCmd(c, "run", "--name", "committer", "busybox", "/bin/sh", "-c", "echo koye > /foo")
    72  
    73  	imageID := cli.DockerCmd(c, "commit", "committer").Stdout()
    74  	imageID = strings.TrimSpace(imageID)
    75  
    76  	out := cli.DockerCmd(c, "run", imageID, "cat", "/foo").Combined()
    77  	actual := strings.TrimSpace(out)
    78  	assert.Equal(c, actual, "koye")
    79  }
    80  
    81  func (s *DockerCLICommitSuite) TestCommitHardlink(c *testing.T) {
    82  	testRequires(c, DaemonIsLinux)
    83  	firstOutput := cli.DockerCmd(c, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2").Combined()
    84  
    85  	chunks := strings.Split(strings.TrimSpace(firstOutput), " ")
    86  	inode := chunks[0]
    87  	chunks = strings.SplitAfterN(strings.TrimSpace(firstOutput), " ", 2)
    88  	assert.Assert(c, strings.Contains(chunks[1], chunks[0]), "Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
    89  	imageID := cli.DockerCmd(c, "commit", "hardlinks", "hardlinks").Stdout()
    90  	imageID = strings.TrimSpace(imageID)
    91  
    92  	secondOutput := cli.DockerCmd(c, "run", "-t", imageID, "ls", "-di", "file1", "file2").Combined()
    93  
    94  	chunks = strings.Split(strings.TrimSpace(secondOutput), " ")
    95  	inode = chunks[0]
    96  	chunks = strings.SplitAfterN(strings.TrimSpace(secondOutput), " ", 2)
    97  	assert.Assert(c, strings.Contains(chunks[1], chunks[0]), "Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
    98  }
    99  
   100  func (s *DockerCLICommitSuite) TestCommitTTY(c *testing.T) {
   101  	cli.DockerCmd(c, "run", "-t", "--name", "tty", "busybox", "/bin/ls")
   102  
   103  	imageID := cli.DockerCmd(c, "commit", "tty", "ttytest").Stdout()
   104  	imageID = strings.TrimSpace(imageID)
   105  
   106  	cli.DockerCmd(c, "run", imageID, "/bin/ls")
   107  }
   108  
   109  func (s *DockerCLICommitSuite) TestCommitWithHostBindMount(c *testing.T) {
   110  	testRequires(c, DaemonIsLinux)
   111  	cli.DockerCmd(c, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
   112  
   113  	imageID := cli.DockerCmd(c, "commit", "bind-commit", "bindtest").Stdout()
   114  	imageID = strings.TrimSpace(imageID)
   115  
   116  	cli.DockerCmd(c, "run", imageID, "true")
   117  }
   118  
   119  func (s *DockerCLICommitSuite) TestCommitChange(c *testing.T) {
   120  	cli.DockerCmd(c, "run", "--name", "test", "busybox", "true")
   121  
   122  	imageID := cli.DockerCmd(c, "commit",
   123  		"--change", `EXPOSE 8080`,
   124  		"--change", `ENV DEBUG true`,
   125  		"--change", `ENV test 1`,
   126  		"--change", `ENV PATH /foo`,
   127  		"--change", `LABEL foo bar`,
   128  		"--change", `CMD ["/bin/sh"]`,
   129  		"--change", `WORKDIR /opt`,
   130  		"--change", `ENTRYPOINT ["/bin/sh"]`,
   131  		"--change", `USER testuser`,
   132  		"--change", `VOLUME /var/lib/docker`,
   133  		"--change", `ONBUILD /usr/local/bin/python-build --dir /app/src`,
   134  		"test", "test-commit",
   135  	).Stdout()
   136  	imageID = strings.TrimSpace(imageID)
   137  
   138  	expectedEnv := "[DEBUG=true test=1 PATH=/foo]"
   139  	// bug fixed in 1.36, add min APi >= 1.36 requirement
   140  	// PR record https://github.com/moby/moby/pull/35582
   141  	if versions.GreaterThan(testEnv.DaemonAPIVersion(), "1.35") && testEnv.DaemonInfo.OSType != "windows" {
   142  		// The ordering here is due to `PATH` being overridden from the container's
   143  		// ENV.  On windows, the container doesn't have a `PATH` ENV variable so
   144  		// the ordering is the same as the cli.
   145  		expectedEnv = "[PATH=/foo DEBUG=true test=1]"
   146  	}
   147  
   148  	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
   149  	prefix = strings.ToUpper(prefix) // Force C: as that's how WORKDIR is normalized on Windows
   150  	expected := map[string]string{
   151  		"Config.ExposedPorts": "map[8080/tcp:{}]",
   152  		"Config.Env":          expectedEnv,
   153  		"Config.Labels":       "map[foo:bar]",
   154  		"Config.Cmd":          "[/bin/sh]",
   155  		"Config.WorkingDir":   prefix + slash + "opt",
   156  		"Config.Entrypoint":   "[/bin/sh]",
   157  		"Config.User":         "testuser",
   158  		"Config.Volumes":      "map[/var/lib/docker:{}]",
   159  		"Config.OnBuild":      "[/usr/local/bin/python-build --dir /app/src]",
   160  	}
   161  
   162  	for conf, value := range expected {
   163  		res := inspectField(c, imageID, conf)
   164  		if res != value {
   165  			c.Errorf("%s('%s'), expected %s", conf, res, value)
   166  		}
   167  	}
   168  }
   169  
   170  func (s *DockerCLICommitSuite) TestCommitChangeLabels(c *testing.T) {
   171  	cli.DockerCmd(c, "run", "--name", "test", "--label", "some=label", "busybox", "true")
   172  
   173  	imageID := cli.DockerCmd(c, "commit", "--change", "LABEL some=label2", "test", "test-commit").Stdout()
   174  	imageID = strings.TrimSpace(imageID)
   175  
   176  	assert.Equal(c, inspectField(c, imageID, "Config.Labels"), "map[some:label2]")
   177  	// check that container labels didn't change
   178  	assert.Equal(c, inspectField(c, "test", "Config.Labels"), "map[some:label]")
   179  }