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