2015-12-21 22:43:59 +00:00
|
|
|
package client
|
2015-07-01 08:16:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2015-07-12 17:12:48 +00:00
|
|
|
|
2015-12-18 18:17:54 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-07-12 17:12:48 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2015-07-01 08:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type arguments struct {
|
|
|
|
args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
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"),
|
|
|
|
}
|
2015-12-18 18:17:54 +00:00
|
|
|
valids := map[*arguments]*types.ExecConfig{
|
2015-07-01 08:16:36 +00:00
|
|
|
&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: "uid",
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 18:17:54 +00:00
|
|
|
func compareExecConfig(config1 *types.ExecConfig, config2 *types.ExecConfig) bool {
|
2015-07-01 08:16:36 +00:00
|
|
|
if config1.AttachStderr != config2.AttachStderr {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config1.AttachStdin != config2.AttachStdin {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config1.AttachStdout != config2.AttachStdout {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config1.Container != config2.Container {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config1.Detach != config2.Detach {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config1.Privileged != config2.Privileged {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config1.Tty != config2.Tty {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config1.User != config2.User {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(config1.Cmd) != len(config2.Cmd) {
|
|
|
|
return false
|
2015-07-12 17:12:48 +00:00
|
|
|
}
|
|
|
|
for index, value := range config1.Cmd {
|
|
|
|
if value != config2.Cmd[index] {
|
|
|
|
return false
|
2015-07-01 08:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|