github.com/go/docker@v1.12.0-rc2/cmd/dockerd/daemon_unix_test.go (about)

     1  // +build !windows
     2  
     3  package main
     4  
     5  import (
     6  	"io/ioutil"
     7  	"testing"
     8  
     9  	cliflags "github.com/docker/docker/cli/flags"
    10  	"github.com/docker/docker/daemon"
    11  	"github.com/docker/docker/opts"
    12  	"github.com/docker/docker/pkg/mflag"
    13  )
    14  
    15  func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) {
    16  	c := &daemon.Config{}
    17  	common := &cliflags.CommonFlags{
    18  		Debug:    true,
    19  		LogLevel: "info",
    20  	}
    21  
    22  	f, err := ioutil.TempFile("", "docker-config-")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	configFile := f.Name()
    28  	f.Write([]byte(`{"log-opts": {"max-size": "1k"}}`))
    29  	f.Close()
    30  
    31  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
    32  	flags.String([]string{daemonConfigFileFlag}, "", "")
    33  	flags.BoolVar(&c.EnableSelinuxSupport, []string{"-selinux-enabled"}, true, "")
    34  	flags.StringVar(&c.LogConfig.Type, []string{"-log-driver"}, "json-file", "")
    35  	flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "")
    36  	flags.Set(daemonConfigFileFlag, configFile)
    37  
    38  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	if loadedConfig == nil {
    43  		t.Fatalf("expected configuration %v, got nil", c)
    44  	}
    45  	if !loadedConfig.Debug {
    46  		t.Fatalf("expected debug mode, got false")
    47  	}
    48  	if loadedConfig.LogLevel != "info" {
    49  		t.Fatalf("expected info log level, got %v", loadedConfig.LogLevel)
    50  	}
    51  	if !loadedConfig.EnableSelinuxSupport {
    52  		t.Fatalf("expected enabled selinux support, got disabled")
    53  	}
    54  	if loadedConfig.LogConfig.Type != "json-file" {
    55  		t.Fatalf("expected LogConfig type json-file, got %v", loadedConfig.LogConfig.Type)
    56  	}
    57  	if maxSize := loadedConfig.LogConfig.Config["max-size"]; maxSize != "1k" {
    58  		t.Fatalf("expected log max-size `1k`, got %s", maxSize)
    59  	}
    60  }
    61  
    62  func TestLoadDaemonConfigWithNetwork(t *testing.T) {
    63  	c := &daemon.Config{}
    64  	common := &cliflags.CommonFlags{}
    65  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
    66  	flags.String([]string{"-bip"}, "", "")
    67  	flags.String([]string{"-ip"}, "", "")
    68  
    69  	f, err := ioutil.TempFile("", "docker-config-")
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	configFile := f.Name()
    75  	f.Write([]byte(`{"bip": "127.0.0.2", "ip": "127.0.0.1"}`))
    76  	f.Close()
    77  
    78  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if loadedConfig == nil {
    83  		t.Fatalf("expected configuration %v, got nil", c)
    84  	}
    85  	if loadedConfig.IP != "127.0.0.2" {
    86  		t.Fatalf("expected IP 127.0.0.2, got %v", loadedConfig.IP)
    87  	}
    88  	if loadedConfig.DefaultIP.String() != "127.0.0.1" {
    89  		t.Fatalf("expected DefaultIP 127.0.0.1, got %s", loadedConfig.DefaultIP)
    90  	}
    91  }
    92  
    93  func TestLoadDaemonConfigWithMapOptions(t *testing.T) {
    94  	c := &daemon.Config{}
    95  	common := &cliflags.CommonFlags{}
    96  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
    97  
    98  	flags.Var(opts.NewNamedMapOpts("cluster-store-opts", c.ClusterOpts, nil), []string{"-cluster-store-opt"}, "")
    99  	flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "")
   100  
   101  	f, err := ioutil.TempFile("", "docker-config-")
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	configFile := f.Name()
   107  	f.Write([]byte(`{
   108  		"cluster-store-opts": {"kv.cacertfile": "/var/lib/docker/discovery_certs/ca.pem"},
   109  		"log-opts": {"tag": "test"}
   110  }`))
   111  	f.Close()
   112  
   113  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	if loadedConfig == nil {
   118  		t.Fatal("expected configuration, got nil")
   119  	}
   120  	if loadedConfig.ClusterOpts == nil {
   121  		t.Fatal("expected cluster options, got nil")
   122  	}
   123  
   124  	expectedPath := "/var/lib/docker/discovery_certs/ca.pem"
   125  	if caPath := loadedConfig.ClusterOpts["kv.cacertfile"]; caPath != expectedPath {
   126  		t.Fatalf("expected %s, got %s", expectedPath, caPath)
   127  	}
   128  
   129  	if loadedConfig.LogConfig.Config == nil {
   130  		t.Fatal("expected log config options, got nil")
   131  	}
   132  	if tag := loadedConfig.LogConfig.Config["tag"]; tag != "test" {
   133  		t.Fatalf("expected log tag `test`, got %s", tag)
   134  	}
   135  }
   136  
   137  func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) {
   138  	c := &daemon.Config{}
   139  	common := &cliflags.CommonFlags{}
   140  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   141  	flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "")
   142  
   143  	f, err := ioutil.TempFile("", "docker-config-")
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	if err := flags.ParseFlags([]string{}, false); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  
   152  	configFile := f.Name()
   153  	f.Write([]byte(`{
   154  		"userland-proxy": false
   155  }`))
   156  	f.Close()
   157  
   158  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	if loadedConfig == nil {
   163  		t.Fatal("expected configuration, got nil")
   164  	}
   165  
   166  	if loadedConfig.EnableUserlandProxy {
   167  		t.Fatal("expected userland proxy to be disabled, got enabled")
   168  	}
   169  
   170  	// make sure reloading doesn't generate configuration
   171  	// conflicts after normalizing boolean values.
   172  	err = daemon.ReloadConfiguration(configFile, flags, func(reloadedConfig *daemon.Config) {
   173  		if reloadedConfig.EnableUserlandProxy {
   174  			t.Fatal("expected userland proxy to be disabled, got enabled")
   175  		}
   176  	})
   177  	if err != nil {
   178  		t.Fatal(err)
   179  	}
   180  }
   181  
   182  func TestLoadDaemonConfigWithTrueDefaultValuesLeaveDefaults(t *testing.T) {
   183  	c := &daemon.Config{}
   184  	common := &cliflags.CommonFlags{}
   185  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   186  	flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "")
   187  
   188  	f, err := ioutil.TempFile("", "docker-config-")
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	if err := flags.ParseFlags([]string{}, false); err != nil {
   194  		t.Fatal(err)
   195  	}
   196  
   197  	configFile := f.Name()
   198  	f.Write([]byte(`{}`))
   199  	f.Close()
   200  
   201  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   202  	if err != nil {
   203  		t.Fatal(err)
   204  	}
   205  	if loadedConfig == nil {
   206  		t.Fatal("expected configuration, got nil")
   207  	}
   208  
   209  	if !loadedConfig.EnableUserlandProxy {
   210  		t.Fatal("expected userland proxy to be enabled, got disabled")
   211  	}
   212  }