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