Bläddra i källkod

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

Solomon Hykes 12 år sedan
förälder
incheckning
6ce64e8458
5 ändrade filer med 88 tillägg och 62 borttagningar
  1. 3 3
      commands.go
  2. 5 4
      container.go
  3. 60 41
      container_test.go
  4. 2 2
      runtime.go
  5. 18 12
      runtime_test.go

+ 3 - 3
commands.go

@@ -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())
 	}

+ 5 - 4
container.go

@@ -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 {

+ 60 - 41
container_test.go

@@ -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,9 +115,9 @@ func TestOutput(t *testing.T) {
 	}
 	defer nuke(runtime)
 	container, err := runtime.Create(
-		GetTestImage(runtime).Id,
 		&Config{
-			Cmd: []string{"echo", "-n", "foobar"},
+			Image: GetTestImage(runtime).Id,
+			Cmd:   []string{"echo", "-n", "foobar"},
 		},
 	)
 	if err != nil {
@@ -139,8 +139,9 @@ func TestKill(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"cat", "/dev/zero"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"cat", "/dev/zero"},
 	},
 	)
 	if err != nil {
@@ -180,8 +181,10 @@ func TestExitCode(t *testing.T) {
 	}
 	defer nuke(runtime)
 
-	trueContainer, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"/bin/true", ""},
+	trueContainer, err := runtime.Create(&Config{
+
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"/bin/true", ""},
 	},
 	)
 	if err != nil {
@@ -192,8 +195,9 @@ func TestExitCode(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	falseContainer, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"/bin/false", ""},
+	falseContainer, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"/bin/false", ""},
 	},
 	)
 	if err != nil {
@@ -219,8 +223,9 @@ func TestRestart(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"echo", "-n", "foobar"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"echo", "-n", "foobar"},
 	},
 	)
 	if err != nil {
@@ -251,8 +256,9 @@ func TestRestartStdin(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"cat"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"cat"},
 
 		OpenStdin: true,
 	},
@@ -300,8 +306,9 @@ func TestUser(t *testing.T) {
 	defer nuke(runtime)
 
 	// Default user must be root
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"id"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"id"},
 	},
 	)
 	if err != nil {
@@ -317,8 +324,9 @@ func TestUser(t *testing.T) {
 	}
 
 	// Set a username
-	container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"id"},
+	container, err = runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"id"},
 
 		User: "root",
 	},
@@ -336,8 +344,9 @@ func TestUser(t *testing.T) {
 	}
 
 	// Set a UID
-	container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"id"},
+	container, err = runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"id"},
 
 		User: "0",
 	},
@@ -355,8 +364,9 @@ func TestUser(t *testing.T) {
 	}
 
 	// Set a different user by uid
-	container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"id"},
+	container, err = runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"id"},
 
 		User: "1",
 	},
@@ -376,8 +386,9 @@ func TestUser(t *testing.T) {
 	}
 
 	// Set a different user by username
-	container, err = runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"id"},
+	container, err = runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"id"},
 
 		User: "daemon",
 	},
@@ -402,8 +413,9 @@ func TestMultipleContainers(t *testing.T) {
 	}
 	defer nuke(runtime)
 
-	container1, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"cat", "/dev/zero"},
+	container1, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"cat", "/dev/zero"},
 	},
 	)
 	if err != nil {
@@ -411,8 +423,9 @@ func TestMultipleContainers(t *testing.T) {
 	}
 	defer runtime.Destroy(container1)
 
-	container2, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"cat", "/dev/zero"},
+	container2, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"cat", "/dev/zero"},
 	},
 	)
 	if err != nil {
@@ -452,8 +465,9 @@ func TestStdin(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"cat"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"cat"},
 
 		OpenStdin: true,
 	},
@@ -485,8 +499,9 @@ func TestTty(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"cat"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"cat"},
 
 		OpenStdin: true,
 	},
@@ -518,8 +533,9 @@ func TestEnv(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"/usr/bin/env"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"/usr/bin/env"},
 	},
 	)
 	if err != nil {
@@ -590,8 +606,9 @@ func TestLXCConfig(t *testing.T) {
 	memMin := 33554432
 	memMax := 536870912
 	mem := memMin + rand.Intn(memMax-memMin)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"/bin/true"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"/bin/true"},
 
 		Hostname: "foobar",
 		Memory:   int64(mem),
@@ -616,8 +633,9 @@ func BenchmarkRunSequencial(b *testing.B) {
 	}
 	defer nuke(runtime)
 	for i := 0; i < b.N; i++ {
-		container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-			Cmd: []string{"echo", "-n", "foo"},
+		container, err := runtime.Create(&Config{
+			Image: GetTestImage(runtime).Id,
+			Cmd:   []string{"echo", "-n", "foo"},
 		},
 		)
 		if err != nil {
@@ -650,8 +668,9 @@ 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{
-				Cmd: []string{"echo", "-n", "foo"},
+			container, err := runtime.Create(&Config{
+				Image: GetTestImage(runtime).Id,
+				Cmd:   []string{"echo", "-n", "foo"},
 			},
 			)
 			if err != nil {

+ 2 - 2
runtime.go

@@ -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
 	}

+ 18 - 12
runtime_test.go

@@ -112,8 +112,9 @@ 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{
-		Cmd: []string{"ls", "-al"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"ls", "-al"},
 	},
 	)
 	if err != nil {
@@ -158,8 +159,9 @@ func TestDestroy(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"ls", "-al"},
+	container, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"ls", "-al"},
 	},
 	)
 	if err != nil {
@@ -204,8 +206,9 @@ func TestGet(t *testing.T) {
 		t.Fatal(err)
 	}
 	defer nuke(runtime)
-	container1, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"ls", "-al"},
+	container1, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"ls", "-al"},
 	},
 	)
 	if err != nil {
@@ -213,8 +216,9 @@ func TestGet(t *testing.T) {
 	}
 	defer runtime.Destroy(container1)
 
-	container2, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"ls", "-al"},
+	container2, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"ls", "-al"},
 	},
 	)
 	if err != nil {
@@ -222,8 +226,9 @@ func TestGet(t *testing.T) {
 	}
 	defer runtime.Destroy(container2)
 
-	container3, err := runtime.Create(GetTestImage(runtime).Id, &Config{
-		Cmd: []string{"ls", "-al"},
+	container3, err := runtime.Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"ls", "-al"},
 	},
 	)
 	if err != nil {
@@ -264,8 +269,9 @@ func TestRestore(t *testing.T) {
 	}
 
 	// Create a container with one instance of docker
-	container1, err := runtime1.Create(GetTestImage(runtime1).Id, &Config{
-		Cmd: []string{"ls", "-al"},
+	container1, err := runtime1.Create(&Config{
+		Image: GetTestImage(runtime1).Id,
+		Cmd:   []string{"ls", "-al"},
 	},
 	)
 	if err != nil {