1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.tools; |
---|
19 | |
---|
20 | import java.io.ByteArrayOutputStream; |
---|
21 | import java.io.DataOutputStream; |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.PrintStream; |
---|
24 | import java.util.Arrays; |
---|
25 | import java.util.LinkedList; |
---|
26 | import java.util.List; |
---|
27 | import java.util.Random; |
---|
28 | |
---|
29 | import org.apache.commons.logging.LogFactory; |
---|
30 | import org.apache.commons.logging.impl.Log4JLogger; |
---|
31 | import org.apache.hadoop.conf.Configuration; |
---|
32 | import org.apache.hadoop.fs.FileStatus; |
---|
33 | import org.apache.hadoop.fs.FileSystem; |
---|
34 | import org.apache.hadoop.fs.FsShell; |
---|
35 | import org.apache.hadoop.fs.Path; |
---|
36 | import org.apache.hadoop.fs.permission.FsPermission; |
---|
37 | import org.apache.hadoop.fs.permission.PermissionStatus; |
---|
38 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
39 | import org.apache.hadoop.hdfs.server.datanode.DataNode; |
---|
40 | import org.apache.hadoop.hdfs.server.namenode.FSNamesystem; |
---|
41 | import org.apache.hadoop.io.IOUtils; |
---|
42 | import org.apache.hadoop.mapred.MiniMRCluster; |
---|
43 | import org.apache.hadoop.mapred.TaskTracker; |
---|
44 | import org.apache.log4j.Level; |
---|
45 | |
---|
46 | public class TestDistCh extends junit.framework.TestCase { |
---|
47 | { |
---|
48 | ((Log4JLogger)LogFactory.getLog("org.apache.hadoop.hdfs.StateChange") |
---|
49 | ).getLogger().setLevel(Level.OFF); |
---|
50 | ((Log4JLogger)DataNode.LOG).getLogger().setLevel(Level.OFF); |
---|
51 | ((Log4JLogger)FSNamesystem.LOG).getLogger().setLevel(Level.OFF); |
---|
52 | ((Log4JLogger)TaskTracker.LOG).getLogger().setLevel(Level.OFF); |
---|
53 | } |
---|
54 | |
---|
55 | static final Long RANDOM_NUMBER_GENERATOR_SEED = null; |
---|
56 | |
---|
57 | private static final Random RANDOM = new Random(); |
---|
58 | static { |
---|
59 | final long seed = RANDOM_NUMBER_GENERATOR_SEED == null? |
---|
60 | RANDOM.nextLong(): RANDOM_NUMBER_GENERATOR_SEED; |
---|
61 | System.out.println("seed=" + seed); |
---|
62 | RANDOM.setSeed(seed); |
---|
63 | } |
---|
64 | |
---|
65 | static final String TEST_ROOT_DIR = |
---|
66 | new Path(System.getProperty("test.build.data","/tmp") |
---|
67 | ).toString().replace(' ', '+'); |
---|
68 | |
---|
69 | static final int NUN_SUBS = 5; |
---|
70 | |
---|
71 | static class FileTree { |
---|
72 | private final FileSystem fs; |
---|
73 | private final String root; |
---|
74 | private final Path rootdir; |
---|
75 | private int fcount = 0; |
---|
76 | |
---|
77 | Path createSmallFile(Path dir) throws IOException { |
---|
78 | final Path f = new Path(dir, "f" + ++fcount); |
---|
79 | assertTrue(!fs.exists(f)); |
---|
80 | final DataOutputStream out = fs.create(f); |
---|
81 | try { |
---|
82 | out.writeBytes("createSmallFile: f=" + f); |
---|
83 | } finally { |
---|
84 | out.close(); |
---|
85 | } |
---|
86 | assertTrue(fs.exists(f)); |
---|
87 | return f; |
---|
88 | } |
---|
89 | |
---|
90 | Path mkdir(Path dir) throws IOException { |
---|
91 | assertTrue(fs.mkdirs(dir)); |
---|
92 | assertTrue(fs.getFileStatus(dir).isDir()); |
---|
93 | return dir; |
---|
94 | } |
---|
95 | |
---|
96 | FileTree(FileSystem fs, String name) throws IOException { |
---|
97 | this.fs = fs; |
---|
98 | this.root = "/test/" + name; |
---|
99 | this.rootdir = mkdir(new Path(root)); |
---|
100 | |
---|
101 | for(int i = 0; i < 3; i++) { |
---|
102 | createSmallFile(rootdir); |
---|
103 | } |
---|
104 | |
---|
105 | for(int i = 0; i < NUN_SUBS; i++) { |
---|
106 | final Path sub = mkdir(new Path(root, "sub" + i)); |
---|
107 | int num_files = RANDOM.nextInt(3); |
---|
108 | for(int j = 0; j < num_files; j++) { |
---|
109 | createSmallFile(sub); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | System.out.println("rootdir = " + rootdir); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | static class ChPermissionStatus extends PermissionStatus { |
---|
118 | ChPermissionStatus(FileStatus filestatus) { |
---|
119 | this(filestatus, "", "", ""); |
---|
120 | } |
---|
121 | |
---|
122 | ChPermissionStatus(FileStatus filestatus, String owner, String group, String permission) { |
---|
123 | super("".equals(owner)? filestatus.getOwner(): owner, |
---|
124 | "".equals(group)? filestatus.getGroup(): group, |
---|
125 | "".equals(permission)? filestatus.getPermission(): new FsPermission(Short.parseShort(permission, 8))); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | public void testDistCh() throws Exception { |
---|
130 | final Configuration conf = new Configuration(); |
---|
131 | final MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
132 | final FileSystem fs = cluster.getFileSystem(); |
---|
133 | final MiniMRCluster mr = new MiniMRCluster(2, fs.getUri().toString(), 1); |
---|
134 | final FsShell shell = new FsShell(conf); |
---|
135 | |
---|
136 | try { |
---|
137 | final FileTree tree = new FileTree(fs, "testDistCh"); |
---|
138 | final FileStatus rootstatus = fs.getFileStatus(tree.rootdir); |
---|
139 | |
---|
140 | runLsr(shell, tree.root, 0); |
---|
141 | |
---|
142 | //generate random arguments |
---|
143 | final String[] args = new String[RANDOM.nextInt(NUN_SUBS-1) + 1]; |
---|
144 | final PermissionStatus[] newstatus = new PermissionStatus[NUN_SUBS]; |
---|
145 | final List<Integer> indices = new LinkedList<Integer>(); |
---|
146 | for(int i = 0; i < NUN_SUBS; i++) { |
---|
147 | indices.add(i); |
---|
148 | } |
---|
149 | for(int i = 0; i < args.length; i++) { |
---|
150 | final int index = indices.remove(RANDOM.nextInt(indices.size())); |
---|
151 | final String sub = "sub" + index; |
---|
152 | final boolean changeOwner = RANDOM.nextBoolean(); |
---|
153 | final boolean changeGroup = RANDOM.nextBoolean(); |
---|
154 | final boolean changeMode = !changeOwner && !changeGroup? true: RANDOM.nextBoolean(); |
---|
155 | |
---|
156 | final String owner = changeOwner? sub: ""; |
---|
157 | final String group = changeGroup? sub: ""; |
---|
158 | final String permission = changeMode? RANDOM.nextInt(8) + "" + RANDOM.nextInt(8) + "" + RANDOM.nextInt(8): ""; |
---|
159 | |
---|
160 | args[i] = tree.root + "/" + sub + ":" + owner + ":" + group + ":" + permission; |
---|
161 | newstatus[index] = new ChPermissionStatus(rootstatus, owner, group, permission); |
---|
162 | } |
---|
163 | for(int i = 0; i < NUN_SUBS; i++) { |
---|
164 | if (newstatus[i] == null) { |
---|
165 | newstatus[i] = new ChPermissionStatus(rootstatus); |
---|
166 | } |
---|
167 | } |
---|
168 | System.out.println("args=" + Arrays.asList(args).toString().replace(",", ",\n ")); |
---|
169 | System.out.println("newstatus=" + Arrays.asList(newstatus).toString().replace(",", ",\n ")); |
---|
170 | |
---|
171 | //run DistCh |
---|
172 | new DistCh(mr.createJobConf()).run(args); |
---|
173 | runLsr(shell, tree.root, 0); |
---|
174 | |
---|
175 | //check results |
---|
176 | for(int i = 0; i < NUN_SUBS; i++) { |
---|
177 | Path sub = new Path(tree.root + "/sub" + i); |
---|
178 | checkFileStatus(newstatus[i], fs.getFileStatus(sub)); |
---|
179 | for(FileStatus status : fs.listStatus(sub)) { |
---|
180 | checkFileStatus(newstatus[i], status); |
---|
181 | } |
---|
182 | } |
---|
183 | } finally { |
---|
184 | cluster.shutdown(); |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | static final FsPermission UMASK = FsPermission.createImmutable((short)0111); |
---|
189 | |
---|
190 | static void checkFileStatus(PermissionStatus expected, FileStatus actual) { |
---|
191 | assertEquals(expected.getUserName(), actual.getOwner()); |
---|
192 | assertEquals(expected.getGroupName(), actual.getGroup()); |
---|
193 | FsPermission perm = expected.getPermission(); |
---|
194 | if (!actual.isDir()) { |
---|
195 | perm = perm.applyUMask(UMASK); |
---|
196 | } |
---|
197 | assertEquals(perm, actual.getPermission()); |
---|
198 | } |
---|
199 | |
---|
200 | private static String runLsr(final FsShell shell, String root, int returnvalue |
---|
201 | ) throws Exception { |
---|
202 | System.out.println("root=" + root + ", returnvalue=" + returnvalue); |
---|
203 | final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
---|
204 | final PrintStream out = new PrintStream(bytes); |
---|
205 | final PrintStream oldOut = System.out; |
---|
206 | final PrintStream oldErr = System.err; |
---|
207 | System.setOut(out); |
---|
208 | System.setErr(out); |
---|
209 | final String results; |
---|
210 | try { |
---|
211 | assertEquals(returnvalue, shell.run(new String[]{"-lsr", root})); |
---|
212 | results = bytes.toString(); |
---|
213 | } finally { |
---|
214 | IOUtils.closeStream(out); |
---|
215 | System.setOut(oldOut); |
---|
216 | System.setErr(oldErr); |
---|
217 | } |
---|
218 | System.out.println("results:\n" + results); |
---|
219 | return results; |
---|
220 | } |
---|
221 | } |
---|