HDFS-13558. TestDatanodeHttpXFrame does not shut down cluster. Contributed by Anbang Hu.
(cherry picked from commit 26f1e22fc9
)
This commit is contained in:
parent
0460201c3a
commit
e5ff9f3922
|
@ -23,6 +23,7 @@ import org.apache.hadoop.hdfs.HdfsConfiguration;
|
||||||
import org.apache.hadoop.hdfs.MiniDFSCluster;
|
import org.apache.hadoop.hdfs.MiniDFSCluster;
|
||||||
import org.apache.hadoop.hdfs.server.datanode.DataNode;
|
import org.apache.hadoop.hdfs.server.datanode.DataNode;
|
||||||
import org.apache.hadoop.http.HttpServer2;
|
import org.apache.hadoop.http.HttpServer2;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -36,13 +37,24 @@ import java.net.URL;
|
||||||
* Test that X-Frame-Options works correctly with DatanodeHTTPServer.
|
* Test that X-Frame-Options works correctly with DatanodeHTTPServer.
|
||||||
*/
|
*/
|
||||||
public class TestDatanodeHttpXFrame {
|
public class TestDatanodeHttpXFrame {
|
||||||
|
|
||||||
|
private MiniDFSCluster cluster = null;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException exception = ExpectedException.none();
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void cleanUp() {
|
||||||
|
if (cluster != null) {
|
||||||
|
cluster.shutdown();
|
||||||
|
cluster = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDataNodeXFrameOptionsEnabled() throws Exception {
|
public void testDataNodeXFrameOptionsEnabled() throws Exception {
|
||||||
boolean xFrameEnabled = true;
|
boolean xFrameEnabled = true;
|
||||||
MiniDFSCluster cluster = createCluster(xFrameEnabled, null);
|
cluster = createCluster(xFrameEnabled, null);
|
||||||
HttpURLConnection conn = getConn(cluster);
|
HttpURLConnection conn = getConn(cluster);
|
||||||
String xfoHeader = conn.getHeaderField("X-FRAME-OPTIONS");
|
String xfoHeader = conn.getHeaderField("X-FRAME-OPTIONS");
|
||||||
Assert.assertTrue("X-FRAME-OPTIONS is absent in the header",
|
Assert.assertTrue("X-FRAME-OPTIONS is absent in the header",
|
||||||
|
@ -54,7 +66,7 @@ public class TestDatanodeHttpXFrame {
|
||||||
@Test
|
@Test
|
||||||
public void testNameNodeXFrameOptionsDisabled() throws Exception {
|
public void testNameNodeXFrameOptionsDisabled() throws Exception {
|
||||||
boolean xFrameEnabled = false;
|
boolean xFrameEnabled = false;
|
||||||
MiniDFSCluster cluster = createCluster(xFrameEnabled, null);
|
cluster = createCluster(xFrameEnabled, null);
|
||||||
HttpURLConnection conn = getConn(cluster);
|
HttpURLConnection conn = getConn(cluster);
|
||||||
String xfoHeader = conn.getHeaderField("X-FRAME-OPTIONS");
|
String xfoHeader = conn.getHeaderField("X-FRAME-OPTIONS");
|
||||||
Assert.assertTrue("unexpected X-FRAME-OPTION in header", xfoHeader == null);
|
Assert.assertTrue("unexpected X-FRAME-OPTION in header", xfoHeader == null);
|
||||||
|
@ -63,25 +75,25 @@ public class TestDatanodeHttpXFrame {
|
||||||
@Test
|
@Test
|
||||||
public void testDataNodeXFramewithInvalidOptions() throws Exception {
|
public void testDataNodeXFramewithInvalidOptions() throws Exception {
|
||||||
exception.expect(IllegalArgumentException.class);
|
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 {
|
value) throws IOException {
|
||||||
Configuration conf = new HdfsConfiguration();
|
Configuration conf = new HdfsConfiguration();
|
||||||
conf.setBoolean(DFSConfigKeys.DFS_XFRAME_OPTION_ENABLED, enabled);
|
conf.setBoolean(DFSConfigKeys.DFS_XFRAME_OPTION_ENABLED, enabled);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
conf.set(DFSConfigKeys.DFS_XFRAME_OPTION_VALUE, value);
|
conf.set(DFSConfigKeys.DFS_XFRAME_OPTION_VALUE, value);
|
||||||
}
|
}
|
||||||
MiniDFSCluster cluster =
|
MiniDFSCluster dfsCluster =
|
||||||
new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
|
new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
|
||||||
cluster.waitActive();
|
dfsCluster.waitActive();
|
||||||
return cluster;
|
return dfsCluster;
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpURLConnection getConn(MiniDFSCluster cluster)
|
private static HttpURLConnection getConn(MiniDFSCluster dfsCluster)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
DataNode datanode = cluster.getDataNodes().get(0);
|
DataNode datanode = dfsCluster.getDataNodes().get(0);
|
||||||
URL newURL = new URL("http://localhost:" + datanode.getInfoPort());
|
URL newURL = new URL("http://localhost:" + datanode.getInfoPort());
|
||||||
HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) newURL.openConnection();
|
||||||
conn.connect();
|
conn.connect();
|
||||||
|
|
Loading…
Reference in New Issue