github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/container/container_unit_test.go (about)

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