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

Commit

Permalink
tests: Refactor CC proxy test for Kata
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jodh-intel authored and Eric Ernst committed Aug 23, 2018
1 parent 61607e7 commit 3dda260
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 74 deletions.
75 changes: 1 addition & 74 deletions virtcontainers/cc_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 17 additions & 0 deletions virtcontainers/kata_proxy_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
90 changes: 90 additions & 0 deletions virtcontainers/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 3dda260

Please sign in to comment.