1 | /* Licensed to the Apache Software Foundation (ASF) under one |
---|
2 | * or more contributor license agreements. See the NOTICE file |
---|
3 | * distributed with this work for additional information |
---|
4 | * regarding copyright ownership. The ASF licenses this file |
---|
5 | * to you under the Apache License, Version 2.0 (the |
---|
6 | * "License"); you may not use this file except in compliance |
---|
7 | * with the License. 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, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | package org.apache.hadoop.mapred; |
---|
18 | |
---|
19 | import java.io.File; |
---|
20 | import java.io.FileOutputStream; |
---|
21 | |
---|
22 | import junit.framework.TestCase; |
---|
23 | |
---|
24 | import org.apache.hadoop.conf.Configuration; |
---|
25 | import org.apache.hadoop.fs.*; |
---|
26 | import org.apache.hadoop.util.ToolRunner; |
---|
27 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
28 | |
---|
29 | /** |
---|
30 | * check for the job submission options of |
---|
31 | * -libjars -files -archives |
---|
32 | */ |
---|
33 | |
---|
34 | public class TestCommandLineJobSubmission extends TestCase { |
---|
35 | // Input output paths for this.. |
---|
36 | // these are all dummy and does not test |
---|
37 | // much in map reduce except for the command line |
---|
38 | // params |
---|
39 | static final Path input = new Path("/test/input/"); |
---|
40 | static final Path output = new Path("/test/output"); |
---|
41 | File buildDir = new File(System.getProperty("test.build.data", "/tmp")); |
---|
42 | public void testJobShell() throws Exception { |
---|
43 | MiniDFSCluster dfs = null; |
---|
44 | MiniMRCluster mr = null; |
---|
45 | FileSystem fs = null; |
---|
46 | Path testFile = new Path(input, "testfile"); |
---|
47 | try { |
---|
48 | Configuration conf = new Configuration(); |
---|
49 | //start the mini mr and dfs cluster. |
---|
50 | dfs = new MiniDFSCluster(conf, 2 , true, null); |
---|
51 | fs = dfs.getFileSystem(); |
---|
52 | FSDataOutputStream stream = fs.create(testFile); |
---|
53 | stream.write("teststring".getBytes()); |
---|
54 | stream.close(); |
---|
55 | mr = new MiniMRCluster(2, fs.getUri().toString(), 1); |
---|
56 | File thisbuildDir = new File(buildDir, "jobCommand"); |
---|
57 | assertTrue("create build dir", thisbuildDir.mkdirs()); |
---|
58 | File f = new File(thisbuildDir, "files_tmp"); |
---|
59 | FileOutputStream fstream = new FileOutputStream(f); |
---|
60 | fstream.write("somestrings".getBytes()); |
---|
61 | fstream.close(); |
---|
62 | String[] args = new String[6]; |
---|
63 | args[0] = "-files"; |
---|
64 | args[1] = f.toString(); |
---|
65 | args[2] = "-libjars"; |
---|
66 | // the testjob.jar as a temporary jar file |
---|
67 | // rather than creating its own |
---|
68 | args[3] = "build/test/testjar/testjob.jar"; |
---|
69 | args[4] = input.toString(); |
---|
70 | args[5] = output.toString(); |
---|
71 | |
---|
72 | JobConf jobConf = mr.createJobConf(); |
---|
73 | //before running the job, verify that libjar is not in client classpath |
---|
74 | assertTrue("libjar not in client classpath", loadLibJar(jobConf)==null); |
---|
75 | int ret = ToolRunner.run(jobConf, |
---|
76 | new testshell.ExternalMapReduce(), args); |
---|
77 | //after running the job, verify that libjar is in the client classpath |
---|
78 | assertTrue("libjar added to client classpath", loadLibJar(jobConf)!=null); |
---|
79 | |
---|
80 | assertTrue("not failed ", ret != -1); |
---|
81 | f.delete(); |
---|
82 | thisbuildDir.delete(); |
---|
83 | } finally { |
---|
84 | if (dfs != null) {dfs.shutdown();}; |
---|
85 | if (mr != null) {mr.shutdown();}; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | @SuppressWarnings("unchecked") |
---|
90 | private Class loadLibJar(JobConf jobConf) { |
---|
91 | try { |
---|
92 | return jobConf.getClassByName("testjar.ClassWordCount"); |
---|
93 | } catch (ClassNotFoundException e) { |
---|
94 | return null; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|