Skip to content

Commit

Permalink
Merge pull request kata-containers#1890 from devimc/topic/virtcontain…
Browse files Browse the repository at this point in the history
…ers/useTestity

virtcontainers: convert virtcontainers tests to testify/assert
  • Loading branch information
chavafg authored Jul 22, 2019
2 parents c7af16d + f2423e7 commit ff5f1b4
Show file tree
Hide file tree
Showing 36 changed files with 1,040 additions and 1,962 deletions.
70 changes: 22 additions & 48 deletions virtcontainers/acrn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/kata-containers/runtime/virtcontainers/device/config"
Expand All @@ -37,6 +36,7 @@ func newAcrnConfig() HypervisorConfig {
}

func testAcrnKernelParameters(t *testing.T, kernelParams []Param, debug bool) {
assert := assert.New(t)
acrnConfig := newAcrnConfig()
acrnConfig.KernelParams = kernelParams

Expand All @@ -52,9 +52,7 @@ func testAcrnKernelParameters(t *testing.T, kernelParams []Param, debug bool) {
expected := fmt.Sprintf("panic=1 maxcpus=%d foo=foo bar=bar", a.config.NumVCPUs)

params := a.kernelParameters()
if params != expected {
t.Fatalf("Got: %v, Expecting: %v", params, expected)
}
assert.Equal(params, expected)
}

func TestAcrnKernelParameters(t *testing.T) {
Expand All @@ -74,35 +72,27 @@ func TestAcrnKernelParameters(t *testing.T) {
}

func TestAcrnCapabilities(t *testing.T) {
assert := assert.New(t)
a := &acrn{
ctx: context.Background(),
arch: &acrnArchBase{},
}

caps := a.capabilities()
if !caps.IsBlockDeviceSupported() {
t.Fatal("Block device should be supported")
}

if !caps.IsBlockDeviceHotplugSupported() {
t.Fatal("Block device hotplug should be supported")
}
assert.True(caps.IsBlockDeviceSupported())
assert.True(caps.IsBlockDeviceHotplugSupported())
}

func testAcrnAddDevice(t *testing.T, devInfo interface{}, devType deviceType, expected []Device) {
assert := assert.New(t)
a := &acrn{
ctx: context.Background(),
arch: &acrnArchBase{},
}

err := a.addDevice(devInfo, devType)
if err != nil {
t.Fatal(err)
}

if reflect.DeepEqual(a.acrnConfig.Devices, expected) == false {
t.Fatalf("Got %v\nExpecting %v", a.acrnConfig.Devices, expected)
}
assert.NoError(err)
assert.Exactly(a.acrnConfig.Devices, expected)
}

func TestAcrnAddDeviceSerialPortDev(t *testing.T) {
Expand Down Expand Up @@ -204,23 +194,20 @@ func TestAcrnUpdateBlockDeviceInvalidIdx(t *testing.T) {
}

func TestAcrnGetSandboxConsole(t *testing.T) {
assert := assert.New(t)
a := &acrn{
ctx: context.Background(),
}
sandboxID := "testSandboxID"
expected := filepath.Join(store.RunVMStoragePath, sandboxID, consoleSocket)

result, err := a.getSandboxConsole(sandboxID)
if err != nil {
t.Fatal(err)
}

if result != expected {
t.Fatalf("Got %s\nExpecting %s", result, expected)
}
assert.NoError(err)
assert.Equal(result, expected)
}

func TestAcrnCreateSandbox(t *testing.T) {
assert := assert.New(t)
acrnConfig := newAcrnConfig()
a := &acrn{}

Expand All @@ -233,35 +220,27 @@ func TestAcrnCreateSandbox(t *testing.T) {
}

vcStore, err := store.NewVCSandboxStore(sandbox.ctx, sandbox.id)
if err != nil {
t.Fatal(err)
}
assert.NoError(err)
sandbox.store = vcStore

if err = globalSandboxList.addSandbox(sandbox); err != nil {
t.Fatalf("Could not add sandbox to global list: %v", err)
}
err = globalSandboxList.addSandbox(sandbox)
assert.NoError(err)

defer globalSandboxList.removeSandbox(sandbox.id)

// Create the hypervisor fake binary
testAcrnPath := filepath.Join(testDir, testHypervisor)
_, err = os.Create(testAcrnPath)
if err != nil {
t.Fatalf("Could not create hypervisor file %s: %v", testAcrnPath, err)
}

if err := a.createSandbox(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig, sandbox.store); err != nil {
t.Fatal(err)
}
assert.NoError(err)

if reflect.DeepEqual(acrnConfig, a.config) == false {
t.Fatalf("Got %v\nExpecting %v", a.config, acrnConfig)
}
err = a.createSandbox(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig, sandbox.store)
assert.NoError(err)
assert.Exactly(acrnConfig, a.config)
}

func TestAcrnMemoryTopology(t *testing.T) {
mem := uint32(1000)
assert := assert.New(t)

a := &acrn{
arch: &acrnArchBase{},
Expand All @@ -275,11 +254,6 @@ func TestAcrnMemoryTopology(t *testing.T) {
}

memory, err := a.memoryTopology()
if err != nil {
t.Fatal(err)
}

if reflect.DeepEqual(memory, expectedOut) == false {
t.Fatalf("Got %v\nExpecting %v", memory, expectedOut)
}
assert.NoError(err)
assert.Exactly(memory, expectedOut)
}
42 changes: 12 additions & 30 deletions virtcontainers/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
package virtcontainers

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func testSetAgentType(t *testing.T, value string, expected AgentType) {
var agentType AgentType
assert := assert.New(t)

err := (&agentType).Set(value)
if err != nil {
t.Fatal(err)
}

if agentType != expected {
t.Fatal(err)
}
assert.NoError(err)
assert.Equal(agentType, expected)
}

func TestSetNoopAgentType(t *testing.T) {
Expand All @@ -33,22 +30,16 @@ func TestSetKataAgentType(t *testing.T) {

func TestSetUnknownAgentType(t *testing.T) {
var agentType AgentType
assert := assert.New(t)

err := (&agentType).Set("unknown")
if err == nil {
t.Fatal()
}

if agentType == NoopAgentType {
t.Fatal()
}
assert.Error(err)
assert.NotEqual(agentType, NoopAgentType)
}

func testStringFromAgentType(t *testing.T, agentType AgentType, expected string) {
agentTypeStr := (&agentType).String()
if agentTypeStr != expected {
t.Fatal()
}
assert.Equal(t, agentTypeStr, expected)
}

func TestStringFromNoopAgentType(t *testing.T) {
Expand All @@ -66,10 +57,7 @@ func TestStringFromUnknownAgentType(t *testing.T) {

func testNewAgentFromAgentType(t *testing.T, agentType AgentType, expected agent) {
ag := newAgent(agentType)

if reflect.DeepEqual(ag, expected) == false {
t.Fatal()
}
assert.Exactly(t, ag, expected)
}

func TestNewAgentFromNoopAgentType(t *testing.T) {
Expand All @@ -87,14 +75,8 @@ func TestNewAgentFromUnknownAgentType(t *testing.T) {

func testNewAgentConfig(t *testing.T, config SandboxConfig, expected interface{}) {
agentConfig, err := newAgentConfig(config.AgentType, config.AgentConfig)
if err != nil {
t.Fatal(err)

}

if reflect.DeepEqual(agentConfig, expected) == false {
t.Fatal()
}
assert.NoError(t, err)
assert.Exactly(t, agentConfig, expected)
}

func TestNewAgentConfigFromNoopAgentType(t *testing.T) {
Expand Down
Loading

0 comments on commit ff5f1b4

Please sign in to comment.