graph_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package docker
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "testing"
  10. "time"
  11. )
  12. func TestInit(t *testing.T) {
  13. graph := tempGraph(t)
  14. defer os.RemoveAll(graph.Root)
  15. // Root should exist
  16. if _, err := os.Stat(graph.Root); err != nil {
  17. t.Fatal(err)
  18. }
  19. // All() should be empty
  20. if l, err := graph.All(); err != nil {
  21. t.Fatal(err)
  22. } else if len(l) != 0 {
  23. t.Fatalf("List() should return %d, not %d", 0, len(l))
  24. }
  25. }
  26. // FIXME: Do more extensive tests (ex: create multiple, delete, recreate;
  27. // create multiple, check the amount of images and paths, etc..)
  28. func TestGraphCreate(t *testing.T) {
  29. graph := tempGraph(t)
  30. defer os.RemoveAll(graph.Root)
  31. archive, err := fakeTar()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. image, err := graph.Create(archive, nil, "Testing")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if err := ValidateId(image.Id); err != nil {
  40. t.Fatal(err)
  41. }
  42. if image.Comment != "Testing" {
  43. t.Fatalf("Wrong comment: should be '%s', not '%s'", "Testing", image.Comment)
  44. }
  45. if image.DockerVersion != VERSION {
  46. t.Fatalf("Wrong docker_version: should be '%s', not '%s'", VERSION, image.DockerVersion)
  47. }
  48. if images, err := graph.All(); err != nil {
  49. t.Fatal(err)
  50. } else if l := len(images); l != 1 {
  51. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  52. }
  53. }
  54. func TestRegister(t *testing.T) {
  55. graph := tempGraph(t)
  56. defer os.RemoveAll(graph.Root)
  57. archive, err := fakeTar()
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. image := &Image{
  62. Id: GenerateId(),
  63. Comment: "testing",
  64. Created: time.Now(),
  65. }
  66. err = graph.Register(archive, image)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if images, err := graph.All(); err != nil {
  71. t.Fatal(err)
  72. } else if l := len(images); l != 1 {
  73. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  74. }
  75. if resultImg, err := graph.Get(image.Id); err != nil {
  76. t.Fatal(err)
  77. } else {
  78. if resultImg.Id != image.Id {
  79. t.Fatalf("Wrong image ID. Should be '%s', not '%s'", image.Id, resultImg.Id)
  80. }
  81. if resultImg.Comment != image.Comment {
  82. t.Fatalf("Wrong image comment. Should be '%s', not '%s'", image.Comment, resultImg.Comment)
  83. }
  84. }
  85. }
  86. func TestMount(t *testing.T) {
  87. graph := tempGraph(t)
  88. defer os.RemoveAll(graph.Root)
  89. archive, err := fakeTar()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. image, err := graph.Create(archive, nil, "Testing")
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. tmp, err := ioutil.TempDir("", "docker-test-graph-mount-")
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. defer os.RemoveAll(tmp)
  102. rootfs := path.Join(tmp, "rootfs")
  103. if err := os.MkdirAll(rootfs, 0700); err != nil {
  104. t.Fatal(err)
  105. }
  106. rw := path.Join(tmp, "rw")
  107. if err := os.MkdirAll(rw, 0700); err != nil {
  108. t.Fatal(err)
  109. }
  110. if err := image.Mount(rootfs, rw); err != nil {
  111. t.Fatal(err)
  112. }
  113. // FIXME: test for mount contents
  114. defer func() {
  115. if err := Unmount(rootfs); err != nil {
  116. t.Error(err)
  117. }
  118. }()
  119. }
  120. // Test that an image can be deleted by its shorthand prefix
  121. func TestDeletePrefix(t *testing.T) {
  122. graph := tempGraph(t)
  123. defer os.RemoveAll(graph.Root)
  124. img := createTestImage(graph, t)
  125. if err := graph.Delete(TruncateId(img.Id)); err != nil {
  126. t.Fatal(err)
  127. }
  128. assertNImages(graph, t, 0)
  129. }
  130. func createTestImage(graph *Graph, t *testing.T) *Image {
  131. archive, err := fakeTar()
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. img, err := graph.Create(archive, nil, "Test image")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. return img
  140. }
  141. func TestDelete(t *testing.T) {
  142. graph := tempGraph(t)
  143. defer os.RemoveAll(graph.Root)
  144. archive, err := fakeTar()
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. assertNImages(graph, t, 0)
  149. img, err := graph.Create(archive, nil, "Bla bla")
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. assertNImages(graph, t, 1)
  154. if err := graph.Delete(img.Id); err != nil {
  155. t.Fatal(err)
  156. }
  157. assertNImages(graph, t, 0)
  158. // Test 2 create (same name) / 1 delete
  159. img1, err := graph.Create(archive, nil, "Testing")
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. if _, err = graph.Create(archive, nil, "Testing"); err != nil {
  164. t.Fatal(err)
  165. }
  166. assertNImages(graph, t, 2)
  167. if err := graph.Delete(img1.Id); err != nil {
  168. t.Fatal(err)
  169. }
  170. assertNImages(graph, t, 1)
  171. // Test delete wrong name
  172. if err := graph.Delete("Not_foo"); err == nil {
  173. t.Fatalf("Deleting wrong ID should return an error")
  174. }
  175. assertNImages(graph, t, 1)
  176. // Test delete twice (pull -> rm -> pull -> rm)
  177. if err := graph.Register(archive, img1); err != nil {
  178. t.Fatal(err)
  179. }
  180. if err := graph.Delete(img1.Id); err != nil {
  181. t.Fatal(err)
  182. }
  183. assertNImages(graph, t, 1)
  184. }
  185. func assertNImages(graph *Graph, t *testing.T, n int) {
  186. if images, err := graph.All(); err != nil {
  187. t.Fatal(err)
  188. } else if actualN := len(images); actualN != n {
  189. t.Fatalf("Expected %d images, found %d", n, actualN)
  190. }
  191. }
  192. /*
  193. * HELPER FUNCTIONS
  194. */
  195. func tempGraph(t *testing.T) *Graph {
  196. tmp, err := ioutil.TempDir("", "docker-graph-")
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. graph, err := NewGraph(tmp)
  201. if err != nil {
  202. t.Fatal(err)
  203. }
  204. return graph
  205. }
  206. func testArchive(t *testing.T) Archive {
  207. archive, err := fakeTar()
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. return archive
  212. }
  213. func fakeTar() (io.Reader, error) {
  214. content := []byte("Hello world!\n")
  215. buf := new(bytes.Buffer)
  216. tw := tar.NewWriter(buf)
  217. for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
  218. hdr := new(tar.Header)
  219. hdr.Size = int64(len(content))
  220. hdr.Name = name
  221. if err := tw.WriteHeader(hdr); err != nil {
  222. return nil, err
  223. }
  224. tw.Write([]byte(content))
  225. }
  226. tw.Close()
  227. return buf, nil
  228. }