github.com/containers/podman/v4@v4.9.4/test/e2e/restart_test.go (about)

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