...

Source file src/github.com/containerd/continuity/fs/copy_test.go

Documentation: github.com/containerd/continuity/fs

     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  
    17  package fs
    18  
    19  import (
    20  	_ "crypto/sha256"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/containerd/continuity/fs/fstest"
    27  )
    28  
    29  // TODO: Create copy directory which requires privilege
    30  //  chown
    31  //  mknod
    32  //  setxattr fstest.SetXAttr("/home", "trusted.overlay.opaque", "y"),
    33  
    34  func TestCopyDirectory(t *testing.T) {
    35  	apply := fstest.Apply(
    36  		fstest.CreateDir("/etc/", 0o755),
    37  		fstest.CreateFile("/etc/hosts", []byte("localhost 127.0.0.1"), 0o644),
    38  		fstest.Link("/etc/hosts", "/etc/hosts.allow"),
    39  		fstest.CreateDir("/usr/local/lib", 0o755),
    40  		fstest.CreateFile("/usr/local/lib/libnothing.so", []byte{0x00, 0x00}, 0o755),
    41  		fstest.Symlink("libnothing.so", "/usr/local/lib/libnothing.so.2"),
    42  		fstest.CreateDir("/home", 0o755),
    43  	)
    44  
    45  	if err := testCopy(t, apply); err != nil {
    46  		t.Fatalf("Copy test failed: %+v", err)
    47  	}
    48  }
    49  
    50  // This test used to fail because link-no-nothing.txt would be copied first,
    51  // then file operations in dst during the CopyDir would follow the symlink and
    52  // fail.
    53  func TestCopyDirectoryWithLocalSymlink(t *testing.T) {
    54  	apply := fstest.Apply(
    55  		fstest.CreateFile("nothing.txt", []byte{0x00, 0x00}, 0o755),
    56  		fstest.Symlink("nothing.txt", "link-no-nothing.txt"),
    57  	)
    58  
    59  	if err := testCopy(t, apply); err != nil {
    60  		t.Fatalf("Copy test failed: %+v", err)
    61  	}
    62  }
    63  
    64  // TestCopyWithLargeFile tests copying a file whose size > 2^32 bytes.
    65  func TestCopyWithLargeFile(t *testing.T) {
    66  	if testing.Short() {
    67  		t.SkipNow()
    68  	}
    69  	apply := fstest.Apply(
    70  		fstest.CreateDir("/banana", 0o755),
    71  		fstest.CreateRandomFile("/banana/split", time.Now().UnixNano(), 3*1024*1024*1024, 0o644),
    72  	)
    73  
    74  	if err := testCopy(t, apply); err != nil {
    75  		t.Fatal(err)
    76  	}
    77  }
    78  
    79  func testCopy(t testing.TB, apply fstest.Applier) error {
    80  	t1 := t.TempDir()
    81  	t2 := t.TempDir()
    82  
    83  	if err := apply.Apply(t1); err != nil {
    84  		return fmt.Errorf("failed to apply changes: %w", err)
    85  	}
    86  
    87  	if err := CopyDir(t2, t1); err != nil {
    88  		return fmt.Errorf("failed to copy: %w", err)
    89  	}
    90  
    91  	return fstest.CheckDirectoryEqual(t1, t2)
    92  }
    93  
    94  func BenchmarkLargeCopy100MB(b *testing.B) {
    95  	benchmarkLargeCopyFile(b, 100*1024*1024)
    96  }
    97  
    98  func BenchmarkLargeCopy1GB(b *testing.B) {
    99  	benchmarkLargeCopyFile(b, 1024*1024*1024)
   100  }
   101  
   102  func benchmarkLargeCopyFile(b *testing.B, size int64) {
   103  	b.StopTimer()
   104  	base := b.TempDir()
   105  	apply := fstest.Apply(
   106  		fstest.CreateRandomFile("/large", time.Now().UnixNano(), size, 0o644),
   107  	)
   108  	if err := apply.Apply(base); err != nil {
   109  		b.Fatal("failed to apply changes:", err)
   110  	}
   111  
   112  	for i := 0; i < b.N; i++ {
   113  		copied := b.TempDir()
   114  		b.StartTimer()
   115  		if err := CopyDir(copied, base); err != nil {
   116  			b.Fatal("failed to copy:", err)
   117  		}
   118  		b.StopTimer()
   119  		if i == 0 {
   120  			if err := fstest.CheckDirectoryEqual(base, copied); err != nil {
   121  				b.Fatal("check failed:", err)
   122  			}
   123  		}
   124  		os.RemoveAll(copied)
   125  	}
   126  }
   127  

View as plain text