Files
kata-containers/src/runtime/virtcontainers/virtiofsd_test.go
Greg Kurz f0278f41d7 runtime/virtiofsd: Drop all references to "--cache=none"
This syntax belongs to the legacy C virtiofsd implementation that
we don't support anymore since kata-containers 3.1.3 because
of other API breaking changes.

People have been warned to switch from "none" to "never" since
kata-containers 2.5.2. Let's officially do that.

The compat code that would convert "none" to "never" isn't
needed anymore. Just drop it.

Fixes #7864

Signed-off-by: Greg Kurz <groug@kaod.org>
(cherry picked from commit 72c510d057)
Signed-off-by: Greg Kurz <groug@kaod.org>
2023-10-18 12:17:43 +02:00

159 lines
3.8 KiB
Go

// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"context"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestVirtiofsdStart(t *testing.T) {
// nolint: govet
type fields struct {
path string
socketPath string
cache string
extraArgs []string
sourcePath string
PID int
ctx context.Context
}
sourcePath := t.TempDir()
socketDir := t.TempDir()
socketPath := path.Join(socketDir, "socket.s")
validConfig := fields{
path: "/usr/bin/virtiofsd-path",
socketPath: socketPath,
sourcePath: sourcePath,
}
NoDirectorySocket := validConfig
NoDirectorySocket.socketPath = "/tmp/path/to/virtiofsd/socket.sock"
// nolint: govet
tests := []struct {
name string
fields fields
wantErr bool
}{
{"empty config", fields{}, true},
{"Directory socket does not exist", NoDirectorySocket, 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,
PID: tt.fields.PID,
ctx: tt.fields.ctx,
}
ctx := context.Background()
_, err := v.Start(ctx, nil)
if (err != nil) != tt.wantErr {
t.Errorf("virtiofsd.Start() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func TestVirtiofsdArgs(t *testing.T) {
assert := assert.New(t)
v := &virtiofsd{
path: "/usr/bin/virtiofsd",
sourcePath: "/run/kata-shared/foo",
cache: "never",
}
expected := "--syslog --cache=never --shared-dir=/run/kata-shared/foo --fd=123"
args, err := v.args(123)
assert.NoError(err)
assert.Equal(expected, strings.Join(args, " "))
expected = "--syslog --cache=never --shared-dir=/run/kata-shared/foo --fd=456"
args, err = v.args(456)
assert.NoError(err)
assert.Equal(expected, strings.Join(args, " "))
}
func TestValid(t *testing.T) {
a := assert.New(t)
sourcePath := t.TempDir()
socketDir := t.TempDir()
socketPath := socketDir + "socket.s"
newVirtiofsdFunc := func() *virtiofsd {
return &virtiofsd{
path: "/usr/bin/virtiofsd",
sourcePath: sourcePath,
socketPath: socketPath,
cache: "auto",
}
}
type fieldFunc func(v *virtiofsd)
type assertFunc func(name string, assert *assert.Assertions, v *virtiofsd)
// nolint: govet
tests := []struct {
name string
f fieldFunc
wantErr error
customAssert assertFunc
}{
{"valid case", nil, nil, nil},
{"no path", func(v *virtiofsd) {
v.path = ""
}, errVirtiofsdDaemonPathEmpty, nil},
{"no sourcePath", func(v *virtiofsd) {
v.sourcePath = ""
}, errVirtiofsdSourcePathEmpty, nil},
{"no socketPath", func(v *virtiofsd) {
v.socketPath = ""
}, errVirtiofsdSocketPathEmpty, nil},
{"source is not available", func(v *virtiofsd) {
v.sourcePath = "/foo/bar"
}, errVirtiofsdSourceNotAvailable, nil},
{"invalid cache mode", func(v *virtiofsd) {
v.cache = "foo"
}, errVirtiofsdInvalidVirtiofsCacheMode("foo"), nil},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := newVirtiofsdFunc()
if tt.f != nil {
tt.f(v)
}
err := v.valid()
if tt.wantErr != nil && err == nil {
t.Errorf("test case %+s: virtiofsd.valid() should get error `%+v`, but got nil", tt.name, tt.wantErr)
} else if tt.wantErr == nil && err != nil {
t.Errorf("test case %+s: virtiofsd.valid() should get no erro, but got `%+v`", tt.name, err)
} else if tt.wantErr != nil && err != nil {
a.Equal(err.Error(), tt.wantErr.Error(), "test case %+s", tt.name)
}
if tt.customAssert != nil {
tt.customAssert(tt.name, a, v)
}
})
}
}