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

Commit

Permalink
ut: add more UTs
Browse files Browse the repository at this point in the history
Let's make codecov happier;)

Signed-off-by: Peng Tao <[email protected]>
  • Loading branch information
bergwolf committed Sep 14, 2018
1 parent 07c1f18 commit d75841e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 21 deletions.
12 changes: 12 additions & 0 deletions virtcontainers/cc_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ package virtcontainers

import (
"testing"

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

func TestCCProxyStart(t *testing.T) {
proxy := &ccProxy{}

testProxyStart(t, nil, proxy)
}

func TestCCProxy(t *testing.T) {
proxy := &ccProxy{}
assert := assert.New(t)

err := proxy.stop(0)
assert.Nil(err)

assert.False(proxy.consoleWatched())
}
13 changes: 11 additions & 2 deletions virtcontainers/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ func TestVMConfigValid(t *testing.T) {

config := vc.VMConfig{
HypervisorType: vc.MockHypervisor,
AgentType: vc.NoopAgentType,
ProxyType: vc.NoopProxyType,
HypervisorConfig: vc.HypervisorConfig{
KernelPath: testDir,
ImagePath: testDir,
Expand All @@ -109,6 +107,14 @@ func TestVMConfigValid(t *testing.T) {
f := factory{}

err := f.validateNewVMConfig(config)
assert.NotNil(err)

config.AgentType = vc.NoopAgentType
err = f.validateNewVMConfig(config)
assert.NotNil(err)

config.ProxyType = vc.NoopProxyType
err = f.validateNewVMConfig(config)
assert.Nil(err)
}

Expand Down Expand Up @@ -168,6 +174,9 @@ func TestFactoryGetVM(t *testing.T) {
ProxyType: vc.NoopProxyType,
}

err := vmConfig.Valid()
assert.Nil(err)

ctx := context.Background()

// direct factory
Expand Down
15 changes: 13 additions & 2 deletions virtcontainers/factory/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func TestTemplateFactory(t *testing.T) {
ProxyType: vc.NoopProxyType,
}

err := vmConfig.Valid()
assert.Nil(err)

ctx := context.Background()

// New
Expand All @@ -44,7 +47,7 @@ func TestTemplateFactory(t *testing.T) {
assert.Equal(f.Config(), vmConfig)

// GetBaseVM
_, err := f.GetBaseVM(ctx, vmConfig)
_, err = f.GetBaseVM(ctx, vmConfig)
assert.Nil(err)

// Fetch
Expand All @@ -53,6 +56,8 @@ func TestTemplateFactory(t *testing.T) {
config: vmConfig,
}

assert.Equal(tt.Config(), vmConfig)

err = tt.checkTemplateVM()
assert.Error(err)

Expand All @@ -69,13 +74,19 @@ func TestTemplateFactory(t *testing.T) {
err = tt.createTemplateVM(ctx)
assert.Error(err)

templateProxyType = vc.NoopProxyType
_, err = tt.GetBaseVM(ctx, vmConfig)
assert.Nil(err)

_, err = f.GetBaseVM(ctx, vmConfig)
assert.Nil(err)

templateProxyType = vc.NoopProxyType
err = tt.createTemplateVM(ctx)
assert.Nil(err)

_, err = tt.GetBaseVM(ctx, vmConfig)
assert.Nil(err)

_, err = f.GetBaseVM(ctx, vmConfig)
assert.Nil(err)

Expand Down
31 changes: 14 additions & 17 deletions virtcontainers/no_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,30 @@ package virtcontainers

import (
"testing"

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

func TestNoProxyStart(t *testing.T) {
p := &noProxy{}
assert := assert.New(t)

agentURL := "agentURL"
_, _, err := p.start(proxyParams{
agentURL: agentURL,
})
assert.NotNil(err)

pid, vmURL, err := p.start(proxyParams{
agentURL: agentURL,
logger: testDefaultLogger,
})
if err != nil {
t.Fatal(err)
}
assert.Nil(err)
assert.Equal(vmURL, agentURL)
assert.Equal(pid, 0)

if vmURL != agentURL {
t.Fatalf("Got URL %q, expecting %q", vmURL, agentURL)
}

if pid != 0 {
t.Fatal("Failure since returned PID should be 0")
}
}

func TestNoProxyStop(t *testing.T) {
p := &noProxy{}
err = p.stop(0)
assert.Nil(err)

if err := p.stop(0); err != nil {
t.Fatal(err)
}
assert.False(p.consoleWatched())
}
23 changes: 23 additions & 0 deletions virtcontainers/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestNewVM(t *testing.T) {
assert.Nil(err)
err = vm.Start()
assert.Nil(err)
err = vm.Disconnect()
assert.Nil(err)
err = vm.Save()
assert.Nil(err)
err = vm.Stop()
Expand Down Expand Up @@ -87,3 +89,24 @@ func TestVMConfigValid(t *testing.T) {
err = config.Valid()
assert.Nil(err)
}

func TestSetupProxy(t *testing.T) {
assert := assert.New(t)

config := VMConfig{
HypervisorType: MockHypervisor,
AgentType: NoopAgentType,
}

hypervisor := &mockHypervisor{}
agent := &noopAgent{}

// wrong proxy type
config.ProxyType = "invalidProxyType"
_, _, _, err := setupProxy(hypervisor, agent, config, "foobar")
assert.NotNil(err)

config.ProxyType = NoopProxyType
_, _, _, err = setupProxy(hypervisor, agent, config, "foobar")
assert.Nil(err)
}

0 comments on commit d75841e

Please sign in to comment.