mirror of
https://github.com/aljazceru/kata-containers.git
synced 2026-01-04 15:04:25 +01:00
In oderder to make unit testing simpler, lets add an interface that could be mocked. Let hypervisor have a instance of virtiofsd interface, and this makes a loose dependency to allow mock testing. With the inteface is possible to add startSandbox unit test: - use utils.StartCmd to mock call to start hypervisor process. - Add unit test for startSandbox. Fixes: #2367 Signed-off-by: Jose Carlos Venegas Munoz <jose.carlos.venegas.munoz@intel.com>
75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
// Copyright (c) 2019 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package virtcontainers
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestVirtiofsdStart(t *testing.T) {
|
|
assert := assert.New(t)
|
|
type fields struct {
|
|
path string
|
|
socketPath string
|
|
cache string
|
|
extraArgs []string
|
|
sourcePath string
|
|
debug bool
|
|
PID int
|
|
ctx context.Context
|
|
}
|
|
|
|
sourcePath, err := ioutil.TempDir("", "")
|
|
assert.NoError(err)
|
|
defer os.RemoveAll(sourcePath)
|
|
|
|
validConfig := fields{
|
|
path: "/tmp/a/path",
|
|
socketPath: "/tmp/a/path/to/sock.sock",
|
|
sourcePath: sourcePath,
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
wantErr bool
|
|
}{
|
|
{"empty config", fields{}, true},
|
|
{"valid config", validConfig, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
v := &virtiofsd{
|
|
path: tt.fields.path,
|
|
socketPath: tt.fields.socketPath,
|
|
cache: tt.fields.cache,
|
|
extraArgs: tt.fields.extraArgs,
|
|
sourcePath: tt.fields.sourcePath,
|
|
debug: tt.fields.debug,
|
|
PID: tt.fields.PID,
|
|
ctx: tt.fields.ctx,
|
|
//Mock wait function
|
|
wait: func(runningCmd *exec.Cmd, stderr io.ReadCloser, debug bool) error {
|
|
return nil
|
|
},
|
|
}
|
|
var ctx context.Context
|
|
_, err := v.Start(ctx)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("virtiofsd.Start() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
})
|
|
}
|
|
}
|