github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/persist/fs/fs_test.go (about) 1 // Copyright (c) 2019 Huawei Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package fs 7 8 import ( 9 "fmt" 10 "os" 11 "testing" 12 13 persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func getFsDriver() (*FS, error) { 18 driver, err := MockFSInit() 19 if err != nil { 20 return nil, fmt.Errorf("failed to init fs driver") 21 } 22 fs, ok := driver.(*MockFS) 23 if !ok { 24 return nil, fmt.Errorf("failed to convert driver to *MockFS") 25 } 26 27 return fs.FS, nil 28 } 29 30 func initTestDir() func() { 31 return func() { 32 os.RemoveAll(MockStorageRootPath()) 33 } 34 } 35 36 func TestFsLockShared(t *testing.T) { 37 defer initTestDir()() 38 39 fs, err := getFsDriver() 40 assert.Nil(t, err) 41 assert.NotNil(t, fs) 42 43 sid := "test-fs-driver" 44 fs.sandboxState.SandboxContainer = sid 45 sandboxDir, err := fs.sandboxDir(sid) 46 assert.Nil(t, err) 47 48 err = os.MkdirAll(sandboxDir, dirMode) 49 assert.Nil(t, err) 50 51 // Take 2 shared locks 52 unlockFunc, err := fs.Lock(sid, false) 53 assert.Nil(t, err) 54 55 unlockFunc2, err := fs.Lock(sid, false) 56 assert.Nil(t, err) 57 58 assert.Nil(t, unlockFunc()) 59 assert.Nil(t, unlockFunc2()) 60 assert.NotNil(t, unlockFunc2()) 61 } 62 63 func TestFsLockExclusive(t *testing.T) { 64 defer initTestDir()() 65 66 fs, err := getFsDriver() 67 assert.Nil(t, err) 68 assert.NotNil(t, fs) 69 70 sid := "test-fs-driver" 71 fs.sandboxState.SandboxContainer = sid 72 sandboxDir, err := fs.sandboxDir(sid) 73 assert.Nil(t, err) 74 75 err = os.MkdirAll(sandboxDir, dirMode) 76 assert.Nil(t, err) 77 78 // Take 1 exclusive lock 79 unlockFunc, err := fs.Lock(sid, true) 80 assert.Nil(t, err) 81 82 assert.Nil(t, unlockFunc()) 83 84 unlockFunc, err = fs.Lock(sid, true) 85 assert.Nil(t, err) 86 87 assert.Nil(t, unlockFunc()) 88 assert.NotNil(t, unlockFunc()) 89 } 90 91 func TestFsDriver(t *testing.T) { 92 defer initTestDir()() 93 94 fs, err := getFsDriver() 95 assert.Nil(t, err) 96 assert.NotNil(t, fs) 97 98 ss := persistapi.SandboxState{} 99 cs := make(map[string]persistapi.ContainerState) 100 // missing sandbox container id 101 assert.NotNil(t, fs.ToDisk(ss, cs)) 102 103 id := "test-fs-driver" 104 ss.SandboxContainer = id 105 assert.Nil(t, fs.ToDisk(ss, cs)) 106 107 // try non-existent dir 108 _, _, err = fs.FromDisk("test-fs") 109 assert.NotNil(t, err) 110 111 // since we didn't call ToDisk, state is still empty in disk file 112 ss, cs, err = fs.FromDisk(id) 113 assert.Nil(t, err) 114 assert.NotNil(t, ss) 115 assert.Equal(t, len(cs), 0) 116 117 assert.Equal(t, ss.SandboxContainer, id) 118 assert.Equal(t, ss.State, "") 119 120 // flush all to disk. 121 ss.State = "running" 122 assert.Nil(t, fs.ToDisk(ss, cs)) 123 ss, cs, err = fs.FromDisk(id) 124 assert.Nil(t, err) 125 assert.NotNil(t, ss) 126 assert.Equal(t, len(cs), 0) 127 128 assert.Equal(t, ss.SandboxContainer, id) 129 assert.Equal(t, ss.State, "running") 130 131 // add new container state. 132 cs["test-container"] = persistapi.ContainerState{ 133 State: "ready", 134 } 135 assert.Nil(t, fs.ToDisk(ss, cs)) 136 ss, cs, err = fs.FromDisk(id) 137 assert.Nil(t, err) 138 assert.NotNil(t, ss) 139 assert.Equal(t, len(cs), 1) 140 c, ok := cs["test-container"] 141 assert.True(t, ok) 142 assert.Equal(t, c.State, "ready") 143 144 // clean all container. 145 cs = make(map[string]persistapi.ContainerState) 146 assert.Nil(t, fs.ToDisk(ss, cs)) 147 ss, cs, err = fs.FromDisk(id) 148 assert.Nil(t, err) 149 assert.NotNil(t, ss) 150 assert.Equal(t, len(cs), 0) 151 152 // destroy whole sandbox dir. 153 assert.Nil(t, fs.Destroy(id)) 154 155 dir, err := fs.sandboxDir(id) 156 assert.Nil(t, err) 157 assert.NotEqual(t, len(dir), 0) 158 159 _, err = os.Stat(dir) 160 assert.NotNil(t, err) 161 assert.True(t, os.IsNotExist(err)) 162 } 163 164 func TestGlobalReadWrite(t *testing.T) { 165 defer initTestDir()() 166 167 relPath := "test/123/aaa.json" 168 data := "hello this is testing global read write" 169 170 fs, err := getFsDriver() 171 assert.Nil(t, err) 172 assert.NotNil(t, fs) 173 174 err = fs.GlobalWrite(relPath, []byte(data)) 175 assert.Nil(t, err) 176 177 out, err := fs.GlobalRead(relPath) 178 assert.Nil(t, err) 179 assert.Equal(t, string(out), data) 180 181 out, err = fs.GlobalRead("nonexist") 182 assert.NotNil(t, err) 183 assert.Nil(t, out) 184 }