From d75841ef23802e63a9103227ac243fb8a7c5ae2b Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 12 Sep 2018 11:27:26 +0800 Subject: [PATCH] ut: add more UTs Let's make codecov happier;) Signed-off-by: Peng Tao --- virtcontainers/cc_proxy_test.go | 12 +++++++ virtcontainers/factory/factory_test.go | 13 ++++++-- .../factory/template/template_test.go | 15 +++++++-- virtcontainers/no_proxy_test.go | 31 +++++++++---------- virtcontainers/vm_test.go | 23 ++++++++++++++ 5 files changed, 73 insertions(+), 21 deletions(-) diff --git a/virtcontainers/cc_proxy_test.go b/virtcontainers/cc_proxy_test.go index d5b07f85c2..faa32e891d 100644 --- a/virtcontainers/cc_proxy_test.go +++ b/virtcontainers/cc_proxy_test.go @@ -7,6 +7,8 @@ package virtcontainers import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestCCProxyStart(t *testing.T) { @@ -14,3 +16,13 @@ func TestCCProxyStart(t *testing.T) { 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()) +} diff --git a/virtcontainers/factory/factory_test.go b/virtcontainers/factory/factory_test.go index 6340b22ae8..1b1f7888b4 100644 --- a/virtcontainers/factory/factory_test.go +++ b/virtcontainers/factory/factory_test.go @@ -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, @@ -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) } @@ -168,6 +174,9 @@ func TestFactoryGetVM(t *testing.T) { ProxyType: vc.NoopProxyType, } + err := vmConfig.Valid() + assert.Nil(err) + ctx := context.Background() // direct factory diff --git a/virtcontainers/factory/template/template_test.go b/virtcontainers/factory/template/template_test.go index 09e9cd325a..a5021f6107 100644 --- a/virtcontainers/factory/template/template_test.go +++ b/virtcontainers/factory/template/template_test.go @@ -35,6 +35,9 @@ func TestTemplateFactory(t *testing.T) { ProxyType: vc.NoopProxyType, } + err := vmConfig.Valid() + assert.Nil(err) + ctx := context.Background() // New @@ -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 @@ -53,6 +56,8 @@ func TestTemplateFactory(t *testing.T) { config: vmConfig, } + assert.Equal(tt.Config(), vmConfig) + err = tt.checkTemplateVM() assert.Error(err) @@ -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) diff --git a/virtcontainers/no_proxy_test.go b/virtcontainers/no_proxy_test.go index 5bf6df765c..52375eeaf0 100644 --- a/virtcontainers/no_proxy_test.go +++ b/virtcontainers/no_proxy_test.go @@ -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()) } diff --git a/virtcontainers/vm_test.go b/virtcontainers/vm_test.go index fa5894c850..40a9729d6b 100644 --- a/virtcontainers/vm_test.go +++ b/virtcontainers/vm_test.go @@ -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() @@ -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) +}