github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/daemon/graphdriver/vfs/vfs_test.go (about)

     1  //go:build linux
     2  
     3  package vfs // import "github.com/Prakhar-Agarwal-byte/moby/daemon/graphdriver/vfs"
     4  
     5  import (
     6  	"archive/tar"
     7  	"bytes"
     8  	"errors"
     9  	"io"
    10  	"os"
    11  	"path/filepath"
    12  	"syscall"
    13  	"testing"
    14  
    15  	"github.com/moby/sys/mount"
    16  	"gotest.tools/v3/assert"
    17  
    18  	"github.com/Prakhar-Agarwal-byte/moby/daemon/graphdriver"
    19  	"github.com/Prakhar-Agarwal-byte/moby/daemon/graphdriver/graphtest"
    20  )
    21  
    22  // This avoids creating a new driver for each test if all tests are run
    23  // Make sure to put new tests between TestVfsSetup and TestVfsTeardown
    24  func TestVfsSetup(t *testing.T) {
    25  	graphtest.GetDriver(t, "vfs")
    26  }
    27  
    28  func TestVfsCreateEmpty(t *testing.T) {
    29  	graphtest.DriverTestCreateEmpty(t, "vfs")
    30  }
    31  
    32  func TestVfsCreateBase(t *testing.T) {
    33  	graphtest.DriverTestCreateBase(t, "vfs")
    34  }
    35  
    36  func TestVfsCreateSnap(t *testing.T) {
    37  	graphtest.DriverTestCreateSnap(t, "vfs")
    38  }
    39  
    40  func TestVfsSetQuota(t *testing.T) {
    41  	graphtest.DriverTestSetQuota(t, "vfs", false)
    42  }
    43  
    44  func TestVfsTeardown(t *testing.T) {
    45  	graphtest.PutDriver(t)
    46  }
    47  
    48  func TestXattrUnsupportedByBackingFS(t *testing.T) {
    49  	rootdir := t.TempDir()
    50  	// The ramfs filesystem is unconditionally compiled into the kernel,
    51  	// and does not support extended attributes.
    52  	err := mount.Mount("ramfs", rootdir, "ramfs", "")
    53  	if errors.Is(err, syscall.EPERM) {
    54  		t.Skip("test requires the ability to mount a filesystem")
    55  	}
    56  	assert.NilError(t, err)
    57  	defer mount.Unmount(rootdir)
    58  
    59  	var buf bytes.Buffer
    60  	tw := tar.NewWriter(&buf)
    61  	const (
    62  		filename = "test.txt"
    63  		content  = "hello world\n"
    64  	)
    65  	assert.NilError(t, tw.WriteHeader(&tar.Header{
    66  		Name: filename,
    67  		Mode: 0o644,
    68  		Size: int64(len(content)),
    69  		PAXRecords: map[string]string{
    70  			"SCHILY.xattr.user.test": "helloxattr",
    71  		},
    72  	}))
    73  	_, err = io.WriteString(tw, content)
    74  	assert.NilError(t, err)
    75  	assert.NilError(t, tw.Close())
    76  	testlayer := buf.Bytes()
    77  
    78  	for _, tt := range []struct {
    79  		name        string
    80  		opts        []string
    81  		expectErrIs error
    82  	}{
    83  		{
    84  			name:        "Default",
    85  			expectErrIs: syscall.EOPNOTSUPP,
    86  		},
    87  		{
    88  			name: "vfs.xattrs=i_want_broken_containers",
    89  			opts: []string{"vfs.xattrs=i_want_broken_containers"},
    90  		},
    91  	} {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			subdir := filepath.Join(rootdir, tt.name)
    94  			assert.NilError(t, os.Mkdir(subdir, 0o755))
    95  			d, err := graphdriver.GetDriver("vfs", nil,
    96  				graphdriver.Options{DriverOptions: tt.opts, Root: subdir})
    97  			assert.NilError(t, err)
    98  			defer d.Cleanup()
    99  
   100  			assert.NilError(t, d.Create("test", "", nil))
   101  			_, err = d.ApplyDiff("test", "", bytes.NewReader(testlayer))
   102  			assert.ErrorIs(t, err, tt.expectErrIs)
   103  
   104  			if err == nil {
   105  				path, err := d.Get("test", "")
   106  				assert.NilError(t, err)
   107  				defer d.Put("test")
   108  				actual, err := os.ReadFile(filepath.Join(path, filename))
   109  				assert.NilError(t, err)
   110  				assert.Equal(t, string(actual), content)
   111  			}
   112  		})
   113  	}
   114  }