From d39039d54df7934297f39c11215f2954b8803200 Mon Sep 17 00:00:00 2001 From: Xuan Date: Tue, 19 May 2015 09:56:01 -0700 Subject: [PATCH] YARN-3601. Fix UT TestRMFailover.testRMWebAppRedirect. Contributed by Weiwei Yang (cherry picked from commit 5009ad4a7f712fc578b461ecec53f7f97eaaed0c) --- hadoop-yarn-project/CHANGES.txt | 2 + .../hadoop/yarn/client/TestRMFailover.java | 78 +++++++++---------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/hadoop-yarn-project/CHANGES.txt b/hadoop-yarn-project/CHANGES.txt index 7ac38cea219..eb5c18342b2 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -467,6 +467,8 @@ Release 2.7.1 - UNRELEASED YARN-3526. ApplicationMaster tracking URL is incorrectly redirected on a QJM cluster. (Weiwei Yang via xgong) + YARN-3601. Fix UT TestRMFailover.testRMWebAppRedirect. (Weiwei Yang via xgong) + Release 2.7.0 - 2015-04-20 INCOMPATIBLE CHANGES diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMFailover.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMFailover.java index d4fc5c19199..cd227431a1c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMFailover.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/TestRMFailover.java @@ -21,13 +21,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; -import java.util.List; -import java.util.Map; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -48,7 +48,6 @@ import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; public class TestRMFailover extends ClientBaseWithFixes { @@ -275,10 +274,6 @@ private void verifyResponse(HttpURLConnection response) assertEquals(404, response.getResponseCode()); } - // ignore this testcase, Always gets "too many redirect loops" exception - // Probably because of the limitation of MiniYARNCluster. - // Verified the behavior in a single node cluster. - @Ignore @Test public void testRMWebAppRedirect() throws YarnException, InterruptedException, IOException { @@ -290,59 +285,62 @@ public void testRMWebAppRedirect() throws YarnException, getAdminService(0).transitionToActive(req); String rm1Url = "http://0.0.0.0:18088"; String rm2Url = "http://0.0.0.0:28088"; - String header = getHeader("Refresh", rm2Url); - assertTrue(header.contains("; url=" + rm1Url)); + String redirectURL = getRedirectURL(rm2Url); + // if uri is null, RMWebAppFilter will append a slash at the trail of the redirection url + assertEquals(redirectURL,rm1Url+"/"); - header = getHeader("Refresh", rm2Url + "/metrics"); - assertTrue(header.contains("; url=" + rm1Url)); + redirectURL = getRedirectURL(rm2Url + "/metrics"); + assertEquals(redirectURL,rm1Url + "/metrics"); - header = getHeader("Refresh", rm2Url + "/jmx"); - assertTrue(header.contains("; url=" + rm1Url)); + redirectURL = getRedirectURL(rm2Url + "/jmx"); + assertEquals(redirectURL,rm1Url + "/jmx"); // standby RM links /conf, /stacks, /logLevel, /static, /logs, // /cluster/cluster as well as webService // /ws/v1/cluster/info should not be redirected to active RM - header = getHeader("Refresh", rm2Url + "/cluster/cluster"); - assertEquals(null, header); + redirectURL = getRedirectURL(rm2Url + "/cluster/cluster"); + assertNull(redirectURL); - header = getHeader("Refresh", rm2Url + "/conf"); - assertEquals(null, header); + redirectURL = getRedirectURL(rm2Url + "/conf"); + assertNull(redirectURL); - header = getHeader("Refresh", rm2Url + "/stacks"); - assertEquals(null, header); + redirectURL = getRedirectURL(rm2Url + "/stacks"); + assertNull(redirectURL); - header = getHeader("Refresh", rm2Url + "/logLevel"); - assertEquals(null, header); + redirectURL = getRedirectURL(rm2Url + "/logLevel"); + assertNull(redirectURL); - header = getHeader("Refresh", rm2Url + "/static"); - assertEquals(null, header); + redirectURL = getRedirectURL(rm2Url + "/static"); + assertNull(redirectURL); - header = getHeader("Refresh", rm2Url + "/logs"); - assertEquals(null, header); + redirectURL = getRedirectURL(rm2Url + "/logs"); + assertNull(redirectURL); - header = getHeader("Refresh", rm2Url + "/ws/v1/cluster/info"); - assertEquals(null, header); + redirectURL = getRedirectURL(rm2Url + "/ws/v1/cluster/info"); + assertNull(redirectURL); - header = getHeader("Refresh", rm2Url + "/ws/v1/cluster/apps"); - assertTrue(header.contains("; url=" + rm1Url)); + redirectURL = getRedirectURL(rm2Url + "/ws/v1/cluster/apps"); + assertEquals(redirectURL, rm1Url + "/ws/v1/cluster/apps"); - header = getHeader("Refresh", rm2Url + "/proxy/" + fakeAppId); - assertEquals(null, header); - - // Due to the limitation of MiniYARNCluster and dispatcher is a singleton, - // we couldn't add the test case after explicitFailover(); + redirectURL = getRedirectURL(rm2Url + "/proxy/" + fakeAppId); + assertNull(redirectURL); } - static String getHeader(String field, String url) { - String fieldHeader = null; + // set up http connection with the given url and get the redirection url from the response + // return null if the url is not redirected + static String getRedirectURL(String url) { + String redirectUrl = null; try { - Map> map = - new URL(url).openConnection().getHeaderFields(); - fieldHeader = map.get(field).get(0); + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + // do not automatically follow the redirection + // otherwise we get too many redirections exception + conn.setInstanceFollowRedirects(false); + if(conn.getResponseCode() == HttpServletResponse.SC_TEMPORARY_REDIRECT) + redirectUrl = conn.getHeaderField("Location"); } catch (Exception e) { // throw new RuntimeException(e); } - return fieldHeader; + return redirectUrl; } }