From b8547f31e4ba5d4381c572e7efa4bea3ccb80515 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Thu, 21 Mar 2013 00:41:15 -0700 Subject: [PATCH] Renamed Docker{} to Runtime{} for clarity --- commands.go | 54 ++++++------- container.go | 2 +- container_test.go | 196 +++++++++++++++++++++++----------------------- docker.go | 78 +++++++++--------- docker_test.go | 128 +++++++++++++++--------------- 5 files changed, 229 insertions(+), 229 deletions(-) diff --git a/commands.go b/commands.go index 79619ca44d..480e74530e 100644 --- a/commands.go +++ b/commands.go @@ -116,7 +116,7 @@ func (srv *Server) CmdWait(stdin io.ReadCloser, stdout io.Writer, args ...string return nil } for _, name := range cmd.Args() { - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { fmt.Fprintln(stdout, container.Wait()) } else { return errors.New("No such container: " + name) @@ -133,7 +133,7 @@ func (srv *Server) CmdVersion(stdin io.ReadCloser, stdout io.Writer, args ...str // 'docker info': display system-wide information. func (srv *Server) CmdInfo(stdin io.ReadCloser, stdout io.Writer, args ...string) error { - images, _ := srv.containers.graph.All() + images, _ := srv.runtime.graph.All() var imgcount int if images == nil { imgcount = 0 @@ -149,7 +149,7 @@ func (srv *Server) CmdInfo(stdin io.ReadCloser, stdout io.Writer, args ...string return nil } fmt.Fprintf(stdout, "containers: %d\nversion: %s\nimages: %d\n", - len(srv.containers.List()), + len(srv.runtime.List()), VERSION, imgcount) return nil @@ -165,7 +165,7 @@ func (srv *Server) CmdStop(stdin io.ReadCloser, stdout io.Writer, args ...string return nil } for _, name := range cmd.Args() { - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { if err := container.Stop(); err != nil { return err } @@ -187,7 +187,7 @@ func (srv *Server) CmdRestart(stdin io.ReadCloser, stdout io.Writer, args ...str return nil } for _, name := range cmd.Args() { - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { if err := container.Restart(); err != nil { return err } @@ -209,7 +209,7 @@ func (srv *Server) CmdStart(stdin io.ReadCloser, stdout io.Writer, args ...strin return nil } for _, name := range cmd.Args() { - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { if err := container.Start(); err != nil { return err } @@ -231,7 +231,7 @@ func (srv *Server) CmdMount(stdin io.ReadCloser, stdout io.Writer, args ...strin return nil } for _, name := range cmd.Args() { - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { if err := container.EnsureMounted(); err != nil { return err } @@ -254,9 +254,9 @@ func (srv *Server) CmdInspect(stdin io.ReadCloser, stdout io.Writer, args ...str } name := cmd.Arg(0) var obj interface{} - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { obj = container - } else if image, err := srv.containers.graph.Get(name); err != nil { + } else if image, err := srv.runtime.graph.Get(name); err != nil { return err } else if image != nil { obj = image @@ -291,7 +291,7 @@ func (srv *Server) CmdPort(stdin io.ReadCloser, stdout io.Writer, args ...string } name := cmd.Arg(0) privatePort := cmd.Arg(1) - if container := srv.containers.Get(name); container == nil { + if container := srv.runtime.Get(name); container == nil { return errors.New("No such container: " + name) } else { if frontend, exists := container.NetworkSettings.PortMapping[privatePort]; !exists { @@ -311,7 +311,7 @@ func (srv *Server) CmdRmi(stdin io.ReadCloser, stdout io.Writer, args ...string) return nil } for _, name := range cmd.Args() { - if err := srv.containers.graph.Delete(name); err != nil { + if err := srv.runtime.graph.Delete(name); err != nil { return err } } @@ -324,11 +324,11 @@ func (srv *Server) CmdRm(stdin io.ReadCloser, stdout io.Writer, args ...string) return nil } for _, name := range cmd.Args() { - container := srv.containers.Get(name) + container := srv.runtime.Get(name) if container == nil { return errors.New("No such container: " + name) } - if err := srv.containers.Destroy(container); err != nil { + if err := srv.runtime.Destroy(container); err != nil { fmt.Fprintln(stdout, "Error destroying container "+name+": "+err.Error()) } } @@ -342,7 +342,7 @@ func (srv *Server) CmdKill(stdin io.ReadCloser, stdout io.Writer, args ...string return nil } for _, name := range cmd.Args() { - container := srv.containers.Get(name) + container := srv.runtime.Get(name) if container == nil { return errors.New("No such container: " + name) } @@ -390,7 +390,7 @@ func (srv *Server) CmdImport(stdin io.ReadCloser, stdout io.Writer, args ...stri archive = future.ProgressReader(resp.Body, int(resp.ContentLength), stdout) } fmt.Fprintf(stdout, "Unpacking to %s\n", name) - img, err := srv.containers.graph.Create(archive, "", "") + img, err := srv.runtime.graph.Create(archive, "", "") if err != nil { return err } @@ -420,7 +420,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri fmt.Fprintf(w, "NAME\tID\tCREATED\tPARENT\n") } if *quiet { - images, err := srv.containers.graph.All() + images, err := srv.runtime.graph.All() if err != nil { return err } @@ -486,7 +486,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string) if !*quiet { fmt.Fprintf(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tCOMMENT\n") } - for _, container := range srv.containers.List() { + for _, container := range srv.runtime.List() { if !container.State.Running && !*fl_all { continue } @@ -532,7 +532,7 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri cmd.Usage() return nil } - if container := srv.containers.Get(containerName); container != nil { + if container := srv.runtime.Get(containerName); container != nil { // FIXME: freeze the container before copying it to avoid data corruption? // FIXME: this shouldn't be in commands. rwTar, err := container.ExportRw() @@ -540,7 +540,7 @@ func (srv *Server) CmdCommit(stdin io.ReadCloser, stdout io.Writer, args ...stri return err } // Create a new image from the container's base layers + a new layer from container changes - img, err := srv.containers.graph.Create(rwTar, container.Image, "") + img, err := srv.runtime.graph.Create(rwTar, container.Image, "") if err != nil { return err } @@ -563,7 +563,7 @@ func (srv *Server) CmdTar(stdin io.ReadCloser, stdout io.Writer, args ...string) return errors.New("Sparse mode not yet implemented") // FIXME } name := cmd.Arg(0) - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { if err := container.EnsureMounted(); err != nil { return err } @@ -590,7 +590,7 @@ func (srv *Server) CmdDiff(stdin io.ReadCloser, stdout io.Writer, args ...string if cmd.NArg() < 1 { return errors.New("Not enough arguments") } - if container := srv.containers.Get(cmd.Arg(0)); container == nil { + if container := srv.runtime.Get(cmd.Arg(0)); container == nil { return errors.New("No such container") } else { changes, err := container.Changes() @@ -614,7 +614,7 @@ func (srv *Server) CmdLogs(stdin io.ReadCloser, stdout io.Writer, args ...string return nil } name := cmd.Arg(0) - if container := srv.containers.Get(name); container != nil { + if container := srv.runtime.Get(name); container != nil { log_stdout, err := container.ReadLog("stdout") if err != nil { return err @@ -649,7 +649,7 @@ func (srv *Server) CmdAttach(stdin io.ReadCloser, stdout io.Writer, args ...stri return nil } name := cmd.Arg(0) - container := srv.containers.Get(name) + container := srv.runtime.Get(name) if container == nil { return errors.New("No such container: " + name) } @@ -731,7 +731,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) } // Create new container - container, err := srv.containers.Create(cmdline[0], cmdline[1:], name, + container, err := srv.runtime.Create(cmdline[0], cmdline[1:], name, &Config{ Ports: fl_ports, User: *fl_user, @@ -799,16 +799,16 @@ func NewServer() (*Server, error) { // if err != nil { // return nil, err // } - containers, err := New() + runtime, err := New() if err != nil { return nil, err } srv := &Server{ - containers: containers, + runtime: runtime, } return srv, nil } type Server struct { - containers *Docker + runtime *Runtime } diff --git a/container.go b/container.go index 26e18cc1de..80109ee1af 100644 --- a/container.go +++ b/container.go @@ -45,7 +45,7 @@ type Container struct { stdoutLog *os.File stderrLog *os.File - runtime *Docker // FIXME: rename Docker to Runtime for clarity + runtime *Runtime } type Config struct { diff --git a/container_test.go b/container_test.go index 1658e5832c..64558871ac 100644 --- a/container_test.go +++ b/container_test.go @@ -14,15 +14,15 @@ import ( ) func TestCommitRun(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container1, err := docker.Create( + defer nuke(runtime) + container1, err := runtime.Create( "/bin/sh", []string{"-c", "echo hello > /world"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ Memory: 33554432, }, @@ -30,7 +30,7 @@ func TestCommitRun(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container1) + defer runtime.Destroy(container1) if container1.State.Running { t.Errorf("Container shouldn't be running") @@ -46,14 +46,14 @@ func TestCommitRun(t *testing.T) { if err != nil { t.Error(err) } - img, err := docker.graph.Create(rwTar, container1.Image, "unit test commited image") + img, err := runtime.graph.Create(rwTar, container1.Image, "unit test commited image") if err != nil { t.Error(err) } // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world - container2, err := docker.Create( + container2, err := runtime.Create( "cat", []string{"/world"}, img.Id, @@ -64,7 +64,7 @@ func TestCommitRun(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container2) + defer runtime.Destroy(container2) stdout, err := container2.StdoutPipe() stderr, err := container2.StderrPipe() @@ -82,15 +82,15 @@ func TestCommitRun(t *testing.T) { } func TestRun(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "ls", []string{"-al"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ Memory: 33554432, }, @@ -98,7 +98,7 @@ func TestRun(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) if container.State.Running { t.Errorf("Container shouldn't be running") @@ -112,21 +112,21 @@ func TestRun(t *testing.T) { } func TestOutput(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "echo", []string{"-n", "foobar"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err := container.Output() if err != nil { t.Fatal(err) @@ -137,21 +137,21 @@ func TestOutput(t *testing.T) { } func TestKill(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "cat", []string{"/dev/zero"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) if container.State.Running { t.Errorf("Container shouldn't be running") @@ -179,36 +179,36 @@ func TestKill(t *testing.T) { } func TestExitCode(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) + defer nuke(runtime) - trueContainer, err := docker.Create( + trueContainer, err := runtime.Create( "/bin/true", []string{""}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(trueContainer) + defer runtime.Destroy(trueContainer) if err := trueContainer.Run(); err != nil { t.Fatal(err) } - falseContainer, err := docker.Create( + falseContainer, err := runtime.Create( "/bin/false", []string{""}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(falseContainer) + defer runtime.Destroy(falseContainer) if err := falseContainer.Run(); err != nil { t.Fatal(err) } @@ -223,21 +223,21 @@ func TestExitCode(t *testing.T) { } func TestRestart(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "echo", []string{"-n", "foobar"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err := container.Output() if err != nil { t.Fatal(err) @@ -257,15 +257,15 @@ func TestRestart(t *testing.T) { } func TestRestartStdin(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "cat", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ OpenStdin: true, }, @@ -273,7 +273,7 @@ func TestRestartStdin(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) stdin, err := container.StdinPipe() stdout, err := container.StdoutPipe() @@ -306,23 +306,23 @@ func TestRestartStdin(t *testing.T) { } func TestUser(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) + defer nuke(runtime) // Default user must be root - container, err := docker.Create( + container, err := runtime.Create( "id", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err := container.Output() if err != nil { t.Fatal(err) @@ -332,10 +332,10 @@ func TestUser(t *testing.T) { } // Set a username - container, err = docker.Create( + container, err = runtime.Create( "id", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ User: "root", }, @@ -343,7 +343,7 @@ func TestUser(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err = container.Output() if err != nil || container.State.ExitCode != 0 { t.Fatal(err) @@ -353,10 +353,10 @@ func TestUser(t *testing.T) { } // Set a UID - container, err = docker.Create( + container, err = runtime.Create( "id", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ User: "0", }, @@ -364,7 +364,7 @@ func TestUser(t *testing.T) { if err != nil || container.State.ExitCode != 0 { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err = container.Output() if err != nil || container.State.ExitCode != 0 { t.Fatal(err) @@ -374,10 +374,10 @@ func TestUser(t *testing.T) { } // Set a different user by uid - container, err = docker.Create( + container, err = runtime.Create( "id", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ User: "1", }, @@ -385,7 +385,7 @@ func TestUser(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err = container.Output() if err != nil { t.Fatal(err) @@ -397,10 +397,10 @@ func TestUser(t *testing.T) { } // Set a different user by username - container, err = docker.Create( + container, err = runtime.Create( "id", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ User: "daemon", }, @@ -408,7 +408,7 @@ func TestUser(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err = container.Output() if err != nil || container.State.ExitCode != 0 { t.Fatal(err) @@ -419,33 +419,33 @@ func TestUser(t *testing.T) { } func TestMultipleContainers(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) + defer nuke(runtime) - container1, err := docker.Create( + container1, err := runtime.Create( "cat", []string{"/dev/zero"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container1) + defer runtime.Destroy(container1) - container2, err := docker.Create( + container2, err := runtime.Create( "cat", []string{"/dev/zero"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container2) + defer runtime.Destroy(container2) // Start both containers if err := container1.Start(); err != nil { @@ -474,15 +474,15 @@ func TestMultipleContainers(t *testing.T) { } func TestStdin(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "cat", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ OpenStdin: true, }, @@ -490,7 +490,7 @@ func TestStdin(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) stdin, err := container.StdinPipe() stdout, err := container.StdoutPipe() @@ -509,15 +509,15 @@ func TestStdin(t *testing.T) { } func TestTty(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "cat", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ OpenStdin: true, }, @@ -525,7 +525,7 @@ func TestTty(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) stdin, err := container.StdinPipe() stdout, err := container.StdoutPipe() @@ -544,21 +544,21 @@ func TestTty(t *testing.T) { } func TestEnv(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "/usr/bin/env", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) stdout, err := container.StdoutPipe() if err != nil { t.Fatal(err) @@ -613,20 +613,20 @@ func grepFile(t *testing.T, path string, pattern string) { } func TestLXCConfig(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) + defer nuke(runtime) // Memory is allocated randomly for testing rand.Seed(time.Now().UTC().UnixNano()) memMin := 33554432 memMax := 536870912 mem := memMin + rand.Intn(memMax-memMin) - container, err := docker.Create( + container, err := runtime.Create( "/bin/true", []string{}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{ Hostname: "foobar", Memory: int64(mem), @@ -635,7 +635,7 @@ func TestLXCConfig(t *testing.T) { if err != nil { t.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) container.generateLXCConfig() grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar") grepFile(t, container.lxcConfigPath(), @@ -645,22 +645,22 @@ func TestLXCConfig(t *testing.T) { } func BenchmarkRunSequencial(b *testing.B) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { b.Fatal(err) } - defer nuke(docker) + defer nuke(runtime) for i := 0; i < b.N; i++ { - container, err := docker.Create( + container, err := runtime.Create( "echo", []string{"-n", "foo"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { b.Fatal(err) } - defer docker.Destroy(container) + defer runtime.Destroy(container) output, err := container.Output() if err != nil { b.Fatal(err) @@ -668,18 +668,18 @@ func BenchmarkRunSequencial(b *testing.B) { if string(output) != "foo" { b.Fatalf("Unexecpted output: %v", string(output)) } - if err := docker.Destroy(container); err != nil { + if err := runtime.Destroy(container); err != nil { b.Fatal(err) } } } func BenchmarkRunParallel(b *testing.B) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { b.Fatal(err) } - defer nuke(docker) + defer nuke(runtime) var tasks []chan error @@ -687,17 +687,17 @@ func BenchmarkRunParallel(b *testing.B) { complete := make(chan error) tasks = append(tasks, complete) go func(i int, complete chan error) { - container, err := docker.Create( + container, err := runtime.Create( "echo", []string{"-n", "foo"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { complete <- err return } - defer docker.Destroy(container) + defer runtime.Destroy(container) if err := container.Start(); err != nil { complete <- err return @@ -709,7 +709,7 @@ func BenchmarkRunParallel(b *testing.B) { // if string(output) != "foo" { // complete <- fmt.Errorf("Unexecpted output: %v", string(output)) // } - if err := docker.Destroy(container); err != nil { + if err := runtime.Destroy(container); err != nil { complete <- err return } diff --git a/docker.go b/docker.go index 65654ba752..e362cdafe8 100644 --- a/docker.go +++ b/docker.go @@ -14,7 +14,7 @@ import ( "time" ) -type Docker struct { +type Runtime struct { root string repository string containers *list.List @@ -28,16 +28,16 @@ func init() { sysInitPath = SelfPath() } -func (docker *Docker) List() []*Container { +func (runtime *Runtime) List() []*Container { containers := new(History) - for e := docker.containers.Front(); e != nil; e = e.Next() { + for e := runtime.containers.Front(); e != nil; e = e.Next() { containers.Add(e.Value.(*Container)) } return *containers } -func (docker *Docker) getContainerElement(id string) *list.Element { - for e := docker.containers.Front(); e != nil; e = e.Next() { +func (runtime *Runtime) getContainerElement(id string) *list.Element { + for e := runtime.containers.Front(); e != nil; e = e.Next() { container := e.Value.(*Container) if container.Id == id { return e @@ -46,23 +46,23 @@ func (docker *Docker) getContainerElement(id string) *list.Element { return nil } -func (docker *Docker) Get(id string) *Container { - e := docker.getContainerElement(id) +func (runtime *Runtime) Get(id string) *Container { + e := runtime.getContainerElement(id) if e == nil { return nil } return e.Value.(*Container) } -func (docker *Docker) Exists(id string) bool { - return docker.Get(id) != nil +func (runtime *Runtime) Exists(id string) bool { + return runtime.Get(id) != nil } -func (docker *Docker) containerRoot(id string) string { - return path.Join(docker.repository, id) +func (runtime *Runtime) containerRoot(id string) string { + return path.Join(runtime.repository, id) } -func (docker *Docker) Create(command string, args []string, image string, config *Config) (*Container, error) { +func (runtime *Runtime) Create(command string, args []string, image string, config *Config) (*Container, error) { container := &Container{ // FIXME: we should generate the ID here instead of receiving it as an argument Id: GenerateId(), @@ -75,7 +75,7 @@ func (docker *Docker) Create(command string, args []string, image string, config // FIXME: do we need to store this in the container? SysInitPath: sysInitPath, } - container.root = docker.containerRoot(container.Id) + container.root = runtime.containerRoot(container.Id) // Step 1: create the container directory. // This doubles as a barrier to avoid race conditions. if err := os.Mkdir(container.root, 0700); err != nil { @@ -86,36 +86,36 @@ func (docker *Docker) Create(command string, args []string, image string, config return nil, err } // Step 3: register the container - if err := docker.Register(container); err != nil { + if err := runtime.Register(container); err != nil { return nil, err } return container, nil } -func (docker *Docker) Load(id string) (*Container, error) { - container := &Container{root: docker.containerRoot(id)} +func (runtime *Runtime) Load(id string) (*Container, error) { + container := &Container{root: runtime.containerRoot(id)} if err := container.FromDisk(); err != nil { return nil, err } if container.Id != id { return container, fmt.Errorf("Container %s is stored at %s", container.Id, id) } - if err := docker.Register(container); err != nil { + if err := runtime.Register(container); err != nil { return nil, err } return container, nil } // Register makes a container object usable by the runtime as -func (docker *Docker) Register(container *Container) error { - if container.runtime != nil || docker.Exists(container.Id) { +func (runtime *Runtime) Register(container *Container) error { + if container.runtime != nil || runtime.Exists(container.Id) { return fmt.Errorf("Container is already loaded") } if err := validateId(container.Id); err != nil { return err } - container.runtime = docker - container.networkManager = docker.networkManager // FIXME: infer from docker.runtime + container.runtime = runtime + container.networkManager = runtime.networkManager // FIXME: infer from docker.runtime // Setup state lock (formerly in newState() lock := new(sync.Mutex) container.State.stateChangeLock = lock @@ -130,18 +130,18 @@ func (docker *Docker) Register(container *Container) error { container.stdinPipe = NopWriteCloser(ioutil.Discard) // Silently drop stdin } // Setup logging of stdout and stderr to disk - if err := docker.LogToDisk(container.stdout, container.logPath("stdout")); err != nil { + if err := runtime.LogToDisk(container.stdout, container.logPath("stdout")); err != nil { return err } - if err := docker.LogToDisk(container.stderr, container.logPath("stderr")); err != nil { + if err := runtime.LogToDisk(container.stderr, container.logPath("stderr")); err != nil { return err } // done - docker.containers.PushBack(container) + runtime.containers.PushBack(container) return nil } -func (docker *Docker) LogToDisk(src *writeBroadcaster, dst string) error { +func (runtime *Runtime) LogToDisk(src *writeBroadcaster, dst string) error { log, err := os.OpenFile(dst, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600) if err != nil { return err @@ -150,8 +150,8 @@ func (docker *Docker) LogToDisk(src *writeBroadcaster, dst string) error { return nil } -func (docker *Docker) Destroy(container *Container) error { - element := docker.getContainerElement(container.Id) +func (runtime *Runtime) Destroy(container *Container) error { + element := runtime.getContainerElement(container.Id) if element == nil { return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.Id) } @@ -167,21 +167,21 @@ func (docker *Docker) Destroy(container *Container) error { } } // Deregister the container before removing its directory, to avoid race conditions - docker.containers.Remove(element) + runtime.containers.Remove(element) if err := os.RemoveAll(container.root); err != nil { return fmt.Errorf("Unable to remove filesystem for %v: %v", container.Id, err) } return nil } -func (docker *Docker) restore() error { - dir, err := ioutil.ReadDir(docker.repository) +func (runtime *Runtime) restore() error { + dir, err := ioutil.ReadDir(runtime.repository) if err != nil { return err } for _, v := range dir { id := v.Name() - container, err := docker.Load(id) + container, err := runtime.Load(id) if err != nil { log.Printf("Failed to load container %v: %v", id, err) continue @@ -191,14 +191,14 @@ func (docker *Docker) restore() error { return nil } -func New() (*Docker, error) { +func New() (*Runtime, error) { return NewFromDirectory("/var/lib/docker") } -func NewFromDirectory(root string) (*Docker, error) { - docker_repo := path.Join(root, "containers") +func NewFromDirectory(root string) (*Runtime, error) { + runtime_repo := path.Join(root, "containers") - if err := os.MkdirAll(docker_repo, 0700); err != nil && !os.IsExist(err) { + if err := os.MkdirAll(runtime_repo, 0700); err != nil && !os.IsExist(err) { return nil, err } @@ -211,18 +211,18 @@ func NewFromDirectory(root string) (*Docker, error) { return nil, err } - docker := &Docker{ + runtime := &Runtime{ root: root, - repository: docker_repo, + repository: runtime_repo, containers: list.New(), networkManager: netManager, graph: graph, } - if err := docker.restore(); err != nil { + if err := runtime.restore(); err != nil { return nil, err } - return docker, nil + return runtime, nil } type History []*Container diff --git a/docker_test.go b/docker_test.go index a8329e10dd..093dae67fd 100644 --- a/docker_test.go +++ b/docker_test.go @@ -16,8 +16,8 @@ const unitTestImageName string = "busybox" var unitTestStoreBase string var srv *Server -func nuke(docker *Docker) error { - return os.RemoveAll(docker.root) +func nuke(runtime *Runtime) error { + return os.RemoveAll(runtime.root) } func CopyDirectory(source, dest string) error { @@ -57,13 +57,13 @@ func init() { unitTestStoreBase = root // Make it our Store root - docker, err := NewFromDirectory(root) + runtime, err := NewFromDirectory(root) if err != nil { panic(err) } // Create the "Server" srv := &Server{ - containers: docker, + runtime: runtime, } // Retrieve the Image if err := srv.CmdImport(os.Stdin, os.Stdout, unitTestImageName); err != nil { @@ -71,7 +71,7 @@ func init() { } } -func newTestDocker() (*Docker, error) { +func newTestRuntime() (*Runtime, error) { root, err := ioutil.TempDir("", "docker-test") if err != nil { return nil, err @@ -84,16 +84,16 @@ func newTestDocker() (*Docker, error) { return nil, err } - docker, err := NewFromDirectory(root) + runtime, err := NewFromDirectory(root) if err != nil { return nil, err } - return docker, nil + return runtime, nil } -func GetTestImage(docker *Docker) *graph.Image { - imgs, err := docker.graph.All() +func GetTestImage(runtime *Runtime) *graph.Image { + imgs, err := runtime.graph.All() if err != nil { panic(err) } else if len(imgs) < 1 { @@ -103,20 +103,20 @@ func GetTestImage(docker *Docker) *graph.Image { } func TestCreate(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) + defer nuke(runtime) // Make sure we start we 0 containers - if len(docker.List()) != 0 { - t.Errorf("Expected 0 containers, %v found", len(docker.List())) + if len(runtime.List()) != 0 { + t.Errorf("Expected 0 containers, %v found", len(runtime.List())) } - container, err := docker.Create( + container, err := runtime.Create( "ls", []string{"-al"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { @@ -124,69 +124,69 @@ func TestCreate(t *testing.T) { } defer func() { - if err := docker.Destroy(container); err != nil { + if err := runtime.Destroy(container); err != nil { t.Error(err) } }() // Make sure we can find the newly created container with List() - if len(docker.List()) != 1 { - t.Errorf("Expected 1 container, %v found", len(docker.List())) + if len(runtime.List()) != 1 { + t.Errorf("Expected 1 container, %v found", len(runtime.List())) } // Make sure the container List() returns is the right one - if docker.List()[0].Id != container.Id { - t.Errorf("Unexpected container %v returned by List", docker.List()[0]) + if runtime.List()[0].Id != container.Id { + t.Errorf("Unexpected container %v returned by List", runtime.List()[0]) } // Make sure we can get the container with Get() - if docker.Get(container.Id) == nil { + if runtime.Get(container.Id) == nil { t.Errorf("Unable to get newly created container") } // Make sure it is the right container - if docker.Get(container.Id) != container { + if runtime.Get(container.Id) != container { t.Errorf("Get() returned the wrong container") } // Make sure Exists returns it as existing - if !docker.Exists(container.Id) { + if !runtime.Exists(container.Id) { t.Errorf("Exists() returned false for a newly created container") } } func TestDestroy(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container, err := docker.Create( + defer nuke(runtime) + container, err := runtime.Create( "ls", []string{"-al"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } // Destroy - if err := docker.Destroy(container); err != nil { + if err := runtime.Destroy(container); err != nil { t.Error(err) } - // Make sure docker.Exists() behaves correctly - if docker.Exists("test_destroy") { + // Make sure runtime.Exists() behaves correctly + if runtime.Exists("test_destroy") { t.Errorf("Exists() returned true") } - // Make sure docker.List() doesn't list the destroyed container - if len(docker.List()) != 0 { - t.Errorf("Expected 0 container, %v found", len(docker.List())) + // Make sure runtime.List() doesn't list the destroyed container + if len(runtime.List()) != 0 { + t.Errorf("Expected 0 container, %v found", len(runtime.List())) } - // Make sure docker.Get() refuses to return the unexisting container - if docker.Get(container.Id) != nil { + // Make sure runtime.Get() refuses to return the unexisting container + if runtime.Get(container.Id) != nil { t.Errorf("Unable to get newly created container") } @@ -197,61 +197,61 @@ func TestDestroy(t *testing.T) { } // Test double destroy - if err := docker.Destroy(container); err == nil { + if err := runtime.Destroy(container); err == nil { // It should have failed t.Errorf("Double destroy did not fail") } } func TestGet(t *testing.T) { - docker, err := newTestDocker() + runtime, err := newTestRuntime() if err != nil { t.Fatal(err) } - defer nuke(docker) - container1, err := docker.Create( + defer nuke(runtime) + container1, err := runtime.Create( "ls", []string{"-al"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container1) + defer runtime.Destroy(container1) - container2, err := docker.Create( + container2, err := runtime.Create( "ls", []string{"-al"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container2) + defer runtime.Destroy(container2) - container3, err := docker.Create( + container3, err := runtime.Create( "ls", []string{"-al"}, - GetTestImage(docker).Id, + GetTestImage(runtime).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker.Destroy(container3) + defer runtime.Destroy(container3) - if docker.Get(container1.Id) != container1 { - t.Errorf("Get(test1) returned %v while expecting %v", docker.Get(container1.Id), container1) + if runtime.Get(container1.Id) != container1 { + t.Errorf("Get(test1) returned %v while expecting %v", runtime.Get(container1.Id), container1) } - if docker.Get(container2.Id) != container2 { - t.Errorf("Get(test2) returned %v while expecting %v", docker.Get(container2.Id), container2) + if runtime.Get(container2.Id) != container2 { + t.Errorf("Get(test2) returned %v while expecting %v", runtime.Get(container2.Id), container2) } - if docker.Get(container3.Id) != container3 { - t.Errorf("Get(test3) returned %v while expecting %v", docker.Get(container3.Id), container3) + if runtime.Get(container3.Id) != container3 { + t.Errorf("Get(test3) returned %v while expecting %v", runtime.Get(container3.Id), container3) } } @@ -269,24 +269,24 @@ func TestRestore(t *testing.T) { t.Fatal(err) } - docker1, err := NewFromDirectory(root) + runtime1, err := NewFromDirectory(root) if err != nil { t.Fatal(err) } // Create a container with one instance of docker - container1, err := docker1.Create( + container1, err := runtime1.Create( "ls", []string{"-al"}, - GetTestImage(docker1).Id, + GetTestImage(runtime1).Id, &Config{}, ) if err != nil { t.Fatal(err) } - defer docker1.Destroy(container1) - if len(docker1.List()) != 1 { - t.Errorf("Expected 1 container, %v found", len(docker1.List())) + defer runtime1.Destroy(container1) + if len(runtime1.List()) != 1 { + t.Errorf("Expected 1 container, %v found", len(runtime1.List())) } if err := container1.Run(); err != nil { t.Fatal(err) @@ -294,15 +294,15 @@ func TestRestore(t *testing.T) { // Here are are simulating a docker restart - that is, reloading all containers // from scratch - docker2, err := NewFromDirectory(root) + runtime2, err := NewFromDirectory(root) if err != nil { t.Fatal(err) } - defer nuke(docker2) - if len(docker2.List()) != 1 { - t.Errorf("Expected 1 container, %v found", len(docker2.List())) + defer nuke(runtime2) + if len(runtime2.List()) != 1 { + t.Errorf("Expected 1 container, %v found", len(runtime2.List())) } - container2 := docker2.Get(container1.Id) + container2 := runtime2.Get(container1.Id) if container2 == nil { t.Fatal("Unable to Get container") }