mount_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package layer
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "sort"
  8. "testing"
  9. "github.com/docker/docker/pkg/archive"
  10. )
  11. func TestMountInit(t *testing.T) {
  12. // TODO Windows: Figure out why this is failing
  13. if runtime.GOOS == "windows" {
  14. t.Skip("Failing on Windows")
  15. }
  16. ls, _, cleanup := newTestStore(t)
  17. defer cleanup()
  18. basefile := newTestFile("testfile.txt", []byte("base data!"), 0644)
  19. initfile := newTestFile("testfile.txt", []byte("init data!"), 0777)
  20. li := initWithFiles(basefile)
  21. layer, err := createLayer(ls, "", li)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. mountInit := func(root string) error {
  26. return initfile.ApplyFile(root)
  27. }
  28. rwLayerOpts := &CreateRWLayerOpts{
  29. InitFunc: mountInit,
  30. }
  31. m, err := ls.CreateRWLayer("fun-mount", layer.ChainID(), rwLayerOpts)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. path, err := m.Mount("")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. f, err := os.Open(filepath.Join(path, "testfile.txt"))
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. defer f.Close()
  44. fi, err := f.Stat()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. b, err := ioutil.ReadAll(f)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if expected := "init data!"; string(b) != expected {
  53. t.Fatalf("Unexpected test file contents %q, expected %q", string(b), expected)
  54. }
  55. if fi.Mode().Perm() != 0777 {
  56. t.Fatalf("Unexpected filemode %o, expecting %o", fi.Mode().Perm(), 0777)
  57. }
  58. }
  59. func TestMountSize(t *testing.T) {
  60. // TODO Windows: Figure out why this is failing
  61. if runtime.GOOS == "windows" {
  62. t.Skip("Failing on Windows")
  63. }
  64. ls, _, cleanup := newTestStore(t)
  65. defer cleanup()
  66. content1 := []byte("Base contents")
  67. content2 := []byte("Mutable contents")
  68. contentInit := []byte("why am I excluded from the size ☹")
  69. li := initWithFiles(newTestFile("file1", content1, 0644))
  70. layer, err := createLayer(ls, "", li)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. mountInit := func(root string) error {
  75. return newTestFile("file-init", contentInit, 0777).ApplyFile(root)
  76. }
  77. rwLayerOpts := &CreateRWLayerOpts{
  78. InitFunc: mountInit,
  79. }
  80. m, err := ls.CreateRWLayer("mount-size", layer.ChainID(), rwLayerOpts)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. path, err := m.Mount("")
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if err := ioutil.WriteFile(filepath.Join(path, "file2"), content2, 0755); err != nil {
  89. t.Fatal(err)
  90. }
  91. mountSize, err := m.Size()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if expected := len(content2); int(mountSize) != expected {
  96. t.Fatalf("Unexpected mount size %d, expected %d", int(mountSize), expected)
  97. }
  98. }
  99. func TestMountChanges(t *testing.T) {
  100. // TODO Windows: Figure out why this is failing
  101. if runtime.GOOS == "windows" {
  102. t.Skip("Failing on Windows")
  103. }
  104. ls, _, cleanup := newTestStore(t)
  105. defer cleanup()
  106. basefiles := []FileApplier{
  107. newTestFile("testfile1.txt", []byte("base data!"), 0644),
  108. newTestFile("testfile2.txt", []byte("base data!"), 0644),
  109. newTestFile("testfile3.txt", []byte("base data!"), 0644),
  110. }
  111. initfile := newTestFile("testfile1.txt", []byte("init data!"), 0777)
  112. li := initWithFiles(basefiles...)
  113. layer, err := createLayer(ls, "", li)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. mountInit := func(root string) error {
  118. return initfile.ApplyFile(root)
  119. }
  120. rwLayerOpts := &CreateRWLayerOpts{
  121. InitFunc: mountInit,
  122. }
  123. m, err := ls.CreateRWLayer("mount-changes", layer.ChainID(), rwLayerOpts)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. path, err := m.Mount("")
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. if err := os.Chmod(filepath.Join(path, "testfile1.txt"), 0755); err != nil {
  132. t.Fatal(err)
  133. }
  134. if err := ioutil.WriteFile(filepath.Join(path, "testfile1.txt"), []byte("mount data!"), 0755); err != nil {
  135. t.Fatal(err)
  136. }
  137. if err := os.Remove(filepath.Join(path, "testfile2.txt")); err != nil {
  138. t.Fatal(err)
  139. }
  140. if err := os.Chmod(filepath.Join(path, "testfile3.txt"), 0755); err != nil {
  141. t.Fatal(err)
  142. }
  143. if err := ioutil.WriteFile(filepath.Join(path, "testfile4.txt"), []byte("mount data!"), 0644); err != nil {
  144. t.Fatal(err)
  145. }
  146. changes, err := m.Changes()
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. if expected := 4; len(changes) != expected {
  151. t.Fatalf("Wrong number of changes %d, expected %d", len(changes), expected)
  152. }
  153. sortChanges(changes)
  154. assertChange(t, changes[0], archive.Change{
  155. Path: "/testfile1.txt",
  156. Kind: archive.ChangeModify,
  157. })
  158. assertChange(t, changes[1], archive.Change{
  159. Path: "/testfile2.txt",
  160. Kind: archive.ChangeDelete,
  161. })
  162. assertChange(t, changes[2], archive.Change{
  163. Path: "/testfile3.txt",
  164. Kind: archive.ChangeModify,
  165. })
  166. assertChange(t, changes[3], archive.Change{
  167. Path: "/testfile4.txt",
  168. Kind: archive.ChangeAdd,
  169. })
  170. }
  171. func assertChange(t *testing.T, actual, expected archive.Change) {
  172. if actual.Path != expected.Path {
  173. t.Fatalf("Unexpected change path %s, expected %s", actual.Path, expected.Path)
  174. }
  175. if actual.Kind != expected.Kind {
  176. t.Fatalf("Unexpected change type %s, expected %s", actual.Kind, expected.Kind)
  177. }
  178. }
  179. func sortChanges(changes []archive.Change) {
  180. cs := &changeSorter{
  181. changes: changes,
  182. }
  183. sort.Sort(cs)
  184. }
  185. type changeSorter struct {
  186. changes []archive.Change
  187. }
  188. func (cs *changeSorter) Len() int {
  189. return len(cs.changes)
  190. }
  191. func (cs *changeSorter) Swap(i, j int) {
  192. cs.changes[i], cs.changes[j] = cs.changes[j], cs.changes[i]
  193. }
  194. func (cs *changeSorter) Less(i, j int) bool {
  195. return cs.changes[i].Path < cs.changes[j].Path
  196. }