2014-03-08 02:04:38 +00:00
|
|
|
package graph
|
2013-11-14 06:08:08 +00:00
|
|
|
|
|
|
|
import (
|
2014-03-08 02:04:38 +00:00
|
|
|
"bytes"
|
2014-10-02 01:26:06 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
_ "github.com/docker/docker/daemon/graphdriver/vfs" // import the vfs driver so it is used in the tests
|
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
2013-11-14 06:08:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2013-11-19 22:57:53 +00:00
|
|
|
testImageName = "myapp"
|
|
|
|
testImageID = "foo"
|
2013-11-14 06:08:08 +00:00
|
|
|
)
|
|
|
|
|
2014-03-08 02:04:38 +00:00
|
|
|
func fakeTar() (io.Reader, error) {
|
2014-04-28 15:04:28 +00:00
|
|
|
uid := os.Getuid()
|
|
|
|
gid := os.Getgid()
|
|
|
|
|
2014-03-08 02:04:38 +00:00
|
|
|
content := []byte("Hello world!\n")
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buf)
|
|
|
|
for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
|
|
|
|
hdr := new(tar.Header)
|
2014-04-28 15:04:28 +00:00
|
|
|
|
|
|
|
// Leaving these fields blank requires root privileges
|
|
|
|
hdr.Uid = uid
|
|
|
|
hdr.Gid = gid
|
|
|
|
|
2014-03-08 02:04:38 +00:00
|
|
|
hdr.Size = int64(len(content))
|
|
|
|
hdr.Name = name
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tw.Write([]byte(content))
|
|
|
|
}
|
|
|
|
tw.Close()
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2013-11-14 06:08:08 +00:00
|
|
|
func mkTestTagStore(root string, t *testing.T) *TagStore {
|
2014-06-05 08:34:20 +00:00
|
|
|
driver, err := graphdriver.New(root, nil)
|
2013-11-19 22:57:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
graph, err := NewGraph(root, driver)
|
2013-11-14 06:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-18 18:48:19 +00:00
|
|
|
store, err := NewTagStore(path.Join(root, "tags"), graph, nil)
|
2013-11-14 06:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
archive, err := fakeTar()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-03-08 01:36:47 +00:00
|
|
|
img := &image.Image{ID: testImageID}
|
2014-08-28 10:57:54 +00:00
|
|
|
if err := graph.Register(img, nil, archive); err != nil {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := store.Set(testImageName, "", testImageID, false); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLookupImage(t *testing.T) {
|
|
|
|
tmp, err := utils.TestDirectory("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
store := mkTestTagStore(tmp, t)
|
2013-11-20 13:52:06 +00:00
|
|
|
defer store.graph.driver.Cleanup()
|
2013-11-14 06:08:08 +00:00
|
|
|
|
|
|
|
if img, err := store.LookupImage(testImageName); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if img == nil {
|
|
|
|
t.Errorf("Expected 1 image, none found")
|
|
|
|
}
|
|
|
|
if img, err := store.LookupImage(testImageName + ":" + DEFAULTTAG); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if img == nil {
|
|
|
|
t.Errorf("Expected 1 image, none found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if img, err := store.LookupImage(testImageName + ":" + "fail"); err == nil {
|
|
|
|
t.Errorf("Expected error, none found")
|
|
|
|
} else if img != nil {
|
|
|
|
t.Errorf("Expected 0 image, 1 found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if img, err := store.LookupImage("fail:fail"); err == nil {
|
|
|
|
t.Errorf("Expected error, none found")
|
|
|
|
} else if img != nil {
|
|
|
|
t.Errorf("Expected 0 image, 1 found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if img, err := store.LookupImage(testImageID); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if img == nil {
|
|
|
|
t.Errorf("Expected 1 image, none found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if img, err := store.LookupImage(testImageName + ":" + testImageID); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if img == nil {
|
|
|
|
t.Errorf("Expected 1 image, none found")
|
|
|
|
}
|
|
|
|
}
|
2014-09-15 21:13:54 +00:00
|
|
|
|
|
|
|
func TestValidTagName(t *testing.T) {
|
|
|
|
validTags := []string{"9", "foo", "foo-test", "bar.baz.boo"}
|
|
|
|
for _, tag := range validTags {
|
2014-09-29 09:52:13 +00:00
|
|
|
if err := ValidateTagName(tag); err != nil {
|
2014-09-15 21:13:54 +00:00
|
|
|
t.Errorf("'%s' should've been a valid tag", tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidTagName(t *testing.T) {
|
|
|
|
validTags := []string{"-9", ".foo", "-test", ".", "-"}
|
|
|
|
for _, tag := range validTags {
|
2014-09-29 09:52:13 +00:00
|
|
|
if err := ValidateTagName(tag); err == nil {
|
2014-09-15 21:13:54 +00:00
|
|
|
t.Errorf("'%s' shouldn't have been a valid tag", tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-02 01:26:06 +00:00
|
|
|
|
|
|
|
func TestOfficialName(t *testing.T) {
|
|
|
|
names := map[string]bool{
|
|
|
|
"library/ubuntu": true,
|
|
|
|
"nonlibrary/ubuntu": false,
|
|
|
|
"ubuntu": true,
|
|
|
|
"other/library": false,
|
|
|
|
}
|
|
|
|
for name, isOfficial := range names {
|
|
|
|
result := isOfficialName(name)
|
|
|
|
if result != isOfficial {
|
|
|
|
t.Errorf("Unexpected result for %s\n\tExpecting: %v\n\tActual: %v", name, isOfficial, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|