Navigation Menu

Skip to content

Commit

Permalink
virtcontainers/persist: Improved test coverage 65% to 87.5%
Browse files Browse the repository at this point in the history
Expanded tests on manager_test.go to cover more lines of code.

Fixes: #259

Signed-off-by: Eduardo Berrocal <eduardo.berrocal@intel.com>
  • Loading branch information
eberroca committed Apr 25, 2023
1 parent 7fdaab4 commit 9c38204
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/runtime/virtcontainers/persist/manager_test.go
Expand Up @@ -7,7 +7,9 @@
package persist

import (
"errors"
"os"
"strings"
"testing"

persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
Expand All @@ -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)

Expand All @@ -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("")
}

0 comments on commit 9c38204

Please sign in to comment.