github.com/vincentwoo/docker@v0.7.3-0.20160116130405-82401a4b13c0/docker/daemon_test.go (about) 1 // +build daemon 2 3 package main 4 5 import ( 6 "io/ioutil" 7 "strings" 8 "testing" 9 10 "github.com/docker/docker/cli" 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 := &cli.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 := &cli.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.TLSOptions.CAFile != "/tmp/ca.pem" { 54 t.Fatalf("expected /tmp/ca.pem, got %s: %q", loadedConfig.TLSOptions.CAFile, loadedConfig) 55 } 56 } 57 58 func TestLoadDaemonCliConfigWithConflicts(t *testing.T) { 59 c := &daemon.Config{} 60 common := &cli.CommonFlags{} 61 f, err := ioutil.TempFile("", "docker-config-") 62 if err != nil { 63 t.Fatal(err) 64 } 65 66 configFile := f.Name() 67 f.Write([]byte(`{"labels": ["l3=foo"]}`)) 68 f.Close() 69 70 var labels []string 71 72 flags := mflag.NewFlagSet("test", mflag.ContinueOnError) 73 flags.String([]string{daemonConfigFileFlag}, "", "") 74 flags.Var(opts.NewNamedListOptsRef("labels", &labels, opts.ValidateLabel), []string{"-label"}, "") 75 76 flags.Set(daemonConfigFileFlag, configFile) 77 if err := flags.Set("-label", "l1=bar"); err != nil { 78 t.Fatal(err) 79 } 80 if err := flags.Set("-label", "l2=baz"); err != nil { 81 t.Fatal(err) 82 } 83 84 _, err = loadDaemonCliConfig(c, flags, common, configFile) 85 if err == nil { 86 t.Fatalf("expected configuration error, got nil") 87 } 88 if !strings.Contains(err.Error(), "labels") { 89 t.Fatalf("expected labels conflict, got %v", err) 90 } 91 }