func TestLoadDaemonCliConfigWithConflicts(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{} f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{"labels": ["l3=foo"]}`)) f.Close() var labels []string flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{daemonConfigFileFlag}, "", "") flags.Var(opts.NewNamedListOptsRef("labels", &labels, opts.ValidateLabel), []string{"-label"}, "") flags.Set(daemonConfigFileFlag, configFile) if err := flags.Set("-label", "l1=bar"); err != nil { t.Fatal(err) } if err := flags.Set("-label", "l2=baz"); err != nil { t.Fatal(err) } _, err = loadDaemonCliConfig(c, flags, common, configFile) if err == nil { t.Fatalf("expected configuration error, got nil") } if !strings.Contains(err.Error(), "labels") { t.Fatalf("expected labels conflict, got %v", err) } }
func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{"-tlscacert"}, "", "") flags.String([]string{"-log-driver"}, "", "") f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`)) f.Close() loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatal("expected configuration, got nil") } if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" { t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile) } if loadedConfig.LogConfig.Type != "syslog" { t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type) } }
func TestLoadDaemonConfigWithTrueDefaultValuesLeaveDefaults(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "") f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } if err := flags.ParseFlags([]string{}, false); err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{}`)) f.Close() loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatal("expected configuration, got nil") } if !loadedConfig.EnableUserlandProxy { t.Fatal("expected userland proxy to be enabled, got disabled") } }
func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{} f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{"log-level": "warn"}`)) f.Close() flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{"-log-level"}, "", "") loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatalf("expected configuration %v, got nil", c) } if loadedConfig.LogLevel != "warn" { t.Fatalf("expected warn log level, got %v", loadedConfig.LogLevel) } if logrus.GetLevel() != logrus.WarnLevel { t.Fatalf("expected warn log level, got %v", logrus.GetLevel()) } }
func prepare(args []string) (dockerOpt *docker.CreateContainerOptions, removeOnExit bool, eqCfg config.Config, err error) { if len(args) < 3 { // FIXME err = fmt.Errorf("bad argument: %s", args) return } flagSet := flag.NewFlagSet("run", flag.ExitOnError) dockerOpt, err = parseRun(flagSet, args[1:]) if err != nil { return } removeOnExit = flagSet.IsSet("-rm") eqCfgPath := flagSet.Lookup("-eq-config").Value.String() eqCfg, err = newConfig(eqCfgPath) if err != nil { err = fmt.Errorf("bad config: %s", err) return } log.Debugf("Earthquake Config=%s", eqCfg) if err = checkPrerequisite(eqCfg); err != nil { err = fmt.Errorf("prerequisite error: %s", err) } return }
func rewriteTLSFlags(dcli *DockerfCli, machine string, cluster string) { if machine == "" { return } tlsFlagArgs := func(machineName string) []string { mp := dmachine.NewMachineProxy("container-tls-config") if flags, err := mp.Config(machineName, cluster); err != nil { fmt.Printf("Load Machine(name:%s, cluster:%s) configs error:%s\n", machine, cluster, err.Error()) return []string{} } else { return strings.Split(flags, " ") } }(machine) tlsFlagset := flag.NewFlagSet("tls-docker-machine", flag.ExitOnError) flTls = tlsFlagset.Bool([]string{"-tls"}, false, "Use TLS; implied by --tlsverify") flTlsVerify = tlsFlagset.Bool([]string{"-tlsverify"}, false, "Use TLS and verify the remote") flCa = tlsFlagset.String([]string{"-tlscacert"}, "", "Trust certs signed only by this CA") flCert = tlsFlagset.String([]string{"-tlscert"}, "", "Path to TLS certificate file") flKey = tlsFlagset.String([]string{"-tlskey"}, "", "Path to TLS key file") listOpts := opts.NewListOpts(nil) tlsFlagset.Var(&listOpts, []string{"H", "-host"}, "Daemon socket(s) to connect to") tlsFlagset.Parse(tlsFlagArgs) flHosts = listOpts.GetAll() }
func TestLoadDaemonConfigWithLegacyRegistryOptions(t *testing.T) { c := &daemon.Config{} common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) c.ServiceOptions.InstallCliFlags(flags, absentFromHelp) f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() defer os.Remove(configFile) f.Write([]byte(`{"disable-legacy-registry": true}`)) f.Close() loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatal("expected configuration, got nil") } if !loadedConfig.V2Only { t.Fatal("expected disable-legacy-registry to be true, got false") } }
func TestLoadDaemonConfigWithNetwork(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{"-bip"}, "", "") flags.String([]string{"-ip"}, "", "") f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{"bip": "127.0.0.2", "ip": "127.0.0.1"}`)) f.Close() loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatalf("expected configuration %v, got nil", c) } if loadedConfig.IP != "127.0.0.2" { t.Fatalf("expected IP 127.0.0.2, got %v", loadedConfig.IP) } if loadedConfig.DefaultIP.String() != "127.0.0.1" { t.Fatalf("expected DefaultIP 127.0.0.1, got %s", loadedConfig.DefaultIP) } }
// Subcmd is borrowed from Docker UI and performs the same function of configuring the subCmds func (cli *NetworkCli) Subcmd(chain, name, signature, description string, exitOnError bool) *flag.FlagSet { var errorHandling flag.ErrorHandling if exitOnError { errorHandling = flag.ExitOnError } else { errorHandling = flag.ContinueOnError } flags := flag.NewFlagSet(name, errorHandling) flags.Usage = func() { flags.ShortUsage() flags.PrintDefaults() } flags.ShortUsage = func() { options := "" if signature != "" { signature = " " + signature } if flags.FlagCountUndeprecated() > 0 { options = " [OPTIONS]" } fmt.Fprintf(cli.out, "\nUsage: %s %s%s%s\n\n%s\n\n", chain, name, options, signature, description) flags.SetOutput(cli.out) } return flags }
func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{ TLSOptions: &tlsconfig.Options{ CAFile: "/tmp/ca.pem", }, } f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{"tlsverify": true}`)) f.Close() flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.Bool([]string{"-tlsverify"}, false, "") loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatalf("expected configuration %v, got nil", c) } if !loadedConfig.TLS { t.Fatalf("expected TLS enabled, got %q", loadedConfig) } }
func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) { c := &daemon.Config{} common := &cliflags.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) c.ServiceOptions.InstallCliFlags(flags, absentFromHelp) f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() defer os.Remove(configFile) f.Write([]byte(`{"registry-mirrors": ["https://mirrors.docker.com"], "insecure-registries": ["https://insecure.docker.com"]}`)) f.Close() loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatal("expected configuration, got nil") } m := loadedConfig.Mirrors if len(m) != 1 { t.Fatalf("expected 1 mirror, got %d", len(m)) } r := loadedConfig.InsecureRegistries if len(r) != 1 { t.Fatalf("expected 1 insecure registries, got %d", len(r)) } }
func (cli *DockerCli) Subcmd(name, signature, description string) *flag.FlagSet { flags := flag.NewFlagSet(name, flag.ContinueOnError) flags.Usage = func() { fmt.Fprintf(cli.err, "\nUsage: docker %s %s\n\n%s\n\n", name, signature, description) flags.PrintDefaults() os.Exit(2) } return flags }
// RUN some command yo // // run a command and commit the image. Args are automatically prepended with // 'sh -c' in the event there is only one argument. The difference in // processing: // // RUN echo hi # sh -c echo hi // RUN [ "echo", "hi" ] # echo hi // func run(b *Builder, args []string, attributes map[string]bool, original string) error { if b.image == "" && !b.noBaseImage { return fmt.Errorf("Please provide a source image with `from` prior to run") } args = handleJsonArgs(args, attributes) if !attributes["json"] { args = append([]string{"/bin/sh", "-c"}, args...) } runCmd := flag.NewFlagSet("run", flag.ContinueOnError) runCmd.SetOutput(ioutil.Discard) runCmd.Usage = nil config, _, _, err := runconfig.Parse(runCmd, append([]string{b.image}, args...)) if err != nil { return err } cmd := b.Config.Cmd // set Cmd manually, this is special case only for Dockerfiles b.Config.Cmd = config.Cmd runconfig.Merge(b.Config, config) defer func(cmd []string) { b.Config.Cmd = cmd }(cmd) logrus.Debugf("[BUILDER] Command to be executed: %v", b.Config.Cmd) hit, err := b.probeCache() if err != nil { return err } if hit { return nil } c, err := b.create() if err != nil { return err } // Ensure that we keep the container mounted until the commit // to avoid unmounting and then mounting directly again c.Mount() defer c.Unmount() err = b.run(c) if err != nil { return err } if err := b.commit(c.ID, cmd, "run"); err != nil { return err } return nil }
func (cli *DockerCli) Subcmd(name, signature, description string) *flag.FlagSet { flags := flag.NewFlagSet(name, flag.ContinueOnError) flags.Usage = func() { options := "" if flags.FlagCountUndeprecated() > 0 { options = "[OPTIONS] " } fmt.Fprintf(cli.err, "\nUsage: docker %s %s%s\n\n%s\n\n", name, options, signature, description) flags.PrintDefaults() os.Exit(2) } return flags }
func TestFindConfigurationConflictsWithUnknownKeys(t *testing.T) { config := map[string]interface{}{"tls-verify": "true"} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.Bool([]string{"-tlsverify"}, false, "") err := findConfigurationConflicts(config, flags) if err == nil { t.Fatal("expected error, got nil") } if !strings.Contains(err.Error(), "the following directives don't match any configuration option: tls-verify") { t.Fatalf("expected tls-verify conflict, got %v", err) } }
func TestFindConfigurationConflictsWithMergedValues(t *testing.T) { var hosts []string config := map[string]interface{}{"hosts": "tcp://127.0.0.1:2345"} base := mflag.NewFlagSet("base", mflag.ContinueOnError) base.Var(opts.NewNamedListOptsRef("hosts", &hosts, nil), []string{"H", "-host"}, "") flags := mflag.NewFlagSet("test", mflag.ContinueOnError) mflag.Merge(flags, base) err := findConfigurationConflicts(config, flags) if err != nil { t.Fatal(err) } flags.Set("-host", "unix:///var/run/docker.sock") err = findConfigurationConflicts(config, flags) if err == nil { t.Fatal("expected error, got nil") } if !strings.Contains(err.Error(), "hosts: (from flag: [unix:///var/run/docker.sock], from file: tcp://127.0.0.1:2345)") { t.Fatalf("expected hosts conflict, got %v", err) } }
func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{ Debug: true, LogLevel: "info", } f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{"log-opts": {"max-size": "1k"}}`)) f.Close() flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{daemonConfigFileFlag}, "", "") flags.BoolVar(&c.EnableSelinuxSupport, []string{"-selinux-enabled"}, true, "") flags.StringVar(&c.LogConfig.Type, []string{"-log-driver"}, "json-file", "") flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "") flags.Set(daemonConfigFileFlag, configFile) loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatalf("expected configuration %v, got nil", c) } if !loadedConfig.Debug { t.Fatalf("expected debug mode, got false") } if loadedConfig.LogLevel != "info" { t.Fatalf("expected info log level, got %v", loadedConfig.LogLevel) } if !loadedConfig.EnableSelinuxSupport { t.Fatalf("expected enabled selinux support, got disabled") } if loadedConfig.LogConfig.Type != "json-file" { t.Fatalf("expected LogConfig type json-file, got %v", loadedConfig.LogConfig.Type) } if maxSize := loadedConfig.LogConfig.Config["max-size"]; maxSize != "1k" { t.Fatalf("expected log max-size `1k`, got %s", maxSize) } }
func TestFindConfigurationConflicts(t *testing.T) { config := map[string]interface{}{"authorization-plugins": "foobar"} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{"-authorization-plugins"}, "", "") if err := flags.Set("-authorization-plugins", "asdf"); err != nil { t.Fatal(err) } err := findConfigurationConflicts(config, flags) if err == nil { t.Fatal("expected error, got nil") } if !strings.Contains(err.Error(), "authorization-plugins: (from flag: asdf, from file: foobar)") { t.Fatalf("expected authorization-plugins conflict, got %v", err) } }
// Subcmd is a subcommand of the main "docker" command. // A subcommand represents an action that can be performed // from the Docker command line client. // // Multiple subcommand synopses may be provided with one 'Usage' line being // printed for each in the following way: // // Usage: docker <subcmd-name> [OPTIONS] <synopsis 0> // docker <subcmd-name> [OPTIONS] <synopsis 1> // ... // // If no undeprecated flags are added to the returned FlagSet, "[OPTIONS]" will // not be included on the usage synopsis lines. If no synopses are given, only // one usage synopsis line will be printed with nothing following the // "[OPTIONS]" section // // To see all available subcommands, run "docker --help". func (cli *DockerCli) Subcmd(name string, synopses []string, description string, exitOnError bool) *flag.FlagSet { var errorHandling flag.ErrorHandling if exitOnError { errorHandling = flag.ExitOnError } else { errorHandling = flag.ContinueOnError } flags := flag.NewFlagSet(name, errorHandling) flags.Usage = func() { flags.ShortUsage() flags.PrintDefaults() } flags.ShortUsage = func() { options := "" if flags.FlagCountUndeprecated() > 0 { options = " [OPTIONS]" } if len(synopses) == 0 { synopses = []string{""} } // Allow for multiple command usage synopses. for i, synopsis := range synopses { lead := "\t" if i == 0 { // First line needs the word 'Usage'. lead = "Usage:\t" } if synopsis != "" { synopsis = " " + synopsis } fmt.Fprintf(flags.Out(), "\n%sdocker %s%s%s", lead, name, options, synopsis) } fmt.Fprintf(flags.Out(), "\n\n%s\n", description) } return flags }
func initDaemon(c *cli.Context) (*daemon.Daemon, *graph.TagStore, *graph.Graph, graphdriver.Driver) { t, tc, g, d := initTagStoreAndConfig(c) config := &daemon.Config{} config.DisableBridge = true config.GraphDriver = c.GlobalString("driver") config.GraphOptions = c.GlobalStringSlice("storage-opt") home := c.GlobalString("home") config.Root = home config.TrustKeyPath = filepath.Join(c.GlobalString("configdir"), "key.json") flags := mflag.NewFlagSet("graphc", mflag.ExitOnError) config.InstallFlags(flags, func(string) string { return "" }) daemon, err := daemon.NewDaemon(config, tc.Registry) if err != nil { fmt.Printf("Failed to instantiate daemon: %s\n", err) os.Exit(1) } return daemon, t, g, d }
func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{ Debug: true, } flags := mflag.NewFlagSet("test", mflag.ContinueOnError) loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz") if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatalf("expected configuration %v, got nil", c) } if !loadedConfig.Debug { t.Fatalf("expected debug to be copied from the common flags, got false") } }
func GetClusterSubCmdFlags(name, signature, description string, exitOnError bool) *flag.FlagSet { fs := flag.NewFlagSet(name, flag.ExitOnError) fs.Usage = func() { options := "" if fs.FlagCountUndeprecated() > 0 { options = " [OPTIONS]" if signature != "" { signature = " " + signature } } fmt.Fprintf(os.Stdout, "Usage: dockerf cluster %s%s%s\n\n%s\n\n", name, options, signature, description) fs.SetOutput(os.Stdout) fs.PrintDefaults() os.Exit(0) } return fs }
func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "") f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } if err := flags.ParseFlags([]string{}, false); err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{ "userland-proxy": false }`)) f.Close() loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatal("expected configuration, got nil") } if loadedConfig.EnableUserlandProxy { t.Fatal("expected userland proxy to be disabled, got enabled") } // make sure reloading doesn't generate configuration // conflicts after normalizing boolean values. err = daemon.ReloadConfiguration(configFile, flags, func(reloadedConfig *daemon.Config) { if reloadedConfig.EnableUserlandProxy { t.Fatal("expected userland proxy to be disabled, got enabled") } }) if err != nil { t.Fatal(err) } }
func TestLoadDaemonConfigWithMapOptions(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.Var(opts.NewNamedMapOpts("cluster-store-opts", c.ClusterOpts, nil), []string{"-cluster-store-opt"}, "") flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "") f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{ "cluster-store-opts": {"kv.cacertfile": "/var/lib/docker/discovery_certs/ca.pem"}, "log-opts": {"tag": "test"} }`)) f.Close() loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile) if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatal("expected configuration, got nil") } if loadedConfig.ClusterOpts == nil { t.Fatal("expected cluster options, got nil") } expectedPath := "/var/lib/docker/discovery_certs/ca.pem" if caPath := loadedConfig.ClusterOpts["kv.cacertfile"]; caPath != expectedPath { t.Fatalf("expected %s, got %s", expectedPath, caPath) } if loadedConfig.LogConfig.Config == nil { t.Fatal("expected log config options, got nil") } if tag := loadedConfig.LogConfig.Config["tag"]; tag != "test" { t.Fatalf("expected log tag `test`, got %s", tag) } }
func InitDockerCfg(mirrors []string, insecureRegistries []string, graphdriver, root string) { if dockerCfg.LogConfig.Config == nil { dockerCfg.LogConfig.Config = make(map[string]string) } dockerCfg.LogConfig.Config = make(map[string]string) var errhandler flag.ErrorHandling = flag.ContinueOnError flags := flag.NewFlagSet("", errhandler) dockerCfg.InstallFlags(flags, presentInHelp) dockerCfg.GraphDriver = graphdriver dockerCfg.Root = root dockerCfg.TrustKeyPath = path.Join(root, "keys") // disable docker network flags.Set("-bridge", "none") flags.Set("-iptables", "false") flags.Set("-ipmasq", "false") // disable log driver dockerCfg.LogConfig.Type = "none" // debug mode if glog.V(3) { dockerutils.EnableDebug() } registryOpts := ®istry.Options{ Mirrors: opts.NewListOpts(nil), InsecureRegistries: opts.NewListOpts(nil), } registryOpts.InstallFlags(flags, absentFromHelp) for _, m := range mirrors { registryOpts.Mirrors.Set(m) } for _, ir := range insecureRegistries { registryOpts.InsecureRegistries.Set(ir) } registryCfg = registry.NewService(registryOpts) }
func (cli *DockerCli) Subcmd(name, signature, description string, exitOnError bool) *flag.FlagSet { var errorHandling flag.ErrorHandling if exitOnError { errorHandling = flag.ExitOnError } else { errorHandling = flag.ContinueOnError } flags := flag.NewFlagSet(name, errorHandling) flags.Usage = func() { options := "" if flags.FlagCountUndeprecated() > 0 { options = "[OPTIONS] " } fmt.Fprintf(cli.out, "\nUsage: docker %s %s%s\n\n%s\n\n", name, options, signature, description) flags.SetOutput(cli.out) flags.PrintDefaults() os.Exit(0) } return flags }
func (c *Container) parseCmd() { flags := flag.NewFlagSet("run", flag.ExitOnError) flRemove := flags.Bool([]string{"#rm", "-rm"}, false, "") flDetach := flags.Bool([]string{"d", "-detach"}, false, "") flName := flags.String([]string{"#name", "-name"}, "", "") args, err := shlex.Split(c.ContainerCfg.Cmd) if err != nil { c.Err = err return } log.Debugf("Parsing [%s]", strings.Join(args, ",")) c.Config, c.HostConfig, _, c.Err = runconfig.Parse(flags, args) c.Name = *flName c.detach = *flDetach c.remove = *flRemove }
func TestFindConfigurationConflictsWithNamedOptions(t *testing.T) { config := map[string]interface{}{"hosts": []string{"qwer"}} flags := mflag.NewFlagSet("test", mflag.ContinueOnError) var hosts []string flags.Var(opts.NewNamedListOptsRef("hosts", &hosts, opts.ValidateHost), []string{"H", "-host"}, "Daemon socket(s) to connect to") if err := flags.Set("-host", "tcp://127.0.0.1:4444"); err != nil { t.Fatal(err) } if err := flags.Set("H", "unix:///var/run/docker.sock"); err != nil { t.Fatal(err) } err := findConfigurationConflicts(config, flags) if err == nil { t.Fatal("expected error, got nil") } if !strings.Contains(err.Error(), "hosts") { t.Fatalf("expected hosts conflict, got %v", err) } }
func TestLoadDaemonCliConfigWithTLS(t *testing.T) { c := &daemon.Config{} common := &cli.CommonFlags{ TLS: true, TLSOptions: &tlsconfig.Options{ CAFile: "/tmp/ca.pem", }, } flags := mflag.NewFlagSet("test", mflag.ContinueOnError) loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz") if err != nil { t.Fatal(err) } if loadedConfig == nil { t.Fatalf("expected configuration %v, got nil", c) } if loadedConfig.CommonTLSOptions.CAFile != "/tmp/ca.pem" { t.Fatalf("expected /tmp/ca.pem, got %s: %q", loadedConfig.CommonTLSOptions.CAFile, loadedConfig) } }
func TestDaemonConfigurationMergeConflictsWithInnerStructs(t *testing.T) { f, err := ioutil.TempFile("", "docker-config-") if err != nil { t.Fatal(err) } configFile := f.Name() f.Write([]byte(`{"tlscacert": "/etc/certificates/ca.pem"}`)) f.Close() flags := mflag.NewFlagSet("test", mflag.ContinueOnError) flags.String([]string{"tlscacert"}, "", "") flags.Set("tlscacert", "~/.docker/ca.pem") _, err = MergeDaemonConfigurations(&Config{}, flags, configFile) if err == nil { t.Fatal("expected error, got nil") } if !strings.Contains(err.Error(), "tlscacert") { t.Fatalf("expected tlscacert conflict, got %v", err) } }