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

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	cliflags "github.com/docker/docker/cli/flags"
    11  	"github.com/docker/docker/daemon"
    12  	"github.com/docker/docker/opts"
    13  	"github.com/docker/docker/pkg/mflag"
    14  	"github.com/docker/go-connections/tlsconfig"
    15  )
    16  
    17  func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
    18  	c := &daemon.Config{}
    19  	common := &cliflags.CommonFlags{
    20  		Debug: true,
    21  	}
    22  
    23  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
    24  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if loadedConfig == nil {
    29  		t.Fatalf("expected configuration %v, got nil", c)
    30  	}
    31  	if !loadedConfig.Debug {
    32  		t.Fatalf("expected debug to be copied from the common flags, got false")
    33  	}
    34  }
    35  
    36  func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
    37  	c := &daemon.Config{}
    38  	common := &cliflags.CommonFlags{
    39  		TLS: true,
    40  		TLSOptions: &tlsconfig.Options{
    41  			CAFile: "/tmp/ca.pem",
    42  		},
    43  	}
    44  
    45  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
    46  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz")
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	if loadedConfig == nil {
    51  		t.Fatalf("expected configuration %v, got nil", c)
    52  	}
    53  	if loadedConfig.CommonTLSOptions.CAFile != "/tmp/ca.pem" {
    54  		t.Fatalf("expected /tmp/ca.pem, got %s: %q", loadedConfig.CommonTLSOptions.CAFile, loadedConfig)
    55  	}
    56  }
    57  
    58  func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
    59  	c := &daemon.Config{}
    60  	common := &cliflags.CommonFlags{}
    61  	f, err := ioutil.TempFile("", "docker-config-")
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	configFile := f.Name()
    66  	defer os.Remove(configFile)
    67  
    68  	f.Write([]byte(`{"labels": ["l3=foo"]}`))
    69  	f.Close()
    70  
    71  	var labels []string
    72  
    73  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
    74  	flags.String([]string{daemonConfigFileFlag}, "", "")
    75  	flags.Var(opts.NewNamedListOptsRef("labels", &labels, opts.ValidateLabel), []string{"-label"}, "")
    76  
    77  	flags.Set(daemonConfigFileFlag, configFile)
    78  	if err := flags.Set("-label", "l1=bar"); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	if err := flags.Set("-label", "l2=baz"); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  
    85  	_, err = loadDaemonCliConfig(c, flags, common, configFile)
    86  	if err == nil {
    87  		t.Fatalf("expected configuration error, got nil")
    88  	}
    89  	if !strings.Contains(err.Error(), "labels") {
    90  		t.Fatalf("expected labels conflict, got %v", err)
    91  	}
    92  }
    93  
    94  func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
    95  	c := &daemon.Config{}
    96  	common := &cliflags.CommonFlags{
    97  		TLSOptions: &tlsconfig.Options{
    98  			CAFile: "/tmp/ca.pem",
    99  		},
   100  	}
   101  
   102  	f, err := ioutil.TempFile("", "docker-config-")
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	configFile := f.Name()
   107  	defer os.Remove(configFile)
   108  
   109  	f.Write([]byte(`{"tlsverify": true}`))
   110  	f.Close()
   111  
   112  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   113  	flags.Bool([]string{"-tlsverify"}, false, "")
   114  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	if loadedConfig == nil {
   119  		t.Fatalf("expected configuration %v, got nil", c)
   120  	}
   121  
   122  	if !loadedConfig.TLS {
   123  		t.Fatalf("expected TLS enabled, got %q", loadedConfig)
   124  	}
   125  }
   126  
   127  func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
   128  	c := &daemon.Config{}
   129  	common := &cliflags.CommonFlags{
   130  		TLSOptions: &tlsconfig.Options{
   131  			CAFile: "/tmp/ca.pem",
   132  		},
   133  	}
   134  
   135  	f, err := ioutil.TempFile("", "docker-config-")
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	configFile := f.Name()
   140  	defer os.Remove(configFile)
   141  
   142  	f.Write([]byte(`{"tlsverify": false}`))
   143  	f.Close()
   144  
   145  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   146  	flags.Bool([]string{"-tlsverify"}, false, "")
   147  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	if loadedConfig == nil {
   152  		t.Fatalf("expected configuration %v, got nil", c)
   153  	}
   154  
   155  	if !loadedConfig.TLS {
   156  		t.Fatalf("expected TLS enabled, got %q", loadedConfig)
   157  	}
   158  }
   159  
   160  func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
   161  	c := &daemon.Config{}
   162  	common := &cliflags.CommonFlags{
   163  		TLSOptions: &tlsconfig.Options{
   164  			CAFile: "/tmp/ca.pem",
   165  		},
   166  	}
   167  
   168  	f, err := ioutil.TempFile("", "docker-config-")
   169  	if err != nil {
   170  		t.Fatal(err)
   171  	}
   172  	configFile := f.Name()
   173  	defer os.Remove(configFile)
   174  
   175  	f.Write([]byte(`{}`))
   176  	f.Close()
   177  
   178  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   179  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   180  	if err != nil {
   181  		t.Fatal(err)
   182  	}
   183  	if loadedConfig == nil {
   184  		t.Fatalf("expected configuration %v, got nil", c)
   185  	}
   186  
   187  	if loadedConfig.TLS {
   188  		t.Fatalf("expected TLS disabled, got %q", loadedConfig)
   189  	}
   190  }
   191  
   192  func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
   193  	c := &daemon.Config{}
   194  	common := &cliflags.CommonFlags{}
   195  
   196  	f, err := ioutil.TempFile("", "docker-config-")
   197  	if err != nil {
   198  		t.Fatal(err)
   199  	}
   200  	configFile := f.Name()
   201  	defer os.Remove(configFile)
   202  
   203  	f.Write([]byte(`{"log-level": "warn"}`))
   204  	f.Close()
   205  
   206  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   207  	flags.String([]string{"-log-level"}, "", "")
   208  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   209  	if err != nil {
   210  		t.Fatal(err)
   211  	}
   212  	if loadedConfig == nil {
   213  		t.Fatalf("expected configuration %v, got nil", c)
   214  	}
   215  	if loadedConfig.LogLevel != "warn" {
   216  		t.Fatalf("expected warn log level, got %v", loadedConfig.LogLevel)
   217  	}
   218  
   219  	if logrus.GetLevel() != logrus.WarnLevel {
   220  		t.Fatalf("expected warn log level, got %v", logrus.GetLevel())
   221  	}
   222  }
   223  
   224  func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
   225  	c := &daemon.Config{}
   226  	common := &cliflags.CommonFlags{}
   227  
   228  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   229  	flags.String([]string{"-tlscacert"}, "", "")
   230  	flags.String([]string{"-log-driver"}, "", "")
   231  
   232  	f, err := ioutil.TempFile("", "docker-config-")
   233  	if err != nil {
   234  		t.Fatal(err)
   235  	}
   236  	configFile := f.Name()
   237  	defer os.Remove(configFile)
   238  
   239  	f.Write([]byte(`{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`))
   240  	f.Close()
   241  
   242  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   243  	if err != nil {
   244  		t.Fatal(err)
   245  	}
   246  	if loadedConfig == nil {
   247  		t.Fatal("expected configuration, got nil")
   248  	}
   249  	if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" {
   250  		t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile)
   251  	}
   252  	if loadedConfig.LogConfig.Type != "syslog" {
   253  		t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type)
   254  	}
   255  }
   256  
   257  func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
   258  	c := &daemon.Config{}
   259  	common := &cliflags.CommonFlags{}
   260  	flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
   261  	c.ServiceOptions.InstallCliFlags(flags, absentFromHelp)
   262  
   263  	f, err := ioutil.TempFile("", "docker-config-")
   264  	if err != nil {
   265  		t.Fatal(err)
   266  	}
   267  	configFile := f.Name()
   268  	defer os.Remove(configFile)
   269  
   270  	f.Write([]byte(`{"registry-mirrors": ["https://mirrors.docker.com"], "insecure-registries": ["https://insecure.docker.com"], "disable-legacy-registry": true}`))
   271  	f.Close()
   272  
   273  	loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
   274  	if err != nil {
   275  		t.Fatal(err)
   276  	}
   277  	if loadedConfig == nil {
   278  		t.Fatal("expected configuration, got nil")
   279  	}
   280  
   281  	m := loadedConfig.Mirrors
   282  	if len(m) != 1 {
   283  		t.Fatalf("expected 1 mirror, got %d", len(m))
   284  	}
   285  
   286  	r := loadedConfig.InsecureRegistries
   287  	if len(r) != 1 {
   288  		t.Fatalf("expected 1 insecure registries, got %d", len(r))
   289  	}
   290  
   291  	if !loadedConfig.V2Only {
   292  		t.Fatal("expected disable-legacy-registry to be true, got false")
   293  	}
   294  }