github.com/intel/goresctrl@v0.5.0/pkg/cgroups/fsimock_test.go (about)

     1  package cgroups
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sort"
     7  	"testing"
     8  
     9  	"github.com/intel/goresctrl/pkg/testutils"
    10  )
    11  
    12  var fsMockUtFiles map[string]mockFile = map[string]mockFile{
    13  	"/my/emptyfile": {},
    14  	"/my/emptydir": {
    15  		info: &mockFileInfo{mode: os.ModeDir},
    16  	},
    17  	"/my/dir/data0": {data: []byte("abc")},
    18  	"/my/dir/data1": {data: []byte("xyz")},
    19  }
    20  
    21  func TestWalk(t *testing.T) {
    22  	fs := NewFsiMock(fsMockUtFiles)
    23  	foundNotInMyDir := []string{}
    24  	err := fs.Walk("/", func(path string, info os.FileInfo, err error) error {
    25  		if filepath.Base(path) == "dir" {
    26  			return filepath.SkipDir
    27  		}
    28  		foundNotInMyDir = append(foundNotInMyDir, path)
    29  		return nil
    30  	})
    31  	testutils.VerifyNoError(t, err)
    32  	sort.Strings(foundNotInMyDir)
    33  	testutils.VerifyStringSlices(t, []string{"/", "/my", "/my/emptydir", "/my/emptyfile"}, foundNotInMyDir)
    34  }
    35  
    36  func TestReadWrite(t *testing.T) {
    37  	var info os.FileInfo
    38  	fs := NewFsiMock(fsMockUtFiles)
    39  	f, err := fs.OpenFile("/my/dir/data0", os.O_WRONLY, 0)
    40  	testutils.VerifyNoError(t, err)
    41  	_, err = f.Write([]byte{})
    42  	testutils.VerifyNoError(t, err)
    43  	_, err = f.Write([]byte("01"))
    44  	testutils.VerifyNoError(t, err)
    45  	info, err = fs.Lstat("/my/dir/data0")
    46  	testutils.VerifyNoError(t, err)
    47  	if info.Size() != 3 {
    48  		t.Errorf("expected file size %d, got %d", 3, info.Size())
    49  	}
    50  	_, err = f.Write([]byte("23"))
    51  	testutils.VerifyNoError(t, err)
    52  	if info.Size() != 4 {
    53  		t.Errorf("expected file size %d, got %d", 4, info.Size())
    54  	}
    55  	f.Close()
    56  	f, err = fs.OpenFile("/my/dir/data0", os.O_RDONLY, 0)
    57  	testutils.VerifyNoError(t, err)
    58  	buf := make([]byte, 10)
    59  	bytes, err := f.Read(buf)
    60  	testutils.VerifyNoError(t, err)
    61  	if bytes != 4 {
    62  		t.Errorf("expected to read %d bytes, Read returned %d", 4, bytes)
    63  	}
    64  	testutils.VerifyStringSlices(t, []string{"0123"}, []string{string(buf[:bytes])})
    65  }