示例#1
0
func parseRun(args []string) (*Config, *HostConfig, *flag.FlagSet, error) {
	cmd := flag.NewFlagSet("run", flag.ContinueOnError)
	cmd.SetOutput(ioutil.Discard)
	cmd.Usage = nil
	return Parse(cmd, args)
}
示例#2
0
func TestParseExec(t *testing.T) {
	invalids := map[*arguments]error{
		&arguments{[]string{"-unknown"}}: fmt.Errorf("flag provided but not defined: -unknown"),
		&arguments{[]string{"-u"}}:       fmt.Errorf("flag needs an argument: -u"),
		&arguments{[]string{"--user"}}:   fmt.Errorf("flag needs an argument: --user"),
	}
	valids := map[*arguments]*ExecConfig{
		&arguments{
			[]string{"container", "command"},
		}: {
			Container:    "container",
			Cmd:          []string{"command"},
			AttachStdout: true,
			AttachStderr: true,
		},
		&arguments{
			[]string{"container", "command1", "command2"},
		}: {
			Container:    "container",
			Cmd:          []string{"command1", "command2"},
			AttachStdout: true,
			AttachStderr: true,
		},
		&arguments{
			[]string{"-i", "-t", "-u", "uid", "container", "command"},
		}: {
			User:         "******",
			AttachStdin:  true,
			AttachStdout: true,
			AttachStderr: true,
			Tty:          true,
			Container:    "container",
			Cmd:          []string{"command"},
		},
		&arguments{
			[]string{"-d", "container", "command"},
		}: {
			AttachStdin:  false,
			AttachStdout: false,
			AttachStderr: false,
			Detach:       true,
			Container:    "container",
			Cmd:          []string{"command"},
		},
		&arguments{
			[]string{"-t", "-i", "-d", "container", "command"},
		}: {
			AttachStdin:  false,
			AttachStdout: false,
			AttachStderr: false,
			Detach:       true,
			Tty:          true,
			Container:    "container",
			Cmd:          []string{"command"},
		},
	}
	for invalid, expectedError := range invalids {
		cmd := flag.NewFlagSet("exec", flag.ContinueOnError)
		cmd.ShortUsage = func() {}
		cmd.SetOutput(ioutil.Discard)
		_, err := ParseExec(cmd, invalid.args)
		if err == nil || err.Error() != expectedError.Error() {
			t.Fatalf("Expected an error [%v] for %v, got %v", expectedError, invalid, err)
		}

	}
	for valid, expectedExecConfig := range valids {
		cmd := flag.NewFlagSet("exec", flag.ContinueOnError)
		cmd.ShortUsage = func() {}
		cmd.SetOutput(ioutil.Discard)
		execConfig, err := ParseExec(cmd, valid.args)
		if err != nil {
			t.Fatal(err)
		}
		if !compareExecConfig(expectedExecConfig, execConfig) {
			t.Fatalf("Expected [%v] for %v, got [%v]", expectedExecConfig, valid, execConfig)
		}
	}
}