github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/pkg/archive/archive_unix_test.go (about) 1 // +build !windows 2 3 package archive // import "github.com/docker/docker/pkg/archive" 4 5 import ( 6 "bytes" 7 "fmt" 8 "io/ioutil" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "strings" 13 "syscall" 14 "testing" 15 16 "github.com/docker/docker/pkg/system" 17 "golang.org/x/sys/unix" 18 "gotest.tools/assert" 19 is "gotest.tools/assert/cmp" 20 "gotest.tools/skip" 21 ) 22 23 func TestCanonicalTarNameForPath(t *testing.T) { 24 cases := []struct{ in, expected string }{ 25 {"foo", "foo"}, 26 {"foo/bar", "foo/bar"}, 27 {"foo/dir/", "foo/dir/"}, 28 } 29 for _, v := range cases { 30 if CanonicalTarNameForPath(v.in) != v.expected { 31 t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, CanonicalTarNameForPath(v.in)) 32 } 33 } 34 } 35 36 func TestCanonicalTarName(t *testing.T) { 37 cases := []struct { 38 in string 39 isDir bool 40 expected string 41 }{ 42 {"foo", false, "foo"}, 43 {"foo", true, "foo/"}, 44 {"foo/bar", false, "foo/bar"}, 45 {"foo/bar", true, "foo/bar/"}, 46 } 47 for _, v := range cases { 48 if canonicalTarName(v.in, v.isDir) != v.expected { 49 t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, canonicalTarName(v.in, v.isDir)) 50 } 51 } 52 } 53 54 func TestChmodTarEntry(t *testing.T) { 55 cases := []struct { 56 in, expected os.FileMode 57 }{ 58 {0000, 0000}, 59 {0777, 0777}, 60 {0644, 0644}, 61 {0755, 0755}, 62 {0444, 0444}, 63 } 64 for _, v := range cases { 65 if out := chmodTarEntry(v.in); out != v.expected { 66 t.Fatalf("wrong chmod. expected:%v got:%v", v.expected, out) 67 } 68 } 69 } 70 71 func TestTarWithHardLink(t *testing.T) { 72 origin, err := ioutil.TempDir("", "docker-test-tar-hardlink") 73 assert.NilError(t, err) 74 defer os.RemoveAll(origin) 75 76 err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700) 77 assert.NilError(t, err) 78 79 err = os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2")) 80 assert.NilError(t, err) 81 82 var i1, i2 uint64 83 i1, err = getNlink(filepath.Join(origin, "1")) 84 assert.NilError(t, err) 85 86 // sanity check that we can hardlink 87 if i1 != 2 { 88 t.Skipf("skipping since hardlinks don't work here; expected 2 links, got %d", i1) 89 } 90 91 dest, err := ioutil.TempDir("", "docker-test-tar-hardlink-dest") 92 assert.NilError(t, err) 93 defer os.RemoveAll(dest) 94 95 // we'll do this in two steps to separate failure 96 fh, err := Tar(origin, Uncompressed) 97 assert.NilError(t, err) 98 99 // ensure we can read the whole thing with no error, before writing back out 100 buf, err := ioutil.ReadAll(fh) 101 assert.NilError(t, err) 102 103 bRdr := bytes.NewReader(buf) 104 err = Untar(bRdr, dest, &TarOptions{Compression: Uncompressed}) 105 assert.NilError(t, err) 106 107 i1, err = getInode(filepath.Join(dest, "1")) 108 assert.NilError(t, err) 109 110 i2, err = getInode(filepath.Join(dest, "2")) 111 assert.NilError(t, err) 112 113 assert.Check(t, is.Equal(i1, i2)) 114 } 115 116 func TestTarWithHardLinkAndRebase(t *testing.T) { 117 tmpDir, err := ioutil.TempDir("", "docker-test-tar-hardlink-rebase") 118 assert.NilError(t, err) 119 defer os.RemoveAll(tmpDir) 120 121 origin := filepath.Join(tmpDir, "origin") 122 err = os.Mkdir(origin, 0700) 123 assert.NilError(t, err) 124 125 err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700) 126 assert.NilError(t, err) 127 128 err = os.Link(filepath.Join(origin, "1"), filepath.Join(origin, "2")) 129 assert.NilError(t, err) 130 131 var i1, i2 uint64 132 i1, err = getNlink(filepath.Join(origin, "1")) 133 assert.NilError(t, err) 134 135 // sanity check that we can hardlink 136 if i1 != 2 { 137 t.Skipf("skipping since hardlinks don't work here; expected 2 links, got %d", i1) 138 } 139 140 dest := filepath.Join(tmpDir, "dest") 141 bRdr, err := TarResourceRebase(origin, "origin") 142 assert.NilError(t, err) 143 144 dstDir, srcBase := SplitPathDirEntry(origin) 145 _, dstBase := SplitPathDirEntry(dest) 146 content := RebaseArchiveEntries(bRdr, srcBase, dstBase) 147 err = Untar(content, dstDir, &TarOptions{Compression: Uncompressed, NoLchown: true, NoOverwriteDirNonDir: true}) 148 assert.NilError(t, err) 149 150 i1, err = getInode(filepath.Join(dest, "1")) 151 assert.NilError(t, err) 152 i2, err = getInode(filepath.Join(dest, "2")) 153 assert.NilError(t, err) 154 155 assert.Check(t, is.Equal(i1, i2)) 156 } 157 158 func getNlink(path string) (uint64, error) { 159 stat, err := os.Stat(path) 160 if err != nil { 161 return 0, err 162 } 163 statT, ok := stat.Sys().(*syscall.Stat_t) 164 if !ok { 165 return 0, fmt.Errorf("expected type *syscall.Stat_t, got %t", stat.Sys()) 166 } 167 // We need this conversion on ARM64 168 return uint64(statT.Nlink), nil 169 } 170 171 func getInode(path string) (uint64, error) { 172 stat, err := os.Stat(path) 173 if err != nil { 174 return 0, err 175 } 176 statT, ok := stat.Sys().(*syscall.Stat_t) 177 if !ok { 178 return 0, fmt.Errorf("expected type *syscall.Stat_t, got %t", stat.Sys()) 179 } 180 return statT.Ino, nil 181 } 182 183 func TestTarWithBlockCharFifo(t *testing.T) { 184 skip.If(t, os.Getuid() != 0, "skipping test that requires root") 185 origin, err := ioutil.TempDir("", "docker-test-tar-hardlink") 186 assert.NilError(t, err) 187 188 defer os.RemoveAll(origin) 189 err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700) 190 assert.NilError(t, err) 191 192 err = system.Mknod(filepath.Join(origin, "2"), unix.S_IFBLK, int(system.Mkdev(int64(12), int64(5)))) 193 assert.NilError(t, err) 194 err = system.Mknod(filepath.Join(origin, "3"), unix.S_IFCHR, int(system.Mkdev(int64(12), int64(5)))) 195 assert.NilError(t, err) 196 err = system.Mknod(filepath.Join(origin, "4"), unix.S_IFIFO, int(system.Mkdev(int64(12), int64(5)))) 197 assert.NilError(t, err) 198 199 dest, err := ioutil.TempDir("", "docker-test-tar-hardlink-dest") 200 assert.NilError(t, err) 201 defer os.RemoveAll(dest) 202 203 // we'll do this in two steps to separate failure 204 fh, err := Tar(origin, Uncompressed) 205 assert.NilError(t, err) 206 207 // ensure we can read the whole thing with no error, before writing back out 208 buf, err := ioutil.ReadAll(fh) 209 assert.NilError(t, err) 210 211 bRdr := bytes.NewReader(buf) 212 err = Untar(bRdr, dest, &TarOptions{Compression: Uncompressed}) 213 assert.NilError(t, err) 214 215 changes, err := ChangesDirs(origin, dest) 216 assert.NilError(t, err) 217 218 if len(changes) > 0 { 219 t.Fatalf("Tar with special device (block, char, fifo) should keep them (recreate them when untar) : %v", changes) 220 } 221 } 222 223 // TestTarUntarWithXattr is Unix as Lsetxattr is not supported on Windows 224 func TestTarUntarWithXattr(t *testing.T) { 225 skip.If(t, os.Getuid() != 0, "skipping test that requires root") 226 if _, err := exec.LookPath("setcap"); err != nil { 227 t.Skip("setcap not installed") 228 } 229 if _, err := exec.LookPath("getcap"); err != nil { 230 t.Skip("getcap not installed") 231 } 232 233 origin, err := ioutil.TempDir("", "docker-test-untar-origin") 234 assert.NilError(t, err) 235 defer os.RemoveAll(origin) 236 err = ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700) 237 assert.NilError(t, err) 238 239 err = ioutil.WriteFile(filepath.Join(origin, "2"), []byte("welcome!"), 0700) 240 assert.NilError(t, err) 241 err = ioutil.WriteFile(filepath.Join(origin, "3"), []byte("will be ignored"), 0700) 242 assert.NilError(t, err) 243 // there is no known Go implementation of setcap/getcap with support for v3 file capability 244 out, err := exec.Command("setcap", "cap_block_suspend+ep", filepath.Join(origin, "2")).CombinedOutput() 245 assert.NilError(t, err, string(out)) 246 247 for _, c := range []Compression{ 248 Uncompressed, 249 Gzip, 250 } { 251 changes, err := tarUntar(t, origin, &TarOptions{ 252 Compression: c, 253 ExcludePatterns: []string{"3"}, 254 }) 255 256 if err != nil { 257 t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err) 258 } 259 260 if len(changes) != 1 || changes[0].Path != "/3" { 261 t.Fatalf("Unexpected differences after tarUntar: %v", changes) 262 } 263 out, err := exec.Command("getcap", filepath.Join(origin, "2")).CombinedOutput() 264 assert.NilError(t, err, string(out)) 265 assert.Check(t, is.Contains(string(out), "= cap_block_suspend+ep"), "untar should have kept the 'security.capability' xattr") 266 } 267 } 268 269 func TestCopyInfoDestinationPathSymlink(t *testing.T) { 270 tmpDir, _ := getTestTempDirs(t) 271 defer removeAllPaths(tmpDir) 272 273 root := strings.TrimRight(tmpDir, "/") + "/" 274 275 type FileTestData struct { 276 resource FileData 277 file string 278 expected CopyInfo 279 } 280 281 testData := []FileTestData{ 282 //Create a directory: /tmp/archive-copy-test*/dir1 283 //Test will "copy" file1 to dir1 284 {resource: FileData{filetype: Dir, path: "dir1", permissions: 0740}, file: "file1", expected: CopyInfo{Path: root + "dir1/file1", Exists: false, IsDir: false}}, 285 286 //Create a symlink directory to dir1: /tmp/archive-copy-test*/dirSymlink -> dir1 287 //Test will "copy" file2 to dirSymlink 288 {resource: FileData{filetype: Symlink, path: "dirSymlink", contents: root + "dir1", permissions: 0600}, file: "file2", expected: CopyInfo{Path: root + "dirSymlink/file2", Exists: false, IsDir: false}}, 289 290 //Create a file in tmp directory: /tmp/archive-copy-test*/file1 291 //Test to cover when the full file path already exists. 292 {resource: FileData{filetype: Regular, path: "file1", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "file1", Exists: true}}, 293 294 //Create a directory: /tmp/archive-copy*/dir2 295 //Test to cover when the full directory path already exists 296 {resource: FileData{filetype: Dir, path: "dir2", permissions: 0740}, file: "", expected: CopyInfo{Path: root + "dir2", Exists: true, IsDir: true}}, 297 298 //Create a symlink to a non-existent target: /tmp/archive-copy*/symlink1 -> noSuchTarget 299 //Negative test to cover symlinking to a target that does not exit 300 {resource: FileData{filetype: Symlink, path: "symlink1", contents: "noSuchTarget", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "noSuchTarget", Exists: false}}, 301 302 //Create a file in tmp directory for next test: /tmp/existingfile 303 {resource: FileData{filetype: Regular, path: "existingfile", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "existingfile", Exists: true}}, 304 305 //Create a symlink to an existing file: /tmp/archive-copy*/symlink2 -> /tmp/existingfile 306 //Test to cover when the parent directory of a new file is a symlink 307 {resource: FileData{filetype: Symlink, path: "symlink2", contents: "existingfile", permissions: 0600}, file: "", expected: CopyInfo{Path: root + "existingfile", Exists: true}}, 308 } 309 310 var dirs []FileData 311 for _, data := range testData { 312 dirs = append(dirs, data.resource) 313 } 314 provisionSampleDir(t, tmpDir, dirs) 315 316 for _, info := range testData { 317 p := filepath.Join(tmpDir, info.resource.path, info.file) 318 ci, err := CopyInfoDestinationPath(p) 319 assert.Check(t, err) 320 assert.Check(t, is.DeepEqual(info.expected, ci)) 321 } 322 }