| 366 | 370 |
deleted file mode 100644 |
| ... | ... |
@@ -1,302 +0,0 @@ |
| 1 |
-package docker |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "archive/tar" |
|
| 5 |
- "bytes" |
|
| 6 |
- "errors" |
|
| 7 |
- "github.com/dotcloud/docker/archive" |
|
| 8 |
- "github.com/dotcloud/docker/graphdriver" |
|
| 9 |
- "github.com/dotcloud/docker/utils" |
|
| 10 |
- "io" |
|
| 11 |
- "io/ioutil" |
|
| 12 |
- "os" |
|
| 13 |
- "testing" |
|
| 14 |
- "time" |
|
| 15 |
-) |
|
| 16 |
- |
|
| 17 |
-func TestInit(t *testing.T) {
|
|
| 18 |
- graph := tempGraph(t) |
|
| 19 |
- defer nukeGraph(graph) |
|
| 20 |
- // Root should exist |
|
| 21 |
- if _, err := os.Stat(graph.Root); err != nil {
|
|
| 22 |
- t.Fatal(err) |
|
| 23 |
- } |
|
| 24 |
- // Map() should be empty |
|
| 25 |
- if l, err := graph.Map(); err != nil {
|
|
| 26 |
- t.Fatal(err) |
|
| 27 |
- } else if len(l) != 0 {
|
|
| 28 |
- t.Fatalf("len(Map()) should return %d, not %d", 0, len(l))
|
|
| 29 |
- } |
|
| 30 |
-} |
|
| 31 |
- |
|
| 32 |
-// Test that Register can be interrupted cleanly without side effects |
|
| 33 |
-func TestInterruptedRegister(t *testing.T) {
|
|
| 34 |
- graph := tempGraph(t) |
|
| 35 |
- defer nukeGraph(graph) |
|
| 36 |
- badArchive, w := io.Pipe() // Use a pipe reader as a fake archive which never yields data |
|
| 37 |
- image := &Image{
|
|
| 38 |
- ID: GenerateID(), |
|
| 39 |
- Comment: "testing", |
|
| 40 |
- Created: time.Now(), |
|
| 41 |
- } |
|
| 42 |
- go graph.Register(nil, badArchive, image) |
|
| 43 |
- time.Sleep(200 * time.Millisecond) |
|
| 44 |
- w.CloseWithError(errors.New("But I'm not a tarball!")) // (Nobody's perfect, darling)
|
|
| 45 |
- if _, err := graph.Get(image.ID); err == nil {
|
|
| 46 |
- t.Fatal("Image should not exist after Register is interrupted")
|
|
| 47 |
- } |
|
| 48 |
- // Registering the same image again should succeed if the first register was interrupted |
|
| 49 |
- goodArchive, err := fakeTar() |
|
| 50 |
- if err != nil {
|
|
| 51 |
- t.Fatal(err) |
|
| 52 |
- } |
|
| 53 |
- if err := graph.Register(nil, goodArchive, image); err != nil {
|
|
| 54 |
- t.Fatal(err) |
|
| 55 |
- } |
|
| 56 |
-} |
|
| 57 |
- |
|
| 58 |
-// FIXME: Do more extensive tests (ex: create multiple, delete, recreate; |
|
| 59 |
-// create multiple, check the amount of images and paths, etc..) |
|
| 60 |
-func TestGraphCreate(t *testing.T) {
|
|
| 61 |
- graph := tempGraph(t) |
|
| 62 |
- defer nukeGraph(graph) |
|
| 63 |
- archive, err := fakeTar() |
|
| 64 |
- if err != nil {
|
|
| 65 |
- t.Fatal(err) |
|
| 66 |
- } |
|
| 67 |
- image, err := graph.Create(archive, nil, "Testing", "", nil) |
|
| 68 |
- if err != nil {
|
|
| 69 |
- t.Fatal(err) |
|
| 70 |
- } |
|
| 71 |
- if err := ValidateID(image.ID); err != nil {
|
|
| 72 |
- t.Fatal(err) |
|
| 73 |
- } |
|
| 74 |
- if image.Comment != "Testing" {
|
|
| 75 |
- t.Fatalf("Wrong comment: should be '%s', not '%s'", "Testing", image.Comment)
|
|
| 76 |
- } |
|
| 77 |
- if image.DockerVersion != VERSION {
|
|
| 78 |
- t.Fatalf("Wrong docker_version: should be '%s', not '%s'", VERSION, image.DockerVersion)
|
|
| 79 |
- } |
|
| 80 |
- images, err := graph.Map() |
|
| 81 |
- if err != nil {
|
|
| 82 |
- t.Fatal(err) |
|
| 83 |
- } else if l := len(images); l != 1 {
|
|
| 84 |
- t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
|
|
| 85 |
- } |
|
| 86 |
- if images[image.ID] == nil {
|
|
| 87 |
- t.Fatalf("Could not find image with id %s", image.ID)
|
|
| 88 |
- } |
|
| 89 |
-} |
|
| 90 |
- |
|
| 91 |
-func TestRegister(t *testing.T) {
|
|
| 92 |
- graph := tempGraph(t) |
|
| 93 |
- defer nukeGraph(graph) |
|
| 94 |
- archive, err := fakeTar() |
|
| 95 |
- if err != nil {
|
|
| 96 |
- t.Fatal(err) |
|
| 97 |
- } |
|
| 98 |
- image := &Image{
|
|
| 99 |
- ID: GenerateID(), |
|
| 100 |
- Comment: "testing", |
|
| 101 |
- Created: time.Now(), |
|
| 102 |
- } |
|
| 103 |
- err = graph.Register(nil, archive, image) |
|
| 104 |
- if err != nil {
|
|
| 105 |
- t.Fatal(err) |
|
| 106 |
- } |
|
| 107 |
- if images, err := graph.Map(); err != nil {
|
|
| 108 |
- t.Fatal(err) |
|
| 109 |
- } else if l := len(images); l != 1 {
|
|
| 110 |
- t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
|
|
| 111 |
- } |
|
| 112 |
- if resultImg, err := graph.Get(image.ID); err != nil {
|
|
| 113 |
- t.Fatal(err) |
|
| 114 |
- } else {
|
|
| 115 |
- if resultImg.ID != image.ID {
|
|
| 116 |
- t.Fatalf("Wrong image ID. Should be '%s', not '%s'", image.ID, resultImg.ID)
|
|
| 117 |
- } |
|
| 118 |
- if resultImg.Comment != image.Comment {
|
|
| 119 |
- t.Fatalf("Wrong image comment. Should be '%s', not '%s'", image.Comment, resultImg.Comment)
|
|
| 120 |
- } |
|
| 121 |
- } |
|
| 122 |
-} |
|
| 123 |
- |
|
| 124 |
-// Test that an image can be deleted by its shorthand prefix |
|
| 125 |
-func TestDeletePrefix(t *testing.T) {
|
|
| 126 |
- graph := tempGraph(t) |
|
| 127 |
- defer nukeGraph(graph) |
|
| 128 |
- img := createTestImage(graph, t) |
|
| 129 |
- if err := graph.Delete(utils.TruncateID(img.ID)); err != nil {
|
|
| 130 |
- t.Fatal(err) |
|
| 131 |
- } |
|
| 132 |
- assertNImages(graph, t, 0) |
|
| 133 |
-} |
|
| 134 |
- |
|
| 135 |
-func createTestImage(graph *Graph, t *testing.T) *Image {
|
|
| 136 |
- archive, err := fakeTar() |
|
| 137 |
- if err != nil {
|
|
| 138 |
- t.Fatal(err) |
|
| 139 |
- } |
|
| 140 |
- img, err := graph.Create(archive, nil, "Test image", "", nil) |
|
| 141 |
- if err != nil {
|
|
| 142 |
- t.Fatal(err) |
|
| 143 |
- } |
|
| 144 |
- return img |
|
| 145 |
-} |
|
| 146 |
- |
|
| 147 |
-func TestDelete(t *testing.T) {
|
|
| 148 |
- graph := tempGraph(t) |
|
| 149 |
- defer nukeGraph(graph) |
|
| 150 |
- archive, err := fakeTar() |
|
| 151 |
- if err != nil {
|
|
| 152 |
- t.Fatal(err) |
|
| 153 |
- } |
|
| 154 |
- assertNImages(graph, t, 0) |
|
| 155 |
- img, err := graph.Create(archive, nil, "Bla bla", "", nil) |
|
| 156 |
- if err != nil {
|
|
| 157 |
- t.Fatal(err) |
|
| 158 |
- } |
|
| 159 |
- assertNImages(graph, t, 1) |
|
| 160 |
- if err := graph.Delete(img.ID); err != nil {
|
|
| 161 |
- t.Fatal(err) |
|
| 162 |
- } |
|
| 163 |
- assertNImages(graph, t, 0) |
|
| 164 |
- |
|
| 165 |
- archive, err = fakeTar() |
|
| 166 |
- if err != nil {
|
|
| 167 |
- t.Fatal(err) |
|
| 168 |
- } |
|
| 169 |
- // Test 2 create (same name) / 1 delete |
|
| 170 |
- img1, err := graph.Create(archive, nil, "Testing", "", nil) |
|
| 171 |
- if err != nil {
|
|
| 172 |
- t.Fatal(err) |
|
| 173 |
- } |
|
| 174 |
- archive, err = fakeTar() |
|
| 175 |
- if err != nil {
|
|
| 176 |
- t.Fatal(err) |
|
| 177 |
- } |
|
| 178 |
- if _, err = graph.Create(archive, nil, "Testing", "", nil); err != nil {
|
|
| 179 |
- t.Fatal(err) |
|
| 180 |
- } |
|
| 181 |
- assertNImages(graph, t, 2) |
|
| 182 |
- if err := graph.Delete(img1.ID); err != nil {
|
|
| 183 |
- t.Fatal(err) |
|
| 184 |
- } |
|
| 185 |
- assertNImages(graph, t, 1) |
|
| 186 |
- |
|
| 187 |
- // Test delete wrong name |
|
| 188 |
- if err := graph.Delete("Not_foo"); err == nil {
|
|
| 189 |
- t.Fatalf("Deleting wrong ID should return an error")
|
|
| 190 |
- } |
|
| 191 |
- assertNImages(graph, t, 1) |
|
| 192 |
- |
|
| 193 |
- archive, err = fakeTar() |
|
| 194 |
- if err != nil {
|
|
| 195 |
- t.Fatal(err) |
|
| 196 |
- } |
|
| 197 |
- // Test delete twice (pull -> rm -> pull -> rm) |
|
| 198 |
- if err := graph.Register(nil, archive, img1); err != nil {
|
|
| 199 |
- t.Fatal(err) |
|
| 200 |
- } |
|
| 201 |
- if err := graph.Delete(img1.ID); err != nil {
|
|
| 202 |
- t.Fatal(err) |
|
| 203 |
- } |
|
| 204 |
- assertNImages(graph, t, 1) |
|
| 205 |
-} |
|
| 206 |
- |
|
| 207 |
-func TestByParent(t *testing.T) {
|
|
| 208 |
- archive1, _ := fakeTar() |
|
| 209 |
- archive2, _ := fakeTar() |
|
| 210 |
- archive3, _ := fakeTar() |
|
| 211 |
- |
|
| 212 |
- graph := tempGraph(t) |
|
| 213 |
- defer nukeGraph(graph) |
|
| 214 |
- parentImage := &Image{
|
|
| 215 |
- ID: GenerateID(), |
|
| 216 |
- Comment: "parent", |
|
| 217 |
- Created: time.Now(), |
|
| 218 |
- Parent: "", |
|
| 219 |
- } |
|
| 220 |
- childImage1 := &Image{
|
|
| 221 |
- ID: GenerateID(), |
|
| 222 |
- Comment: "child1", |
|
| 223 |
- Created: time.Now(), |
|
| 224 |
- Parent: parentImage.ID, |
|
| 225 |
- } |
|
| 226 |
- childImage2 := &Image{
|
|
| 227 |
- ID: GenerateID(), |
|
| 228 |
- Comment: "child2", |
|
| 229 |
- Created: time.Now(), |
|
| 230 |
- Parent: parentImage.ID, |
|
| 231 |
- } |
|
| 232 |
- _ = graph.Register(nil, archive1, parentImage) |
|
| 233 |
- _ = graph.Register(nil, archive2, childImage1) |
|
| 234 |
- _ = graph.Register(nil, archive3, childImage2) |
|
| 235 |
- |
|
| 236 |
- byParent, err := graph.ByParent() |
|
| 237 |
- if err != nil {
|
|
| 238 |
- t.Fatal(err) |
|
| 239 |
- } |
|
| 240 |
- numChildren := len(byParent[parentImage.ID]) |
|
| 241 |
- if numChildren != 2 {
|
|
| 242 |
- t.Fatalf("Expected 2 children, found %d", numChildren)
|
|
| 243 |
- } |
|
| 244 |
-} |
|
| 245 |
- |
|
| 246 |
-func assertNImages(graph *Graph, t *testing.T, n int) {
|
|
| 247 |
- if images, err := graph.Map(); err != nil {
|
|
| 248 |
- t.Fatal(err) |
|
| 249 |
- } else if actualN := len(images); actualN != n {
|
|
| 250 |
- t.Fatalf("Expected %d images, found %d", n, actualN)
|
|
| 251 |
- } |
|
| 252 |
-} |
|
| 253 |
- |
|
| 254 |
-/* |
|
| 255 |
- * HELPER FUNCTIONS |
|
| 256 |
- */ |
|
| 257 |
- |
|
| 258 |
-func tempGraph(t *testing.T) *Graph {
|
|
| 259 |
- tmp, err := ioutil.TempDir("", "docker-graph-")
|
|
| 260 |
- if err != nil {
|
|
| 261 |
- t.Fatal(err) |
|
| 262 |
- } |
|
| 263 |
- backend, err := graphdriver.New(tmp) |
|
| 264 |
- if err != nil {
|
|
| 265 |
- t.Fatal(err) |
|
| 266 |
- } |
|
| 267 |
- graph, err := NewGraph(tmp, backend) |
|
| 268 |
- if err != nil {
|
|
| 269 |
- t.Fatal(err) |
|
| 270 |
- } |
|
| 271 |
- return graph |
|
| 272 |
-} |
|
| 273 |
- |
|
| 274 |
-func nukeGraph(graph *Graph) {
|
|
| 275 |
- graph.driver.Cleanup() |
|
| 276 |
- os.RemoveAll(graph.Root) |
|
| 277 |
-} |
|
| 278 |
- |
|
| 279 |
-func testArchive(t *testing.T) archive.Archive {
|
|
| 280 |
- archive, err := fakeTar() |
|
| 281 |
- if err != nil {
|
|
| 282 |
- t.Fatal(err) |
|
| 283 |
- } |
|
| 284 |
- return archive |
|
| 285 |
-} |
|
| 286 |
- |
|
| 287 |
-func fakeTar() (io.Reader, error) {
|
|
| 288 |
- content := []byte("Hello world!\n")
|
|
| 289 |
- buf := new(bytes.Buffer) |
|
| 290 |
- tw := tar.NewWriter(buf) |
|
| 291 |
- for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
|
|
| 292 |
- hdr := new(tar.Header) |
|
| 293 |
- hdr.Size = int64(len(content)) |
|
| 294 |
- hdr.Name = name |
|
| 295 |
- if err := tw.WriteHeader(hdr); err != nil {
|
|
| 296 |
- return nil, err |
|
| 297 |
- } |
|
| 298 |
- tw.Write([]byte(content)) |
|
| 299 |
- } |
|
| 300 |
- tw.Close() |
|
| 301 |
- return buf, nil |
|
| 302 |
-} |
| ... | ... |
@@ -1,12 +1,17 @@ |
| 1 | 1 |
package docker |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "errors" |
|
| 4 | 5 |
"github.com/dotcloud/docker" |
| 6 |
+ "github.com/dotcloud/docker/archive" |
|
| 5 | 7 |
"github.com/dotcloud/docker/graphdriver" |
| 8 |
+ "github.com/dotcloud/docker/utils" |
|
| 9 |
+ "io" |
|
| 6 | 10 |
"io/ioutil" |
| 7 | 11 |
"os" |
| 8 | 12 |
"path" |
| 9 | 13 |
"testing" |
| 14 |
+ "time" |
|
| 10 | 15 |
) |
| 11 | 16 |
|
| 12 | 17 |
func TestMount(t *testing.T) {
|
| ... | ... |
@@ -41,19 +46,272 @@ func TestMount(t *testing.T) {
|
| 41 | 41 |
} |
| 42 | 42 |
} |
| 43 | 43 |
|
| 44 |
-//FIXME: duplicate |
|
| 45 |
-func tempGraph(t *testing.T) (*docker.Graph, graphdriver.Driver) {
|
|
| 46 |
- tmp, err := ioutil.TempDir("", "docker-graph-")
|
|
| 44 |
+func TestInit(t *testing.T) {
|
|
| 45 |
+ graph, _ := tempGraph(t) |
|
| 46 |
+ defer nukeGraph(graph) |
|
| 47 |
+ // Root should exist |
|
| 48 |
+ if _, err := os.Stat(graph.Root); err != nil {
|
|
| 49 |
+ t.Fatal(err) |
|
| 50 |
+ } |
|
| 51 |
+ // Map() should be empty |
|
| 52 |
+ if l, err := graph.Map(); err != nil {
|
|
| 53 |
+ t.Fatal(err) |
|
| 54 |
+ } else if len(l) != 0 {
|
|
| 55 |
+ t.Fatalf("len(Map()) should return %d, not %d", 0, len(l))
|
|
| 56 |
+ } |
|
| 57 |
+} |
|
| 58 |
+ |
|
| 59 |
+// Test that Register can be interrupted cleanly without side effects |
|
| 60 |
+func TestInterruptedRegister(t *testing.T) {
|
|
| 61 |
+ graph, _ := tempGraph(t) |
|
| 62 |
+ defer nukeGraph(graph) |
|
| 63 |
+ badArchive, w := io.Pipe() // Use a pipe reader as a fake archive which never yields data |
|
| 64 |
+ image := &docker.Image{
|
|
| 65 |
+ ID: docker.GenerateID(), |
|
| 66 |
+ Comment: "testing", |
|
| 67 |
+ Created: time.Now(), |
|
| 68 |
+ } |
|
| 69 |
+ go graph.Register(nil, badArchive, image) |
|
| 70 |
+ time.Sleep(200 * time.Millisecond) |
|
| 71 |
+ w.CloseWithError(errors.New("But I'm not a tarball!")) // (Nobody's perfect, darling)
|
|
| 72 |
+ if _, err := graph.Get(image.ID); err == nil {
|
|
| 73 |
+ t.Fatal("Image should not exist after Register is interrupted")
|
|
| 74 |
+ } |
|
| 75 |
+ // Registering the same image again should succeed if the first register was interrupted |
|
| 76 |
+ goodArchive, err := fakeTar() |
|
| 77 |
+ if err != nil {
|
|
| 78 |
+ t.Fatal(err) |
|
| 79 |
+ } |
|
| 80 |
+ if err := graph.Register(nil, goodArchive, image); err != nil {
|
|
| 81 |
+ t.Fatal(err) |
|
| 82 |
+ } |
|
| 83 |
+} |
|
| 84 |
+ |
|
| 85 |
+// FIXME: Do more extensive tests (ex: create multiple, delete, recreate; |
|
| 86 |
+// create multiple, check the amount of images and paths, etc..) |
|
| 87 |
+func TestGraphCreate(t *testing.T) {
|
|
| 88 |
+ graph, _ := tempGraph(t) |
|
| 89 |
+ defer nukeGraph(graph) |
|
| 90 |
+ archive, err := fakeTar() |
|
| 91 |
+ if err != nil {
|
|
| 92 |
+ t.Fatal(err) |
|
| 93 |
+ } |
|
| 94 |
+ image, err := graph.Create(archive, nil, "Testing", "", nil) |
|
| 95 |
+ if err != nil {
|
|
| 96 |
+ t.Fatal(err) |
|
| 97 |
+ } |
|
| 98 |
+ if err := docker.ValidateID(image.ID); err != nil {
|
|
| 99 |
+ t.Fatal(err) |
|
| 100 |
+ } |
|
| 101 |
+ if image.Comment != "Testing" {
|
|
| 102 |
+ t.Fatalf("Wrong comment: should be '%s', not '%s'", "Testing", image.Comment)
|
|
| 103 |
+ } |
|
| 104 |
+ if image.DockerVersion != docker.VERSION {
|
|
| 105 |
+ t.Fatalf("Wrong docker_version: should be '%s', not '%s'", docker.VERSION, image.DockerVersion)
|
|
| 106 |
+ } |
|
| 107 |
+ images, err := graph.Map() |
|
| 108 |
+ if err != nil {
|
|
| 109 |
+ t.Fatal(err) |
|
| 110 |
+ } else if l := len(images); l != 1 {
|
|
| 111 |
+ t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
|
|
| 112 |
+ } |
|
| 113 |
+ if images[image.ID] == nil {
|
|
| 114 |
+ t.Fatalf("Could not find image with id %s", image.ID)
|
|
| 115 |
+ } |
|
| 116 |
+} |
|
| 117 |
+ |
|
| 118 |
+func TestRegister(t *testing.T) {
|
|
| 119 |
+ graph, _ := tempGraph(t) |
|
| 120 |
+ defer nukeGraph(graph) |
|
| 121 |
+ archive, err := fakeTar() |
|
| 122 |
+ if err != nil {
|
|
| 123 |
+ t.Fatal(err) |
|
| 124 |
+ } |
|
| 125 |
+ image := &docker.Image{
|
|
| 126 |
+ ID: docker.GenerateID(), |
|
| 127 |
+ Comment: "testing", |
|
| 128 |
+ Created: time.Now(), |
|
| 129 |
+ } |
|
| 130 |
+ err = graph.Register(nil, archive, image) |
|
| 131 |
+ if err != nil {
|
|
| 132 |
+ t.Fatal(err) |
|
| 133 |
+ } |
|
| 134 |
+ if images, err := graph.Map(); err != nil {
|
|
| 135 |
+ t.Fatal(err) |
|
| 136 |
+ } else if l := len(images); l != 1 {
|
|
| 137 |
+ t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
|
|
| 138 |
+ } |
|
| 139 |
+ if resultImg, err := graph.Get(image.ID); err != nil {
|
|
| 140 |
+ t.Fatal(err) |
|
| 141 |
+ } else {
|
|
| 142 |
+ if resultImg.ID != image.ID {
|
|
| 143 |
+ t.Fatalf("Wrong image ID. Should be '%s', not '%s'", image.ID, resultImg.ID)
|
|
| 144 |
+ } |
|
| 145 |
+ if resultImg.Comment != image.Comment {
|
|
| 146 |
+ t.Fatalf("Wrong image comment. Should be '%s', not '%s'", image.Comment, resultImg.Comment)
|
|
| 147 |
+ } |
|
| 148 |
+ } |
|
| 149 |
+} |
|
| 150 |
+ |
|
| 151 |
+// Test that an image can be deleted by its shorthand prefix |
|
| 152 |
+func TestDeletePrefix(t *testing.T) {
|
|
| 153 |
+ graph, _ := tempGraph(t) |
|
| 154 |
+ defer nukeGraph(graph) |
|
| 155 |
+ img := createTestImage(graph, t) |
|
| 156 |
+ if err := graph.Delete(utils.TruncateID(img.ID)); err != nil {
|
|
| 157 |
+ t.Fatal(err) |
|
| 158 |
+ } |
|
| 159 |
+ assertNImages(graph, t, 0) |
|
| 160 |
+} |
|
| 161 |
+ |
|
| 162 |
+func createTestImage(graph *docker.Graph, t *testing.T) *docker.Image {
|
|
| 163 |
+ archive, err := fakeTar() |
|
| 164 |
+ if err != nil {
|
|
| 165 |
+ t.Fatal(err) |
|
| 166 |
+ } |
|
| 167 |
+ img, err := graph.Create(archive, nil, "Test image", "", nil) |
|
| 47 | 168 |
if err != nil {
|
| 48 | 169 |
t.Fatal(err) |
| 49 | 170 |
} |
| 50 |
- driver, err := graphdriver.New(tmp) |
|
| 171 |
+ return img |
|
| 172 |
+} |
|
| 173 |
+ |
|
| 174 |
+func TestDelete(t *testing.T) {
|
|
| 175 |
+ graph, _ := tempGraph(t) |
|
| 176 |
+ defer nukeGraph(graph) |
|
| 177 |
+ archive, err := fakeTar() |
|
| 178 |
+ if err != nil {
|
|
| 179 |
+ t.Fatal(err) |
|
| 180 |
+ } |
|
| 181 |
+ assertNImages(graph, t, 0) |
|
| 182 |
+ img, err := graph.Create(archive, nil, "Bla bla", "", nil) |
|
| 183 |
+ if err != nil {
|
|
| 184 |
+ t.Fatal(err) |
|
| 185 |
+ } |
|
| 186 |
+ assertNImages(graph, t, 1) |
|
| 187 |
+ if err := graph.Delete(img.ID); err != nil {
|
|
| 188 |
+ t.Fatal(err) |
|
| 189 |
+ } |
|
| 190 |
+ assertNImages(graph, t, 0) |
|
| 191 |
+ |
|
| 192 |
+ archive, err = fakeTar() |
|
| 193 |
+ if err != nil {
|
|
| 194 |
+ t.Fatal(err) |
|
| 195 |
+ } |
|
| 196 |
+ // Test 2 create (same name) / 1 delete |
|
| 197 |
+ img1, err := graph.Create(archive, nil, "Testing", "", nil) |
|
| 198 |
+ if err != nil {
|
|
| 199 |
+ t.Fatal(err) |
|
| 200 |
+ } |
|
| 201 |
+ archive, err = fakeTar() |
|
| 202 |
+ if err != nil {
|
|
| 203 |
+ t.Fatal(err) |
|
| 204 |
+ } |
|
| 205 |
+ if _, err = graph.Create(archive, nil, "Testing", "", nil); err != nil {
|
|
| 206 |
+ t.Fatal(err) |
|
| 207 |
+ } |
|
| 208 |
+ assertNImages(graph, t, 2) |
|
| 209 |
+ if err := graph.Delete(img1.ID); err != nil {
|
|
| 210 |
+ t.Fatal(err) |
|
| 211 |
+ } |
|
| 212 |
+ assertNImages(graph, t, 1) |
|
| 213 |
+ |
|
| 214 |
+ // Test delete wrong name |
|
| 215 |
+ if err := graph.Delete("Not_foo"); err == nil {
|
|
| 216 |
+ t.Fatalf("Deleting wrong ID should return an error")
|
|
| 217 |
+ } |
|
| 218 |
+ assertNImages(graph, t, 1) |
|
| 219 |
+ |
|
| 220 |
+ archive, err = fakeTar() |
|
| 51 | 221 |
if err != nil {
|
| 52 | 222 |
t.Fatal(err) |
| 53 | 223 |
} |
| 54 |
- graph, err := docker.NewGraph(tmp, driver) |
|
| 224 |
+ // Test delete twice (pull -> rm -> pull -> rm) |
|
| 225 |
+ if err := graph.Register(nil, archive, img1); err != nil {
|
|
| 226 |
+ t.Fatal(err) |
|
| 227 |
+ } |
|
| 228 |
+ if err := graph.Delete(img1.ID); err != nil {
|
|
| 229 |
+ t.Fatal(err) |
|
| 230 |
+ } |
|
| 231 |
+ assertNImages(graph, t, 1) |
|
| 232 |
+} |
|
| 233 |
+ |
|
| 234 |
+func TestByParent(t *testing.T) {
|
|
| 235 |
+ archive1, _ := fakeTar() |
|
| 236 |
+ archive2, _ := fakeTar() |
|
| 237 |
+ archive3, _ := fakeTar() |
|
| 238 |
+ |
|
| 239 |
+ graph, _ := tempGraph(t) |
|
| 240 |
+ defer nukeGraph(graph) |
|
| 241 |
+ parentImage := &docker.Image{
|
|
| 242 |
+ ID: docker.GenerateID(), |
|
| 243 |
+ Comment: "parent", |
|
| 244 |
+ Created: time.Now(), |
|
| 245 |
+ Parent: "", |
|
| 246 |
+ } |
|
| 247 |
+ childImage1 := &docker.Image{
|
|
| 248 |
+ ID: docker.GenerateID(), |
|
| 249 |
+ Comment: "child1", |
|
| 250 |
+ Created: time.Now(), |
|
| 251 |
+ Parent: parentImage.ID, |
|
| 252 |
+ } |
|
| 253 |
+ childImage2 := &docker.Image{
|
|
| 254 |
+ ID: docker.GenerateID(), |
|
| 255 |
+ Comment: "child2", |
|
| 256 |
+ Created: time.Now(), |
|
| 257 |
+ Parent: parentImage.ID, |
|
| 258 |
+ } |
|
| 259 |
+ _ = graph.Register(nil, archive1, parentImage) |
|
| 260 |
+ _ = graph.Register(nil, archive2, childImage1) |
|
| 261 |
+ _ = graph.Register(nil, archive3, childImage2) |
|
| 262 |
+ |
|
| 263 |
+ byParent, err := graph.ByParent() |
|
| 264 |
+ if err != nil {
|
|
| 265 |
+ t.Fatal(err) |
|
| 266 |
+ } |
|
| 267 |
+ numChildren := len(byParent[parentImage.ID]) |
|
| 268 |
+ if numChildren != 2 {
|
|
| 269 |
+ t.Fatalf("Expected 2 children, found %d", numChildren)
|
|
| 270 |
+ } |
|
| 271 |
+} |
|
| 272 |
+ |
|
| 273 |
+/* |
|
| 274 |
+ * HELPER FUNCTIONS |
|
| 275 |
+ */ |
|
| 276 |
+ |
|
| 277 |
+func assertNImages(graph *docker.Graph, t *testing.T, n int) {
|
|
| 278 |
+ if images, err := graph.Map(); err != nil {
|
|
| 279 |
+ t.Fatal(err) |
|
| 280 |
+ } else if actualN := len(images); actualN != n {
|
|
| 281 |
+ t.Fatalf("Expected %d images, found %d", n, actualN)
|
|
| 282 |
+ } |
|
| 283 |
+} |
|
| 284 |
+ |
|
| 285 |
+func tempGraph(t *testing.T) (*docker.Graph, graphdriver.Driver) {
|
|
| 286 |
+ tmp, err := ioutil.TempDir("", "docker-graph-")
|
|
| 287 |
+ if err != nil {
|
|
| 288 |
+ t.Fatal(err) |
|
| 289 |
+ } |
|
| 290 |
+ driver, err := graphdriver.New(tmp) |
|
| 291 |
+ if err != nil {
|
|
| 292 |
+ t.Fatal(err) |
|
| 293 |
+ } |
|
| 294 |
+ graph, err := docker.NewGraph(tmp, driver) |
|
| 295 |
+ if err != nil {
|
|
| 296 |
+ t.Fatal(err) |
|
| 297 |
+ } |
|
| 298 |
+ return graph, driver |
|
| 299 |
+} |
|
| 300 |
+ |
|
| 301 |
+func nukeGraph(graph *docker.Graph) {
|
|
| 302 |
+ graph.Driver().Cleanup() |
|
| 303 |
+ os.RemoveAll(graph.Root) |
|
| 304 |
+} |
|
| 305 |
+ |
|
| 306 |
+func testArchive(t *testing.T) archive.Archive {
|
|
| 307 |
+ archive, err := fakeTar() |
|
| 55 | 308 |
if err != nil {
|
| 56 | 309 |
t.Fatal(err) |
| 57 | 310 |
} |
| 58 |
- return graph, driver |
|
| 311 |
+ return archive |
|
| 59 | 312 |
} |
| 60 | 313 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,24 @@ |
| 0 |
+package docker |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "io" |
|
| 4 |
+ "archive/tar" |
|
| 5 |
+ "bytes" |
|
| 6 |
+) |
|
| 7 |
+ |
|
| 8 |
+func fakeTar() (io.Reader, error) {
|
|
| 9 |
+ content := []byte("Hello world!\n")
|
|
| 10 |
+ buf := new(bytes.Buffer) |
|
| 11 |
+ tw := tar.NewWriter(buf) |
|
| 12 |
+ for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
|
|
| 13 |
+ hdr := new(tar.Header) |
|
| 14 |
+ hdr.Size = int64(len(content)) |
|
| 15 |
+ hdr.Name = name |
|
| 16 |
+ if err := tw.WriteHeader(hdr); err != nil {
|
|
| 17 |
+ return nil, err |
|
| 18 |
+ } |
|
| 19 |
+ tw.Write([]byte(content)) |
|
| 20 |
+ } |
|
| 21 |
+ tw.Close() |
|
| 22 |
+ return buf, nil |
|
| 23 |
+} |