From 23eb13f23af1d68885b89e503f47a7e7b7e63812 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Wed, 4 Jul 2018 12:01:14 +0100 Subject: [PATCH 1/2] proxy: Pass sandbox to proxy Add the `-sandbox` option when launching the proxy. This isn't strictly required by the proxy, but is extremely useful for log analysis to allow log entries to be matched to sandboxes as the proxy will add a `sandbox` field in each log entry. Fixes #463. Signed-off-by: James O. D. Hunt --- virtcontainers/kata_proxy.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/virtcontainers/kata_proxy.go b/virtcontainers/kata_proxy.go index 5d996e7ead..a1d3d3aaa9 100644 --- a/virtcontainers/kata_proxy.go +++ b/virtcontainers/kata_proxy.go @@ -43,7 +43,13 @@ func (p *kataProxy) start(sandbox *Sandbox, params proxyParams) (int, string, er return -1, "", err } - args := []string{config.Path, "-listen-socket", proxyURL, "-mux-socket", params.agentURL} + args := []string{ + config.Path, + "-listen-socket", proxyURL, + "-mux-socket", params.agentURL, + "-sandbox", sandbox.ID(), + } + if config.Debug { args = append(args, "-log", "debug") console, err := sandbox.hypervisor.getSandboxConsole(sandbox.id) From a38b251120d9412e49a0f5bd3b7fd95be8f88c0f Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Fri, 6 Jul 2018 11:50:14 +0100 Subject: [PATCH 2/2] 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 --- virtcontainers/cc_proxy_test.go | 75 +------------------------- virtcontainers/kata_proxy_test.go | 17 ++++++ virtcontainers/proxy_test.go | 90 +++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 74 deletions(-) create mode 100644 virtcontainers/kata_proxy_test.go 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) + } +}