graph_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 images, err := graph.All(); err != nil {
  46. t.Fatal(err)
  47. } else if l := len(images); l != 1 {
  48. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  49. }
  50. }
  51. func TestRegister(t *testing.T) {
  52. graph := tempGraph(t)
  53. defer os.RemoveAll(graph.Root)
  54. archive, err := fakeTar()
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. image := &Image{
  59. Id: GenerateId(),
  60. Comment: "testing",
  61. Created: time.Now(),
  62. }
  63. err = graph.Register(archive, image)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. if images, err := graph.All(); err != nil {
  68. t.Fatal(err)
  69. } else if l := len(images); l != 1 {
  70. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  71. }
  72. if resultImg, err := graph.Get(image.Id); err != nil {
  73. t.Fatal(err)
  74. } else {
  75. if resultImg.Id != image.Id {
  76. t.Fatalf("Wrong image ID. Should be '%s', not '%s'", image.Id, resultImg.Id)
  77. }
  78. if resultImg.Comment != image.Comment {
  79. t.Fatalf("Wrong image comment. Should be '%s', not '%s'", image.Comment, resultImg.Comment)
  80. }
  81. }
  82. }
  83. func TestMount(t *testing.T) {
  84. graph := tempGraph(t)
  85. defer os.RemoveAll(graph.Root)
  86. archive, err := fakeTar()
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. image, err := graph.Create(archive, nil, "Testing")
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. tmp, err := ioutil.TempDir("", "docker-test-graph-mount-")
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. defer os.RemoveAll(tmp)
  99. rootfs := path.Join(tmp, "rootfs")
  100. if err := os.MkdirAll(rootfs, 0700); err != nil {
  101. t.Fatal(err)
  102. }
  103. rw := path.Join(tmp, "rw")
  104. if err := os.MkdirAll(rw, 0700); err != nil {
  105. t.Fatal(err)
  106. }
  107. if err := image.Mount(rootfs, rw); err != nil {
  108. t.Fatal(err)
  109. }
  110. // FIXME: test for mount contents
  111. defer func() {
  112. if err := Unmount(rootfs); err != nil {
  113. t.Error(err)
  114. }
  115. }()
  116. }
  117. func TestDelete(t *testing.T) {
  118. graph := tempGraph(t)
  119. defer os.RemoveAll(graph.Root)
  120. archive, err := fakeTar()
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. assertNImages(graph, t, 0)
  125. img, err := graph.Create(archive, nil, "Bla bla")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. assertNImages(graph, t, 1)
  130. if err := graph.Delete(img.Id); err != nil {
  131. t.Fatal(err)
  132. }
  133. assertNImages(graph, t, 0)
  134. // Test 2 create (same name) / 1 delete
  135. img1, err := graph.Create(archive, nil, "Testing")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. if _, err = graph.Create(archive, nil, "Testing"); err != nil {
  140. t.Fatal(err)
  141. }
  142. assertNImages(graph, t, 2)
  143. if err := graph.Delete(img1.Id); err != nil {
  144. t.Fatal(err)
  145. }
  146. assertNImages(graph, t, 1)
  147. // Test delete wrong name
  148. if err := graph.Delete("Not_foo"); err == nil {
  149. t.Fatalf("Deleting wrong ID should return an error")
  150. }
  151. assertNImages(graph, t, 1)
  152. // Test delete twice (pull -> rm -> pull -> rm)
  153. if err := graph.Register(archive, img1); err != nil {
  154. t.Fatal(err)
  155. }
  156. if err := graph.Delete(img1.Id); err != nil {
  157. t.Fatal(err)
  158. }
  159. assertNImages(graph, t, 1)
  160. }
  161. func assertNImages(graph *Graph, t *testing.T, n int) {
  162. if images, err := graph.All(); err != nil {
  163. t.Fatal(err)
  164. } else if actualN := len(images); actualN != n {
  165. t.Fatalf("Expected %d images, found %d", n, actualN)
  166. }
  167. }
  168. /*
  169. * HELPER FUNCTIONS
  170. */
  171. func tempGraph(t *testing.T) *Graph {
  172. tmp, err := ioutil.TempDir("", "docker-graph-")
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. graph, err := NewGraph(tmp)
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. return graph
  181. }
  182. func testArchive(t *testing.T) Archive {
  183. archive, err := fakeTar()
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. return archive
  188. }
  189. func fakeTar() (io.Reader, error) {
  190. content := []byte("Hello world!\n")
  191. buf := new(bytes.Buffer)
  192. tw := tar.NewWriter(buf)
  193. for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
  194. hdr := new(tar.Header)
  195. hdr.Size = int64(len(content))
  196. hdr.Name = name
  197. if err := tw.WriteHeader(hdr); err != nil {
  198. return nil, err
  199. }
  200. tw.Write([]byte(content))
  201. }
  202. tw.Close()
  203. return buf, nil
  204. }