Merge pull request #6729 from nedsouza/259/tests_coverage_virtcontainers_persist

virtcontainers/persist: Improved test coverage 65% to 87.5%
This commit is contained in:
Archana Shinde 2023-05-04 16:18:55 -07:00 committed by GitHub
commit 9443c4aea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,9 @@
package persist
import (
"errors"
"os"
"strings"
"testing"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
@ -20,14 +22,29 @@ func TestGetDriverByName(t *testing.T) {
assert.NotNil(t, err)
assert.Nil(t, nonexist)
// testing correct driver is returned
fsDriver, err := GetDriverByName("fs")
assert.Nil(t, err)
assert.NotNil(t, fsDriver)
// testing case when expErr is set
expErr = errors.New("TEST-ERROR")
defer func() {
expErr = nil
}()
nonexist, err = GetDriverByName("fs")
assert.NotNil(t, err)
assert.Nil(t, nonexist)
b := err.Error()
assert.True(t, strings.Contains(b, "TEST-ERROR"))
}
func TestGetDriver(t *testing.T) {
assert := assert.New(t)
// testing correct driver is returned
fsd, err := GetDriver()
assert.NoError(err)
@ -39,5 +56,32 @@ func TestGetDriver(t *testing.T) {
}
assert.NoError(err)
assert.Equal(expectedFS, fsd)
assert.Equal(expectedFS, fsd) // driver should match correct one for UID
// testing case when expErr is set
expErr = errors.New("TEST-ERROR")
nonexist, err := GetDriver()
assert.NotNil(err)
assert.Nil(nonexist)
expErr = nil
// testing case when driver can't be found on supportedDrivers variable
supportedDriversBU := supportedDrivers
supportedDrivers = nil
fsd, err = GetDriver()
assert.Nil(fsd)
assert.NotNil(err)
b := err.Error()
assert.True(strings.Contains(b, "Could not find a FS driver"))
supportedDrivers = supportedDriversBU
// testing case when mock driver is activated
fs.EnableMockTesting(t.TempDir())
mock, err := GetDriver()
assert.NoError(err)
expectedFS, err = fs.MockFSInit(fs.MockStorageRootPath())
assert.NoError(err)
assert.Equal(expectedFS, mock)
fs.EnableMockTesting("")
}