forked from containers/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
216 lines (186 loc) · 6.57 KB
/
options_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
package types
import (
"bytes"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/containers/storage/pkg/idtools"
"github.com/containers/storage/pkg/unshare"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)
func TestGetRootlessStorageOpts(t *testing.T) {
envDriver, envDriverSet := os.LookupEnv("STORAGE_DRIVER")
os.Unsetenv("STORAGE_DRIVER")
const vfsDriver = "vfs"
t.Run("systemDriver=<unset>", func(t *testing.T) {
systemOpts := StoreOptions{}
td := t.TempDir()
home := filepath.Join(td, "unset-driver-home")
runhome := filepath.Join(td, "unset-driver-runhome")
defer os.RemoveAll(home)
defer os.RemoveAll(runhome)
systemOpts.GraphRoot = home
systemOpts.RunRoot = runhome
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
expectedDriver := vfsDriver
if canUseRootlessOverlay(home, runhome) {
expectedDriver = overlayDriver
}
assert.Equal(t, storageOpts.GraphDriverName, expectedDriver)
})
t.Run("systemDriver=btrfs", func(t *testing.T) {
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = "btrfs"
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Equal(t, storageOpts.GraphDriverName, "btrfs")
})
t.Run("systemDriver=overlay", func(t *testing.T) {
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = overlayDriver
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Equal(t, storageOpts.GraphDriverName, overlayDriver)
})
t.Run("systemDriver=overlay2", func(t *testing.T) {
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = "overlay2"
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Equal(t, storageOpts.GraphDriverName, overlayDriver)
})
t.Run("systemDriver=vfs", func(t *testing.T) {
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = vfsDriver
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Equal(t, storageOpts.GraphDriverName, vfsDriver)
})
t.Run("systemDriver=aufs", func(t *testing.T) {
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = "aufs"
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Assert(t, storageOpts.GraphDriverName == overlayDriver || storageOpts.GraphDriverName == vfsDriver, fmt.Sprintf("The rootless driver should be set to 'overlay' or 'vfs' not '%v'", storageOpts.GraphDriverName))
})
t.Run("systemDriver=devmapper", func(t *testing.T) {
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = "devmapper"
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Assert(t, storageOpts.GraphDriverName == overlayDriver || storageOpts.GraphDriverName == vfsDriver, fmt.Sprintf("The rootless driver should be set to 'overlay' or 'vfs' not '%v'", storageOpts.GraphDriverName))
})
t.Run("systemDriver=zfs", func(t *testing.T) {
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = "zfs"
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Assert(t, storageOpts.GraphDriverName == overlayDriver || storageOpts.GraphDriverName == vfsDriver, fmt.Sprintf("The rootless driver should be set to 'overlay' or 'vfs' not '%v'", storageOpts.GraphDriverName))
})
t.Run("STORAGE_DRIVER=btrfs", func(t *testing.T) {
t.Setenv("STORAGE_DRIVER", "btrfs")
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = vfsDriver
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Equal(t, storageOpts.GraphDriverName, "btrfs")
})
t.Run("STORAGE_DRIVER=zfs", func(t *testing.T) {
t.Setenv("STORAGE_DRIVER", "zfs")
systemOpts := StoreOptions{}
systemOpts.GraphDriverName = vfsDriver
storageOpts, err := getRootlessStorageOpts(systemOpts)
assert.NilError(t, err)
assert.Equal(t, storageOpts.GraphDriverName, "zfs")
})
if envDriverSet {
os.Setenv("STORAGE_DRIVER", envDriver)
} else {
os.Unsetenv("STORAGE_DRIVER")
}
}
func TestGetRootlessStorageOpts2(t *testing.T) {
opts := StoreOptions{
RootlessStoragePath: "/$HOME/$UID/containers/storage",
}
expectedPath := filepath.Join(os.Getenv("HOME"), fmt.Sprintf("%d", unshare.GetRootlessUID()), "containers/storage")
storageOpts, err := getRootlessStorageOpts(opts)
assert.NilError(t, err)
assert.Equal(t, storageOpts.GraphRoot, expectedPath)
}
func TestSetRemapUIDsGIDsOpts(t *testing.T) {
var remapOpts StoreOptions
uidmap := []idtools.IDMap{
{
ContainerID: 0,
HostID: 1000000000,
Size: 30000,
},
}
gidmap := []idtools.IDMap{
{
ContainerID: 0,
HostID: 1500000000,
Size: 60000,
},
}
err := ReloadConfigurationFile("./storage_test.conf", &remapOpts)
require.NoError(t, err)
if !reflect.DeepEqual(uidmap, remapOpts.UIDMap) {
t.Errorf("Failed to set UIDMap: Expected %v Actual %v", uidmap, remapOpts.UIDMap)
}
if !reflect.DeepEqual(gidmap, remapOpts.GIDMap) {
t.Errorf("Failed to set GIDMap: Expected %v Actual %v", gidmap, remapOpts.GIDMap)
}
}
func TestSetRemapUserGroupOpts(t *testing.T) {
var remapOpts StoreOptions
user := os.Getenv("USER")
if user == "root" {
t.Skip("This test is enabled only rootless user")
}
configPath := "./remap_user_test.conf"
config := fmt.Sprintf(`
[storage]
driver = ""
[storage.options]
remap-uids = "0:1000000000:30000"
remap-gids = "0:1500000000:60000"
remap-user = "%s"
remap-group = "%s"
`, user, user)
f, err := os.Create(configPath)
require.NoError(t, err)
defer func() {
f.Close()
os.Remove(configPath)
}()
_, err = f.Write([]byte(config))
require.NoError(t, err)
mappings, err := idtools.NewIDMappings(user, user)
require.NoError(t, err)
err = ReloadConfigurationFile(configPath, &remapOpts)
require.NoError(t, err)
if !reflect.DeepEqual(mappings.UIDs(), remapOpts.UIDMap) {
t.Errorf("Failed to set UIDMap: Expected %v Actual %v", mappings.UIDs(), remapOpts.UIDMap)
}
if !reflect.DeepEqual(mappings.GIDs(), remapOpts.GIDMap) {
t.Errorf("Failed to set GIDMap: Expected %v Actual %v", mappings.GIDs(), remapOpts.GIDMap)
}
}
func TestReloadConfigurationFile(t *testing.T) {
content := bytes.NewBufferString("")
logrus.SetOutput(content)
var storageOpts StoreOptions
err := ReloadConfigurationFile("./storage_broken.conf", &storageOpts)
require.NoError(t, err)
assert.Equal(t, storageOpts.RunRoot, "/run/containers/test")
logrus.SetOutput(os.Stderr)
assert.Equal(t, strings.Contains(content.String(), "Failed to decode the keys [\\\"foo\\\" \\\"storage.options.graphroot\\\"] from \\\"./storage_broken.conf\\\"\""), true)
}