Docker: Fixed an issue with container unmarshaling which prevented

docker.restore() to work properly.
This commit is contained in:
Andrea Luzzardi 2013-01-25 14:09:21 -08:00
parent 8070ce8bfc
commit 1a65969e9a
3 changed files with 34 additions and 2 deletions

View File

@ -70,7 +70,7 @@ func loadContainer(containerPath string) (*Container, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
var container *Container container := &Container{}
if err := json.Unmarshal(data, container); err != nil { if err := json.Unmarshal(data, container); err != nil {
return nil, err return nil, err
} }

View File

@ -4,6 +4,7 @@ import (
"container/list" "container/list"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"path" "path"
) )
@ -82,7 +83,7 @@ func (docker *Docker) restore() error {
for _, v := range dir { for _, v := range dir {
container, err := loadContainer(path.Join(docker.repository, v.Name())) container, err := loadContainer(path.Join(docker.repository, v.Name()))
if err != nil { if err != nil {
fmt.Errorf("Failed to load %v: %v", v.Name(), err) log.Printf("Failed to load container %v: %v", v.Name(), err)
continue continue
} }
docker.containers.PushBack(container) docker.containers.PushBack(container)

View File

@ -173,3 +173,34 @@ func TestGet(t *testing.T) {
} }
} }
func TestRestore(t *testing.T) {
root, err := ioutil.TempDir("", "docker-test")
if err != nil {
t.Fatal(err)
}
docker, err := NewFromDirectory(root)
if err != nil {
t.Fatal(err)
}
container, err := docker.Create(
"test",
"ls",
[]string{"-al"},
[]string{"/var/lib/docker/images/ubuntu"},
&Config{},
)
if len(docker.List()) != 1 {
t.Errorf("Expected 1 container, %v found", len(docker.List()))
}
defer docker.Destroy(container)
docker2, err := NewFromDirectory(root)
if err != nil {
t.Fatal(err)
}
if len(docker2.List()) != 1 {
t.Errorf("Expected 1 container, %v found", len(docker2.List()))
}
}