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.hdfs; |
---|
19 | |
---|
20 | import java.io.File; |
---|
21 | import java.io.FileNotFoundException; |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.PrintWriter; |
---|
24 | |
---|
25 | import junit.framework.TestCase; |
---|
26 | |
---|
27 | import org.apache.hadoop.conf.Configuration; |
---|
28 | import org.apache.hadoop.fs.FileSystem; |
---|
29 | import org.apache.hadoop.fs.FsShell; |
---|
30 | import org.apache.hadoop.fs.Path; |
---|
31 | import org.apache.hadoop.hdfs.server.namenode.NameNode; |
---|
32 | import org.apache.hadoop.net.NetUtils; |
---|
33 | import org.apache.hadoop.util.ToolRunner; |
---|
34 | |
---|
35 | public class TestDFSShellGenericOptions extends TestCase { |
---|
36 | |
---|
37 | public void testDFSCommand() throws IOException { |
---|
38 | String namenode = null; |
---|
39 | MiniDFSCluster cluster = null; |
---|
40 | try { |
---|
41 | Configuration conf = new Configuration(); |
---|
42 | cluster = new MiniDFSCluster(conf, 1, true, null); |
---|
43 | namenode = FileSystem.getDefaultUri(conf).toString(); |
---|
44 | String [] args = new String[4]; |
---|
45 | args[2] = "-mkdir"; |
---|
46 | args[3] = "/data"; |
---|
47 | testFsOption(args, namenode); |
---|
48 | testConfOption(args, namenode); |
---|
49 | testPropertyOption(args, namenode); |
---|
50 | } finally { |
---|
51 | if (cluster != null) { cluster.shutdown(); } |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | private void testFsOption(String [] args, String namenode) { |
---|
56 | // prepare arguments to create a directory /data |
---|
57 | args[0] = "-fs"; |
---|
58 | args[1] = namenode; |
---|
59 | execute(args, namenode); |
---|
60 | } |
---|
61 | |
---|
62 | private void testConfOption(String[] args, String namenode) { |
---|
63 | // prepare configuration hdfs-site.xml |
---|
64 | File configDir = new File(new File("build", "test"), "minidfs"); |
---|
65 | assertTrue(configDir.mkdirs()); |
---|
66 | File siteFile = new File(configDir, "hdfs-site.xml"); |
---|
67 | PrintWriter pw; |
---|
68 | try { |
---|
69 | pw = new PrintWriter(siteFile); |
---|
70 | pw.print("<?xml version=\"1.0\"?>\n"+ |
---|
71 | "<?xml-stylesheet type=\"text/xsl\" href=\"configuration.xsl\"?>\n"+ |
---|
72 | "<configuration>\n"+ |
---|
73 | " <property>\n"+ |
---|
74 | " <name>fs.default.name</name>\n"+ |
---|
75 | " <value>"+namenode+"</value>\n"+ |
---|
76 | " </property>\n"+ |
---|
77 | "</configuration>\n"); |
---|
78 | pw.close(); |
---|
79 | |
---|
80 | // prepare arguments to create a directory /data |
---|
81 | args[0] = "-conf"; |
---|
82 | args[1] = siteFile.getPath(); |
---|
83 | execute(args, namenode); |
---|
84 | } catch (FileNotFoundException e) { |
---|
85 | e.printStackTrace(); |
---|
86 | } finally { |
---|
87 | siteFile.delete(); |
---|
88 | configDir.delete(); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | private void testPropertyOption(String[] args, String namenode) { |
---|
93 | // prepare arguments to create a directory /data |
---|
94 | args[0] = "-D"; |
---|
95 | args[1] = "fs.default.name="+namenode; |
---|
96 | execute(args, namenode); |
---|
97 | } |
---|
98 | |
---|
99 | private void execute(String [] args, String namenode) { |
---|
100 | FsShell shell=new FsShell(); |
---|
101 | FileSystem fs=null; |
---|
102 | try { |
---|
103 | ToolRunner.run(shell, args); |
---|
104 | fs = new DistributedFileSystem(NameNode.getAddress(namenode), |
---|
105 | shell.getConf()); |
---|
106 | assertTrue("Directory does not get created", |
---|
107 | fs.isDirectory(new Path("/data"))); |
---|
108 | fs.delete(new Path("/data"), true); |
---|
109 | } catch (Exception e) { |
---|
110 | System.err.println(e.getMessage()); |
---|
111 | e.printStackTrace(); |
---|
112 | } finally { |
---|
113 | if (fs!=null) { |
---|
114 | try { |
---|
115 | fs.close(); |
---|
116 | } catch (IOException ignored) { |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|