github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/container/container_unit_test.go (about) 1 package container // import "github.com/docker/docker/container" 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/docker/docker/api/types/container" 10 swarmtypes "github.com/docker/docker/api/types/swarm" 11 "github.com/docker/docker/daemon/logger/jsonfilelog" 12 "github.com/docker/docker/pkg/signal" 13 "gotest.tools/v3/assert" 14 ) 15 16 func TestContainerStopSignal(t *testing.T) { 17 c := &Container{ 18 Config: &container.Config{}, 19 } 20 21 def, err := signal.ParseSignal(signal.DefaultStopSignal) 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 s := c.StopSignal() 27 if s != int(def) { 28 t.Fatalf("Expected %v, got %v", def, s) 29 } 30 31 c = &Container{ 32 Config: &container.Config{StopSignal: "SIGKILL"}, 33 } 34 s = c.StopSignal() 35 if s != 9 { 36 t.Fatalf("Expected 9, got %v", s) 37 } 38 } 39 40 func TestContainerStopTimeout(t *testing.T) { 41 c := &Container{ 42 Config: &container.Config{}, 43 } 44 45 s := c.StopTimeout() 46 if s != DefaultStopTimeout { 47 t.Fatalf("Expected %v, got %v", DefaultStopTimeout, s) 48 } 49 50 stopTimeout := 15 51 c = &Container{ 52 Config: &container.Config{StopTimeout: &stopTimeout}, 53 } 54 s = c.StopTimeout() 55 if s != stopTimeout { 56 t.Fatalf("Expected %v, got %v", stopTimeout, s) 57 } 58 } 59 60 func TestContainerSecretReferenceDestTarget(t *testing.T) { 61 ref := &swarmtypes.SecretReference{ 62 File: &swarmtypes.SecretReferenceFileTarget{ 63 Name: "app", 64 }, 65 } 66 67 d := getSecretTargetPath(ref) 68 expected := filepath.Join(containerSecretMountPath, "app") 69 if d != expected { 70 t.Fatalf("expected secret dest %q; received %q", expected, d) 71 } 72 } 73 74 func TestContainerLogPathSetForJSONFileLogger(t *testing.T) { 75 containerRoot, err := os.MkdirTemp("", "TestContainerLogPathSetForJSONFileLogger") 76 assert.NilError(t, err) 77 defer os.RemoveAll(containerRoot) 78 79 c := &Container{ 80 Config: &container.Config{}, 81 HostConfig: &container.HostConfig{ 82 LogConfig: container.LogConfig{ 83 Type: jsonfilelog.Name, 84 }, 85 }, 86 ID: "TestContainerLogPathSetForJSONFileLogger", 87 Root: containerRoot, 88 } 89 90 logger, err := c.StartLogger() 91 assert.NilError(t, err) 92 defer logger.Close() 93 94 expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID))) 95 assert.NilError(t, err) 96 assert.Equal(t, c.LogPath, expectedLogPath) 97 } 98 99 func TestContainerLogPathSetForRingLogger(t *testing.T) { 100 containerRoot, err := os.MkdirTemp("", "TestContainerLogPathSetForRingLogger") 101 assert.NilError(t, err) 102 defer os.RemoveAll(containerRoot) 103 104 c := &Container{ 105 Config: &container.Config{}, 106 HostConfig: &container.HostConfig{ 107 LogConfig: container.LogConfig{ 108 Type: jsonfilelog.Name, 109 Config: map[string]string{ 110 "mode": string(container.LogModeNonBlock), 111 }, 112 }, 113 }, 114 ID: "TestContainerLogPathSetForRingLogger", 115 Root: containerRoot, 116 } 117 118 logger, err := c.StartLogger() 119 assert.NilError(t, err) 120 defer logger.Close() 121 122 expectedLogPath, err := filepath.Abs(filepath.Join(containerRoot, fmt.Sprintf("%s-json.log", c.ID))) 123 assert.NilError(t, err) 124 assert.Equal(t, c.LogPath, expectedLogPath) 125 }