graph_test.go 7.7 KB

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