diff --git a/commands/commands.go b/commands.go similarity index 99% rename from commands/commands.go rename to commands.go index da8585cc93..03125687a0 100644 --- a/commands/commands.go +++ b/commands.go @@ -1,4 +1,4 @@ -package commands +package docker import ( "bufio" @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/dotcloud/docker" "github.com/dotcloud/docker/fs" "github.com/dotcloud/docker/future" "github.com/dotcloud/docker/rcli" @@ -550,7 +549,7 @@ func (srv *Server) CmdPs(stdin io.ReadCloser, stdout io.Writer, args ...string) if !*quiet { command := fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " ")) if !*fl_full { - command = docker.Trunc(command, 20) + command = Trunc(command, 20) } for idx, field := range []string{ /* ID */ container.Id, @@ -741,10 +740,10 @@ func (srv *Server) CmdLogs(stdin io.ReadCloser, stdout io.Writer, args ...string return errors.New("No such container: " + cmd.Arg(0)) } -func (srv *Server) CreateContainer(img *fs.Image, ports []int, user string, tty bool, openStdin bool, memory int64, comment string, cmd string, args ...string) (*docker.Container, error) { +func (srv *Server) CreateContainer(img *fs.Image, ports []int, user string, tty bool, openStdin bool, memory int64, comment string, cmd string, args ...string) (*Container, error) { id := future.RandomId()[:8] container, err := srv.containers.Create(id, cmd, args, img, - &docker.Config{ + &Config{ Hostname: id, Ports: ports, User: user, @@ -945,12 +944,12 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) return nil } -func New() (*Server, error) { +func NewServer() (*Server, error) { future.Seed() // if err != nil { // return nil, err // } - containers, err := docker.New() + containers, err := New() if err != nil { return nil, err } @@ -1001,6 +1000,6 @@ func (srv *Server) CmdWeb(stdin io.ReadCloser, stdout io.Writer, args ...string) } type Server struct { - containers *docker.Docker + containers *Docker images *fs.Store } diff --git a/docker/docker.go b/docker/docker.go index ad121a8f60..79fe582e97 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -3,7 +3,6 @@ package main import ( "flag" "github.com/dotcloud/docker" - "github.com/dotcloud/docker/commands" "github.com/dotcloud/docker/future" "github.com/dotcloud/docker/rcli" "github.com/dotcloud/docker/term" @@ -36,7 +35,7 @@ func main() { } func daemon() error { - service, err := commands.New() + service, err := docker.NewServer() if err != nil { return err } @@ -78,7 +77,7 @@ func runCommand(args []string) error { } } } else { - service, err := commands.New() + service, err := docker.NewServer() if err != nil { return err } diff --git a/docker_test.go b/docker_test.go index 3f51e8d84c..003ef5fa9b 100644 --- a/docker_test.go +++ b/docker_test.go @@ -4,17 +4,28 @@ import ( "github.com/dotcloud/docker/fs" "io" "io/ioutil" - "log" "os" + "os/exec" "testing" ) const testLayerPath string = "/var/lib/docker/docker-ut.tar" +const unitTestImageName string = "busybox" + +var unitTestStoreBase string +var srv *Server func nuke(docker *Docker) error { return os.RemoveAll(docker.root) } +func CopyDirectory(source, dest string) error { + if _, err := exec.Command("cp", "-ra", source, dest).Output(); err != nil { + return err + } + return nil +} + func layerArchive(tarfile string) (io.Reader, error) { // FIXME: need to close f somewhere f, err := os.Open(tarfile) @@ -28,15 +39,29 @@ func init() { // Hack to run sys init during unit testing if SelfPath() == "/sbin/init" { SysInit() + return } - // Make sure the unit test image is there - if _, err := os.Stat(testLayerPath); err != nil { - if !os.IsNotExist(err) { - panic(err) - } - log.Fatalf("Unit test base image not found. Please fix the problem by running \"debootstrap --arch=amd64 quantal %v\"", testLayerPath) - return + // Create a temp directory + root, err := ioutil.TempDir("", "docker-test") + if err != nil { + panic(err) + } + unitTestStoreBase = root + + // Make it our Store root + docker, err := NewFromDirectory(root) + if err != nil { + panic(err) + } + // Create the "Server" + srv := &Server{ + images: docker.Store, + containers: docker, + } + // Retrieve the Image + if err := srv.CmdImport(os.Stdin, os.Stdout, unitTestImageName); err != nil { + panic(err) } } @@ -45,19 +70,19 @@ func newTestDocker() (*Docker, error) { if err != nil { return nil, err } + if err := os.Remove(root); err != nil { + return nil, err + } + if err := CopyDirectory(unitTestStoreBase, root); err != nil { + panic(err) + return nil, err + } + docker, err := NewFromDirectory(root) if err != nil { return nil, err } - if layer, err := layerArchive(testLayerPath); err != nil { - panic(err) - } else { - _, err = docker.Store.Create(layer, nil, "docker-ut", "unit tests") - if err != nil { - panic(err) - } - } return docker, nil } @@ -231,25 +256,22 @@ func TestGet(t *testing.T) { } func TestRestore(t *testing.T) { + root, err := ioutil.TempDir("", "docker-test") if err != nil { t.Fatal(err) } + if err := os.Remove(root); err != nil { + t.Fatal(err) + } + if err := CopyDirectory(unitTestStoreBase, root); err != nil { + t.Fatal(err) + } docker1, err := NewFromDirectory(root) if err != nil { t.Fatal(err) } - defer nuke(docker1) - - if layer, err := layerArchive(testLayerPath); err != nil { - panic(err) - } else { - _, err = docker1.Store.Create(layer, nil, "docker-ut", "unit tests") - if err != nil { - panic(err) - } - } // Create a container with one instance of docker container1, err := docker1.Create(