-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AlluxioFuseFileSystemTest.java
409 lines (351 loc) · 14.6 KB
/
AlluxioFuseFileSystemTest.java
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.fuse;
import static jnr.constants.platform.OpenFlags.O_RDONLY;
import static jnr.constants.platform.OpenFlags.O_WRONLY;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.times;
import alluxio.AlluxioURI;
import alluxio.ConfigurationRule;
import alluxio.PropertyKey;
import alluxio.client.file.FileInStream;
import alluxio.client.file.FileOutStream;
import alluxio.client.file.FileSystem;
import alluxio.client.file.URIStatus;
import alluxio.client.file.options.SetAttributeOptions;
import alluxio.security.authorization.Mode;
import alluxio.wire.FileInfo;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import jnr.ffi.Pointer;
import jnr.ffi.Runtime;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.stubbing.Answer;
import ru.serce.jnrfuse.ErrorCodes;
import ru.serce.jnrfuse.struct.FileStat;
import ru.serce.jnrfuse.struct.FuseFileInfo;
import java.util.Collections;
import java.util.List;
/**
* Isolation tests for {@link AlluxioFuseFileSystem}.
*/
public class AlluxioFuseFileSystemTest {
private static final String TEST_ROOT_PATH = "/t/root";
private static final AlluxioURI BASE_EXPECTED_URI = new AlluxioURI(TEST_ROOT_PATH);
private AlluxioFuseFileSystem mFuseFs;
private FileSystem mFileSystem;
private FuseFileInfo mFileInfo;
@Rule
public ConfigurationRule mConfiguration =
new ConfigurationRule(ImmutableMap.of(PropertyKey.FUSE_CACHED_PATHS_MAX, "0",
PropertyKey.FUSE_USER_GROUP_TRANSLATION_ENABLED, "true"));
@Before
public void before() throws Exception {
final List<String> empty = Collections.emptyList();
AlluxioFuseOptions opts =
new AlluxioFuseOptions("/doesnt/matter", TEST_ROOT_PATH, false, empty);
mFileSystem = mock(FileSystem.class);
try {
mFuseFs = new AlluxioFuseFileSystem(mFileSystem, opts);
} catch (UnsatisfiedLinkError e) {
// stop test and ignore if FuseFileSystem fails to create due to missing libfuse library
Assume.assumeNoException(e);
}
mFileInfo = allocateNativeFileInfo();
}
@Test
public void chmod() throws Exception {
long mode = 123;
mFuseFs.chmod("/foo/bar", mode);
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
SetAttributeOptions options = SetAttributeOptions.defaults().setMode(new Mode((short) mode));
verify(mFileSystem).setAttribute(expectedPath, options);
}
@Test
public void chown() throws Exception {
long uid = AlluxioFuseUtils.getUid(System.getProperty("user.name"));
long gid = AlluxioFuseUtils.getGid(System.getProperty("user.name"));
mFuseFs.chown("/foo/bar", uid, gid);
String userName = System.getProperty("user.name");
String groupName = AlluxioFuseUtils.getGroupName(gid);
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
SetAttributeOptions options =
SetAttributeOptions.defaults().setGroup(groupName).setOwner(userName);
verify(mFileSystem).setAttribute(expectedPath, options);
}
@Test
public void chownWithoutValidGid() throws Exception {
long uid = AlluxioFuseUtils.getUid(System.getProperty("user.name"));
long gid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE;
mFuseFs.chown("/foo/bar", uid, gid);
String userName = System.getProperty("user.name");
String groupName = AlluxioFuseUtils.getGroupName(userName);
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
SetAttributeOptions options =
SetAttributeOptions.defaults().setGroup(groupName).setOwner(userName);
verify(mFileSystem).setAttribute(expectedPath, options);
gid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE_UNSIGNED;
mFuseFs.chown("/foo/bar", uid, gid);
verify(mFileSystem, times(2)).setAttribute(expectedPath, options);
}
@Test
public void chownWithoutValidUid() throws Exception {
String userName = System.getProperty("user.name");
long uid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE;
long gid = AlluxioFuseUtils.getGid(userName);
mFuseFs.chown("/foo/bar", uid, gid);
String groupName = AlluxioFuseUtils.getGroupName(userName);
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
SetAttributeOptions options =
SetAttributeOptions.defaults().setGroup(groupName);
verify(mFileSystem).setAttribute(expectedPath, options);
uid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE_UNSIGNED;
mFuseFs.chown("/foo/bar", uid, gid);
verify(mFileSystem, times(2)).setAttribute(expectedPath, options);
}
@Test
public void chownWithoutValidUidAndGid() throws Exception {
long uid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE;
long gid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE;
mFuseFs.chown("/foo/bar", uid, gid);
verify(mFileSystem, never()).setAttribute(any());
uid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE_UNSIGNED;
gid = AlluxioFuseFileSystem.ID_NOT_SET_VALUE_UNSIGNED;
mFuseFs.chown("/foo/bar", uid, gid);
verify(mFileSystem, never()).setAttribute(any());
}
@Test
public void create() throws Exception {
mFileInfo.flags.set(O_WRONLY.intValue());
mFuseFs.create("/foo/bar", 0, mFileInfo);
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
verify(mFileSystem).createFile(expectedPath);
}
@Test
public void flush() throws Exception {
FileOutStream fos = mock(FileOutStream.class);
AlluxioURI anyURI = any();
when(mFileSystem.createFile(anyURI)).thenReturn(fos);
// open a file
mFileInfo.flags.set(O_WRONLY.intValue());
mFuseFs.create("/foo/bar", 0, mFileInfo);
// then call flush into it
mFuseFs.flush("/foo/bar", mFileInfo);
verify(fos).flush();
}
@Test
public void getattr() throws Exception {
// set up status
FileInfo info = new FileInfo();
info.setLength(10);
info.setLastModificationTimeMs(1000);
String userName = System.getProperty("user.name");
info.setOwner(userName);
info.setGroup(AlluxioFuseUtils.getGroupName(userName));
info.setFolder(true);
info.setMode(123);
URIStatus status = new URIStatus(info);
// mock fs
when(mFileSystem.exists(any(AlluxioURI.class))).thenReturn(true);
when(mFileSystem.getStatus(any(AlluxioURI.class))).thenReturn(status);
FileStat stat = new FileStat(Runtime.getSystemRuntime());
assertEquals(0, mFuseFs.getattr("/foo", stat));
assertEquals(status.getLength(), stat.st_size.longValue());
assertEquals(status.getLastModificationTimeMs() / 1000, stat.st_ctim.tv_sec.get());
assertEquals((status.getLastModificationTimeMs() % 1000) * 1000,
stat.st_ctim.tv_nsec.longValue());
assertEquals(status.getLastModificationTimeMs() / 1000, stat.st_mtim.tv_sec.get());
assertEquals((status.getLastModificationTimeMs() % 1000) * 1000,
stat.st_mtim.tv_nsec.longValue());
assertEquals(AlluxioFuseUtils.getUid(System.getProperty("user.name")), stat.st_uid.get());
assertEquals(AlluxioFuseUtils.getGid(System.getProperty("user.name")), stat.st_gid.get());
assertEquals(123 | FileStat.S_IFDIR, stat.st_mode.intValue());
}
@Test
public void mkDir() throws Exception {
mFuseFs.mkdir("/foo/bar", -1);
verify(mFileSystem).createDirectory(BASE_EXPECTED_URI.join("/foo/bar"));
}
@Test
public void openWithoutDelay() throws Exception {
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
setUpOpenMock(expectedPath);
mFuseFs.open("/foo/bar", mFileInfo);
verify(mFileSystem).exists(expectedPath);
verify(mFileSystem).getStatus(expectedPath);
verify(mFileSystem).openFile(expectedPath);
}
@Test
public void incompleteFileCannotOpen() throws Exception {
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
FileInfo fi = setUpOpenMock(expectedPath);
fi.setCompleted(false);
mFuseFs.open("/foo/bar", mFileInfo);
verify(mFileSystem).exists(expectedPath);
verify(mFileSystem, atLeast(100)).getStatus(expectedPath);
verify(mFileSystem, never()).openFile(expectedPath);
}
@Test
public void openWithDelay() throws Exception {
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
FileInfo fi = setUpOpenMock(expectedPath);
fi.setCompleted(false);
// Use another thread to open file so that
// we could change the file status when opening it
Thread t = new Thread(() -> mFuseFs.open("/foo/bar", mFileInfo));
t.start();
Thread.sleep(1000);
// If the file exists but is not completed, we will wait for the file to complete
verify(mFileSystem).exists(expectedPath);
verify(mFileSystem, atLeast(10)).getStatus(expectedPath);
verify(mFileSystem, never()).openFile(expectedPath);
fi.setCompleted(true);
t.join();
verify(mFileSystem).openFile(expectedPath);
}
@Test
public void read() throws Exception {
// mocks set-up
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
setUpOpenMock(expectedPath);
FileInStream fakeInStream = mock(FileInStream.class);
when(fakeInStream.read(any(byte[].class),
anyInt(), anyInt())).then((Answer<Integer>) invocationOnMock -> {
byte[] myDest = (byte[]) invocationOnMock.getArguments()[0];
for (byte i = 0; i < 4; i++) {
myDest[i] = i;
}
return 4;
});
when(mFileSystem.openFile(expectedPath)).thenReturn(fakeInStream);
mFileInfo.flags.set(O_RDONLY.intValue());
// prepare something to read to it
Runtime r = Runtime.getSystemRuntime();
Pointer ptr = r.getMemoryManager().allocateTemporary(4, true);
// actual test
mFuseFs.open("/foo/bar", mFileInfo);
mFuseFs.read("/foo/bar", ptr, 4, 0, mFileInfo);
final byte[] dst = new byte[4];
ptr.get(0, dst, 0, 4);
final byte[] expected = new byte[] {0, 1, 2, 3};
assertArrayEquals("Source and dst data should be equal", expected, dst);
}
@Test
public void rename() throws Exception {
AlluxioURI oldPath = BASE_EXPECTED_URI.join("/old");
AlluxioURI newPath = BASE_EXPECTED_URI.join("/new");
when(mFileSystem.exists(oldPath)).thenReturn(true);
when(mFileSystem.exists(newPath)).thenReturn(false);
mFuseFs.rename("/old", "/new");
verify(mFileSystem).rename(oldPath, newPath);
}
@Test
public void renameOldNotExist() throws Exception {
AlluxioURI oldPath = BASE_EXPECTED_URI.join("/old");
when(mFileSystem.exists(oldPath)).thenReturn(false);
assertEquals(-ErrorCodes.ENOENT(), mFuseFs.rename("/old", "/new"));
}
@Test
public void renameNewExist() throws Exception {
AlluxioURI oldPath = BASE_EXPECTED_URI.join("/old");
AlluxioURI newPath = BASE_EXPECTED_URI.join("/new");
when(mFileSystem.exists(oldPath)).thenReturn(true);
when(mFileSystem.exists(newPath)).thenReturn(true);
mFuseFs.rename("/old", "/new");
assertEquals(-ErrorCodes.EEXIST(), mFuseFs.rename("/old", "/new"));
}
@Test
public void rmdir() throws Exception {
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
FileInfo info = new FileInfo();
info.setFolder(true);
URIStatus status = new URIStatus(info);
when(mFileSystem.getStatus(expectedPath)).thenReturn(status);
when(mFileSystem.exists(expectedPath)).thenReturn(true);
doNothing().when(mFileSystem).delete(expectedPath);
mFuseFs.rmdir("/foo/bar");
verify(mFileSystem).delete(expectedPath);
}
@Test
public void write() throws Exception {
FileOutStream fos = mock(FileOutStream.class);
AlluxioURI anyURI = any();
when(mFileSystem.createFile(anyURI)).thenReturn(fos);
// open a file
mFileInfo.flags.set(O_WRONLY.intValue());
mFuseFs.create("/foo/bar", 0, mFileInfo);
// prepare something to write into it
Runtime r = Runtime.getSystemRuntime();
Pointer ptr = r.getMemoryManager().allocateTemporary(4, true);
byte[] expected = {42, -128, 1, 3};
ptr.put(0, expected, 0, 4);
mFuseFs.write("/foo/bar", ptr, 4, 0, mFileInfo);
verify(fos).write(expected);
// the second write is no-op because the writes must be sequential and overwriting is supported
mFuseFs.write("/foo/bar", ptr, 4, 0, mFileInfo);
verify(fos, times(1)).write(expected);
}
@Test
public void unlink() throws Exception {
AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
FileInfo info = new FileInfo();
info.setFolder(false);
URIStatus status = new URIStatus(info);
when(mFileSystem.getStatus(expectedPath)).thenReturn(status);
when(mFileSystem.exists(expectedPath)).thenReturn(true);
doNothing().when(mFileSystem).delete(expectedPath);
mFuseFs.unlink("/foo/bar");
verify(mFileSystem).delete(expectedPath);
}
@Test
public void pathTranslation() throws Exception {
final LoadingCache<String, AlluxioURI> resolver = mFuseFs.getPathResolverCache();
AlluxioURI expected = new AlluxioURI(TEST_ROOT_PATH);
AlluxioURI actual = resolver.apply("/");
assertEquals("/ should resolve to " + expected, expected, actual);
expected = new AlluxioURI(TEST_ROOT_PATH + "/home/foo");
actual = resolver.apply("/home/foo");
assertEquals("/home/foo should resolve to " + expected, expected, actual);
}
// Allocate native memory for a FuseFileInfo data struct and return its pointer
private FuseFileInfo allocateNativeFileInfo() {
final Runtime runtime = Runtime.getSystemRuntime();
final Pointer pt = runtime.getMemoryManager().allocateTemporary(36, true);
return FuseFileInfo.of(pt);
}
/**
* Sets up mock for open() operation.
*
* @param uri the path to run operations on
* @return the file information
*/
private FileInfo setUpOpenMock(AlluxioURI uri) throws Exception {
FileInfo fi = new FileInfo();
fi.setCompleted(true);
fi.setFolder(false);
URIStatus status = new URIStatus(fi);
when(mFileSystem.exists(uri)).thenReturn(true);
when(mFileSystem.getStatus(uri)).thenReturn(status);
return fi;
}
}