2016-06-20 13:27:56 +00:00
|
|
|
package container
|
2015-07-01 08:16:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2015-07-12 17:12:48 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-07-01 08:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type arguments struct {
|
2016-11-10 02:39:23 +00:00
|
|
|
options execOptions
|
|
|
|
execCmd []string
|
2015-07-01 08:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseExec(t *testing.T) {
|
2015-12-18 18:17:54 +00:00
|
|
|
valids := map[*arguments]*types.ExecConfig{
|
2015-07-01 08:16:36 +00:00
|
|
|
&arguments{
|
2016-06-20 13:27:56 +00:00
|
|
|
execCmd: []string{"command"},
|
2015-07-01 08:16:36 +00:00
|
|
|
}: {
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
},
|
|
|
|
&arguments{
|
2016-06-20 13:27:56 +00:00
|
|
|
execCmd: []string{"command1", "command2"},
|
2015-07-01 08:16:36 +00:00
|
|
|
}: {
|
|
|
|
Cmd: []string{"command1", "command2"},
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
},
|
|
|
|
&arguments{
|
2016-06-20 13:27:56 +00:00
|
|
|
options: execOptions{
|
|
|
|
interactive: true,
|
|
|
|
tty: true,
|
|
|
|
user: "uid",
|
|
|
|
},
|
|
|
|
execCmd: []string{"command"},
|
2015-07-01 08:16:36 +00:00
|
|
|
}: {
|
|
|
|
User: "uid",
|
|
|
|
AttachStdin: true,
|
|
|
|
AttachStdout: true,
|
|
|
|
AttachStderr: true,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
},
|
|
|
|
&arguments{
|
2016-06-20 13:27:56 +00:00
|
|
|
options: execOptions{
|
|
|
|
detach: true,
|
|
|
|
},
|
|
|
|
execCmd: []string{"command"},
|
2015-07-01 08:16:36 +00:00
|
|
|
}: {
|
|
|
|
AttachStdin: false,
|
|
|
|
AttachStdout: false,
|
|
|
|
AttachStderr: false,
|
|
|
|
Detach: true,
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
},
|
|
|
|
&arguments{
|
2016-06-20 13:27:56 +00:00
|
|
|
options: execOptions{
|
|
|
|
tty: true,
|
|
|
|
interactive: true,
|
|
|
|
detach: true,
|
|
|
|
},
|
|
|
|
execCmd: []string{"command"},
|
2015-07-01 08:16:36 +00:00
|
|
|
}: {
|
|
|
|
AttachStdin: false,
|
|
|
|
AttachStdout: false,
|
|
|
|
AttachStderr: false,
|
|
|
|
Detach: true,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"command"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for valid, expectedExecConfig := range valids {
|
2016-11-10 02:39:23 +00:00
|
|
|
execConfig, err := parseExec(&valid.options, valid.execCmd)
|
2015-07-01 08:16:36 +00:00
|
|
|
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.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
|
|
|
|
}
|