forked from kubernetes/kubernetes
/
volume_test.go
295 lines (274 loc) · 7.83 KB
/
volume_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package volume
import (
"io/ioutil"
"os"
"path"
"reflect"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
)
type MockDiskUtil struct{}
// TODO(jonesdl) To fully test this, we could create a loopback device
// and mount that instead.
func (util *MockDiskUtil) AttachDisk(PD *GCEPersistentDisk) error {
err := os.MkdirAll(path.Join(PD.RootDir, "global", "pd", PD.PDName), 0750)
if err != nil {
return err
}
return nil
}
func (util *MockDiskUtil) DetachDisk(PD *GCEPersistentDisk, devicePath string) error {
err := os.RemoveAll(path.Join(PD.RootDir, "global", "pd", PD.PDName))
if err != nil {
return err
}
return nil
}
type MockMounter struct{}
func (mounter *MockMounter) Mount(source string, target string, fstype string, flags uintptr, data string) error {
return nil
}
func (mounter *MockMounter) Unmount(target string, flags int) error {
return nil
}
func (mounter *MockMounter) RefCount(vol Interface) (string, int, error) {
return "", 0, nil
}
func TestCreateVolumeBuilders(t *testing.T) {
tempDir := "CreateVolumes"
createVolumesTests := []struct {
volume api.Volume
path string
podID string
}{
{
api.Volume{
Name: "host-dir",
Source: &api.VolumeSource{
HostDir: &api.HostDir{"/dir/path"},
},
},
"/dir/path",
"",
},
{
api.Volume{
Name: "empty-dir",
Source: &api.VolumeSource{
EmptyDir: &api.EmptyDir{},
},
},
path.Join(tempDir, "/my-id/volumes/empty/empty-dir"),
"my-id",
},
{
api.Volume{
Name: "gce-pd",
Source: &api.VolumeSource{
GCEPersistentDisk: &api.GCEPersistentDisk{"my-disk", "ext4", 0, false},
},
},
path.Join(tempDir, "/my-id/volumes/gce-pd/gce-pd"),
"my-id",
},
{api.Volume{}, "", ""},
{
api.Volume{
Name: "empty-dir",
Source: &api.VolumeSource{},
},
"",
"",
},
}
for _, createVolumesTest := range createVolumesTests {
tt := createVolumesTest
vb, err := CreateVolumeBuilder(&tt.volume, tt.podID, tempDir)
if tt.volume.Source == nil {
if vb != nil {
t.Errorf("Expected volume to be nil")
}
continue
}
if tt.volume.Source.HostDir == nil && tt.volume.Source.EmptyDir == nil && tt.volume.Source.GCEPersistentDisk == nil {
if err != ErrUnsupportedVolumeType {
t.Errorf("Unexpected error: %v", err)
}
continue
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
path := vb.GetPath()
if path != tt.path {
t.Errorf("Unexpected bind path. Expected %v, got %v", tt.path, path)
}
}
}
func TestCreateVolumeCleaners(t *testing.T) {
tempDir := "CreateVolumeCleaners"
createVolumeCleanerTests := []struct {
kind string
name string
podID string
}{
{"empty", "empty-vol", "my-id"},
{"", "", ""},
{"gce-pd", "gce-pd-vol", "my-id"},
}
for _, tt := range createVolumeCleanerTests {
vol, err := CreateVolumeCleaner(tt.kind, tt.name, tt.podID, tempDir)
if tt.kind == "" && err != nil && vol == nil {
continue
}
if err != nil {
t.Errorf("Unexpected error occured: %v", err)
}
actualKind := reflect.TypeOf(vol).Elem().Name()
if tt.kind == "empty" && actualKind != "EmptyDir" {
t.Errorf("CreateVolumeCleaner returned invalid type. Expected EmptyDirectory, got %v, %v", tt.kind, actualKind)
}
if tt.kind == "gce-pd" && actualKind != "GCEPersistentDisk" {
t.Errorf("CreateVolumeCleaner returned invalid type. Expected PersistentDisk, got %v, %v", tt.kind, actualKind)
}
}
}
func TestSetUpAndTearDown(t *testing.T) {
tempDir, err := ioutil.TempDir("", "CreateVolumes")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
defer os.RemoveAll(tempDir)
fakeID := "my-id"
type VolumeTester interface {
Builder
Cleaner
}
volumes := []VolumeTester{
&EmptyDir{"empty", fakeID, tempDir},
&GCEPersistentDisk{"pd", fakeID, tempDir, "pd-disk", "ext4", "", false, &MockDiskUtil{}, &MockMounter{}},
}
for _, vol := range volumes {
err = vol.SetUp()
path := vol.GetPath()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Errorf("SetUp() failed, volume path not created: %v", path)
}
err = vol.TearDown()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if _, err := os.Stat(path); !os.IsNotExist(err) {
t.Errorf("TearDown() failed, original volume path not properly removed: %v", path)
}
}
}
func TestGetActiveVolumes(t *testing.T) {
tempDir, err := ioutil.TempDir("", "CreateVolumes")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
defer os.RemoveAll(tempDir)
getActiveVolumesTests := []struct {
name string
podID string
kind string
identifier string
}{
{"fakeName", "fakeID", "empty", "fakeID/fakeName"},
{"fakeName2", "fakeID2", "empty", "fakeID2/fakeName2"},
}
expectedIdentifiers := []string{}
for _, test := range getActiveVolumesTests {
volumeDir := path.Join(tempDir, test.podID, "volumes", test.kind, test.name)
os.MkdirAll(volumeDir, 0750)
expectedIdentifiers = append(expectedIdentifiers, test.identifier)
}
volumeMap := GetCurrentVolumes(tempDir)
for _, name := range expectedIdentifiers {
if _, ok := volumeMap[name]; !ok {
t.Errorf("Expected volume map entry not found: %v", name)
}
}
}
type fakeExec struct {
cmds [][]string
dirs []string
data []byte
err error
action func([]string, string)
}
func (f *fakeExec) ExecCommand(cmd []string, dir string) ([]byte, error) {
f.cmds = append(f.cmds, cmd)
f.dirs = append(f.dirs, dir)
f.action(cmd, dir)
return f.data, f.err
}
func TestGitVolume(t *testing.T) {
var fcmd exec.FakeCmd
fcmd = exec.FakeCmd{
CombinedOutputScript: []exec.FakeCombinedOutputAction{
func() ([]byte, error) {
os.MkdirAll(path.Join(fcmd.Dirs[0], "kubernetes"), 0750)
return []byte{}, nil
},
func() ([]byte, error) { return []byte{}, nil },
func() ([]byte, error) { return []byte{}, nil },
},
}
fake := exec.FakeExec{
CommandScript: []exec.FakeCommandAction{
func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
func(cmd string, args ...string) exec.Cmd { return exec.InitFakeCmd(&fcmd, cmd, args...) },
},
}
dir := os.TempDir() + "/git"
g := GitDir{
Source: "https://github.com/GoogleCloudPlatform/kubernetes.git",
Revision: "2a30ce65c5ab586b98916d83385c5983edd353a1",
PodID: "foo",
RootDir: dir,
Name: "test-pod",
exec: &fake,
}
err := g.SetUp()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
expectedCmds := [][]string{
{"git", "clone", g.Source},
{"git", "checkout", g.Revision},
{"git", "reset", "--hard"},
}
if fake.CommandCalls != len(expectedCmds) {
t.Errorf("unexpected command calls: expected 3, saw: %d", fake.CommandCalls)
}
if !reflect.DeepEqual(expectedCmds, fcmd.CombinedOutputLog) {
t.Errorf("unexpected commands: %v, expected: %v", fcmd.CombinedOutputLog, expectedCmds)
}
expectedDirs := []string{g.GetPath(), g.GetPath() + "/kubernetes", g.GetPath() + "/kubernetes"}
if len(fcmd.Dirs) != 3 || !reflect.DeepEqual(expectedDirs, fcmd.Dirs) {
t.Errorf("unexpected directories: %v, expected: %v", fcmd.Dirs, expectedDirs)
}
err = g.TearDown()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}