store_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package fs
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/dotcloud/docker/fake"
  6. "github.com/dotcloud/docker/future"
  7. "io/ioutil"
  8. "os"
  9. "testing"
  10. "time"
  11. )
  12. func TestInit(t *testing.T) {
  13. store, err := TempStore("testinit")
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. defer nuke(store)
  18. paths, err := store.Paths()
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if l := len(paths); l != 0 {
  23. t.Fatal("Fresh store should be empty after init (len=%d)", l)
  24. }
  25. }
  26. func TestCreate(t *testing.T) {
  27. store, err := TempStore("testcreate")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. defer nuke(store)
  32. archive, err := fake.FakeTar()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. image, err := store.Create(archive, nil, "foo", "Testing")
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if images, err := store.Images(); err != nil {
  41. t.Fatal(err)
  42. } else if l := len(images); l != 1 {
  43. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  44. }
  45. if images, err := store.List("foo"); err != nil {
  46. t.Fatal(err)
  47. } else if l := len(images); l != 1 {
  48. t.Fatalf("Path foo has wrong number of images (should be %d, not %d)", 1, l)
  49. } else if images[0].Id != image.Id {
  50. t.Fatalf("Imported image should be listed at path foo (%s != %s)", images[0], image)
  51. }
  52. }
  53. func TestRegister(t *testing.T) {
  54. store, err := TempStore("testregister")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer nuke(store)
  59. archive, err := fake.FakeTar()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. image := &Image{
  64. Id: future.RandomId(),
  65. Comment: "testing",
  66. Created: time.Now().Unix(),
  67. store: store,
  68. }
  69. err = store.Register(archive, image, "foo")
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if images, err := store.Images(); err != nil {
  74. t.Fatal(err)
  75. } else if l := len(images); l != 1 {
  76. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  77. }
  78. if images, err := store.List("foo"); err != nil {
  79. t.Fatal(err)
  80. } else if l := len(images); l != 1 {
  81. t.Fatalf("Path foo has wrong number of images (should be %d, not %d)", 1, l)
  82. } else if images[0].Id != image.Id {
  83. t.Fatalf("Imported image should be listed at path foo (%s != %s)", images[0], image)
  84. }
  85. }
  86. func TestTag(t *testing.T) {
  87. store, err := TempStore("testtag")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer nuke(store)
  92. archive, err := fake.FakeTar()
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. image, err := store.Create(archive, nil, "foo", "Testing")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if images, err := store.Images(); err != nil {
  101. t.Fatal(err)
  102. } else if l := len(images); l != 1 {
  103. t.Fatalf("Wrong number of images. Should be %d, not %d", 1, l)
  104. }
  105. if err := store.AddTag(image.Id, "baz"); err != nil {
  106. t.Fatalf("Error while adding a tag to created image: %s", err)
  107. }
  108. if taggedImage, err := store.GetByTag("baz"); err != nil {
  109. t.Fatalf("Error while trying to retrieve image for tag 'baz': %s", err)
  110. } else if taggedImage.Id != image.Id {
  111. t.Fatalf("Expected to retrieve image %s but found %s instead", image.Id, taggedImage.Id)
  112. }
  113. }
  114. // Copy an image to a new path
  115. func TestCopyNewPath(t *testing.T) {
  116. store, err := TempStore("testcopynewpath")
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. defer nuke(store)
  121. archive, err := fake.FakeTar()
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. src, err := store.Create(archive, nil, "foo", "Testing")
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. dst, err := src.Copy("bar")
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. // ID should be the same
  134. if src.Id != dst.Id {
  135. t.Fatal("Different IDs")
  136. }
  137. // Check number of images at source path
  138. if images, err := store.List("foo"); err != nil {
  139. t.Fatal(err)
  140. } else if l := len(images); l != 1 {
  141. t.Fatal("Wrong number of images at source path (should be %d, not %d)", 1, l)
  142. }
  143. // Check number of images at destination path
  144. if images, err := store.List("bar"); err != nil {
  145. t.Fatal(err)
  146. } else if l := len(images); l != 1 {
  147. t.Fatal("Wrong number of images at destination path (should be %d, not %d)", 1, l)
  148. }
  149. if err := healthCheck(store); err != nil {
  150. t.Fatal(err)
  151. }
  152. }
  153. // Copying an image to the same path twice should fail
  154. func TestCopySameName(t *testing.T) {
  155. store, err := TempStore("testcopysamename")
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. defer nuke(store)
  160. archive, err := fake.FakeTar()
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. src, err := store.Create(archive, nil, "foo", "Testing")
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. _, err = src.Copy("foo")
  169. if err == nil {
  170. t.Fatal("Copying an image to the same patch twice should fail.")
  171. }
  172. }
  173. func TestMountPoint(t *testing.T) {
  174. store, err := TempStore("test-mountpoint")
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. defer nuke(store)
  179. archive, err := fake.FakeTar()
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. image, err := store.Create(archive, nil, "foo", "Testing")
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. mountpoint, err := image.Mountpoint("/tmp/a", "/tmp/b")
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if mountpoint.Root != "/tmp/a" {
  192. t.Fatal("Wrong mountpoint root (should be %s, not %s)", "/tmp/a", mountpoint.Root)
  193. }
  194. if mountpoint.Rw != "/tmp/b" {
  195. t.Fatal("Wrong mountpoint root (should be %s, not %s)", "/tmp/b", mountpoint.Rw)
  196. }
  197. }
  198. func TestMountpointDuplicateRoot(t *testing.T) {
  199. store, err := TempStore("test-mountpoint")
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. defer nuke(store)
  204. archive, err := fake.FakeTar()
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. image, err := store.Create(archive, nil, "foo", "Testing")
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. _, err = image.Mountpoint("/tmp/a", "/tmp/b")
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. if _, err = image.Mountpoint("/tmp/a", "/tmp/foobar"); err == nil {
  217. t.Fatal("Duplicate mountpoint root should fail")
  218. }
  219. }
  220. func TestMount(t *testing.T) {
  221. store, err := TempStore("test-mount")
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. defer nuke(store)
  226. archive, err := fake.FakeTar()
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. image, err := store.Create(archive, nil, "foo", "Testing")
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. // Create mount targets
  235. root, err := ioutil.TempDir("", "docker-fs-test")
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. rw, err := ioutil.TempDir("", "docker-fs-test")
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. mountpoint, err := image.Mount(root, rw)
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. defer mountpoint.Umount()
  248. // Mountpoint should be marked as mounted
  249. if !mountpoint.Mounted() {
  250. t.Fatal("Mountpoint not mounted")
  251. }
  252. // There should be one mountpoint registered
  253. if mps, err := image.Mountpoints(); err != nil {
  254. t.Fatal(err)
  255. } else if len(mps) != 1 {
  256. t.Fatal("Wrong number of mountpoints registered (should be %d, not %d)", 1, len(mps))
  257. }
  258. // Unmounting should work
  259. if err := mountpoint.Umount(); err != nil {
  260. t.Fatal(err)
  261. }
  262. // De-registering should work
  263. if err := mountpoint.Deregister(); err != nil {
  264. t.Fatal(err)
  265. }
  266. if mps, err := image.Mountpoints(); err != nil {
  267. t.Fatal(err)
  268. } else if len(mps) != 0 {
  269. t.Fatal("Wrong number of mountpoints registered (should be %d, not %d)", 0, len(mps))
  270. }
  271. // General health check
  272. if err := healthCheck(store); err != nil {
  273. t.Fatal(err)
  274. }
  275. }
  276. func TempStore(prefix string) (*Store, error) {
  277. dir, err := ioutil.TempDir("", "docker-fs-test-"+prefix)
  278. if err != nil {
  279. return nil, err
  280. }
  281. return New(dir)
  282. }
  283. func nuke(store *Store) error {
  284. return os.RemoveAll(store.Root)
  285. }
  286. // Look for inconsistencies in a store.
  287. func healthCheck(store *Store) error {
  288. parents := make(map[string]bool)
  289. paths, err := store.Paths()
  290. if err != nil {
  291. return err
  292. }
  293. for _, path := range paths {
  294. images, err := store.List(path)
  295. if err != nil {
  296. return err
  297. }
  298. IDs := make(map[string]bool) // All IDs for this path
  299. for _, img := range images {
  300. // Check for duplicate IDs per path
  301. if _, exists := IDs[img.Id]; exists {
  302. return errors.New(fmt.Sprintf("Duplicate ID: %s", img.Id))
  303. } else {
  304. IDs[img.Id] = true
  305. }
  306. // Store parent for 2nd pass
  307. if parent := img.Parent; parent != "" {
  308. parents[parent] = true
  309. }
  310. }
  311. }
  312. // Check non-existing parents
  313. for parent := range parents {
  314. if _, exists := parents[parent]; !exists {
  315. return errors.New("Reference to non-registered parent: " + parent)
  316. }
  317. }
  318. return nil
  319. }