Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: Expand unit test coverage for asset
Browse files Browse the repository at this point in the history
Add additional test cases that cover more asset types and functions to
increase unit test coverage.

Fixes #2835

Signed-off-by: Chelsea Mafrica <[email protected]>
  • Loading branch information
Chelsea Mafrica committed Jul 27, 2020
1 parent dae0786 commit 1e2a361
Showing 1 changed file with 73 additions and 15 deletions.
88 changes: 73 additions & 15 deletions virtcontainers/types/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package types

import (
"fmt"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -65,6 +66,27 @@ func TestAssetHash(t *testing.T) {
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) {
assert := assert.New(t)

Expand All @@ -79,23 +101,59 @@ func TestAssetNew(t *testing.T) {
_, err = tmpfile.Write(assetContent)
assert.Nil(err)

anno := map[string]string{
annotations.KernelPath: tmpfile.Name(),
annotations.KernelHash: assetContentHash,
type testData struct {
inputPathVar string
inputHashVar string
inputAssetType AssetType
inputHash string
expectError bool
expectNilAsset bool
}
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,
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},
}

_, err = NewAsset(anno, KernelAsset)
assert.NotNil(err)
for i, d := range data {
msg := fmt.Sprintf("test[%d]: %+v", i, d)

anno := map[string]string{
d.inputPathVar: tmpfile.Name(),
d.inputHashVar: d.inputHash,
}

if d.expectNilAsset {
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)
}
}
}

0 comments on commit 1e2a361

Please sign in to comment.