mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-29 08:47:56 +00:00
virtcontainers: Expand unit test coverage for asset
Add additional test cases that cover more asset types and functions to increase unit test coverage. Fixes #424 Signed-off-by: Chelsea Mafrica <chelsea.e.mafrica@intel.com>
This commit is contained in:
parent
2fef265701
commit
d1d5c69b64
@ -6,6 +6,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
@ -65,6 +66,27 @@ func TestAssetHash(t *testing.T) {
|
|||||||
assert.Equal(assetContentHash, a.computedHash)
|
assert.Equal(assetContentHash, a.computedHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testPath(t *testing.T, a *Asset, correctPath string, msg string) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
returnedPath := a.Path()
|
||||||
|
assert.Equal(returnedPath, correctPath, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testType(t *testing.T, a *Asset, correctType AssetType, msg string) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
returnedType := a.Type()
|
||||||
|
assert.Equal(returnedType, correctType, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testValid(t *testing.T, a *Asset, msg string) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
v := a.Valid()
|
||||||
|
assert.True(v, msg)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAssetNew(t *testing.T) {
|
func TestAssetNew(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
@ -79,23 +101,59 @@ func TestAssetNew(t *testing.T) {
|
|||||||
_, err = tmpfile.Write(assetContent)
|
_, err = tmpfile.Write(assetContent)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
|
type testData struct {
|
||||||
|
inputPathVar string
|
||||||
|
inputHashVar string
|
||||||
|
inputAssetType AssetType
|
||||||
|
inputHash string
|
||||||
|
expectError bool
|
||||||
|
expectNilAsset bool
|
||||||
|
}
|
||||||
|
|
||||||
|
data := []testData{
|
||||||
|
// Successful with correct hash
|
||||||
|
{annotations.KernelPath, annotations.KernelHash, KernelAsset, assetContentHash, false, false},
|
||||||
|
{annotations.ImagePath, annotations.ImageHash, ImageAsset, assetContentHash, false, false},
|
||||||
|
{annotations.InitrdPath, annotations.InitrdHash, InitrdAsset, assetContentHash, false, false},
|
||||||
|
{annotations.HypervisorPath, annotations.HypervisorHash, HypervisorAsset, assetContentHash, false, false},
|
||||||
|
{annotations.JailerPath, annotations.JailerHash, JailerAsset, assetContentHash, false, false},
|
||||||
|
{annotations.FirmwarePath, annotations.FirmwareHash, FirmwareAsset, assetContentHash, false, false},
|
||||||
|
|
||||||
|
// Failure with incorrect hash
|
||||||
|
{annotations.KernelPath, annotations.KernelHash, KernelAsset, assetContentWrongHash, true, false},
|
||||||
|
{annotations.ImagePath, annotations.ImageHash, ImageAsset, assetContentWrongHash, true, false},
|
||||||
|
{annotations.InitrdPath, annotations.InitrdHash, InitrdAsset, assetContentWrongHash, true, false},
|
||||||
|
{annotations.HypervisorPath, annotations.HypervisorHash, HypervisorAsset, assetContentWrongHash, true, false},
|
||||||
|
{annotations.JailerPath, annotations.JailerHash, JailerAsset, assetContentWrongHash, true, false},
|
||||||
|
{annotations.FirmwarePath, annotations.FirmwareHash, FirmwareAsset, assetContentWrongHash, true, false},
|
||||||
|
|
||||||
|
// Other failures
|
||||||
|
{annotations.KernelPath, annotations.KernelHash, ImageAsset, assetContentHash, false, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, d := range data {
|
||||||
|
msg := fmt.Sprintf("test[%d]: %+v", i, d)
|
||||||
|
|
||||||
anno := map[string]string{
|
anno := map[string]string{
|
||||||
annotations.KernelPath: tmpfile.Name(),
|
d.inputPathVar: tmpfile.Name(),
|
||||||
annotations.KernelHash: assetContentHash,
|
d.inputHashVar: d.inputHash,
|
||||||
}
|
|
||||||
a, err := NewAsset(anno, ImageAsset)
|
|
||||||
assert.Nil(err)
|
|
||||||
assert.Nil(a)
|
|
||||||
|
|
||||||
a, err = NewAsset(anno, KernelAsset)
|
|
||||||
assert.Nil(err)
|
|
||||||
assert.Equal(assetContentHash, a.computedHash)
|
|
||||||
|
|
||||||
anno = map[string]string{
|
|
||||||
annotations.KernelPath: tmpfile.Name(),
|
|
||||||
annotations.KernelHash: assetContentWrongHash,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = NewAsset(anno, KernelAsset)
|
if d.expectNilAsset {
|
||||||
assert.NotNil(err)
|
a, err := NewAsset(anno, d.inputAssetType)
|
||||||
|
assert.NoError(err, msg)
|
||||||
|
assert.Nil(a, msg)
|
||||||
|
} else if d.expectError {
|
||||||
|
_, err := NewAsset(anno, d.inputAssetType)
|
||||||
|
assert.NotNil(err, msg)
|
||||||
|
} else {
|
||||||
|
a, err := NewAsset(anno, d.inputAssetType)
|
||||||
|
assert.Nil(err, msg)
|
||||||
|
assert.Equal(assetContentHash, a.computedHash, msg)
|
||||||
|
|
||||||
|
testPath(t, a, tmpfile.Name(), msg)
|
||||||
|
testType(t, a, d.inputAssetType, msg)
|
||||||
|
testValid(t, a, msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user