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.nfs.nfs3;
19 
20 import static org.junit.Assert.assertTrue;
21 
22 import java.io.File;
23 import java.net.URL;
24 
25 import org.apache.hadoop.fs.FileUtil;
26 import org.apache.hadoop.hdfs.DFSConfigKeys;
27 import org.apache.hadoop.hdfs.DFSTestUtil;
28 import org.apache.hadoop.hdfs.MiniDFSCluster;
29 import org.apache.hadoop.hdfs.nfs.conf.NfsConfigKeys;
30 import org.apache.hadoop.hdfs.nfs.conf.NfsConfiguration;
31 import org.apache.hadoop.http.HttpConfig;
32 import org.apache.hadoop.security.ssl.KeyStoreTestUtil;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 
37 public class TestNfs3HttpServer {
38   private static final String BASEDIR = System.getProperty("test.build.dir",
39       "target/test-dir") + "/" + TestNfs3HttpServer.class.getSimpleName();
40   private static NfsConfiguration conf = new NfsConfiguration();
41   private static MiniDFSCluster cluster;
42   private static String keystoresDir;
43   private static String sslConfDir;
44 
45   @BeforeClass
setUp()46   public static void setUp() throws Exception {
47     conf.set(DFSConfigKeys.DFS_HTTP_POLICY_KEY,
48         HttpConfig.Policy.HTTP_AND_HTTPS.name());
49     conf.set(NfsConfigKeys.NFS_HTTP_ADDRESS_KEY, "localhost:0");
50     conf.set(NfsConfigKeys.NFS_HTTPS_ADDRESS_KEY, "localhost:0");
51     // Use emphral port in case tests are running in parallel
52     conf.setInt(NfsConfigKeys.DFS_NFS_SERVER_PORT_KEY, 0);
53     conf.setInt(NfsConfigKeys.DFS_NFS_MOUNTD_PORT_KEY, 0);
54 
55     File base = new File(BASEDIR);
56     FileUtil.fullyDelete(base);
57     base.mkdirs();
58     keystoresDir = new File(BASEDIR).getAbsolutePath();
59     sslConfDir = KeyStoreTestUtil.getClasspathDir(TestNfs3HttpServer.class);
60     KeyStoreTestUtil.setupSSLConfig(keystoresDir, sslConfDir, conf, false);
61 
62     cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
63     cluster.waitActive();
64   }
65 
66   @AfterClass
tearDown()67   public static void tearDown() throws Exception {
68     FileUtil.fullyDelete(new File(BASEDIR));
69     if (cluster != null) {
70       cluster.shutdown();
71     }
72     KeyStoreTestUtil.cleanupSSLConfig(keystoresDir, sslConfDir);
73   }
74 
75   @Test
testHttpServer()76   public void testHttpServer() throws Exception {
77     Nfs3 nfs = new Nfs3(conf);
78     nfs.startServiceInternal(false);
79     RpcProgramNfs3 nfsd = (RpcProgramNfs3) nfs.getRpcProgram();
80     Nfs3HttpServer infoServer = nfsd.getInfoServer();
81 
82     String urlRoot = infoServer.getServerURI().toString();
83 
84     // Check default servlets.
85     String pageContents = DFSTestUtil.urlGet(new URL(urlRoot + "/jmx"));
86     assertTrue("Bad contents: " + pageContents,
87         pageContents.contains("java.lang:type="));
88     System.out.println("pc:" + pageContents);
89 
90     int port = infoServer.getSecurePort();
91     assertTrue("Can't get https port", port > 0);
92   }
93 }
94