proxy: decouple from sandbox

A proxy is mostly associated with an agent. Decouple it from sandbox
so that we can start it before linking vm with an actual sandbox.

Signed-off-by: Peng Tao <bergwolf@gmail.com>
This commit is contained in:
Peng Tao
2018-08-17 16:44:10 +08:00
parent f39fa5d489
commit 8f77c33d68
12 changed files with 177 additions and 130 deletions

View File

@@ -13,9 +13,12 @@ import (
"reflect"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var testDefaultLogger = logrus.WithField("proxy", "test")
func testSetProxyType(t *testing.T, value string, expected ProxyType) {
var proxyType ProxyType
@@ -206,7 +209,7 @@ func testDefaultProxyURL(expectedURL string, socketType string, sandboxID string
id: sandboxID,
}
url, err := defaultProxyURL(sandbox, socketType)
url, err := defaultProxyURL(sandbox.id, socketType)
if err != nil {
return err
}
@@ -242,7 +245,7 @@ func TestDefaultProxyURLUnknown(t *testing.T) {
}
}
func testProxyStart(t *testing.T, agent agent, proxy proxy, proxyType ProxyType) {
func testProxyStart(t *testing.T, agent agent, proxy proxy) {
assert := assert.New(t)
assert.NotNil(proxy)
@@ -252,7 +255,6 @@ func testProxyStart(t *testing.T, agent agent, proxy proxy, proxyType ProxyType)
defer os.RemoveAll(tmpdir)
type testData struct {
sandbox *Sandbox
params proxyParams
expectedURI string
expectError bool
@@ -263,60 +265,43 @@ func testProxyStart(t *testing.T, agent agent, proxy proxy, proxyType ProxyType)
expectedURI := fmt.Sprintf("unix://%s", expectedSocketPath)
data := []testData{
{&Sandbox{}, proxyParams{}, "", true},
{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",
},
},
},
// no path
proxyParams{
agentURL: "agentURL",
id: "foobar",
agentURL: "agentURL",
consoleURL: "consoleURL",
logger: testDefaultLogger,
},
"", true,
},
{
// invalid path
proxyParams{
id: "foobar",
path: invalidPath,
agentURL: "agentURL",
consoleURL: "consoleURL",
logger: testDefaultLogger,
},
"", true,
},
{
// good case
proxyParams{
id: testSandboxID,
path: "echo",
agentURL: "agentURL",
consoleURL: "consoleURL",
logger: testDefaultLogger,
},
expectedURI, false,
},
}
for _, d := range data {
pid, uri, err := proxy.start(d.sandbox, d.params)
pid, uri, err := proxy.start(d.params)
if d.expectError {
assert.Error(err)
continue
@@ -327,3 +312,43 @@ func testProxyStart(t *testing.T, agent agent, proxy proxy, proxyType ProxyType)
assert.Equal(d.expectedURI, uri)
}
}
func TestValidateProxyConfig(t *testing.T) {
assert := assert.New(t)
config := ProxyConfig{}
err := validateProxyConfig(config)
assert.Error(err)
config.Path = "foobar"
err = validateProxyConfig(config)
assert.Nil(err)
}
func TestValidateProxyParams(t *testing.T) {
assert := assert.New(t)
p := proxyParams{}
err := validateProxyParams(p)
assert.Error(err)
p.path = "foobar"
err = validateProxyParams(p)
assert.Error(err)
p.id = "foobar1"
err = validateProxyParams(p)
assert.Error(err)
p.agentURL = "foobar2"
err = validateProxyParams(p)
assert.Error(err)
p.consoleURL = "foobar3"
err = validateProxyParams(p)
assert.Error(err)
p.logger = &logrus.Entry{}
err = validateProxyParams(p)
assert.Nil(err)
}