github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/daemon/config/config_unix_test.go (about)

     1  // +build !windows
     2  
     3  package config
     4  
     5  import (
     6  	"io/ioutil"
     7  	"runtime"
     8  
     9  	"testing"
    10  )
    11  
    12  func TestDaemonConfigurationMerge(t *testing.T) {
    13  	f, err := ioutil.TempFile("", "docker-config-")
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  
    18  	configFile := f.Name()
    19  
    20  	f.Write([]byte(`
    21  		{
    22  			"debug": true,
    23  			"default-ulimits": {
    24  				"nofile": {
    25  					"Name": "nofile",
    26  					"Hard": 2048,
    27  					"Soft": 1024
    28  				}
    29  			},
    30  			"log-opts": {
    31  				"tag": "test_tag"
    32  			}
    33  		}`))
    34  
    35  	f.Close()
    36  
    37  	c := &Config{
    38  		CommonConfig: CommonConfig{
    39  			AutoRestart: true,
    40  			LogConfig: LogConfig{
    41  				Type:   "syslog",
    42  				Config: map[string]string{"tag": "test"},
    43  			},
    44  		},
    45  	}
    46  
    47  	cc, err := MergeDaemonConfigurations(c, nil, configFile)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if !cc.Debug {
    52  		t.Fatalf("expected %v, got %v\n", true, cc.Debug)
    53  	}
    54  	if !cc.AutoRestart {
    55  		t.Fatalf("expected %v, got %v\n", true, cc.AutoRestart)
    56  	}
    57  	if cc.LogConfig.Type != "syslog" {
    58  		t.Fatalf("expected syslog config, got %q\n", cc.LogConfig)
    59  	}
    60  
    61  	if configValue, OK := cc.LogConfig.Config["tag"]; !OK {
    62  		t.Fatal("expected syslog config attributes, got nil\n")
    63  	} else {
    64  		if configValue != "test_tag" {
    65  			t.Fatalf("expected syslog config attributes 'tag=test_tag', got 'tag=%s'\n", configValue)
    66  		}
    67  	}
    68  
    69  	if cc.Ulimits == nil {
    70  		t.Fatal("expected default ulimit config, got nil\n")
    71  	} else {
    72  		if _, OK := cc.Ulimits["nofile"]; OK {
    73  			if cc.Ulimits["nofile"].Name != "nofile" ||
    74  				cc.Ulimits["nofile"].Hard != 2048 ||
    75  				cc.Ulimits["nofile"].Soft != 1024 {
    76  				t.Fatalf("expected default ulimit name, hard and soft are nofile, 2048, 1024, got %s, %d, %d\n", cc.Ulimits["nofile"].Name, cc.Ulimits["nofile"].Hard, cc.Ulimits["nofile"].Soft)
    77  			}
    78  		} else {
    79  			t.Fatal("expected default ulimit name nofile, got nil\n")
    80  		}
    81  	}
    82  }
    83  
    84  func TestDaemonConfigurationMergeShmSize(t *testing.T) {
    85  	if runtime.GOOS == "solaris" {
    86  		t.Skip("ShmSize not supported on Solaris\n")
    87  	}
    88  	f, err := ioutil.TempFile("", "docker-config-")
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	configFile := f.Name()
    94  
    95  	f.Write([]byte(`
    96  		{
    97  			"default-shm-size": "1g"
    98  		}`))
    99  
   100  	f.Close()
   101  
   102  	c := &Config{}
   103  	cc, err := MergeDaemonConfigurations(c, nil, configFile)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	expectedValue := 1 * 1024 * 1024 * 1024
   108  	if cc.ShmSize.Value() != int64(expectedValue) {
   109  		t.Fatalf("expected default shm size %d, got %d", expectedValue, cc.ShmSize.Value())
   110  	}
   111  }