Skip to content

Commit 4bb2517

Browse files
committed
new test factory for GlusterFileSystem creation, also variable autmount support - setup() currently uses autmount=true
1 parent fe1dfdf commit 4bb2517

File tree

2 files changed

+134
-84
lines changed

2 files changed

+134
-84
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package org.gluster.test;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.InetAddress;
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
8+
9+
import org.apache.hadoop.conf.Configuration;
10+
import org.apache.hadoop.fs.glusterfs.GlusterFileSystem;
11+
import org.junit.BeforeClass;
12+
13+
/**
14+
* A file system specifically for testing the GlusterFileSystem class.
15+
* Provides a getter for a singleton instance of a {@link GlusterFileSystem}.
16+
*/
17+
public class GFSUtil {
18+
19+
public static Configuration initializeConfig(File mount, boolean automount) throws Exception{
20+
String glusterVolume=System.getProperty("gluster-volume");
21+
String glusterHost=System.getProperty("gluster-host");
22+
final Configuration conf=new Configuration();
23+
/*
24+
* the user can over ride the default gluster volume used for test with
25+
* ENV var
26+
*/
27+
glusterVolume=System.getProperty("GLUSTER_VOLUME");
28+
if(glusterVolume==null||glusterVolume.equals("")){
29+
System.out.println("WARNING: HOST NOT DEFINED IN ENVIRONMENT! See README");
30+
glusterVolume="HadoopVol";
31+
}
32+
33+
glusterHost=System.getProperty("GLUSTER_HOST");
34+
if(glusterHost==null||glusterHost.equals("")){
35+
System.out.println("WARNING: HOST NOT DEFINED IN ENVIRONMENT! See README");
36+
InetAddress addr=InetAddress.getLocalHost();
37+
glusterHost=addr.getHostName();
38+
}
39+
40+
System.out.println("Testing against host="+glusterHost);
41+
System.out.println("Testing against volume="+glusterVolume);
42+
43+
if(glusterVolume==null){
44+
glusterVolume="hadoop-gluster";
45+
}
46+
47+
/* retrieve the local machines hostname */
48+
if(glusterHost==null||"".compareTo(glusterHost)==0){
49+
InetAddress addr=null;
50+
51+
addr=InetAddress.getLocalHost();
52+
53+
glusterHost=addr.getHostName();
54+
}
55+
56+
System.out.println("Confirmed that configuration properties from gluster were found , now creating dirs");
57+
//System.out.println("Now initializing GlusterFS ! - We will mount to "+mount.getAbsolutePath());
58+
//setup the configuration object.
59+
conf.set("fs.glusterfs.automount", automount+"");
60+
conf.set("fs.glusterfs.volname", glusterVolume);
61+
conf.set("fs.glusterfs.mount", mount.getAbsolutePath());
62+
conf.set("fs.glusterfs.server", glusterHost);
63+
conf.set("fs.default.name", "glusterfs://"+glusterHost+":9000");
64+
65+
return conf;
66+
}
67+
68+
static boolean tempCreated = false;
69+
70+
public static File getTempDirectory(){
71+
final File tempDirectory=new File(System.getProperty("java.io.tmpdir"), "gluster-test-mount-point");
72+
73+
//initialize?
74+
if(!tempCreated){
75+
tempDirectory.mkdirs();
76+
tempDirectory.delete();
77+
tempDirectory.mkdir();
78+
}
79+
tempCreated=true;
80+
return tempDirectory;
81+
}
82+
83+
public static File initializeMounts(File tempDirectory) throws Exception {
84+
File mount=new File(tempDirectory, "mount");
85+
mount.mkdir();
86+
return mount;
87+
}
88+
89+
/**
90+
* Singleton - only expose one GFS at a time.
91+
*/
92+
static GlusterFileSystem gfs ;
93+
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{
98+
if(gfs != null){
99+
gfs.close();
100+
}
101+
102+
gfs = new GlusterFileSystem();
103+
//Setup the temp directory.
104+
final File mount = initializeMounts(getTempDirectory());
105+
106+
//Now set up the config object, with automount=true
107+
final Configuration conf = initializeConfig(mount,automount);
108+
109+
//Initialize GlusterFileSystem
110+
gfs.initialize(getTempDirectory().toURI(), conf);
111+
112+
System.out.println("server "+conf.get("fs.glusterfs.server"));
113+
114+
//FYI, we fs.default.name is something like "glusterfs://127.0.0.1:9000")
115+
gfs.initialize(new URI(conf.get("fs.default.name")), conf);
116+
117+
return gfs;
118+
}
119+
120+
}

src/test/java/org/gluster/test/TestGluster.java

Lines changed: 14 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@
2929
import static org.junit.Assert.assertFalse;
3030
import static org.junit.Assert.assertTrue;
3131

32-
import java.io.File;
3332
import java.io.IOException;
34-
import java.net.InetAddress;
3533

36-
import org.apache.hadoop.conf.Configuration;
3734
import org.apache.hadoop.fs.FSDataInputStream;
3835
import org.apache.hadoop.fs.FSDataOutputStream;
3936
import org.apache.hadoop.fs.FileStatus;
@@ -52,86 +49,21 @@
5249
*
5350
*/
5451
public class TestGluster{
55-
56-
protected static File tempDirectory;
57-
protected static String glusterVolume=System.getProperty("gluster-volume");
58-
protected static String glusterHost=System.getProperty("gluster-host");
59-
protected static GlusterFileSystem gfs;
60-
private static File temp;
61-
private static File mount;
62-
63-
@AfterClass
64-
public static void after() throws IOException{
65-
gfs.close();
66-
FileUtils.delete(tempDirectory);
67-
}
68-
52+
53+
static GlusterFileSystem gfs ;
54+
6955
@BeforeClass
70-
public static void before() throws Exception{
71-
/*
72-
* the user can over ride the default gluster volume used for test with
73-
* ENV var
74-
*/
75-
glusterVolume=System.getProperty("GLUSTER_VOLUME");
76-
if(glusterVolume==null||glusterVolume.equals("")){
77-
System.out.println("WARNING: HOST NOT DEFINED IN ENVIRONMENT! See README");
78-
glusterVolume="HadoopVol";
79-
}
80-
81-
glusterHost=System.getProperty("GLUSTER_HOST");
82-
if(glusterHost==null||glusterHost.equals("")){
83-
System.out.println("WARNING: HOST NOT DEFINED IN ENVIRONMENT! See README");
84-
InetAddress addr=InetAddress.getLocalHost();
85-
glusterHost=addr.getHostName();
86-
}
87-
88-
System.out.println("Testing against host="+glusterHost);
89-
System.out.println("Testing against volume="+glusterVolume);
90-
56+
public static void setup() throws Exception {
9157
/**
92-
* Create a temporary directory for the mount point.
58+
* Automount = true.
9359
*/
94-
tempDirectory=new File(System.getProperty("java.io.tmpdir"), "gluster-test-mount-point");
95-
tempDirectory.mkdirs();
96-
tempDirectory.delete();
97-
tempDirectory.mkdir();
98-
99-
if(glusterVolume==null){
100-
glusterVolume="hadoop-gluster";
101-
}
102-
103-
gfs=new GlusterFileSystem();
104-
105-
/* retrieve the local machines hostname */
106-
if(glusterHost==null||"".compareTo(glusterHost)==0){
107-
InetAddress addr=null;
108-
109-
addr=InetAddress.getLocalHost();
110-
111-
glusterHost=addr.getHostName();
112-
}
113-
114-
System.out.println("Confirmed that configuration properties from gluster were found , now creating dirs");
115-
116-
gfs=new GlusterFileSystem();
117-
temp=new File(tempDirectory, "hadoop-temp");
118-
mount=new File(tempDirectory, "mount");
119-
120-
/**
121-
* We mount to "mount" which is /tmp
122-
*/
123-
temp.mkdir();
124-
mount.mkdir();
125-
126-
System.out.println("Now initializing GlusterFS ! - We will mount to "+mount.getAbsolutePath());
127-
128-
Configuration conf=new Configuration();
129-
conf.set("fs.glusterfs.volname", glusterVolume);
130-
conf.set("fs.glusterfs.mount", mount.getAbsolutePath());
131-
conf.set("fs.glusterfs.server", glusterHost);
132-
conf.set("fs.default.name", "glusterfs://"+glusterHost+":9000");
133-
System.out.println("server "+conf.get("fs.glusterfs.server"));
134-
gfs.initialize(temp.toURI(), conf);
60+
gfs= GFSUtil.create(true);
61+
}
62+
63+
@AfterClass
64+
public static void after() throws IOException{
65+
gfs.close();
66+
FileUtils.delete(GFSUtil.getTempDirectory());
13567
}
13668

13769
@org.junit.Test
@@ -409,8 +341,7 @@ public void testFileIO() throws Exception{
409341

410342
// BZ908899
411343
@Test
412-
public void test0aPermissions() throws Exception{
413-
System.out.println("working dir : "+gfs.getWorkingDirectory());
344+
public void testPermissionsChanging() throws Exception{
414345
Path theFile=new Path("/mnt/glusterfs/changePerms/a");
415346

416347
gfs.create(theFile);
@@ -436,7 +367,6 @@ public void test0aPermissions() throws Exception{
436367

437368
}
438369

439-
440370
@Test
441371
public void testCreateNR() throws Exception{
442372
Path nonrec = new Path("nonrec");
@@ -447,4 +377,4 @@ public void testCreateNR() throws Exception{
447377
Assert.assertTrue(this.gfs.exists(new Path("nonrec")));
448378
this.gfs.delete(nonrec);
449379
}
450-
}
380+
}

0 commit comments

Comments
 (0)