graph_test.go 6.5 KB

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