Skip to content

Commit 850a858

Browse files
committed
MainOps and FileBlockLocations tests, with new test deps
1 parent b69c66c commit 850a858

File tree

4 files changed

+208
-6
lines changed

4 files changed

+208
-6
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<artifactId>hadoop-common-test</artifactId>
2424
<version>0.22.0</version>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.apache.hadoop</groupId>
28+
<artifactId>hadoop-test</artifactId>
29+
<version>1.0.0</version>
30+
</dependency>
2631
<dependency>
2732
<groupId>org.slf4j</groupId>
2833
<artifactId>slf4j-api</artifactId>

src/test/java/org/gluster/test/GFSUtil.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public static Configuration initializeConfig(File mount, boolean automount) thro
6161
conf.set("fs.glusterfs.mount", mount.getAbsolutePath());
6262
conf.set("fs.glusterfs.server", glusterHost);
6363
conf.set("fs.default.name", "glusterfs://"+glusterHost+":9000");
64-
64+
//Not necessary - but for 1.0 might be.
65+
conf.set("fs.glusterfs.impl", "org.apache.hadoop.fs.glusterfs.GlusterFileSystem");
6566
return conf;
6667
}
6768

@@ -91,10 +92,7 @@ public static File initializeMounts(File tempDirectory) throws Exception {
9192
*/
9293
static GlusterFileSystem gfs ;
9394

94-
/**
95-
* Use this method to create a new instance of a gluster file system class.
96-
*/
97-
public static GlusterFileSystem create(boolean automount) throws Exception{
95+
public static Configuration createConfiguration(boolean automount) throws Exception{
9896
if(gfs != null){
9997
gfs.close();
10098
}
@@ -105,7 +103,14 @@ public static GlusterFileSystem create(boolean automount) throws Exception{
105103

106104
//Now set up the config object, with automount=true
107105
final Configuration conf = initializeConfig(mount,automount);
108-
106+
return conf;
107+
}
108+
/**
109+
* Use this method to create a new instance of a gluster file system class.
110+
*/
111+
public static GlusterFileSystem create(boolean automount) throws Exception{
112+
113+
Configuration conf = createConfiguration(automount);
109114
//Initialize GlusterFileSystem
110115
gfs.initialize(getTempDirectory().toURI(), conf);
111116

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.gluster.test;
2+
3+
import java.io.IOException;
4+
5+
import junit.framework.Assert;
6+
7+
import org.apache.hadoop.conf.Configuration;
8+
import org.apache.hadoop.fs.FSMainOperationsBaseTest;
9+
import org.apache.hadoop.fs.FileSystem;
10+
import org.apache.hadoop.fs.FileSystemTestHelper;
11+
import org.apache.hadoop.fs.Path;
12+
import org.apache.tools.ant.util.FileUtils;
13+
import org.junit.After;
14+
import org.junit.AfterClass;
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
18+
public class TestFSMainOperationsGlusterFileSystem extends FSMainOperationsBaseTest {
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
fSys =GFSUtil.create(true);
23+
super.setUp();
24+
}
25+
26+
static Path wd = null;
27+
protected Path getDefaultWorkingDirectory() throws IOException {
28+
if (wd == null)
29+
wd = FileSystem.getLocal(new Configuration()).getWorkingDirectory();
30+
return wd;
31+
}
32+
33+
@After
34+
public void tearDown() throws Exception {
35+
fSys.close();
36+
FileUtils.delete(GFSUtil.getTempDirectory());
37+
}
38+
39+
@Test
40+
@Override
41+
public void testWDAbsolute() throws IOException {
42+
Path absoluteDir = FileSystemTestHelper.getTestRootPath(fSys,
43+
"test/existingDir");
44+
fSys.mkdirs(absoluteDir);
45+
fSys.setWorkingDirectory(absoluteDir);
46+
Assert.assertEquals(absoluteDir, fSys.getWorkingDirectory());
47+
}
48+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with this
4+
* work for additional information regarding copyright ownership. The ASF
5+
* licenses this file to you under the Apache License, Version 2.0 (the
6+
* "License"); you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
package org.gluster.test;
19+
20+
import java.io.IOException;
21+
import java.util.Arrays;
22+
import java.util.Comparator;
23+
import java.util.Random;
24+
25+
import junit.framework.TestCase;
26+
27+
import org.apache.hadoop.conf.Configuration;
28+
import org.apache.hadoop.fs.BlockLocation;
29+
import org.apache.hadoop.fs.FSDataOutputStream;
30+
import org.apache.hadoop.fs.FileStatus;
31+
import org.apache.hadoop.fs.FileSystem;
32+
import org.apache.hadoop.fs.Path;
33+
import org.eclipse.jdt.core.dom.ThisExpression;
34+
35+
/**
36+
* Testing the correctness of FileSystem.getFileBlockLocations.
37+
*/
38+
public class TestGetFileBlockLocations extends TestCase {
39+
private static String TEST_ROOT_DIR =
40+
System.getProperty("test.build.data", "/tmp/testGetFileBlockLocations");
41+
private static final int FileLength = 4 * 1024 * 1024; // 4MB
42+
private Configuration conf;
43+
private Path path;
44+
private FileSystem fs;
45+
private Random random;
46+
47+
/**
48+
* @see TestCase#setUp()
49+
*/
50+
@Override
51+
protected void setUp() throws Exception {
52+
conf = GFSUtil.createConfiguration(true);
53+
Path rootPath = new Path(TEST_ROOT_DIR);
54+
path = new Path(rootPath, "TestGetFileBlockLocations");
55+
//Doesn't work with the
56+
System.out.println(conf.get("fs.glusterfs.name"));
57+
fs = rootPath.getFileSystem(conf);
58+
FSDataOutputStream fsdos = fs.create(path, true);
59+
byte[] buffer = new byte[1024];
60+
while (fsdos.getPos() < FileLength) {
61+
fsdos.write(buffer);
62+
}
63+
fsdos.close();
64+
random = new Random(System.nanoTime());
65+
}
66+
67+
private void oneTest(int offBegin, int offEnd, FileStatus status)
68+
throws IOException {
69+
if (offBegin > offEnd) {
70+
int tmp = offBegin;
71+
offBegin = offEnd;
72+
offEnd = tmp;
73+
}
74+
BlockLocation[] locations =
75+
fs.getFileBlockLocations(status, offBegin, offEnd - offBegin);
76+
if (offBegin < status.getLen()) {
77+
Arrays.sort(locations, new Comparator<BlockLocation>() {
78+
public int compare(BlockLocation arg0,BlockLocation arg1){
79+
long cmprv = arg0.getOffset() - arg1.getOffset();
80+
if (cmprv < 0) return -1;
81+
if (cmprv > 0) return 1;
82+
cmprv = arg0.getLength() - arg1.getLength();
83+
if (cmprv < 0) return -1;
84+
if (cmprv > 0) return 1;
85+
return 0;
86+
}
87+
});
88+
offBegin = (int) Math.min(offBegin, status.getLen() - 1);
89+
offEnd = (int) Math.min(offEnd, status.getLen());
90+
BlockLocation first = locations[0];
91+
BlockLocation last = locations[locations.length - 1];
92+
assertTrue(first.getOffset() <= offBegin);
93+
assertTrue(offEnd <= last.getOffset() + last.getLength());
94+
} else {
95+
assertTrue(locations.length == 0);
96+
}
97+
}
98+
/**
99+
* @see TestCase#tearDown()
100+
*/
101+
@Override
102+
protected void tearDown() throws IOException {
103+
fs.delete(path, true);
104+
fs.close();
105+
}
106+
107+
public void testFailureNegativeParameters() throws IOException {
108+
FileStatus status = fs.getFileStatus(path);
109+
try {
110+
BlockLocation[] locations = fs.getFileBlockLocations(status, -1, 100);
111+
fail("Expecting exception being throw");
112+
} catch (IllegalArgumentException e) {
113+
114+
}
115+
116+
try {
117+
BlockLocation[] locations = fs.getFileBlockLocations(status, 100, -1);
118+
fail("Expecting exception being throw");
119+
} catch (IllegalArgumentException e) {
120+
121+
}
122+
}
123+
124+
public void testGetFileBlockLocations1() throws IOException {
125+
FileStatus status = fs.getFileStatus(path);
126+
oneTest(0, (int) status.getLen(), status);
127+
oneTest(0, (int) status.getLen() * 2, status);
128+
oneTest((int) status.getLen() * 2, (int) status.getLen() * 4, status);
129+
oneTest((int) status.getLen() / 2, (int) status.getLen() * 3, status);
130+
for (int i = 0; i < 10; ++i) {
131+
oneTest((int) status.getLen() * i / 10, (int) status.getLen() * (i + 1)
132+
/ 10, status);
133+
}
134+
}
135+
136+
public void testGetFileBlockLocations2() throws IOException {
137+
FileStatus status = fs.getFileStatus(path);
138+
for (int i = 0; i < 1000; ++i) {
139+
int offBegin = random.nextInt((int) (2 * status.getLen()));
140+
int offEnd = random.nextInt((int) (2 * status.getLen()));
141+
oneTest(offBegin, offEnd, status);
142+
}
143+
}
144+
}

0 commit comments

Comments
 (0)