mount_test.go 6.5 KB

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