github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_restart_linux_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "strings" 22 "testing" 23 "time" 24 25 "github.com/containerd/nerdctl/pkg/testutil" 26 "gotest.tools/v3/assert" 27 ) 28 29 func TestRestart(t *testing.T) { 30 t.Parallel() 31 base := testutil.NewBase(t) 32 tID := testutil.Identifier(t) 33 34 base.Cmd("run", "-d", "--name", tID, testutil.NginxAlpineImage).AssertOK() 35 defer base.Cmd("rm", "-f", tID).AssertOK() 36 base.EnsureContainerStarted(tID) 37 38 inspect := base.InspectContainer(tID) 39 pid := inspect.State.Pid 40 41 base.Cmd("restart", tID).AssertOK() 42 base.EnsureContainerStarted(tID) 43 44 newInspect := base.InspectContainer(tID) 45 newPid := newInspect.State.Pid 46 assert.Assert(t, pid != newPid) 47 } 48 49 func TestRestartPIDContainer(t *testing.T) { 50 t.Parallel() 51 base := testutil.NewBase(t) 52 53 baseContainerName := testutil.Identifier(t) 54 base.Cmd("run", "-d", "--name", baseContainerName, testutil.AlpineImage, "sleep", "infinity").AssertOK() 55 defer base.Cmd("rm", "-f", baseContainerName).Run() 56 57 sharedContainerName := fmt.Sprintf("%s-shared", baseContainerName) 58 base.Cmd("run", "-d", "--name", sharedContainerName, fmt.Sprintf("--pid=container:%s", baseContainerName), testutil.AlpineImage, "sleep", "infinity").AssertOK() 59 defer base.Cmd("rm", "-f", sharedContainerName).Run() 60 61 base.Cmd("restart", baseContainerName).AssertOK() 62 base.Cmd("restart", sharedContainerName).AssertOK() 63 64 // output format : <inode number> /proc/1/ns/pid 65 // example output: 4026532581 /proc/1/ns/pid 66 basePSResult := base.Cmd("exec", baseContainerName, "ls", "-Li", "/proc/1/ns/pid").Run() 67 baseOutput := strings.TrimSpace(basePSResult.Stdout()) 68 sharedPSResult := base.Cmd("exec", sharedContainerName, "ls", "-Li", "/proc/1/ns/pid").Run() 69 sharedOutput := strings.TrimSpace(sharedPSResult.Stdout()) 70 71 assert.Equal(t, baseOutput, sharedOutput) 72 } 73 74 func TestRestartWithTime(t *testing.T) { 75 t.Parallel() 76 base := testutil.NewBase(t) 77 tID := testutil.Identifier(t) 78 79 base.Cmd("run", "-d", "--name", tID, testutil.AlpineImage, "sleep", "infinity").AssertOK() 80 defer base.Cmd("rm", "-f", tID).AssertOK() 81 82 inspect := base.InspectContainer(tID) 83 pid := inspect.State.Pid 84 85 timePreRestart := time.Now() 86 base.Cmd("restart", "-t", "5", tID).AssertOK() 87 timePostRestart := time.Now() 88 89 newInspect := base.InspectContainer(tID) 90 newPid := newInspect.State.Pid 91 assert.Assert(t, pid != newPid) 92 // ensure that stop took at least 5 seconds 93 assert.Assert(t, timePostRestart.Sub(timePreRestart) >= time.Second*5) 94 }