diff --git a/virtcontainers/cc_proxy_test.go b/virtcontainers/cc_proxy_test.go index 641e7f1a1f..ed19855271 100644 --- a/virtcontainers/cc_proxy_test.go +++ b/virtcontainers/cc_proxy_test.go @@ -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) } diff --git a/virtcontainers/kata_proxy_test.go b/virtcontainers/kata_proxy_test.go new file mode 100644 index 0000000000..0e202d7a57 --- /dev/null +++ b/virtcontainers/kata_proxy_test.go @@ -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) +} diff --git a/virtcontainers/proxy_test.go b/virtcontainers/proxy_test.go index 753dfaa48a..b796d30780 100644 --- a/virtcontainers/proxy_test.go +++ b/virtcontainers/proxy_test.go @@ -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) + } +}