Services { public String getRMUrl() { String url; if (context.getCluster() != null) { url = getRMUrlFromClusterConfig(); } else { url = getRmUrlFromCustomConfig(); } return removeTrailingSlash(url); } Services(AmbariApi ambariApi, ViewContext context); String getRMUrl(); String getWebHCatURL(); String getTimelineServerUrl(); String getYARNProtocol(); static String addProtocolIfMissing(String url, String protocol); static boolean hasProtocol(String[] urls); static boolean hasProtocol(String url); String getHadoopHttpWebAuthType(); String getTimelineServerAuthType(); static final String HTTPS_ONLY; static final String HTTP_ONLY; static final String YARN_SITE; static final String YARN_HTTP_POLICY; static final String YARN_RESOURCEMANAGER_HA_ENABLED; static final String RM_INFO_API_ENDPOINT; static final String TIMELINE_AUTH_TYPE_PROP_KEY; static final String HADOOP_HTTP_AUTH_TYPE_KEY; }
@Test(expected = AmbariApiException.class) public void shouldCheckForEmptyYarnRMUrlInCustomConfig() { ViewContext viewContext = getViewContext(new HashMap<String, String>()); AmbariApi ambariApi = createNiceMock(AmbariApi.class); expect(ambariApi.isClusterAssociated()).andReturn(false); replay(viewContext); Services services = new Services(ambariApi, viewContext); services.getRMUrl(); }
@Test(expected = AmbariApiException.class) public void shouldCheckIfAllRMUrlsHaveProtocolInCustomConfig() { Map<String, String> map = new HashMap<>(); map.put("yarn.resourcemanager.url", HTTP_RM_URL1 + "," + RM_URL2_HOST_PORT); ViewContext viewContext = getViewContext(map); AmbariApi ambariApi = createNiceMock(AmbariApi.class); expect(ambariApi.isClusterAssociated()).andReturn(false); replay(viewContext); Services services = new Services(ambariApi, viewContext); services.getRMUrl(); }
@Test public void shouldReturnUrlIfSingleIsConfiguredInCustomConfig() { Map<String, String> map = new HashMap<>(); map.put("yarn.resourcemanager.url", HTTP_RM_URL1); ViewContext viewContext = getViewContext(map); AmbariApi ambariApi = createNiceMock(AmbariApi.class); expect(ambariApi.isClusterAssociated()).andReturn(false); replay(viewContext); Services services = new Services(ambariApi, viewContext); assertEquals(HTTP_RM_URL1, services.getRMUrl()); }
@Test public void shouldConnectToFirstUrlWhenMultipleRMUrlIsConfiguredInCustomConfig() throws IOException { Map<String, String> map = new HashMap<>(); map.put("yarn.resourcemanager.url", HTTP_RM_URL1 + ", " + HTTP_RM_URL2); ViewContext viewContext = getViewContext(map); AmbariApi ambariApi = createNiceMock(AmbariApi.class); URLStreamProvider urlStreamProvider = createNiceMock(URLStreamProvider.class); InputStream inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(ambariApi.isClusterAssociated()).andReturn(false); expect(viewContext.getURLStreamProvider()).andReturn(urlStreamProvider); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStream); replayAll(); Services services = new Services(ambariApi, viewContext); assertEquals(HTTP_RM_URL1, services.getRMUrl()); }
@Test public void shouldConnectToSecondUrlWhenTheFirstURLTimesOut() throws Exception { Map<String, String> map = new HashMap<>(); map.put("yarn.resourcemanager.url", HTTP_RM_URL1 + ", " + HTTP_RM_URL2); ViewContext viewContext = getViewContext(map); AmbariApi ambariApi = createNiceMock(AmbariApi.class); URLStreamProvider urlStreamProvider = createNiceMock(URLStreamProvider.class); InputStream inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(ambariApi.isClusterAssociated()).andReturn(false); expect(viewContext.getURLStreamProvider()).andReturn(urlStreamProvider).anyTimes(); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andThrow(new IOException()); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL2 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStream); replayAll(); Services services = new Services(ambariApi, viewContext); assertEquals(HTTP_RM_URL2, services.getRMUrl()); }
@Test(expected = AmbariApiException.class) public void shouldThrowExceptionWhenAllUrlCannotBeReached() throws Exception { Map<String, String> map = new HashMap<>(); map.put("yarn.resourcemanager.url", HTTP_RM_URL1 + ", " + HTTP_RM_URL2); ViewContext viewContext = getViewContext(map); AmbariApi ambariApi = createNiceMock(AmbariApi.class); URLStreamProvider urlStreamProvider = createNiceMock(URLStreamProvider.class); expect(ambariApi.isClusterAssociated()).andReturn(false); expect(viewContext.getURLStreamProvider()).andReturn(urlStreamProvider).anyTimes(); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andThrow(new IOException()); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL2 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andThrow(new IOException()); replayAll(); Services services = new Services(ambariApi, viewContext); services.getRMUrl(); }
@Test public void shouldReturnActiveRMUrlWhenConnectingToStandby() throws Exception { Map<String, String> map = new HashMap<>(); map.put("yarn.resourcemanager.url", HTTP_RM_URL1 + ", " + HTTP_RM_URL2); ViewContext viewContext = getViewContext(map); AmbariApi ambariApi = createNiceMock(AmbariApi.class); URLStreamProvider urlStreamProvider = createNiceMock(URLStreamProvider.class); InputStream inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"STANDBY\"}}"); expect(ambariApi.isClusterAssociated()).andReturn(false); expect(viewContext.getURLStreamProvider()).andReturn(urlStreamProvider).anyTimes(); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStream); InputStream inputStreamActive = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL2 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStreamActive); replayAll(); Services services = new Services(ambariApi, viewContext); assertEquals(HTTP_RM_URL2, services.getRMUrl()); verify(urlStreamProvider); }
@Test public void shouldConnectToRMConfiguredInClusterMode() throws Exception { ViewContext viewContext = getViewContext(new HashMap<String, String>()); AmbariApi ambariApi = createNiceMock(AmbariApi.class); Cluster cluster = createNiceMock(Cluster.class); expect(ambariApi.isClusterAssociated()).andReturn(true).anyTimes(); setClusterExpectation(cluster, "HTTP_ONLY"); expect(viewContext.getCluster()).andReturn(cluster).anyTimes(); replayAll(); Services services = new Services(ambariApi, viewContext); assertEquals(HTTP_RM_URL1, services.getRMUrl()); reset(cluster); setClusterExpectation(cluster, "HTTPS_ONLY"); replay(cluster); assertEquals(HTTPS_RM_URL2, services.getRMUrl()); reset(cluster); setClusterExpectation(cluster, "HTTPS_ONLY_XYZ"); replay(cluster); assertEquals(HTTP_RM_URL1, services.getRMUrl()); }
@Test public void shouldConnectToDefaultHostPortInClusterModeWhenWebaddressConfigIsEmpty() throws Exception { ViewContext viewContext = getViewContext(new HashMap<String, String>()); AmbariApi ambariApi = createNiceMock(AmbariApi.class); Cluster cluster = createNiceMock(Cluster.class); expect(ambariApi.isClusterAssociated()).andReturn(true).anyTimes(); setClusterExpectationWithEmptyWebappConfig(cluster, "HTTP_ONLY"); expect(viewContext.getCluster()).andReturn(cluster).anyTimes(); Services services = new Services(ambariApi, viewContext); replayAll(); assertEquals("http: reset(cluster); setClusterExpectationWithEmptyWebappConfig(cluster, "HTTPS_ONLY"); replay(cluster); assertEquals("https: }
@Test public void shouldConnectToDefaultHostPortInClusterModeWithHAWhenWebaddressConfigIsEmpty() throws Exception { ViewContext viewContext = getViewContext(new HashMap<String, String>()); AmbariApi ambariApi = createNiceMock(AmbariApi.class); Cluster cluster = createNiceMock(Cluster.class); URLStreamProvider urlStreamProvider = createNiceMock(URLStreamProvider.class); expect(ambariApi.isClusterAssociated()).andReturn(true).anyTimes(); setClusterExpectationInHAWithEmptyWebappConfig(cluster, "HTTP_ONLY"); expect(viewContext.getCluster()).andReturn(cluster).anyTimes(); Services services = new Services(ambariApi, viewContext); InputStream inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(viewContext.getURLStreamProvider()).andReturn(urlStreamProvider).anyTimes(); expect(urlStreamProvider.readFrom(eq("http: replayAll(); assertEquals("http: reset(cluster, urlStreamProvider); setClusterExpectationInHAWithEmptyWebappConfig(cluster, "HTTPS_ONLY"); inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(urlStreamProvider.readFrom(eq("https: replay(cluster, urlStreamProvider); assertEquals("https: }
@Test public void shouldFetchRMUrlsWhileHAEnabledInClusterMode() throws Exception { ViewContext viewContext = getViewContext(new HashMap<String, String>()); AmbariApi ambariApi = createNiceMock(AmbariApi.class); Cluster cluster = createNiceMock(Cluster.class); URLStreamProvider urlStreamProvider = createNiceMock(URLStreamProvider.class); Services services = new Services(ambariApi, viewContext); InputStream inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(ambariApi.isClusterAssociated()).andReturn(true).anyTimes(); setClusterExpectationInHA(cluster, "HTTP_ONLY"); expect(viewContext.getCluster()).andReturn(cluster).anyTimes(); expect(viewContext.getURLStreamProvider()).andReturn(urlStreamProvider).anyTimes(); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStream); replayAll(); assertEquals(HTTP_RM_URL1, services.getRMUrl()); reset(cluster, urlStreamProvider); setClusterExpectationInHA(cluster, "HTTP_ONLY"); inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andThrow(new IOException()); expect(urlStreamProvider.readFrom(eq(HTTP_RM_URL2 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStream); replay(cluster, urlStreamProvider); assertEquals(HTTP_RM_URL2, services.getRMUrl()); reset(cluster, urlStreamProvider); setClusterExpectationInHA(cluster, "HTTPS_ONLY"); inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(urlStreamProvider.readFrom(eq(HTTPS_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStream); replay(cluster, urlStreamProvider); assertEquals(HTTPS_RM_URL1, services.getRMUrl()); reset(cluster, urlStreamProvider); setClusterExpectationInHA(cluster, "HTTPS_ONLY"); inputStream = IOUtils.toInputStream("{\"clusterInfo\": {\"haState\": \"ACTIVE\"}}"); expect(urlStreamProvider.readFrom(eq(HTTPS_RM_URL1 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andThrow(new IOException()); expect(urlStreamProvider.readFrom(eq(HTTPS_RM_URL2 + RM_INFO_API_ENDPOINT), eq("GET"), anyString(), EasyMock.<Map<String, String>>anyObject())).andReturn(inputStream); replay(cluster, urlStreamProvider); assertEquals(HTTPS_RM_URL2, services.getRMUrl()); }