gopkg.in/dotcloud/docker.v1@v1.13.1/daemon/config_test.go (about) 1 package daemon 2 3 import ( 4 "io/ioutil" 5 "os" 6 "runtime" 7 "strings" 8 "testing" 9 10 "github.com/docker/docker/opts" 11 "github.com/docker/docker/pkg/testutil/assert" 12 "github.com/spf13/pflag" 13 ) 14 15 func TestDaemonConfigurationNotFound(t *testing.T) { 16 _, err := MergeDaemonConfigurations(&Config{}, nil, "/tmp/foo-bar-baz-docker") 17 if err == nil || !os.IsNotExist(err) { 18 t.Fatalf("expected does not exist error, got %v", err) 19 } 20 } 21 22 func TestDaemonBrokenConfiguration(t *testing.T) { 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(`{"Debug": tru`)) 30 f.Close() 31 32 _, err = MergeDaemonConfigurations(&Config{}, nil, configFile) 33 if err == nil { 34 t.Fatalf("expected error, got %v", err) 35 } 36 } 37 38 func TestParseClusterAdvertiseSettings(t *testing.T) { 39 if runtime.GOOS == "solaris" { 40 t.Skip("ClusterSettings not supported on Solaris\n") 41 } 42 _, err := parseClusterAdvertiseSettings("something", "") 43 if err != errDiscoveryDisabled { 44 t.Fatalf("expected discovery disabled error, got %v\n", err) 45 } 46 47 _, err = parseClusterAdvertiseSettings("", "something") 48 if err == nil { 49 t.Fatalf("expected discovery store error, got %v\n", err) 50 } 51 52 _, err = parseClusterAdvertiseSettings("etcd", "127.0.0.1:8080") 53 if err != nil { 54 t.Fatal(err) 55 } 56 } 57 58 func TestFindConfigurationConflicts(t *testing.T) { 59 config := map[string]interface{}{"authorization-plugins": "foobar"} 60 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 61 62 flags.String("authorization-plugins", "", "") 63 assert.NilError(t, flags.Set("authorization-plugins", "asdf")) 64 65 assert.Error(t, 66 findConfigurationConflicts(config, flags), 67 "authorization-plugins: (from flag: asdf, from file: foobar)") 68 } 69 70 func TestFindConfigurationConflictsWithNamedOptions(t *testing.T) { 71 config := map[string]interface{}{"hosts": []string{"qwer"}} 72 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 73 74 var hosts []string 75 flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, opts.ValidateHost), "host", "H", "Daemon socket(s) to connect to") 76 assert.NilError(t, flags.Set("host", "tcp://127.0.0.1:4444")) 77 assert.NilError(t, flags.Set("host", "unix:///var/run/docker.sock")) 78 79 assert.Error(t, findConfigurationConflicts(config, flags), "hosts") 80 } 81 82 func TestDaemonConfigurationMergeConflicts(t *testing.T) { 83 f, err := ioutil.TempFile("", "docker-config-") 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 configFile := f.Name() 89 f.Write([]byte(`{"debug": true}`)) 90 f.Close() 91 92 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 93 flags.Bool("debug", false, "") 94 flags.Set("debug", "false") 95 96 _, err = MergeDaemonConfigurations(&Config{}, flags, configFile) 97 if err == nil { 98 t.Fatal("expected error, got nil") 99 } 100 if !strings.Contains(err.Error(), "debug") { 101 t.Fatalf("expected debug conflict, got %v", err) 102 } 103 } 104 105 func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) { 106 f, err := ioutil.TempFile("", "docker-config-") 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 configFile := f.Name() 112 f.Write([]byte(`{"tlscacert": "/etc/certificates/ca.pem"}`)) 113 f.Close() 114 115 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 116 flags.String("tlscacert", "", "") 117 flags.Set("tlscacert", "~/.docker/ca.pem") 118 119 _, err = MergeDaemonConfigurations(&Config{}, flags, configFile) 120 if err == nil { 121 t.Fatal("expected error, got nil") 122 } 123 if !strings.Contains(err.Error(), "tlscacert") { 124 t.Fatalf("expected tlscacert conflict, got %v", err) 125 } 126 } 127 128 func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) { 129 config := map[string]interface{}{"tls-verify": "true"} 130 flags := pflag.NewFlagSet("test", pflag.ContinueOnError) 131 132 flags.Bool("tlsverify", false, "") 133 err := findConfigurationConflicts(config, flags) 134 if err == nil { 135 t.Fatal("expected error, got nil") 136 } 137 if !strings.Contains(err.Error(), "the following directives don't match any configuration option: tls-verify") { 138 t.Fatalf("expected tls-verify conflict, got %v", err) 139 } 140 } 141 142 func TestFindConfigurationConflictsWithMergedValues(t *testing.T) { 143 var hosts []string 144 config := map[string]interface{}{"hosts": "tcp://127.0.0.1:2345"} 145 flags := pflag.NewFlagSet("base", pflag.ContinueOnError) 146 flags.VarP(opts.NewNamedListOptsRef("hosts", &hosts, nil), "host", "H", "") 147 148 err := findConfigurationConflicts(config, flags) 149 if err != nil { 150 t.Fatal(err) 151 } 152 153 flags.Set("host", "unix:///var/run/docker.sock") 154 err = findConfigurationConflicts(config, flags) 155 if err == nil { 156 t.Fatal("expected error, got nil") 157 } 158 if !strings.Contains(err.Error(), "hosts: (from flag: [unix:///var/run/docker.sock], from file: tcp://127.0.0.1:2345)") { 159 t.Fatalf("expected hosts conflict, got %v", err) 160 } 161 } 162 163 func TestValidateConfiguration(t *testing.T) { 164 c1 := &Config{ 165 CommonConfig: CommonConfig{ 166 Labels: []string{"one"}, 167 }, 168 } 169 170 err := ValidateConfiguration(c1) 171 if err == nil { 172 t.Fatal("expected error, got nil") 173 } 174 175 c2 := &Config{ 176 CommonConfig: CommonConfig{ 177 Labels: []string{"one=two"}, 178 }, 179 } 180 181 err = ValidateConfiguration(c2) 182 if err != nil { 183 t.Fatalf("expected no error, got error %v", err) 184 } 185 186 c3 := &Config{ 187 CommonConfig: CommonConfig{ 188 DNS: []string{"1.1.1.1"}, 189 }, 190 } 191 192 err = ValidateConfiguration(c3) 193 if err != nil { 194 t.Fatalf("expected no error, got error %v", err) 195 } 196 197 c4 := &Config{ 198 CommonConfig: CommonConfig{ 199 DNS: []string{"1.1.1.1o"}, 200 }, 201 } 202 203 err = ValidateConfiguration(c4) 204 if err == nil { 205 t.Fatal("expected error, got nil") 206 } 207 208 c5 := &Config{ 209 CommonConfig: CommonConfig{ 210 DNSSearch: []string{"a.b.c"}, 211 }, 212 } 213 214 err = ValidateConfiguration(c5) 215 if err != nil { 216 t.Fatalf("expected no error, got error %v", err) 217 } 218 219 c6 := &Config{ 220 CommonConfig: CommonConfig{ 221 DNSSearch: []string{"123456"}, 222 }, 223 } 224 225 err = ValidateConfiguration(c6) 226 if err == nil { 227 t.Fatal("expected error, got nil") 228 } 229 }