1 | /** |
---|
2 | * |
---|
3 | * Licensed under the Apache License, Version 2.0 |
---|
4 | * (the "License"); you may not use this file except in compliance with |
---|
5 | * the License. You may obtain a copy of the License at |
---|
6 | * |
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | * |
---|
9 | * Unless required by applicable law or agreed to in writing, software |
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
---|
12 | * implied. See the License for the specific language governing |
---|
13 | * permissions and limitations under the License. |
---|
14 | * |
---|
15 | * @author: Sriram Rao (Kosmix Corp.) |
---|
16 | * |
---|
17 | * Unit tests for testing the KosmosFileSystem API implementation. |
---|
18 | */ |
---|
19 | |
---|
20 | package org.apache.hadoop.fs.kfs; |
---|
21 | |
---|
22 | import java.io.*; |
---|
23 | import java.net.*; |
---|
24 | |
---|
25 | import junit.framework.TestCase; |
---|
26 | |
---|
27 | import org.apache.hadoop.conf.Configuration; |
---|
28 | import org.apache.hadoop.fs.FSDataInputStream; |
---|
29 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
30 | import org.apache.hadoop.fs.FileSystem; |
---|
31 | import org.apache.hadoop.fs.FileStatus; |
---|
32 | import org.apache.hadoop.fs.FileUtil; |
---|
33 | import org.apache.hadoop.fs.Path; |
---|
34 | |
---|
35 | import org.apache.hadoop.fs.kfs.KosmosFileSystem; |
---|
36 | |
---|
37 | public class TestKosmosFileSystem extends TestCase { |
---|
38 | |
---|
39 | KosmosFileSystem kosmosFileSystem; |
---|
40 | KFSEmulationImpl kfsEmul; |
---|
41 | Path baseDir; |
---|
42 | |
---|
43 | @Override |
---|
44 | protected void setUp() throws IOException { |
---|
45 | Configuration conf = new Configuration(); |
---|
46 | |
---|
47 | kfsEmul = new KFSEmulationImpl(conf); |
---|
48 | kosmosFileSystem = new KosmosFileSystem(kfsEmul); |
---|
49 | // a dummy URI; we are not connecting to any setup here |
---|
50 | kosmosFileSystem.initialize(URI.create("kfs:///"), conf); |
---|
51 | baseDir = new Path(System.getProperty("test.build.data", "/tmp" ) + |
---|
52 | "/kfs-test"); |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | protected void tearDown() throws Exception { |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | // @Test |
---|
61 | // Check all the directory API's in KFS |
---|
62 | public void testDirs() throws Exception { |
---|
63 | Path subDir1 = new Path("dir.1"); |
---|
64 | |
---|
65 | // make the dir |
---|
66 | kosmosFileSystem.mkdirs(baseDir); |
---|
67 | assertTrue(kosmosFileSystem.isDirectory(baseDir)); |
---|
68 | kosmosFileSystem.setWorkingDirectory(baseDir); |
---|
69 | |
---|
70 | kosmosFileSystem.mkdirs(subDir1); |
---|
71 | assertTrue(kosmosFileSystem.isDirectory(subDir1)); |
---|
72 | |
---|
73 | assertFalse(kosmosFileSystem.exists(new Path("test1"))); |
---|
74 | assertFalse(kosmosFileSystem.isDirectory(new Path("test/dir.2"))); |
---|
75 | |
---|
76 | FileStatus[] p = kosmosFileSystem.listStatus(baseDir); |
---|
77 | assertEquals(p.length, 1); |
---|
78 | |
---|
79 | kosmosFileSystem.delete(baseDir, true); |
---|
80 | assertFalse(kosmosFileSystem.exists(baseDir)); |
---|
81 | } |
---|
82 | |
---|
83 | // @Test |
---|
84 | // Check the file API's |
---|
85 | public void testFiles() throws Exception { |
---|
86 | Path subDir1 = new Path("dir.1"); |
---|
87 | Path file1 = new Path("dir.1/foo.1"); |
---|
88 | Path file2 = new Path("dir.1/foo.2"); |
---|
89 | |
---|
90 | kosmosFileSystem.mkdirs(baseDir); |
---|
91 | assertTrue(kosmosFileSystem.isDirectory(baseDir)); |
---|
92 | kosmosFileSystem.setWorkingDirectory(baseDir); |
---|
93 | |
---|
94 | kosmosFileSystem.mkdirs(subDir1); |
---|
95 | |
---|
96 | FSDataOutputStream s1 = kosmosFileSystem.create(file1, true, 4096, (short) 1, (long) 4096, null); |
---|
97 | FSDataOutputStream s2 = kosmosFileSystem.create(file2, true, 4096, (short) 1, (long) 4096, null); |
---|
98 | |
---|
99 | s1.close(); |
---|
100 | s2.close(); |
---|
101 | |
---|
102 | FileStatus[] p = kosmosFileSystem.listStatus(subDir1); |
---|
103 | assertEquals(p.length, 2); |
---|
104 | |
---|
105 | kosmosFileSystem.delete(file1, true); |
---|
106 | p = kosmosFileSystem.listStatus(subDir1); |
---|
107 | assertEquals(p.length, 1); |
---|
108 | |
---|
109 | kosmosFileSystem.delete(file2, true); |
---|
110 | p = kosmosFileSystem.listStatus(subDir1); |
---|
111 | assertEquals(p.length, 0); |
---|
112 | |
---|
113 | kosmosFileSystem.delete(baseDir, true); |
---|
114 | assertFalse(kosmosFileSystem.exists(baseDir)); |
---|
115 | } |
---|
116 | |
---|
117 | // @Test |
---|
118 | // Check file/read write |
---|
119 | public void testFileIO() throws Exception { |
---|
120 | Path subDir1 = new Path("dir.1"); |
---|
121 | Path file1 = new Path("dir.1/foo.1"); |
---|
122 | |
---|
123 | kosmosFileSystem.mkdirs(baseDir); |
---|
124 | assertTrue(kosmosFileSystem.isDirectory(baseDir)); |
---|
125 | kosmosFileSystem.setWorkingDirectory(baseDir); |
---|
126 | |
---|
127 | kosmosFileSystem.mkdirs(subDir1); |
---|
128 | |
---|
129 | FSDataOutputStream s1 = kosmosFileSystem.create(file1, true, 4096, (short) 1, (long) 4096, null); |
---|
130 | |
---|
131 | int bufsz = 4096; |
---|
132 | byte[] data = new byte[bufsz]; |
---|
133 | |
---|
134 | for (int i = 0; i < data.length; i++) |
---|
135 | data[i] = (byte) (i % 16); |
---|
136 | |
---|
137 | // write 4 bytes and read them back; read API should return a byte per call |
---|
138 | s1.write(32); |
---|
139 | s1.write(32); |
---|
140 | s1.write(32); |
---|
141 | s1.write(32); |
---|
142 | // write some data |
---|
143 | s1.write(data, 0, data.length); |
---|
144 | // flush out the changes |
---|
145 | s1.close(); |
---|
146 | |
---|
147 | // Read the stuff back and verify it is correct |
---|
148 | FSDataInputStream s2 = kosmosFileSystem.open(file1, 4096); |
---|
149 | int v; |
---|
150 | |
---|
151 | v = s2.read(); |
---|
152 | assertEquals(v, 32); |
---|
153 | v = s2.read(); |
---|
154 | assertEquals(v, 32); |
---|
155 | v = s2.read(); |
---|
156 | assertEquals(v, 32); |
---|
157 | v = s2.read(); |
---|
158 | assertEquals(v, 32); |
---|
159 | |
---|
160 | assertEquals(s2.available(), data.length); |
---|
161 | |
---|
162 | byte[] buf = new byte[bufsz]; |
---|
163 | s2.read(buf, 0, buf.length); |
---|
164 | for (int i = 0; i < data.length; i++) |
---|
165 | assertEquals(data[i], buf[i]); |
---|
166 | |
---|
167 | assertEquals(s2.available(), 0); |
---|
168 | |
---|
169 | s2.close(); |
---|
170 | |
---|
171 | kosmosFileSystem.delete(file1, true); |
---|
172 | assertFalse(kosmosFileSystem.exists(file1)); |
---|
173 | kosmosFileSystem.delete(subDir1, true); |
---|
174 | assertFalse(kosmosFileSystem.exists(subDir1)); |
---|
175 | kosmosFileSystem.delete(baseDir, true); |
---|
176 | assertFalse(kosmosFileSystem.exists(baseDir)); |
---|
177 | } |
---|
178 | |
---|
179 | } |
---|