github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/proxy_test.go (about)

     1  // Copyright (c) 2017 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/kata-containers/runtime/virtcontainers/persist/fs"
    16  	"github.com/sirupsen/logrus"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  var testDefaultLogger = logrus.WithField("proxy", "test")
    21  
    22  func testSetProxyType(t *testing.T, value string, expected ProxyType) {
    23  	var proxyType ProxyType
    24  	assert := assert.New(t)
    25  
    26  	err := (&proxyType).Set(value)
    27  	assert.NoError(err)
    28  	assert.Equal(proxyType, expected)
    29  }
    30  
    31  func TestSetKataProxyType(t *testing.T) {
    32  	testSetProxyType(t, "kataProxy", KataProxyType)
    33  }
    34  
    35  func TestSetNoopProxyType(t *testing.T) {
    36  	testSetProxyType(t, "noopProxy", NoopProxyType)
    37  }
    38  
    39  func TestSetNoProxyType(t *testing.T) {
    40  	testSetProxyType(t, "noProxy", NoProxyType)
    41  }
    42  
    43  func TestSetKataBuiltInProxyType(t *testing.T) {
    44  	testSetProxyType(t, "kataBuiltInProxy", KataBuiltInProxyType)
    45  }
    46  
    47  func TestSetUnknownProxyType(t *testing.T) {
    48  	var proxyType ProxyType
    49  	assert := assert.New(t)
    50  
    51  	unknownType := "unknown"
    52  
    53  	err := (&proxyType).Set(unknownType)
    54  	assert.Error(err)
    55  	assert.NotEqual(proxyType, NoopProxyType)
    56  	assert.NotEqual(proxyType, NoProxyType)
    57  	assert.NotEqual(proxyType, KataProxyType)
    58  }
    59  
    60  func testStringFromProxyType(t *testing.T, proxyType ProxyType, expected string) {
    61  	proxyTypeStr := (&proxyType).String()
    62  	assert.Equal(t, proxyTypeStr, expected)
    63  }
    64  
    65  func TestStringFromKataProxyType(t *testing.T) {
    66  	proxyType := KataProxyType
    67  	testStringFromProxyType(t, proxyType, "kataProxy")
    68  }
    69  
    70  func TestStringFromNoProxyType(t *testing.T) {
    71  	proxyType := NoProxyType
    72  	testStringFromProxyType(t, proxyType, "noProxy")
    73  }
    74  
    75  func TestStringFromNoopProxyType(t *testing.T) {
    76  	proxyType := NoopProxyType
    77  	testStringFromProxyType(t, proxyType, "noopProxy")
    78  }
    79  
    80  func TestStringFromKataBuiltInProxyType(t *testing.T) {
    81  	proxyType := KataBuiltInProxyType
    82  	testStringFromProxyType(t, proxyType, "kataBuiltInProxy")
    83  }
    84  
    85  func TestStringFromUnknownProxyType(t *testing.T) {
    86  	var proxyType ProxyType
    87  	testStringFromProxyType(t, proxyType, "")
    88  }
    89  
    90  func testNewProxyFromProxyType(t *testing.T, proxyType ProxyType, expected proxy) {
    91  	result, err := newProxy(proxyType)
    92  	assert := assert.New(t)
    93  	assert.NoError(err)
    94  	assert.Exactly(result, expected)
    95  }
    96  
    97  func TestNewProxyFromKataProxyType(t *testing.T) {
    98  	proxyType := KataProxyType
    99  	expectedProxy := &kataProxy{}
   100  	testNewProxyFromProxyType(t, proxyType, expectedProxy)
   101  }
   102  
   103  func TestNewProxyFromNoProxyType(t *testing.T) {
   104  	proxyType := NoProxyType
   105  	expectedProxy := &noProxy{}
   106  	testNewProxyFromProxyType(t, proxyType, expectedProxy)
   107  }
   108  
   109  func TestNewProxyFromNoopProxyType(t *testing.T) {
   110  	proxyType := NoopProxyType
   111  	expectedProxy := &noopProxy{}
   112  	testNewProxyFromProxyType(t, proxyType, expectedProxy)
   113  }
   114  
   115  func TestNewProxyFromKataBuiltInProxyType(t *testing.T) {
   116  	proxyType := KataBuiltInProxyType
   117  	expectedProxy := &kataBuiltInProxy{}
   118  	testNewProxyFromProxyType(t, proxyType, expectedProxy)
   119  }
   120  
   121  func TestNewProxyFromUnknownProxyType(t *testing.T) {
   122  	var proxyType ProxyType
   123  	_, err := newProxy(proxyType)
   124  	assert.NoError(t, err)
   125  }
   126  
   127  func testNewProxyFromSandboxConfig(t *testing.T, sandboxConfig SandboxConfig) {
   128  	assert := assert.New(t)
   129  
   130  	_, err := newProxy(sandboxConfig.ProxyType)
   131  	assert.NoError(err)
   132  
   133  	err = validateProxyConfig(sandboxConfig.ProxyConfig)
   134  	assert.NoError(err)
   135  }
   136  
   137  var testProxyPath = "proxy-path"
   138  
   139  func TestNewProxyConfigFromKataProxySandboxConfig(t *testing.T) {
   140  	proxyConfig := ProxyConfig{
   141  		Path: testProxyPath,
   142  	}
   143  
   144  	sandboxConfig := SandboxConfig{
   145  		ProxyType:   KataProxyType,
   146  		ProxyConfig: proxyConfig,
   147  	}
   148  
   149  	testNewProxyFromSandboxConfig(t, sandboxConfig)
   150  }
   151  
   152  func TestNewProxyConfigNoPathFailure(t *testing.T) {
   153  	assert.Error(t, validateProxyConfig(ProxyConfig{}))
   154  }
   155  
   156  const sandboxID = "123456789"
   157  
   158  func testDefaultProxyURL(expectedURL string, socketType string, sandboxID string) error {
   159  	sandbox := &Sandbox{
   160  		id: sandboxID,
   161  	}
   162  
   163  	url, err := defaultProxyURL(sandbox.id, socketType)
   164  	if err != nil {
   165  		return err
   166  	}
   167  
   168  	if url != expectedURL {
   169  		return fmt.Errorf("Mismatched URL: %s vs %s", url, expectedURL)
   170  	}
   171  
   172  	return nil
   173  }
   174  
   175  func TestDefaultProxyURLUnix(t *testing.T) {
   176  	path := filepath.Join(filepath.Join(fs.MockRunStoragePath(), sandboxID), "proxy.sock")
   177  	socketPath := fmt.Sprintf("unix://%s", path)
   178  	assert.NoError(t, testDefaultProxyURL(socketPath, SocketTypeUNIX, sandboxID))
   179  }
   180  
   181  func TestDefaultProxyURLVSock(t *testing.T) {
   182  	assert.NoError(t, testDefaultProxyURL("", SocketTypeVSOCK, sandboxID))
   183  }
   184  
   185  func TestDefaultProxyURLUnknown(t *testing.T) {
   186  	path := filepath.Join(filepath.Join(fs.MockRunStoragePath(), sandboxID), "proxy.sock")
   187  	socketPath := fmt.Sprintf("unix://%s", path)
   188  	assert.Error(t, testDefaultProxyURL(socketPath, "foobar", sandboxID))
   189  }
   190  
   191  func testProxyStart(t *testing.T, agent agent, proxy proxy) {
   192  	assert := assert.New(t)
   193  
   194  	assert.NotNil(proxy)
   195  
   196  	tmpdir, err := ioutil.TempDir("", "")
   197  	assert.NoError(err)
   198  	defer os.RemoveAll(tmpdir)
   199  
   200  	type testData struct {
   201  		params      proxyParams
   202  		expectedURI string
   203  		expectError bool
   204  	}
   205  
   206  	invalidPath := filepath.Join(tmpdir, "enoent")
   207  	expectedSocketPath := filepath.Join(filepath.Join(fs.MockRunStoragePath(), testSandboxID), "proxy.sock")
   208  	expectedURI := fmt.Sprintf("unix://%s", expectedSocketPath)
   209  
   210  	data := []testData{
   211  		{proxyParams{}, "", true},
   212  		{
   213  			// no path
   214  			proxyParams{
   215  				id:         "foobar",
   216  				agentURL:   "agentURL",
   217  				consoleURL: "consoleURL",
   218  				logger:     testDefaultLogger,
   219  			},
   220  			"", true,
   221  		},
   222  		{
   223  			// invalid path
   224  			proxyParams{
   225  				id:         "foobar",
   226  				path:       invalidPath,
   227  				agentURL:   "agentURL",
   228  				consoleURL: "consoleURL",
   229  				logger:     testDefaultLogger,
   230  			},
   231  			"", true,
   232  		},
   233  		{
   234  			// good case
   235  			proxyParams{
   236  				id:         testSandboxID,
   237  				path:       "echo",
   238  				agentURL:   "agentURL",
   239  				consoleURL: "consoleURL",
   240  				logger:     testDefaultLogger,
   241  			},
   242  			expectedURI, false,
   243  		},
   244  	}
   245  
   246  	for _, d := range data {
   247  		pid, uri, err := proxy.start(d.params)
   248  		if d.expectError {
   249  			assert.Error(err)
   250  			continue
   251  		}
   252  
   253  		assert.NoError(err)
   254  		assert.True(pid > 0)
   255  		assert.Equal(d.expectedURI, uri)
   256  	}
   257  }
   258  
   259  func TestValidateProxyConfig(t *testing.T) {
   260  	assert := assert.New(t)
   261  
   262  	config := ProxyConfig{}
   263  	err := validateProxyConfig(config)
   264  	assert.Error(err)
   265  
   266  	config.Path = "foobar"
   267  	err = validateProxyConfig(config)
   268  	assert.Nil(err)
   269  }
   270  
   271  func TestValidateProxyParams(t *testing.T) {
   272  	assert := assert.New(t)
   273  
   274  	p := proxyParams{}
   275  	err := validateProxyParams(p)
   276  	assert.Error(err)
   277  
   278  	p.path = "foobar"
   279  	err = validateProxyParams(p)
   280  	assert.Error(err)
   281  
   282  	p.id = "foobar1"
   283  	err = validateProxyParams(p)
   284  	assert.Error(err)
   285  
   286  	p.agentURL = "foobar2"
   287  	err = validateProxyParams(p)
   288  	assert.Error(err)
   289  
   290  	p.consoleURL = "foobar3"
   291  	err = validateProxyParams(p)
   292  	assert.Error(err)
   293  
   294  	p.logger = &logrus.Entry{}
   295  	err = validateProxyParams(p)
   296  	assert.Nil(err)
   297  }