-
Notifications
You must be signed in to change notification settings - Fork 6
/
virtiofsd_test.go
133 lines (112 loc) · 2.86 KB
/
virtiofsd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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: "none",
}
expected := "--syslog -o cache=none -o no_posix_lock -o source=/run/kata-shared/foo --fd=123 -f"
args, err := v.args(123)
assert.NoError(err)
assert.Equal(expected, strings.Join(args, " "))
expected = "--syslog -o cache=none -o no_posix_lock -o source=/run/kata-shared/foo --fd=456 -f"
args, err = v.args(456)
assert.NoError(err)
assert.Equal(expected, strings.Join(args, " "))
}
func TestValid(t *testing.T) {
assert := 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,
}
}
// valid case
v := newVirtiofsdFunc()
err := v.valid()
assert.NoError(err)
v = newVirtiofsdFunc()
v.path = ""
err = v.valid()
assert.Equal(errVirtiofsdDaemonPathEmpty, err)
v = newVirtiofsdFunc()
v.sourcePath = ""
err = v.valid()
assert.Equal(errVirtiofsdSourcePathEmpty, err)
v = newVirtiofsdFunc()
v.socketPath = ""
err = v.valid()
assert.Equal(errVirtiofsdSocketPathEmpty, err)
v = newVirtiofsdFunc()
v.sourcePath = "/foo/bar"
err = v.valid()
assert.Equal(errVirtiofsdSourceNotAvailable, err)
}