tests: Refactor CC proxy test for Kata

Reworked `TestCCProxyStart` to create a generic `testProxyStart()` that
is now used for testing both CC and Kata proxies.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2018-07-06 11:50:14 +01:00
parent 23eb13f23a
commit a38b251120
3 changed files with 108 additions and 74 deletions

View File

@ -6,84 +6,11 @@
package virtcontainers
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCCProxyStart(t *testing.T) {
assert := assert.New(t)
tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(tmpdir)
proxy := &ccProxy{}
type testData struct {
sandbox *Sandbox
expectedURI string
expectError bool
}
invalidPath := filepath.Join(tmpdir, "enoent")
expectedSocketPath := filepath.Join(runStoragePath, testSandboxID, "proxy.sock")
expectedURI := fmt.Sprintf("unix://%s", expectedSocketPath)
data := []testData{
{&Sandbox{}, "", true},
{
&Sandbox{
config: &SandboxConfig{
ProxyType: "invalid",
},
}, "", true,
},
{
&Sandbox{
config: &SandboxConfig{
ProxyType: CCProxyType,
// invalid - no path
ProxyConfig: ProxyConfig{},
},
}, "", true,
},
{
&Sandbox{
config: &SandboxConfig{
ProxyType: CCProxyType,
ProxyConfig: ProxyConfig{
Path: invalidPath,
},
},
}, "", true,
},
{
&Sandbox{
id: testSandboxID,
config: &SandboxConfig{
ProxyType: CCProxyType,
ProxyConfig: ProxyConfig{
Path: "echo",
},
},
}, expectedURI, false,
},
}
for _, d := range data {
pid, uri, err := proxy.start(d.sandbox, proxyParams{})
if d.expectError {
assert.Error(err)
continue
}
assert.NoError(err)
assert.True(pid > 0)
assert.Equal(d.expectedURI, uri)
}
testProxyStart(t, nil, proxy, CCProxyType)
}

View File

@ -0,0 +1,17 @@
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"testing"
)
func TestKataProxyStart(t *testing.T) {
agent := &kataAgent{}
proxy := &kataProxy{}
testProxyStart(t, agent, proxy, KataProxyType)
}

View File

@ -7,9 +7,13 @@ package virtcontainers
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func testSetProxyType(t *testing.T, value string, expected ProxyType) {
@ -248,3 +252,89 @@ func TestDefaultProxyURLUnknown(t *testing.T) {
t.Fatal()
}
}
func testProxyStart(t *testing.T, agent agent, proxy proxy, proxyType ProxyType) {
assert := assert.New(t)
assert.NotNil(proxy)
tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(tmpdir)
type testData struct {
sandbox *Sandbox
params proxyParams
expectedURI string
expectError bool
}
invalidPath := filepath.Join(tmpdir, "enoent")
expectedSocketPath := filepath.Join(runStoragePath, testSandboxID, "proxy.sock")
expectedURI := fmt.Sprintf("unix://%s", expectedSocketPath)
data := []testData{
{&Sandbox{}, proxyParams{}, "", true},
{
&Sandbox{
config: &SandboxConfig{
ProxyType: "invalid",
},
},
proxyParams{},
"", true,
},
{
&Sandbox{
config: &SandboxConfig{
ProxyType: proxyType,
// invalid - no path
ProxyConfig: ProxyConfig{},
},
},
proxyParams{},
"", true,
},
{
&Sandbox{
config: &SandboxConfig{
ProxyType: proxyType,
ProxyConfig: ProxyConfig{
Path: invalidPath,
},
},
},
proxyParams{},
"", true,
},
{
&Sandbox{
id: testSandboxID,
agent: agent,
config: &SandboxConfig{
ProxyType: proxyType,
ProxyConfig: ProxyConfig{
Path: "echo",
},
},
},
proxyParams{
agentURL: "agentURL",
},
expectedURI, false,
},
}
for _, d := range data {
pid, uri, err := proxy.start(d.sandbox, d.params)
if d.expectError {
assert.Error(err)
continue
}
assert.NoError(err)
assert.True(pid > 0)
assert.Equal(d.expectedURI, uri)
}
}