Skip to content

Commit

Permalink
HDFS-13558. TestDatanodeHttpXFrame does not shut down cluster. Contri…
Browse files Browse the repository at this point in the history
…buted by Anbang Hu.
  • Loading branch information
Inigo Goiri committed May 17, 2018
1 parent 328f084 commit 26f1e22
Showing 1 changed file with 21 additions and 9 deletions.
Expand Up @@ -23,6 +23,7 @@
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.hdfs.server.datanode.DataNode;
import org.apache.hadoop.http.HttpServer2;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -36,13 +37,24 @@
* Test that X-Frame-Options works correctly with DatanodeHTTPServer.
*/
public class TestDatanodeHttpXFrame {

private MiniDFSCluster cluster = null;

@Rule
public ExpectedException exception = ExpectedException.none();

@After
public void cleanUp() {
if (cluster != null) {
cluster.shutdown();
cluster = null;
}
}

@Test
public void testDataNodeXFrameOptionsEnabled() throws Exception {
boolean xFrameEnabled = true;
MiniDFSCluster cluster = createCluster(xFrameEnabled, null);
cluster = createCluster(xFrameEnabled, null);
HttpURLConnection conn = getConn(cluster);
String xfoHeader = conn.getHeaderField("X-FRAME-OPTIONS");
Assert.assertTrue("X-FRAME-OPTIONS is absent in the header",
Expand All @@ -54,7 +66,7 @@ public void testDataNodeXFrameOptionsEnabled() throws Exception {
@Test
public void testNameNodeXFrameOptionsDisabled() throws Exception {
boolean xFrameEnabled = false;
MiniDFSCluster cluster = createCluster(xFrameEnabled, null);
cluster = createCluster(xFrameEnabled, null);
HttpURLConnection conn = getConn(cluster);
String xfoHeader = conn.getHeaderField("X-FRAME-OPTIONS");
Assert.assertTrue("unexpected X-FRAME-OPTION in header", xfoHeader == null);
Expand All @@ -63,25 +75,25 @@ public void testNameNodeXFrameOptionsDisabled() throws Exception {
@Test
public void testDataNodeXFramewithInvalidOptions() throws Exception {
exception.expect(IllegalArgumentException.class);
createCluster(false, "Hadoop");
cluster = createCluster(false, "Hadoop");
}

private MiniDFSCluster createCluster(boolean enabled, String
private static MiniDFSCluster createCluster(boolean enabled, String
value) throws IOException {
Configuration conf = new HdfsConfiguration();
conf.setBoolean(DFSConfigKeys.DFS_XFRAME_OPTION_ENABLED, enabled);
if (value != null) {
conf.set(DFSConfigKeys.DFS_XFRAME_OPTION_VALUE, value);
}
MiniDFSCluster cluster =
MiniDFSCluster dfsCluster =
new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
cluster.waitActive();
return cluster;
dfsCluster.waitActive();
return dfsCluster;
}

private HttpURLConnection getConn(MiniDFSCluster cluster)
private static HttpURLConnection getConn(MiniDFSCluster dfsCluster)
throws IOException {
DataNode datanode = cluster.getDataNodes().get(0);
DataNode datanode = dfsCluster.getDataNodes().get(0);
URL newURL = new URL("http://localhost:" + datanode.getInfoPort());
HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
conn.connect();
Expand Down

0 comments on commit 26f1e22

Please sign in to comment.