Moved image name into config. runtime.Create() now receives a single Config parameter

This commit is contained in:
Solomon Hykes 2013-03-23 12:39:09 -07:00
parent 031f91df1a
commit 6ce64e8458
5 changed files with 88 additions and 62 deletions

View File

@ -803,18 +803,18 @@ func (srv *Server) CmdTag(stdin io.ReadCloser, stdout io.Writer, args ...string)
}
func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
image, config, err := ParseRun(args)
config, err := ParseRun(args)
if err != nil {
return err
}
if image == "" {
if config.Image == "" {
return fmt.Errorf("Image not specified")
}
if len(config.Cmd) == 0 {
return fmt.Errorf("Command not specified")
}
// Create new container
container, err := srv.runtime.Create(image, config)
container, err := srv.runtime.Create(config)
if err != nil {
return errors.New("Error creating container: " + err.Error())
}

View File

@ -57,9 +57,10 @@ type Config struct {
OpenStdin bool // Open stdin
Env []string
Cmd []string
Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
}
func ParseRun(args []string) (string, *Config, error) {
func ParseRun(args []string) (*Config, error) {
cmd := flag.NewFlagSet("", flag.ContinueOnError)
cmd.SetOutput(ioutil.Discard)
fl_user := cmd.String("u", "", "Username or UID")
@ -73,9 +74,8 @@ func ParseRun(args []string) (string, *Config, error) {
var fl_env ListOpts
cmd.Var(&fl_env, "e", "Set environment variables")
if err := cmd.Parse(args); err != nil {
return "", nil, err
return nil, err
}
image := cmd.Arg(0)
config := &Config{
Ports: fl_ports,
User: *fl_user,
@ -85,8 +85,9 @@ func ParseRun(args []string) (string, *Config, error) {
Detach: *fl_detach,
Env: fl_env,
Cmd: cmd.Args()[1:],
Image: cmd.Arg(0),
}
return image, config, nil
return config, nil
}
type NetworkSettings struct {

View File

@ -20,8 +20,8 @@ func TestCommitRun(t *testing.T) {
}
defer nuke(runtime)
container1, err := runtime.Create(
GetTestImage(runtime).Id,
&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
Memory: 33554432,
},
@ -53,8 +53,8 @@ func TestCommitRun(t *testing.T) {
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
container2, err := runtime.Create(
img.Id,
&Config{
Image: img.Id,
Memory: 33554432,
Cmd: []string{"cat", "/world"},
},
@ -86,8 +86,8 @@ func TestRun(t *testing.T) {
}
defer nuke(runtime)
container, err := runtime.Create(
GetTestImage(runtime).Id,
&Config{
Image: GetTestImage(runtime).Id,
Memory: 33554432,
Cmd: []string{"ls", "-al"},
},
@ -115,8 +115,8 @@ func TestOutput(t *testing.T) {
}
defer nuke(runtime)
container, err := runtime.Create(
GetTestImage(runtime).Id,
&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"echo", "-n", "foobar"},
},
)
@ -139,7 +139,8 @@ func TestKill(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"cat", "/dev/zero"},
},
)
@ -180,7 +181,9 @@ func TestExitCode(t *testing.T) {
}
defer nuke(runtime)
trueContainer, err := runtime.Create(GetTestImage(runtime).Id, &Config{
trueContainer, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"/bin/true", ""},
},
)
@ -192,7 +195,8 @@ func TestExitCode(t *testing.T) {
t.Fatal(err)
}
falseContainer, err := runtime.Create(GetTestImage(runtime).Id, &Config{
falseContainer, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"/bin/false", ""},
},
)
@ -219,7 +223,8 @@ func TestRestart(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"echo", "-n", "foobar"},
},
)
@ -251,7 +256,8 @@ func TestRestartStdin(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"cat"},
OpenStdin: true,
@ -300,7 +306,8 @@ func TestUser(t *testing.T) {
defer nuke(runtime)
// Default user must be root
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"id"},
},
)
@ -317,7 +324,8 @@ func TestUser(t *testing.T) {
}
// Set a username
container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
container, err = runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"id"},
User: "root",
@ -336,7 +344,8 @@ func TestUser(t *testing.T) {
}
// Set a UID
container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
container, err = runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"id"},
User: "0",
@ -355,7 +364,8 @@ func TestUser(t *testing.T) {
}
// Set a different user by uid
container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
container, err = runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"id"},
User: "1",
@ -376,7 +386,8 @@ func TestUser(t *testing.T) {
}
// Set a different user by username
container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
container, err = runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"id"},
User: "daemon",
@ -402,7 +413,8 @@ func TestMultipleContainers(t *testing.T) {
}
defer nuke(runtime)
container1, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container1, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"cat", "/dev/zero"},
},
)
@ -411,7 +423,8 @@ func TestMultipleContainers(t *testing.T) {
}
defer runtime.Destroy(container1)
container2, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container2, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"cat", "/dev/zero"},
},
)
@ -452,7 +465,8 @@ func TestStdin(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"cat"},
OpenStdin: true,
@ -485,7 +499,8 @@ func TestTty(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"cat"},
OpenStdin: true,
@ -518,7 +533,8 @@ func TestEnv(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"/usr/bin/env"},
},
)
@ -590,7 +606,8 @@ func TestLXCConfig(t *testing.T) {
memMin := 33554432
memMax := 536870912
mem := memMin + rand.Intn(memMax-memMin)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"/bin/true"},
Hostname: "foobar",
@ -616,7 +633,8 @@ func BenchmarkRunSequencial(b *testing.B) {
}
defer nuke(runtime)
for i := 0; i < b.N; i++ {
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"echo", "-n", "foo"},
},
)
@ -650,7 +668,8 @@ func BenchmarkRunParallel(b *testing.B) {
complete := make(chan error)
tasks = append(tasks, complete)
go func(i int, complete chan error) {
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"echo", "-n", "foo"},
},
)

View File

@ -64,9 +64,9 @@ func (runtime *Runtime) containerRoot(id string) string {
return path.Join(runtime.repository, id)
}
func (runtime *Runtime) Create(image string, config *Config) (*Container, error) {
func (runtime *Runtime) Create(config *Config) (*Container, error) {
// Lookup image
img, err := runtime.repositories.LookupImage(image)
img, err := runtime.repositories.LookupImage(config.Image)
if err != nil {
return nil, err
}

View File

@ -112,7 +112,8 @@ func TestRuntimeCreate(t *testing.T) {
if len(runtime.List()) != 0 {
t.Errorf("Expected 0 containers, %v found", len(runtime.List()))
}
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"ls", "-al"},
},
)
@ -158,7 +159,8 @@ func TestDestroy(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"ls", "-al"},
},
)
@ -204,7 +206,8 @@ func TestGet(t *testing.T) {
t.Fatal(err)
}
defer nuke(runtime)
container1, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container1, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"ls", "-al"},
},
)
@ -213,7 +216,8 @@ func TestGet(t *testing.T) {
}
defer runtime.Destroy(container1)
container2, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container2, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"ls", "-al"},
},
)
@ -222,7 +226,8 @@ func TestGet(t *testing.T) {
}
defer runtime.Destroy(container2)
container3, err := runtime.Create(GetTestImage(runtime).Id, &Config{
container3, err := runtime.Create(&Config{
Image: GetTestImage(runtime).Id,
Cmd: []string{"ls", "-al"},
},
)
@ -264,7 +269,8 @@ func TestRestore(t *testing.T) {
}
// Create a container with one instance of docker
container1, err := runtime1.Create(GetTestImage(runtime1).Id, &Config{
container1, err := runtime1.Create(&Config{
Image: GetTestImage(runtime1).Id,
Cmd: []string{"ls", "-al"},
},
)