graph_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package graph
  2. import (
  3. "github.com/dotcloud/docker/fake"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "testing"
  8. "time"
  9. )
  10. func TestInit(t *testing.T) {
  11. graph := tempGraph(t)
  12. defer os.RemoveAll(graph.Root)
  13. // Root should exist
  14. if _, err := os.Stat(graph.Root); err != nil {
  15. t.Fatal(err)
  16. }
  17. // All() should be empty
  18. if l, err := graph.All(); err != nil {
  19. t.Fatal(err)
  20. } else if len(l) != 0 {
  21. t.Fatalf("List() should return %d, not %d", 0, len(l))
  22. }
  23. }
  24. // FIXME: Do more extensive tests (ex: create multiple, delete, recreate;
  25. // create multiple, check the amount of images and paths, etc..)
  26. func TestCreate(t *testing.T) {
  27. graph := tempGraph(t)
  28. defer os.RemoveAll(graph.Root)
  29. archive, err := fake.FakeTar()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. image, err := graph.Create(archive, "", "Testing")
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if err := ValidateId(image.Id); err != nil {
  38. t.Fatal(err)
  39. }
  40. if image.Comment != "Testing" {
  41. t.Fatalf("Wrong comment: should be '%s', not '%s'", "Testing", image.Comment)
  42. }
  43. if images, err := graph.All(); err != nil {
  44. t.Fatal(err)
  45. } else if l := len(images); l != 1 {
  46. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  47. }
  48. }
  49. func TestRegister(t *testing.T) {
  50. graph := tempGraph(t)
  51. defer os.RemoveAll(graph.Root)
  52. archive, err := fake.FakeTar()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. image := &Image{
  57. Id: GenerateId(),
  58. Comment: "testing",
  59. Created: time.Now(),
  60. }
  61. err = graph.Register(archive, image)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if images, err := graph.All(); err != nil {
  66. t.Fatal(err)
  67. } else if l := len(images); l != 1 {
  68. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  69. }
  70. if resultImg, err := graph.Get(image.Id); err != nil {
  71. t.Fatal(err)
  72. } else {
  73. if resultImg.Id != image.Id {
  74. t.Fatalf("Wrong image ID. Should be '%s', not '%s'", image.Id, resultImg.Id)
  75. }
  76. if resultImg.Comment != image.Comment {
  77. t.Fatalf("Wrong image comment. Should be '%s', not '%s'", image.Comment, resultImg.Comment)
  78. }
  79. }
  80. }
  81. func TestMount(t *testing.T) {
  82. graph := tempGraph(t)
  83. defer os.RemoveAll(graph.Root)
  84. archive, err := fake.FakeTar()
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. image, err := graph.Create(archive, "", "Testing")
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. tmp, err := ioutil.TempDir("", "docker-test-graph-mount-")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. defer os.RemoveAll(tmp)
  97. rootfs := path.Join(tmp, "rootfs")
  98. if err := os.MkdirAll(rootfs, 0700); err != nil {
  99. t.Fatal(err)
  100. }
  101. rw := path.Join(tmp, "rw")
  102. if err := os.MkdirAll(rw, 0700); err != nil {
  103. t.Fatal(err)
  104. }
  105. if err := image.Mount(rootfs, rw); err != nil {
  106. t.Fatal(err)
  107. }
  108. // FIXME: test for mount contents
  109. defer func() {
  110. if err := Unmount(rootfs); err != nil {
  111. t.Error(err)
  112. }
  113. }()
  114. }
  115. /*
  116. * HELPER FUNCTIONS
  117. */
  118. func tempGraph(t *testing.T) *Graph {
  119. tmp, err := ioutil.TempDir("", "docker-graph-")
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. graph, err := New(tmp)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. return graph
  128. }
  129. func testArchive(t *testing.T) Archive {
  130. archive, err := fake.FakeTar()
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. return archive
  135. }