github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/daemon/graphdriver/graphtest/testutil_unix.go (about)

     1  // +build linux freebsd
     2  
     3  package graphtest
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path"
     9  	"syscall"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/daemon/graphdriver"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func verifyFile(t testing.TB, path string, mode os.FileMode, uid, gid uint32) {
    18  	fi, err := os.Stat(path)
    19  	require.NoError(t, err)
    20  
    21  	actual := fi.Mode()
    22  	assert.Equal(t, mode&os.ModeType, actual&os.ModeType, path)
    23  	assert.Equal(t, mode&os.ModePerm, actual&os.ModePerm, path)
    24  	assert.Equal(t, mode&os.ModeSticky, actual&os.ModeSticky, path)
    25  	assert.Equal(t, mode&os.ModeSetuid, actual&os.ModeSetuid, path)
    26  	assert.Equal(t, mode&os.ModeSetgid, actual&os.ModeSetgid, path)
    27  
    28  	if stat, ok := fi.Sys().(*syscall.Stat_t); ok {
    29  		assert.Equal(t, uid, stat.Uid, path)
    30  		assert.Equal(t, gid, stat.Gid, path)
    31  	}
    32  }
    33  
    34  func createBase(t testing.TB, driver graphdriver.Driver, name string) {
    35  	// We need to be able to set any perms
    36  	oldmask := syscall.Umask(0)
    37  	defer syscall.Umask(oldmask)
    38  
    39  	err := driver.CreateReadWrite(name, "", nil)
    40  	require.NoError(t, err)
    41  
    42  	dir, err := driver.Get(name, "")
    43  	require.NoError(t, err)
    44  	defer driver.Put(name)
    45  
    46  	subdir := path.Join(dir, "a subdir")
    47  	require.NoError(t, os.Mkdir(subdir, 0705|os.ModeSticky))
    48  	require.NoError(t, os.Chown(subdir, 1, 2))
    49  
    50  	file := path.Join(dir, "a file")
    51  	err = ioutil.WriteFile(file, []byte("Some data"), 0222|os.ModeSetuid)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func verifyBase(t testing.TB, driver graphdriver.Driver, name string) {
    56  	dir, err := driver.Get(name, "")
    57  	require.NoError(t, err)
    58  	defer driver.Put(name)
    59  
    60  	subdir := path.Join(dir, "a subdir")
    61  	verifyFile(t, subdir, 0705|os.ModeDir|os.ModeSticky, 1, 2)
    62  
    63  	file := path.Join(dir, "a file")
    64  	verifyFile(t, file, 0222|os.ModeSetuid, 0, 0)
    65  
    66  	files, err := readDir(dir)
    67  	require.NoError(t, err)
    68  	assert.Len(t, files, 2)
    69  }