github.com/containers/podman/v5@v5.1.0-rc1/test/e2e/restart_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"time"
     7  
     8  	. "github.com/containers/podman/v5/test/utils"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("Podman restart", func() {
    15  
    16  	It("podman restart bogus container", func() {
    17  		session := podmanTest.Podman([]string{"start", "123"})
    18  		session.WaitWithDefaultTimeout()
    19  		Expect(session).Should(Exit(125))
    20  	})
    21  
    22  	It("podman restart stopped container by name", func() {
    23  		_, exitCode, _ := podmanTest.RunLsContainer("test1")
    24  		Expect(exitCode).To(Equal(0))
    25  		startTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1"})
    26  		startTime.WaitWithDefaultTimeout()
    27  
    28  		session := podmanTest.Podman([]string{"restart", "test1"})
    29  		session.WaitWithDefaultTimeout()
    30  		Expect(session).Should(ExitCleanly())
    31  		restartTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1"})
    32  		restartTime.WaitWithDefaultTimeout()
    33  		Expect(restartTime.OutputToString()).To(Not(Equal(startTime.OutputToString())))
    34  	})
    35  
    36  	It("podman restart stopped container by ID", func() {
    37  		session := podmanTest.Podman([]string{"create", ALPINE, "ls"})
    38  		session.WaitWithDefaultTimeout()
    39  		Expect(session).Should(ExitCleanly())
    40  		cid := session.OutputToString()
    41  		startTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", cid})
    42  		startTime.WaitWithDefaultTimeout()
    43  
    44  		startSession := podmanTest.Podman([]string{"start", "--attach", cid})
    45  		startSession.WaitWithDefaultTimeout()
    46  		Expect(startSession).Should(ExitCleanly())
    47  
    48  		session2 := podmanTest.Podman([]string{"restart", cid})
    49  		session2.WaitWithDefaultTimeout()
    50  		Expect(session2).Should(ExitCleanly())
    51  		restartTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", cid})
    52  		restartTime.WaitWithDefaultTimeout()
    53  		Expect(restartTime.OutputToString()).To(Not(Equal(startTime.OutputToString())))
    54  	})
    55  
    56  	It("podman restart running container", func() {
    57  		_ = podmanTest.RunTopContainer("test1")
    58  		ok := WaitForContainer(podmanTest)
    59  		Expect(ok).To(BeTrue(), "test1 container is up")
    60  		startTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1"})
    61  		startTime.WaitWithDefaultTimeout()
    62  
    63  		session := podmanTest.Podman([]string{"restart", "test1"})
    64  		session.WaitWithDefaultTimeout()
    65  		Expect(session).Should(ExitCleanly())
    66  		restartTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1"})
    67  		restartTime.WaitWithDefaultTimeout()
    68  		Expect(restartTime.OutputToString()).To(Not(Equal(startTime.OutputToString())))
    69  	})
    70  
    71  	It("podman container restart running container", func() {
    72  		_ = podmanTest.RunTopContainer("test1")
    73  		ok := WaitForContainer(podmanTest)
    74  		Expect(ok).To(BeTrue(), "test1 container is up")
    75  		startTime := podmanTest.Podman([]string{"container", "inspect", "--format='{{.State.StartedAt}}'", "test1"})
    76  		startTime.WaitWithDefaultTimeout()
    77  
    78  		session := podmanTest.Podman([]string{"container", "restart", "test1"})
    79  		session.WaitWithDefaultTimeout()
    80  		Expect(session).Should(ExitCleanly())
    81  		restartTime := podmanTest.Podman([]string{"container", "inspect", "--format='{{.State.StartedAt}}'", "test1"})
    82  		restartTime.WaitWithDefaultTimeout()
    83  		Expect(restartTime.OutputToString()).To(Not(Equal(startTime.OutputToString())))
    84  	})
    85  
    86  	It("podman restart multiple containers", func() {
    87  		_, exitCode, _ := podmanTest.RunLsContainer("test1")
    88  		Expect(exitCode).To(Equal(0))
    89  
    90  		_, exitCode, _ = podmanTest.RunLsContainer("test2")
    91  		Expect(exitCode).To(Equal(0))
    92  		startTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
    93  		startTime.WaitWithDefaultTimeout()
    94  
    95  		session := podmanTest.Podman([]string{"restart", "test1", "test2"})
    96  		session.WaitWithDefaultTimeout()
    97  		Expect(session).Should(ExitCleanly())
    98  		restartTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
    99  		restartTime.WaitWithDefaultTimeout()
   100  		Expect(restartTime.OutputToStringArray()[0]).To(Not(Equal(startTime.OutputToStringArray()[0])))
   101  		Expect(restartTime.OutputToStringArray()[1]).To(Not(Equal(startTime.OutputToStringArray()[1])))
   102  	})
   103  
   104  	It("podman restart the latest container", func() {
   105  		_, exitCode, _ := podmanTest.RunLsContainer("test1")
   106  		Expect(exitCode).To(Equal(0))
   107  
   108  		_, exitCode, _ = podmanTest.RunLsContainer("test2")
   109  		Expect(exitCode).To(Equal(0))
   110  
   111  		startTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
   112  		startTime.WaitWithDefaultTimeout()
   113  
   114  		cid := "-l"
   115  		if IsRemote() {
   116  			cid = "test2"
   117  		}
   118  		session := podmanTest.Podman([]string{"restart", cid})
   119  		session.WaitWithDefaultTimeout()
   120  		Expect(session).Should(ExitCleanly())
   121  		restartTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
   122  		restartTime.WaitWithDefaultTimeout()
   123  		Expect(restartTime.OutputToStringArray()[0]).To(Equal(startTime.OutputToStringArray()[0]))
   124  		Expect(restartTime.OutputToStringArray()[1]).To(Not(Equal(startTime.OutputToStringArray()[1])))
   125  	})
   126  
   127  	It("podman restart non-stop container with short timeout", func() {
   128  		session := podmanTest.Podman([]string{"run", "-d", "--name", "test1", "--env", "STOPSIGNAL=SIGKILL", ALPINE, "sleep", "999"})
   129  		session.WaitWithDefaultTimeout()
   130  		Expect(session).Should(ExitCleanly())
   131  		startTime := time.Now()
   132  		session = podmanTest.Podman([]string{"restart", "-t", "2", "test1"})
   133  		session.WaitWithDefaultTimeout()
   134  		Expect(session).Should(Exit(0))
   135  		timeSince := time.Since(startTime)
   136  		Expect(timeSince).To(BeNumerically("<", 10*time.Second))
   137  		Expect(timeSince).To(BeNumerically(">", 2*time.Second))
   138  		stderr := session.ErrorToString()
   139  		if IsRemote() {
   140  			Expect(stderr).To(BeEmpty())
   141  		} else {
   142  			Expect(stderr).To(ContainSubstring("StopSignal SIGTERM failed to stop container test1 in 2 seconds, resorting to SIGKILL"))
   143  		}
   144  	})
   145  
   146  	It("podman restart --all", func() {
   147  		_, exitCode, _ := podmanTest.RunLsContainer("test1")
   148  		Expect(exitCode).To(Equal(0))
   149  
   150  		test2 := podmanTest.RunTopContainer("test2")
   151  		test2.WaitWithDefaultTimeout()
   152  		Expect(test2).Should(ExitCleanly())
   153  
   154  		startTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
   155  		startTime.WaitWithDefaultTimeout()
   156  
   157  		session := podmanTest.Podman([]string{"restart", "--all"})
   158  		session.WaitWithDefaultTimeout()
   159  		Expect(session).Should(ExitCleanly())
   160  		restartTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
   161  		restartTime.WaitWithDefaultTimeout()
   162  		Expect(restartTime.OutputToStringArray()[0]).To(Not(Equal(startTime.OutputToStringArray()[0])))
   163  		Expect(restartTime.OutputToStringArray()[1]).To(Not(Equal(startTime.OutputToStringArray()[1])))
   164  	})
   165  
   166  	It("podman restart --all --running", func() {
   167  		_, exitCode, _ := podmanTest.RunLsContainer("test1")
   168  		Expect(exitCode).To(Equal(0))
   169  
   170  		test2 := podmanTest.RunTopContainer("test2")
   171  		test2.WaitWithDefaultTimeout()
   172  		Expect(test2).Should(ExitCleanly())
   173  
   174  		startTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
   175  		startTime.WaitWithDefaultTimeout()
   176  
   177  		session := podmanTest.Podman([]string{"restart", "-a", "--running"})
   178  		session.WaitWithDefaultTimeout()
   179  		Expect(session).Should(ExitCleanly())
   180  		restartTime := podmanTest.Podman([]string{"inspect", "--format='{{.State.StartedAt}}'", "test1", "test2"})
   181  		restartTime.WaitWithDefaultTimeout()
   182  		Expect(restartTime.OutputToStringArray()[0]).To(Equal(startTime.OutputToStringArray()[0]))
   183  		Expect(restartTime.OutputToStringArray()[1]).To(Not(Equal(startTime.OutputToStringArray()[1])))
   184  	})
   185  
   186  	It("podman restart a container in a pod and hosts should not duplicated", func() {
   187  		// Fixes: https://github.com/containers/podman/issues/8921
   188  
   189  		_, ec, _ := podmanTest.CreatePod(map[string][]string{"--name": {"foobar99"}})
   190  		Expect(ec).To(Equal(0))
   191  
   192  		session := podmanTest.RunTopContainerInPod("host-restart-test", "foobar99")
   193  		session.WaitWithDefaultTimeout()
   194  		Expect(session).Should(ExitCleanly())
   195  
   196  		testCmd := []string{"exec", "host-restart-test", "sh", "-c", "wc -l < /etc/hosts"}
   197  
   198  		// before restart
   199  		beforeRestart := podmanTest.Podman(testCmd)
   200  		beforeRestart.WaitWithDefaultTimeout()
   201  		Expect(beforeRestart).Should(ExitCleanly())
   202  
   203  		session = podmanTest.Podman([]string{"restart", "host-restart-test"})
   204  		session.WaitWithDefaultTimeout()
   205  		Expect(session).Should(ExitCleanly())
   206  
   207  		afterRestart := podmanTest.Podman(testCmd)
   208  		afterRestart.WaitWithDefaultTimeout()
   209  		Expect(afterRestart).Should(ExitCleanly())
   210  
   211  		// line count should be equal
   212  		Expect(beforeRestart.OutputToString()).To(Equal(afterRestart.OutputToString()))
   213  	})
   214  
   215  	It("podman restart all stopped containers with --all", func() {
   216  		session := podmanTest.RunTopContainer("")
   217  		session.WaitWithDefaultTimeout()
   218  		Expect(session).Should(ExitCleanly())
   219  		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(1))
   220  
   221  		session = podmanTest.RunTopContainer("")
   222  		session.WaitWithDefaultTimeout()
   223  		Expect(session).Should(ExitCleanly())
   224  		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(2))
   225  
   226  		podmanTest.StopContainer("--all")
   227  		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(0))
   228  
   229  		session = podmanTest.Podman([]string{"restart", "--all"})
   230  		session.WaitWithDefaultTimeout()
   231  		Expect(session).Should(ExitCleanly())
   232  		Expect(podmanTest.NumberOfContainersRunning()).To(Equal(2))
   233  	})
   234  
   235  	It("podman restart --cidfile", func() {
   236  		cidFile := filepath.Join(tempdir, "cid")
   237  
   238  		session := podmanTest.Podman([]string{"create", "--cidfile", cidFile, ALPINE, "top"})
   239  		session.WaitWithDefaultTimeout()
   240  		Expect(session).Should(ExitCleanly())
   241  		cid := session.OutputToStringArray()[0]
   242  
   243  		session = podmanTest.Podman([]string{"start", cid})
   244  		session.WaitWithDefaultTimeout()
   245  		Expect(session).Should(ExitCleanly())
   246  
   247  		result := podmanTest.Podman([]string{"restart", "--cidfile", cidFile})
   248  		result.WaitWithDefaultTimeout()
   249  		// FIXME - #20196: Cannot use ExitCleanly()
   250  		Expect(result).Should(Exit(0))
   251  		output := result.OutputToString()
   252  		Expect(output).To(ContainSubstring(cid))
   253  	})
   254  
   255  	It("podman restart multiple --cidfile", func() {
   256  		cidFile1 := filepath.Join(tempdir, "cid-1")
   257  		cidFile2 := filepath.Join(tempdir, "cid-2")
   258  
   259  		session := podmanTest.Podman([]string{"run", "--cidfile", cidFile1, "-d", ALPINE, "top"})
   260  		session.WaitWithDefaultTimeout()
   261  		Expect(session).Should(ExitCleanly())
   262  		cid1 := session.OutputToStringArray()[0]
   263  		Expect(podmanTest.NumberOfContainers()).To(Equal(1))
   264  
   265  		session = podmanTest.Podman([]string{"run", "--cidfile", cidFile2, "-d", ALPINE, "top"})
   266  		session.WaitWithDefaultTimeout()
   267  		Expect(session).Should(ExitCleanly())
   268  		cid2 := session.OutputToStringArray()[0]
   269  		Expect(podmanTest.NumberOfContainers()).To(Equal(2))
   270  
   271  		result := podmanTest.Podman([]string{"restart", "--cidfile", cidFile1, "--cidfile", cidFile2})
   272  		result.WaitWithDefaultTimeout()
   273  		Expect(result).Should(ExitCleanly())
   274  		output := result.OutputToString()
   275  		Expect(output).To(ContainSubstring(cid1))
   276  		Expect(output).To(ContainSubstring(cid2))
   277  		Expect(podmanTest.NumberOfContainers()).To(Equal(2))
   278  	})
   279  
   280  	It("podman restart invalid --latest and --cidfile and --all", func() {
   281  		SkipIfRemote("--latest flag n/a")
   282  		result := podmanTest.Podman([]string{"restart", "--cidfile", "foobar", "--latest"})
   283  		result.WaitWithDefaultTimeout()
   284  		Expect(result).Should(Exit(125))
   285  		Expect(result.ErrorToString()).To(ContainSubstring("cannot be used together"))
   286  		result = podmanTest.Podman([]string{"restart", "--cidfile", "foobar", "--all"})
   287  		result.WaitWithDefaultTimeout()
   288  		Expect(result).Should(Exit(125))
   289  		Expect(result.ErrorToString()).To(ContainSubstring("cannot be used together"))
   290  		result = podmanTest.Podman([]string{"restart", "--cidfile", "foobar", "--all", "--latest"})
   291  		result.WaitWithDefaultTimeout()
   292  		Expect(result).Should(Exit(125))
   293  		Expect(result.ErrorToString()).To(ContainSubstring("cannot be used together"))
   294  		result = podmanTest.Podman([]string{"restart", "--latest", "--all"})
   295  		result.WaitWithDefaultTimeout()
   296  		Expect(result).Should(Exit(125))
   297  		Expect(result.ErrorToString()).To(ContainSubstring("cannot be used together"))
   298  	})
   299  
   300  	It("podman restart --filter", func() {
   301  		session1 := podmanTest.RunTopContainer("")
   302  		session1.WaitWithDefaultTimeout()
   303  		Expect(session1).Should(ExitCleanly())
   304  		cid1 := session1.OutputToString()
   305  
   306  		session1 = podmanTest.RunTopContainer("")
   307  		session1.WaitWithDefaultTimeout()
   308  		Expect(session1).Should(ExitCleanly())
   309  		cid2 := session1.OutputToString()
   310  
   311  		session1 = podmanTest.RunTopContainerWithArgs("", []string{"--label", "test=with,comma"})
   312  		session1.WaitWithDefaultTimeout()
   313  		Expect(session1).Should(ExitCleanly())
   314  		cid3 := session1.OutputToString()
   315  		shortCid3 := cid3[0:5]
   316  
   317  		session1 = podmanTest.Podman([]string{"restart", cid1, "-f", "status=test"})
   318  		session1.WaitWithDefaultTimeout()
   319  		Expect(session1).Should(Exit(125))
   320  
   321  		session1 = podmanTest.Podman([]string{"restart", "-a", "--filter", fmt.Sprintf("id=%swrongid", shortCid3)})
   322  		session1.WaitWithDefaultTimeout()
   323  		Expect(session1).Should(ExitCleanly())
   324  		Expect(session1.OutputToString()).To(BeEmpty())
   325  
   326  		session1 = podmanTest.Podman([]string{"restart", "-a", "--filter", fmt.Sprintf("id=%s", shortCid3)})
   327  		session1.WaitWithDefaultTimeout()
   328  		Expect(session1).Should(ExitCleanly())
   329  		Expect(session1.OutputToString()).To(BeEquivalentTo(cid3))
   330  
   331  		session1 = podmanTest.Podman([]string{"restart", "-a", "--filter", "label=test=with,comma"})
   332  		session1.WaitWithDefaultTimeout()
   333  		Expect(session1).Should(ExitCleanly())
   334  		Expect(session1.OutputToString()).To(BeEquivalentTo(cid3))
   335  
   336  		session1 = podmanTest.Podman([]string{"restart", "-f", fmt.Sprintf("id=%s", cid2)})
   337  		session1.WaitWithDefaultTimeout()
   338  		Expect(session1).Should(ExitCleanly())
   339  		Expect(session1.OutputToString()).To(BeEquivalentTo(cid2))
   340  	})
   341  })