github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/e2e/commit_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/containers/podman/v3/test/utils"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("Podman commit", func() {
    15  	var (
    16  		tempdir    string
    17  		err        error
    18  		podmanTest *PodmanTestIntegration
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		tempdir, err = CreateTempDirInTempDir()
    23  		if err != nil {
    24  			os.Exit(1)
    25  		}
    26  		podmanTest = PodmanTestCreate(tempdir)
    27  		podmanTest.Setup()
    28  		podmanTest.SeedImages()
    29  	})
    30  
    31  	AfterEach(func() {
    32  		podmanTest.Cleanup()
    33  		f := CurrentGinkgoTestDescription()
    34  		processTestResult(f)
    35  
    36  	})
    37  
    38  	It("podman commit container", func() {
    39  		_, ec, _ := podmanTest.RunLsContainer("test1")
    40  		Expect(ec).To(Equal(0))
    41  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
    42  
    43  		session := podmanTest.Podman([]string{"commit", "test1", "foobar.com/test1-image:latest"})
    44  		session.WaitWithDefaultTimeout()
    45  		Expect(session).Should(Exit(0))
    46  
    47  		check := podmanTest.Podman([]string{"inspect", "foobar.com/test1-image:latest"})
    48  		check.WaitWithDefaultTimeout()
    49  		data := check.InspectImageJSON()
    50  		Expect(StringInSlice("foobar.com/test1-image:latest", data[0].RepoTags)).To(BeTrue())
    51  	})
    52  
    53  	It("podman commit single letter container", func() {
    54  		_, ec, _ := podmanTest.RunLsContainer("test1")
    55  		Expect(ec).To(Equal(0))
    56  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
    57  
    58  		session := podmanTest.Podman([]string{"commit", "test1", "a"})
    59  		session.WaitWithDefaultTimeout()
    60  		Expect(session).Should(Exit(0))
    61  
    62  		check := podmanTest.Podman([]string{"inspect", "localhost/a:latest"})
    63  		check.WaitWithDefaultTimeout()
    64  		data := check.InspectImageJSON()
    65  		Expect(StringInSlice("localhost/a:latest", data[0].RepoTags)).To(BeTrue())
    66  	})
    67  
    68  	It("podman container commit container", func() {
    69  		_, ec, _ := podmanTest.RunLsContainer("test1")
    70  		Expect(ec).To(Equal(0))
    71  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
    72  
    73  		session := podmanTest.Podman([]string{"container", "commit", "test1", "foobar.com/test1-image:latest"})
    74  		session.WaitWithDefaultTimeout()
    75  		Expect(session).Should(Exit(0))
    76  
    77  		check := podmanTest.Podman([]string{"image", "inspect", "foobar.com/test1-image:latest"})
    78  		check.WaitWithDefaultTimeout()
    79  		data := check.InspectImageJSON()
    80  		Expect(StringInSlice("foobar.com/test1-image:latest", data[0].RepoTags)).To(BeTrue())
    81  	})
    82  
    83  	It("podman commit container with message", func() {
    84  		_, ec, _ := podmanTest.RunLsContainer("test1")
    85  		Expect(ec).To(Equal(0))
    86  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
    87  
    88  		session := podmanTest.Podman([]string{"commit", "-f", "docker", "--message", "testing-commit", "test1", "foobar.com/test1-image:latest"})
    89  		session.WaitWithDefaultTimeout()
    90  		Expect(session).Should(Exit(0))
    91  
    92  		check := podmanTest.Podman([]string{"inspect", "foobar.com/test1-image:latest"})
    93  		check.WaitWithDefaultTimeout()
    94  		data := check.InspectImageJSON()
    95  		Expect(data[0].Comment).To(Equal("testing-commit"))
    96  	})
    97  
    98  	It("podman commit container with author", func() {
    99  		_, ec, _ := podmanTest.RunLsContainer("test1")
   100  		Expect(ec).To(Equal(0))
   101  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
   102  
   103  		session := podmanTest.Podman([]string{"commit", "--author", "snoopy", "test1", "foobar.com/test1-image:latest"})
   104  		session.WaitWithDefaultTimeout()
   105  		Expect(session).Should(Exit(0))
   106  
   107  		check := podmanTest.Podman([]string{"inspect", "foobar.com/test1-image:latest"})
   108  		check.WaitWithDefaultTimeout()
   109  		data := check.InspectImageJSON()
   110  		Expect(data[0].Author).To(Equal("snoopy"))
   111  	})
   112  
   113  	It("podman commit container with change flag", func() {
   114  		test := podmanTest.Podman([]string{"run", "--name", "test1", "-d", ALPINE, "ls"})
   115  		test.WaitWithDefaultTimeout()
   116  		Expect(test).Should(Exit(0))
   117  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
   118  
   119  		session := podmanTest.Podman([]string{"commit", "--change", "LABEL=image=blue", "test1", "foobar.com/test1-image:latest"})
   120  		session.WaitWithDefaultTimeout()
   121  		Expect(session).Should(Exit(0))
   122  
   123  		check := podmanTest.Podman([]string{"inspect", "foobar.com/test1-image:latest"})
   124  		check.WaitWithDefaultTimeout()
   125  		data := check.InspectImageJSON()
   126  		foundBlue := false
   127  		for _, i := range data[0].Labels {
   128  			if i == "blue" {
   129  				foundBlue = true
   130  				break
   131  			}
   132  		}
   133  		Expect(foundBlue).To(Equal(true))
   134  	})
   135  
   136  	It("podman commit container with change flag and JSON entrypoint with =", func() {
   137  		test := podmanTest.Podman([]string{"run", "--name", "test1", "-d", ALPINE, "ls"})
   138  		test.WaitWithDefaultTimeout()
   139  		Expect(test).Should(Exit(0))
   140  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
   141  
   142  		session := podmanTest.Podman([]string{"commit", "--change", `ENTRYPOINT ["foo", "bar=baz"]`, "test1", "foobar.com/test1-image:latest"})
   143  		session.WaitWithDefaultTimeout()
   144  		Expect(session).Should(Exit(0))
   145  
   146  		check := podmanTest.Podman([]string{"inspect", "foobar.com/test1-image:latest"})
   147  		check.WaitWithDefaultTimeout()
   148  		data := check.InspectImageJSON()
   149  		Expect(len(data)).To(Equal(1))
   150  		Expect(len(data[0].Config.Entrypoint)).To(Equal(2))
   151  		Expect(data[0].Config.Entrypoint[0]).To(Equal("foo"))
   152  		Expect(data[0].Config.Entrypoint[1]).To(Equal("bar=baz"))
   153  	})
   154  
   155  	It("podman commit container with change CMD flag", func() {
   156  		test := podmanTest.Podman([]string{"run", "--name", "test1", "-d", ALPINE, "ls"})
   157  		test.WaitWithDefaultTimeout()
   158  		Expect(test).Should(Exit(0))
   159  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
   160  
   161  		session := podmanTest.Podman([]string{"commit", "--change", "CMD a b c", "test1", "foobar.com/test1-image:latest"})
   162  		session.WaitWithDefaultTimeout()
   163  		Expect(session).Should(Exit(0))
   164  
   165  		session = podmanTest.Podman([]string{"inspect", "--format", "{{.Config.Cmd}}", "foobar.com/test1-image:latest"})
   166  		session.WaitWithDefaultTimeout()
   167  		Expect(session).Should(Exit(0))
   168  		Expect(session.OutputToString()).To(ContainSubstring("sh -c a b c"))
   169  
   170  		session = podmanTest.Podman([]string{"commit", "--change", "CMD=[\"a\",\"b\",\"c\"]", "test1", "foobar.com/test1-image:latest"})
   171  		session.WaitWithDefaultTimeout()
   172  		Expect(session).Should(Exit(0))
   173  
   174  		session = podmanTest.Podman([]string{"inspect", "--format", "{{.Config.Cmd}}", "foobar.com/test1-image:latest"})
   175  		session.WaitWithDefaultTimeout()
   176  		Expect(session).Should(Exit(0))
   177  		Expect(session.OutputToString()).To(Not(ContainSubstring("sh -c")))
   178  	})
   179  
   180  	It("podman commit container with pause flag", func() {
   181  		_, ec, _ := podmanTest.RunLsContainer("test1")
   182  		Expect(ec).To(Equal(0))
   183  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
   184  
   185  		session := podmanTest.Podman([]string{"commit", "--pause=false", "test1", "foobar.com/test1-image:latest"})
   186  		session.WaitWithDefaultTimeout()
   187  		Expect(session).Should(Exit(0))
   188  
   189  		check := podmanTest.Podman([]string{"inspect", "foobar.com/test1-image:latest"})
   190  		check.WaitWithDefaultTimeout()
   191  		Expect(check).Should(Exit(0))
   192  	})
   193  
   194  	It("podman commit with volumes mounts and no include-volumes", func() {
   195  		s := podmanTest.Podman([]string{"run", "--name", "test1", "-v", "/tmp:/foo", "alpine", "date"})
   196  		s.WaitWithDefaultTimeout()
   197  		Expect(s).Should(Exit(0))
   198  
   199  		c := podmanTest.Podman([]string{"commit", "test1", "newimage"})
   200  		c.WaitWithDefaultTimeout()
   201  		Expect(c).Should(Exit(0))
   202  
   203  		inspect := podmanTest.Podman([]string{"inspect", "newimage"})
   204  		inspect.WaitWithDefaultTimeout()
   205  		Expect(inspect).Should(Exit(0))
   206  		image := inspect.InspectImageJSON()
   207  		_, ok := image[0].Config.Volumes["/foo"]
   208  		Expect(ok).To(BeFalse())
   209  	})
   210  
   211  	It("podman commit with volume mounts and --include-volumes", func() {
   212  		// We need to figure out how volumes are going to work correctly with the remote
   213  		// client.  This does not currently work.
   214  		SkipIfRemote("--testing Remote Volumes")
   215  		s := podmanTest.Podman([]string{"run", "--name", "test1", "-v", "/tmp:/foo", "alpine", "date"})
   216  		s.WaitWithDefaultTimeout()
   217  		Expect(s).Should(Exit(0))
   218  
   219  		c := podmanTest.Podman([]string{"commit", "--include-volumes", "test1", "newimage"})
   220  		c.WaitWithDefaultTimeout()
   221  		Expect(c).Should(Exit(0))
   222  
   223  		inspect := podmanTest.Podman([]string{"inspect", "newimage"})
   224  		inspect.WaitWithDefaultTimeout()
   225  		Expect(inspect).Should(Exit(0))
   226  		image := inspect.InspectImageJSON()
   227  		_, ok := image[0].Config.Volumes["/foo"]
   228  		Expect(ok).To(BeTrue())
   229  
   230  		r := podmanTest.Podman([]string{"run", "newimage"})
   231  		r.WaitWithDefaultTimeout()
   232  		Expect(r).Should(Exit(0))
   233  	})
   234  
   235  	It("podman commit container check env variables", func() {
   236  		s := podmanTest.Podman([]string{"run", "--name", "test1", "-e", "TEST=1=1-01=9.01", "-it", "alpine", "true"})
   237  		s.WaitWithDefaultTimeout()
   238  		Expect(s).Should(Exit(0))
   239  
   240  		c := podmanTest.Podman([]string{"commit", "test1", "newimage"})
   241  		c.WaitWithDefaultTimeout()
   242  		Expect(c).Should(Exit(0))
   243  
   244  		inspect := podmanTest.Podman([]string{"inspect", "newimage"})
   245  		inspect.WaitWithDefaultTimeout()
   246  		Expect(inspect).Should(Exit(0))
   247  		image := inspect.InspectImageJSON()
   248  
   249  		envMap := make(map[string]bool)
   250  		for _, v := range image[0].Config.Env {
   251  			envMap[v] = true
   252  		}
   253  		Expect(envMap["TEST=1=1-01=9.01"]).To(BeTrue())
   254  	})
   255  
   256  	It("podman commit container and print id to external file", func() {
   257  		// Switch to temp dir and restore it afterwards
   258  		cwd, err := os.Getwd()
   259  		Expect(err).To(BeNil())
   260  		Expect(os.Chdir(os.TempDir())).To(BeNil())
   261  		targetPath, err := CreateTempDirInTempDir()
   262  		if err != nil {
   263  			os.Exit(1)
   264  		}
   265  		targetFile := filepath.Join(targetPath, "idFile")
   266  		defer Expect(os.RemoveAll(targetFile)).To(BeNil())
   267  		defer Expect(os.Chdir(cwd)).To(BeNil())
   268  
   269  		_, ec, _ := podmanTest.RunLsContainer("test1")
   270  		Expect(ec).To(Equal(0))
   271  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
   272  
   273  		session := podmanTest.Podman([]string{"commit", "test1", "foobar.com/test1-image:latest", "--iidfile", targetFile})
   274  		session.WaitWithDefaultTimeout()
   275  		Expect(session).Should(Exit(0))
   276  
   277  		id, _ := ioutil.ReadFile(targetFile)
   278  		check := podmanTest.Podman([]string{"inspect", "foobar.com/test1-image:latest"})
   279  		check.WaitWithDefaultTimeout()
   280  		data := check.InspectImageJSON()
   281  		Expect(data[0].ID).To(Equal(string(id)))
   282  	})
   283  
   284  	It("podman commit should not commit secret", func() {
   285  		secretsString := "somesecretdata"
   286  		secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
   287  		err := ioutil.WriteFile(secretFilePath, []byte(secretsString), 0755)
   288  		Expect(err).To(BeNil())
   289  
   290  		session := podmanTest.Podman([]string{"secret", "create", "mysecret", secretFilePath})
   291  		session.WaitWithDefaultTimeout()
   292  		Expect(session).Should(Exit(0))
   293  
   294  		session = podmanTest.Podman([]string{"run", "--secret", "mysecret", "--name", "secr", ALPINE, "cat", "/run/secrets/mysecret"})
   295  		session.WaitWithDefaultTimeout()
   296  		Expect(session).Should(Exit(0))
   297  		Expect(session.OutputToString()).To(Equal(secretsString))
   298  
   299  		session = podmanTest.Podman([]string{"commit", "secr", "foobar.com/test1-image:latest"})
   300  		session.WaitWithDefaultTimeout()
   301  		Expect(session).Should(Exit(0))
   302  
   303  		session = podmanTest.Podman([]string{"run", "foobar.com/test1-image:latest", "cat", "/run/secrets/mysecret"})
   304  		session.WaitWithDefaultTimeout()
   305  		Expect(session).To(ExitWithError())
   306  
   307  	})
   308  
   309  	It("podman commit should not commit env secret", func() {
   310  		secretsString := "somesecretdata"
   311  		secretFilePath := filepath.Join(podmanTest.TempDir, "secret")
   312  		err := ioutil.WriteFile(secretFilePath, []byte(secretsString), 0755)
   313  		Expect(err).To(BeNil())
   314  
   315  		session := podmanTest.Podman([]string{"secret", "create", "mysecret", secretFilePath})
   316  		session.WaitWithDefaultTimeout()
   317  		Expect(session).Should(Exit(0))
   318  
   319  		session = podmanTest.Podman([]string{"run", "--secret", "source=mysecret,type=env", "--name", "secr", ALPINE, "printenv", "mysecret"})
   320  		session.WaitWithDefaultTimeout()
   321  		Expect(session).Should(Exit(0))
   322  		Expect(session.OutputToString()).To(Equal(secretsString))
   323  
   324  		session = podmanTest.Podman([]string{"commit", "secr", "foobar.com/test1-image:latest"})
   325  		session.WaitWithDefaultTimeout()
   326  		Expect(session).Should(Exit(0))
   327  
   328  		session = podmanTest.Podman([]string{"run", "foobar.com/test1-image:latest", "printenv", "mysecret"})
   329  		session.WaitWithDefaultTimeout()
   330  		Expect(session.OutputToString()).To(Not(ContainSubstring(secretsString)))
   331  	})
   332  
   333  	It("podman commit adds exposed ports", func() {
   334  		name := "testcon"
   335  		s := podmanTest.Podman([]string{"run", "--name", name, "-p", "8585:80", ALPINE, "true"})
   336  		s.WaitWithDefaultTimeout()
   337  		Expect(s).Should(Exit(0))
   338  
   339  		newImageName := "newimage"
   340  		c := podmanTest.Podman([]string{"commit", name, newImageName})
   341  		c.WaitWithDefaultTimeout()
   342  		Expect(c).Should(Exit(0))
   343  
   344  		inspect := podmanTest.Podman([]string{"inspect", newImageName})
   345  		inspect.WaitWithDefaultTimeout()
   346  		Expect(inspect).Should(Exit(0))
   347  		images := inspect.InspectImageJSON()
   348  		Expect(images).To(HaveLen(1))
   349  		Expect(images[0].Config.ExposedPorts).To(HaveKey("80/tcp"))
   350  
   351  		name = "testcon2"
   352  		s = podmanTest.Podman([]string{"run", "--name", name, "-d", nginx})
   353  		s.WaitWithDefaultTimeout()
   354  		Expect(s).Should(Exit(0))
   355  
   356  		newImageName = "newimage2"
   357  		c = podmanTest.Podman([]string{"commit", name, newImageName})
   358  		c.WaitWithDefaultTimeout()
   359  		Expect(c).Should(Exit(0))
   360  
   361  		inspect = podmanTest.Podman([]string{"inspect", newImageName})
   362  		inspect.WaitWithDefaultTimeout()
   363  		Expect(inspect).Should(Exit(0))
   364  		images = inspect.InspectImageJSON()
   365  		Expect(images).To(HaveLen(1))
   366  		Expect(images[0].Config.ExposedPorts).To(HaveKey("80/tcp"))
   367  	})
   368  })