github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_stop_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 "io" 22 "strings" 23 "testing" 24 25 "github.com/containerd/nerdctl/pkg/testutil" 26 "github.com/containerd/nerdctl/pkg/testutil/nettestutil" 27 28 "gotest.tools/v3/assert" 29 ) 30 31 func TestStopStart(t *testing.T) { 32 const ( 33 hostPort = 8080 34 ) 35 testContainerName := testutil.Identifier(t) 36 base := testutil.NewBase(t) 37 defer base.Cmd("rm", "-f", testContainerName).Run() 38 39 base.Cmd("run", "-d", 40 "--restart=no", 41 "--name", testContainerName, 42 "-p", fmt.Sprintf("127.0.0.1:%d:80", hostPort), 43 testutil.NginxAlpineImage).AssertOK() 44 45 check := func(httpGetRetry int) error { 46 resp, err := nettestutil.HTTPGet(fmt.Sprintf("http://127.0.0.1:%d", hostPort), httpGetRetry, false) 47 if err != nil { 48 return err 49 } 50 respBody, err := io.ReadAll(resp.Body) 51 if err != nil { 52 return err 53 } 54 if !strings.Contains(string(respBody), testutil.NginxAlpineIndexHTMLSnippet) { 55 return fmt.Errorf("expected contain %q, got %q", 56 testutil.NginxAlpineIndexHTMLSnippet, string(respBody)) 57 } 58 return nil 59 } 60 61 assert.NilError(t, check(30)) 62 base.Cmd("stop", testContainerName).AssertOK() 63 base.Cmd("exec", testContainerName, "ps").AssertFail() 64 if check(1) == nil { 65 t.Fatal("expected to get an error") 66 } 67 base.Cmd("start", testContainerName).AssertOK() 68 assert.NilError(t, check(30)) 69 } 70 71 func TestStopWithStopSignal(t *testing.T) { 72 t.Parallel() 73 base := testutil.NewBase(t) 74 testContainerName := testutil.Identifier(t) 75 defer base.Cmd("rm", "-f", testContainerName).Run() 76 77 base.Cmd("run", "-d", "--stop-signal", "SIGQUIT", "--name", testContainerName, testutil.CommonImage, "sh", "-euxc", `#!/bin/sh 78 set -eu 79 trap 'quit=1' QUIT 80 quit=0 81 while [ $quit -ne 1 ]; do 82 printf 'wait quit' 83 sleep 1 84 done 85 echo "signal quit"`).AssertOK() 86 base.Cmd("stop", testContainerName).AssertOK() 87 base.Cmd("logs", "-f", testContainerName).AssertOutContains("signal quit") 88 }