forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount_test.go
225 lines (177 loc) · 4.83 KB
/
mount_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/stretchr/testify/assert"
)
func createSafeAndFakeStorage() (pb.Storage, error) {
dirPath, err := ioutil.TempDir("", "fake-dir")
if err != nil {
return pb.Storage{}, err
}
return pb.Storage{
Source: dirPath,
MountPoint: filepath.Join(dirPath, "test-mount"),
}, nil
}
func TestEphemeralStorageHandlerSuccessful(t *testing.T) {
skipUnlessRoot(t)
storage, err := createSafeAndFakeStorage()
if err != nil {
t.Fatal(err)
}
defer syscall.Unmount(storage.MountPoint, 0)
defer os.RemoveAll(storage.MountPoint)
storage.Fstype = typeTmpFs
storage.Source = typeTmpFs
sbs := make(map[string]*sandboxStorage)
_, err = ephemeralStorageHandler(storage, &sandbox{storages: sbs})
assert.Nil(t, err, "ephemeralStorageHandler() failed: %v", err)
}
func TestVirtio9pStorageHandlerSuccessful(t *testing.T) {
skipUnlessRoot(t)
storage, err := createSafeAndFakeStorage()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(storage.Source)
defer syscall.Unmount(storage.MountPoint, 0)
storage.Fstype = "bind"
storage.Options = []string{"rbind"}
_, err = virtio9pStorageHandler(storage, &sandbox{})
assert.Nil(t, err, "storage9pDriverHandler() failed: %v", err)
}
func TestVirtioBlkStorageHandlerSuccessful(t *testing.T) {
skipUnlessRoot(t)
testDir, err := ioutil.TempDir("", "kata-agent-tmp-")
if err != nil {
t.Fatal(t, err)
}
bridgeID := "02"
deviceID := "03"
pciBus := "0000:01"
completePCIAddr := fmt.Sprintf("0000:00:%s.0/%s:%s.0", bridgeID, pciBus, deviceID)
pciID := fmt.Sprintf("%s/%s", bridgeID, deviceID)
sysBusPrefix = testDir
bridgeBusPath := fmt.Sprintf(pciBusPathFormat, sysBusPrefix, "0000:00:02.0")
err = os.MkdirAll(filepath.Join(bridgeBusPath, pciBus), mountPerm)
assert.Nil(t, err)
devPath, err := createFakeDevicePath()
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(devPath)
dirPath, err := ioutil.TempDir("", "fake-dir")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dirPath)
storage := pb.Storage{
Source: pciID,
MountPoint: filepath.Join(dirPath, "test-mount"),
}
defer syscall.Unmount(storage.MountPoint, 0)
s := &sandbox{
pciDeviceMap: make(map[string]string),
}
s.Lock()
s.pciDeviceMap[completePCIAddr] = devPath
s.Unlock()
storage.Fstype = "bind"
storage.Options = []string{"rbind"}
systemDevPath = ""
_, err = virtioBlkStorageHandler(storage, s)
assert.Nil(t, err, "storageBlockStorageDriverHandler() failed: %v", err)
}
func testAddStoragesSuccessful(t *testing.T, storages []*pb.Storage) {
_, err := addStorages(storages, &sandbox{})
assert.Nil(t, err, "addStorages() failed: %v", err)
}
func TestAddStoragesEmptyStoragesSuccessful(t *testing.T) {
var storages []*pb.Storage
testAddStoragesSuccessful(t, storages)
}
func TestAddStoragesNilStoragesSuccessful(t *testing.T) {
storages := []*pb.Storage{
nil,
}
testAddStoragesSuccessful(t, storages)
}
func noopStorageHandlerReturnNil(storage pb.Storage, s *sandbox) (string, error) {
return "", nil
}
func noopStorageHandlerReturnError(storage pb.Storage, s *sandbox) (string, error) {
return "", fmt.Errorf("Noop handler failure")
}
func TestAddStoragesNoopHandlerSuccessful(t *testing.T) {
noopHandlerTag := "noop"
storageHandlerList = map[string]storageHandler{
noopHandlerTag: noopStorageHandlerReturnNil,
}
storages := []*pb.Storage{
{
Driver: noopHandlerTag,
},
}
testAddStoragesSuccessful(t, storages)
}
func testAddStoragesFailure(t *testing.T, storages []*pb.Storage) {
_, err := addStorages(storages, &sandbox{})
assert.NotNil(t, err, "addStorages() should have failed")
}
func TestAddStoragesUnknownHandlerFailure(t *testing.T) {
storageHandlerList = map[string]storageHandler{}
storages := []*pb.Storage{
{
Driver: "unknown",
},
}
testAddStoragesFailure(t, storages)
}
func TestAddStoragesNoopHandlerFailure(t *testing.T) {
noopHandlerTag := "noop"
storageHandlerList = map[string]storageHandler{
noopHandlerTag: noopStorageHandlerReturnError,
}
storages := []*pb.Storage{
{
Driver: noopHandlerTag,
},
}
testAddStoragesFailure(t, storages)
}
func TestMount(t *testing.T) {
assert := assert.New(t)
type testData struct {
source string
destination string
fsType string
flags int
options string
expectError bool
}
data := []testData{
{"", "", "", 0, "", true},
{"", "/foo", "9p", 0, "", true},
{"proc", "", "9p", 0, "", true},
{"proc", "/proc", "", 0, "", true},
}
for i, d := range data {
err := mount(d.source, d.destination, d.fsType, d.flags, d.options)
if d.expectError {
assert.Errorf(err, "test %d (%+v)", i, d)
} else {
assert.NoErrorf(err, "test %d (%+v)", i, d)
}
}
}