1/* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/ 16 17package fs 18 19import ( 20 _ "crypto/sha256" 21 "io/ioutil" 22 "os" 23 "testing" 24 "time" 25 26 "github.com/containerd/continuity/fs/fstest" 27 "github.com/pkg/errors" 28) 29 30// TODO: Create copy directory which requires privilege 31// chown 32// mknod 33// setxattr fstest.SetXAttr("/home", "trusted.overlay.opaque", "y"), 34 35func TestCopyDirectory(t *testing.T) { 36 apply := fstest.Apply( 37 fstest.CreateDir("/etc/", 0755), 38 fstest.CreateFile("/etc/hosts", []byte("localhost 127.0.0.1"), 0644), 39 fstest.Link("/etc/hosts", "/etc/hosts.allow"), 40 fstest.CreateDir("/usr/local/lib", 0755), 41 fstest.CreateFile("/usr/local/lib/libnothing.so", []byte{0x00, 0x00}, 0755), 42 fstest.Symlink("libnothing.so", "/usr/local/lib/libnothing.so.2"), 43 fstest.CreateDir("/home", 0755), 44 ) 45 46 if err := testCopy(apply); err != nil { 47 t.Fatalf("Copy test failed: %+v", err) 48 } 49} 50 51// This test used to fail because link-no-nothing.txt would be copied first, 52// then file operations in dst during the CopyDir would follow the symlink and 53// fail. 54func TestCopyDirectoryWithLocalSymlink(t *testing.T) { 55 apply := fstest.Apply( 56 fstest.CreateFile("nothing.txt", []byte{0x00, 0x00}, 0755), 57 fstest.Symlink("nothing.txt", "link-no-nothing.txt"), 58 ) 59 60 if err := testCopy(apply); err != nil { 61 t.Fatalf("Copy test failed: %+v", err) 62 } 63} 64 65// TestCopyWithLargeFile tests copying a file whose size > 2^32 bytes. 66func TestCopyWithLargeFile(t *testing.T) { 67 if testing.Short() { 68 t.SkipNow() 69 } 70 apply := fstest.Apply( 71 fstest.CreateDir("/banana", 0755), 72 fstest.CreateRandomFile("/banana/split", time.Now().UnixNano(), 3*1024*1024*1024, 0644), 73 ) 74 75 if err := testCopy(apply); err != nil { 76 t.Fatal(err) 77 } 78} 79 80func testCopy(apply fstest.Applier) error { 81 t1, err := ioutil.TempDir("", "test-copy-src-") 82 if err != nil { 83 return errors.Wrap(err, "failed to create temporary directory") 84 } 85 defer os.RemoveAll(t1) 86 87 t2, err := ioutil.TempDir("", "test-copy-dst-") 88 if err != nil { 89 return errors.Wrap(err, "failed to create temporary directory") 90 } 91 defer os.RemoveAll(t2) 92 93 if err := apply.Apply(t1); err != nil { 94 return errors.Wrap(err, "failed to apply changes") 95 } 96 97 if err := CopyDir(t2, t1); err != nil { 98 return errors.Wrap(err, "failed to copy") 99 } 100 101 return fstest.CheckDirectoryEqual(t1, t2) 102} 103