github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cmd/dockerd/daemon_test.go (about) 1 package main 2 3 import ( 4 "testing" 5 6 "github.com/Sirupsen/logrus" 7 cliflags "github.com/docker/docker/cli/flags" 8 "github.com/docker/docker/daemon" 9 "github.com/docker/docker/pkg/testutil/assert" 10 "github.com/docker/docker/pkg/testutil/tempfile" 11 "github.com/spf13/pflag" 12 ) 13 14 func defaultOptions(configFile string) daemonOptions { 15 opts := daemonOptions{ 16 daemonConfig: &daemon.Config{}, 17 flags: &pflag.FlagSet{}, 18 common: cliflags.NewCommonOptions(), 19 } 20 opts.common.InstallFlags(opts.flags) 21 opts.daemonConfig.InstallFlags(opts.flags) 22 opts.flags.StringVar(&opts.configFile, flagDaemonConfigFile, defaultDaemonConfigFile, "") 23 opts.configFile = configFile 24 return opts 25 } 26 27 func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) { 28 opts := defaultOptions("") 29 opts.common.Debug = true 30 31 loadedConfig, err := loadDaemonCliConfig(opts) 32 assert.NilError(t, err) 33 assert.NotNil(t, loadedConfig) 34 if !loadedConfig.Debug { 35 t.Fatalf("expected debug to be copied from the common flags, got false") 36 } 37 } 38 39 func TestLoadDaemonCliConfigWithTLS(t *testing.T) { 40 opts := defaultOptions("") 41 opts.common.TLSOptions.CAFile = "/tmp/ca.pem" 42 opts.common.TLS = true 43 44 loadedConfig, err := loadDaemonCliConfig(opts) 45 assert.NilError(t, err) 46 assert.NotNil(t, loadedConfig) 47 assert.Equal(t, loadedConfig.CommonTLSOptions.CAFile, "/tmp/ca.pem") 48 } 49 50 func TestLoadDaemonCliConfigWithConflicts(t *testing.T) { 51 tempFile := tempfile.NewTempFile(t, "config", `{"labels": ["l3=foo"]}`) 52 defer tempFile.Remove() 53 configFile := tempFile.Name() 54 55 opts := defaultOptions(configFile) 56 flags := opts.flags 57 58 assert.NilError(t, flags.Set(flagDaemonConfigFile, configFile)) 59 assert.NilError(t, flags.Set("label", "l1=bar")) 60 assert.NilError(t, flags.Set("label", "l2=baz")) 61 62 _, err := loadDaemonCliConfig(opts) 63 assert.Error(t, err, "as a flag and in the configuration file: labels") 64 } 65 66 func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) { 67 tempFile := tempfile.NewTempFile(t, "config", `{"tlsverify": true}`) 68 defer tempFile.Remove() 69 70 opts := defaultOptions(tempFile.Name()) 71 opts.common.TLSOptions.CAFile = "/tmp/ca.pem" 72 73 loadedConfig, err := loadDaemonCliConfig(opts) 74 assert.NilError(t, err) 75 assert.NotNil(t, loadedConfig) 76 assert.Equal(t, loadedConfig.TLS, true) 77 } 78 79 func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) { 80 tempFile := tempfile.NewTempFile(t, "config", `{"tlsverify": false}`) 81 defer tempFile.Remove() 82 83 opts := defaultOptions(tempFile.Name()) 84 opts.common.TLSOptions.CAFile = "/tmp/ca.pem" 85 86 loadedConfig, err := loadDaemonCliConfig(opts) 87 assert.NilError(t, err) 88 assert.NotNil(t, loadedConfig) 89 assert.Equal(t, loadedConfig.TLS, true) 90 } 91 92 func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) { 93 tempFile := tempfile.NewTempFile(t, "config", `{}`) 94 defer tempFile.Remove() 95 96 opts := defaultOptions(tempFile.Name()) 97 opts.common.TLSOptions.CAFile = "/tmp/ca.pem" 98 99 loadedConfig, err := loadDaemonCliConfig(opts) 100 assert.NilError(t, err) 101 assert.NotNil(t, loadedConfig) 102 assert.Equal(t, loadedConfig.TLS, false) 103 } 104 105 func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) { 106 tempFile := tempfile.NewTempFile(t, "config", `{"log-level": "warn"}`) 107 defer tempFile.Remove() 108 109 opts := defaultOptions(tempFile.Name()) 110 loadedConfig, err := loadDaemonCliConfig(opts) 111 assert.NilError(t, err) 112 assert.NotNil(t, loadedConfig) 113 assert.Equal(t, loadedConfig.LogLevel, "warn") 114 assert.Equal(t, logrus.GetLevel(), logrus.WarnLevel) 115 } 116 117 func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) { 118 content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}` 119 tempFile := tempfile.NewTempFile(t, "config", content) 120 defer tempFile.Remove() 121 122 opts := defaultOptions(tempFile.Name()) 123 loadedConfig, err := loadDaemonCliConfig(opts) 124 assert.NilError(t, err) 125 assert.NotNil(t, loadedConfig) 126 assert.Equal(t, loadedConfig.CommonTLSOptions.CAFile, "/etc/certs/ca.pem") 127 assert.Equal(t, loadedConfig.LogConfig.Type, "syslog") 128 } 129 130 func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) { 131 content := `{ 132 "registry-mirrors": ["https://mirrors.docker.com"], 133 "insecure-registries": ["https://insecure.docker.com"] 134 }` 135 tempFile := tempfile.NewTempFile(t, "config", content) 136 defer tempFile.Remove() 137 138 opts := defaultOptions(tempFile.Name()) 139 loadedConfig, err := loadDaemonCliConfig(opts) 140 assert.NilError(t, err) 141 assert.NotNil(t, loadedConfig) 142 143 assert.Equal(t, len(loadedConfig.Mirrors), 1) 144 assert.Equal(t, len(loadedConfig.InsecureRegistries), 1) 145 }