...

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

Documentation: github.com/containerd/continuity/fs

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5     Copyright The containerd Authors.
     6  
     7     Licensed under the Apache License, Version 2.0 (the "License");
     8     you may not use this file except in compliance with the License.
     9     You may obtain a copy of the License at
    10  
    11         http://www.apache.org/licenses/LICENSE-2.0
    12  
    13     Unless required by applicable law or agreed to in writing, software
    14     distributed under the License is distributed on an "AS IS" BASIS,
    15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16     See the License for the specific language governing permissions and
    17     limitations under the License.
    18  */
    19  
    20  package fs
    21  
    22  import (
    23  	"io"
    24  	"math/rand"
    25  	"os"
    26  	"os/exec"
    27  	"path/filepath"
    28  	"testing"
    29  
    30  	"github.com/containerd/continuity/testutil"
    31  	"github.com/containerd/continuity/testutil/loopback"
    32  )
    33  
    34  func TestCopyReflinkWithXFS(t *testing.T) {
    35  	testutil.RequiresRoot(t)
    36  	mnt := t.TempDir()
    37  
    38  	loop, err := loopback.New(1 << 30) // sparse file (max=1GB)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	mkfs := []string{"mkfs.xfs", "-m", "crc=1", "-n", "ftype=1", "-m", "reflink=1"}
    43  	if out, err := exec.Command(mkfs[0], append(mkfs[1:], loop.Device)...).CombinedOutput(); err != nil {
    44  		// not fatal
    45  		t.Skipf("could not mkfs (%v) %s: %v (out: %q)", mkfs, loop.Device, err, string(out))
    46  	}
    47  	loopbackSize, err := loop.HardSize()
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	t.Logf("Loopback file size (after mkfs (%v)): %d", mkfs, loopbackSize)
    52  	if out, err := exec.Command("mount", loop.Device, mnt).CombinedOutput(); err != nil {
    53  		// not fatal
    54  		t.Skipf("could not mount %s: %v (out: %q)", loop.Device, err, string(out))
    55  	}
    56  	unmounted := false
    57  	defer func() {
    58  		if !unmounted {
    59  			testutil.Unmount(t, mnt)
    60  		}
    61  		loop.Close()
    62  	}()
    63  
    64  	aPath := filepath.Join(mnt, "a")
    65  	aSize := int64(100 << 20) // 100MB
    66  	a, err := os.Create(aPath)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	randReader := rand.New(rand.NewSource(42))
    71  	if _, err := io.CopyN(a, randReader, aSize); err != nil {
    72  		a.Close()
    73  		t.Fatal(err)
    74  	}
    75  	if err := a.Close(); err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	bPath := filepath.Join(mnt, "b")
    79  	if err := CopyFile(bPath, aPath); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	testutil.Unmount(t, mnt)
    83  	unmounted = true
    84  	loopbackSize, err = loop.HardSize()
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	t.Logf("Loopback file size (after copying a %d-byte file): %d", aSize, loopbackSize)
    89  	allowedSize := int64(120 << 20) // 120MB
    90  	if loopbackSize > allowedSize {
    91  		t.Fatalf("expected <= %d, got %d", allowedSize, loopbackSize)
    92  	}
    93  }
    94  

View as plain text