AMBARI-20612. Fetching running application logs results in java.io.IOException (Madhuvanthi Radhakrishnan via smohanty)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/09033094 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/09033094 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/09033094 Branch: refs/heads/branch-feature-AMBARI-12556 Commit: 0903309437b9ddea06244bd2836e071b74b56176 Parents: f18fad3 Author: Sumit Mohanty <smoha...@hortonworks.com> Authored: Tue Mar 28 21:40:37 2017 -0700 Committer: Sumit Mohanty <smoha...@hortonworks.com> Committed: Tue Mar 28 21:40:37 2017 -0700 ---------------------------------------------------------------------- .../upgrades/FixYarnWebServiceUrl.java | 103 +++++++ .../HDP/2.3/upgrades/nonrolling-upgrade-2.6.xml | 7 + .../stacks/HDP/2.3/upgrades/upgrade-2.6.xml | 5 + .../HDP/2.4/upgrades/nonrolling-upgrade-2.6.xml | 7 + .../stacks/HDP/2.4/upgrades/upgrade-2.6.xml | 5 + .../HDP/2.5/upgrades/nonrolling-upgrade-2.6.xml | 7 + .../stacks/HDP/2.5/upgrades/upgrade-2.6.xml | 7 + .../upgrades/FixYarnWebServiceUrlTest.java | 305 +++++++++++++++++++ 8 files changed, 446 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrl.java b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrl.java new file mode 100644 index 0000000..ae2631a --- /dev/null +++ b/ambari-server/src/main/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrl.java @@ -0,0 +1,103 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.serveraction.upgrades; + +import java.util.Map; +import java.util.concurrent.ConcurrentMap; + +import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.actionmanager.HostRoleStatus; +import org.apache.ambari.server.agent.CommandReport; +import org.apache.ambari.server.serveraction.AbstractServerAction; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; +import org.apache.ambari.server.state.Config; + +import com.google.inject.Inject; + +/** + * yarn.log.server.web-service.url is added in HDP 2.6 + * It takes value from yarn.timeline-service.webapp.address if the yarn.http.policy is HTTP_ONLY + * and takes value from yarn.timeline-service.webapp.https.address if the yarn.http.policy is HTTPS_ONLY. + * This class is used when moving from HDP-2.3/HDP-2.4/HDP-2.5 to HDP2.6 + */ +public class FixYarnWebServiceUrl extends AbstractServerAction { + private static final String SOURCE_CONFIG_TYPE = "yarn-site"; + private static final String YARN_TIMELINE_WEBAPP_HTTPADDRESS = "yarn.timeline-service.webapp.address"; + private static final String YARN_TIMELINE_WEBAPP_HTTPSADDRESS = "yarn.timeline-service.webapp.https.address"; + private static final String YARN_LOGSERVER_WEBSERVICE_URL = "yarn.log.server.web-service.url"; + private static final String YARN_HTTP_POLICY = "yarn.http.policy"; + private static final String HTTP = "HTTP_ONLY"; + private static final String HTTPS = "HTTPS_ONLY"; + + @Inject + private Clusters clusters; + + @Override + public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) + throws AmbariException, InterruptedException{ + + String clusterName = getExecutionCommand().getClusterName(); + Cluster cluster = clusters.getCluster(clusterName); + Config config = cluster.getDesiredConfigByType(SOURCE_CONFIG_TYPE); + + if (config == null) { + return createCommandReport(0, HostRoleStatus.FAILED,"{}", + String.format("Source type %s not found", SOURCE_CONFIG_TYPE), ""); + } + + Map<String, String> properties = config.getProperties(); + String policy = properties.get(YARN_HTTP_POLICY); + + if (policy == null) { + return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", + String.format("%s/%s property is null", SOURCE_CONFIG_TYPE,YARN_HTTP_POLICY), ""); + } + + if (policy.equalsIgnoreCase(HTTP)) { + String webappHttpAddress = properties.get(YARN_TIMELINE_WEBAPP_HTTPADDRESS); + + if (webappHttpAddress == null) { + return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", + String.format("%s/%s property is null", SOURCE_CONFIG_TYPE, YARN_TIMELINE_WEBAPP_HTTPADDRESS), ""); + } + + properties.put(YARN_LOGSERVER_WEBSERVICE_URL, "http://" + webappHttpAddress + "/ws/v1/applicationhistory"); + + } else if (policy.equalsIgnoreCase(HTTPS)) { + String webappHttpsAddress = properties.get(YARN_TIMELINE_WEBAPP_HTTPSADDRESS); + + if (webappHttpsAddress == null) { + return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", + String.format("%s/%s property is null", SOURCE_CONFIG_TYPE, YARN_TIMELINE_WEBAPP_HTTPSADDRESS), ""); + } + + properties.put(YARN_LOGSERVER_WEBSERVICE_URL, "https://"+webappHttpsAddress+"/ws/v1/applicationhistory"); + + } else { + return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", + String.format("%s/%s property contains an invalid value. It should be from [%s,%s]", SOURCE_CONFIG_TYPE, YARN_HTTP_POLICY,HTTP,HTTPS), ""); + } + + config.setProperties(properties); + config.save(); + + return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", + String.format("The %s/%s property was changed based on %s. ", SOURCE_CONFIG_TYPE, YARN_LOGSERVER_WEBSERVICE_URL, YARN_HTTP_POLICY),""); + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/nonrolling-upgrade-2.6.xml ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/nonrolling-upgrade-2.6.xml b/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/nonrolling-upgrade-2.6.xml index b4b8f2a..61eef1e 100644 --- a/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/nonrolling-upgrade-2.6.xml +++ b/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/nonrolling-upgrade-2.6.xml @@ -342,6 +342,13 @@ </task> </execute-stage> + <!--Yarn Apptimeline server--> + <execute-stage service="YARN" component="APP_TIMELINE_SERVER" title="Apply config changes for App timeline server"> + <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixYarnWebServiceUrl"> + <summary>Fixing YARN Webservice Url</summary> + </task> + </execute-stage> + <execute-stage service="MAPREDUCE2" component="MAPREDUCE2_CLIENT" title="Apply config changes for Mapreduce2 client"> <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixLzoCodecPath"> <summary>Verifying LZO codec path for mapreduce</summary> http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/upgrade-2.6.xml ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/upgrade-2.6.xml b/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/upgrade-2.6.xml index aaf6f88..f2d4980 100644 --- a/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/upgrade-2.6.xml +++ b/ambari-server/src/main/resources/stacks/HDP/2.3/upgrades/upgrade-2.6.xml @@ -739,6 +739,11 @@ <service name="YARN"> <component name="APP_TIMELINE_SERVER"> + <pre-upgrade> + <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixYarnWebServiceUrl"> + <summary>Fixing YARN Webservice Url</summary> + </task> + </pre-upgrade> <pre-downgrade /> <!-- no-op to prevent config changes on downgrade --> <upgrade> http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/nonrolling-upgrade-2.6.xml ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/nonrolling-upgrade-2.6.xml b/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/nonrolling-upgrade-2.6.xml index aa90ac0..c0bdcb8 100644 --- a/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/nonrolling-upgrade-2.6.xml +++ b/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/nonrolling-upgrade-2.6.xml @@ -294,6 +294,13 @@ </task> </execute-stage> + <!--Yarn Apptimeline server--> + <execute-stage service="YARN" component="APP_TIMELINE_SERVER" title="Apply config changes for App timeline server"> + <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixYarnWebServiceUrl"> + <summary>Fixing YARN Webservice Url</summary> + </task> + </execute-stage> + <!-- YARN --> <execute-stage service="YARN" component="NODEMANAGER" title="Add Spark2 shuffle"> <task xsi:type="configure" id="hdp_2_5_0_0_add_spark2_yarn_shuffle"/> http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/upgrade-2.6.xml ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/upgrade-2.6.xml b/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/upgrade-2.6.xml index 9df5757..104c38f 100644 --- a/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/upgrade-2.6.xml +++ b/ambari-server/src/main/resources/stacks/HDP/2.4/upgrades/upgrade-2.6.xml @@ -744,6 +744,11 @@ <service name="YARN"> <component name="APP_TIMELINE_SERVER"> + <pre-upgrade> + <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixYarnWebServiceUrl"> + <summary>Fixing YARN Webservice Url</summary> + </task> + </pre-upgrade> <pre-downgrade /> <!-- no-op to prevent config changes on downgrade --> <upgrade> http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/nonrolling-upgrade-2.6.xml ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/nonrolling-upgrade-2.6.xml b/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/nonrolling-upgrade-2.6.xml index c2ec6fd..ca75344 100644 --- a/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/nonrolling-upgrade-2.6.xml +++ b/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/nonrolling-upgrade-2.6.xml @@ -334,6 +334,13 @@ </task> </execute-stage> + <!--Yarn Apptimeline server--> + <execute-stage service="YARN" component="APP_TIMELINE_SERVER" title="Apply config changes for App timeline server"> + <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixYarnWebServiceUrl"> + <summary>Fixing YARN Webservice Url</summary> + </task> + </execute-stage> + <!--TEZ--> <execute-stage service="TEZ" component="TEZ_CLIENT" title="Verify LZO codec path for Tez"> <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixLzoCodecPath"> http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/upgrade-2.6.xml ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/upgrade-2.6.xml b/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/upgrade-2.6.xml index 381d6cc..a32828f 100644 --- a/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/upgrade-2.6.xml +++ b/ambari-server/src/main/resources/stacks/HDP/2.5/upgrades/upgrade-2.6.xml @@ -677,9 +677,16 @@ <service name="YARN"> <component name="APP_TIMELINE_SERVER"> + <pre-upgrade> + <task xsi:type="server_action" class="org.apache.ambari.server.serveraction.upgrades.FixYarnWebServiceUrl"> + <summary>Fixing YARN Webservice Url</summary> + </task> + </pre-upgrade> + <pre-downgrade/> <upgrade> <task xsi:type="restart-task" /> </upgrade> + </component> <component name="RESOURCEMANAGER"> http://git-wip-us.apache.org/repos/asf/ambari/blob/09033094/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrlTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrlTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrlTest.java new file mode 100644 index 0000000..737ece8 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/FixYarnWebServiceUrlTest.java @@ -0,0 +1,305 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ambari.server.serveraction.upgrades; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +import org.apache.ambari.server.actionmanager.ExecutionCommandWrapper; +import org.apache.ambari.server.actionmanager.HostRoleCommand; +import org.apache.ambari.server.agent.CommandReport; +import org.apache.ambari.server.agent.ExecutionCommand; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; +import org.apache.ambari.server.state.Config; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; + +import com.google.inject.Injector; + +/** + * Test FixYarnWebServiceUrl logic + */ +public class FixYarnWebServiceUrlTest { + + private Injector injector; + private Clusters clusters; + private Cluster cluster; + private Field clustersField; + private static final String SOURCE_CONFIG_TYPE = "yarn-site"; + private static final String YARN_TIMELINE_WEBAPP_HTTPADDRESS = "yarn.timeline-service.webapp.address"; + private static final String YARN_TIMELINE_WEBAPP_HTTPSADDRESS = "yarn.timeline-service.webapp.https.address"; + private static final String YARN_LOGSERVER_WEBSERVICE_URL = "yarn.log.server.web-service.url"; + private static final String YARN_HTTP_POLICY = "yarn.http.policy"; + + @Before + public void setup() throws Exception { + injector = EasyMock.createMock(Injector.class); + clusters = EasyMock.createMock(Clusters.class); + cluster = EasyMock.createMock(Cluster.class); + clustersField = FixYarnWebServiceUrl.class.getDeclaredField("clusters"); + clustersField.setAccessible(true); + + expect(clusters.getCluster((String) anyObject())).andReturn(cluster).anyTimes(); + expect(injector.getInstance(Clusters.class)).andReturn(clusters).atLeastOnce(); + replay(injector, clusters); + } + /** + * Test when http policy is set to HTTP_ONLY + * @throws Exception + */ + @Test + public void testHttpOnly() throws Exception { + + Map<String, String> mockProperties = new HashMap<String, String>() {{ + put(YARN_TIMELINE_WEBAPP_HTTPADDRESS, "c6401.ambari.apache.org:8188"); + put(YARN_TIMELINE_WEBAPP_HTTPSADDRESS, "c6401.ambari.apache.org:8190"); + put(YARN_HTTP_POLICY, "HTTP_ONLY"); + put(YARN_LOGSERVER_WEBSERVICE_URL, "http://localhost:8188/ws/v1/applicationhistory"); + }}; + + Config yarnSiteConfig = EasyMock.createNiceMock(Config.class); + expect(yarnSiteConfig.getType()).andReturn("yarn-site").anyTimes(); + expect(yarnSiteConfig.getProperties()).andReturn(mockProperties).anyTimes(); + + expect(cluster.getDesiredConfigByType(SOURCE_CONFIG_TYPE)).andReturn(yarnSiteConfig).atLeastOnce(); + + Map<String, String> commandParams = new HashMap<String, String>(); + commandParams.put("clusterName", "c1"); + + ExecutionCommand executionCommand = new ExecutionCommand(); + executionCommand.setCommandParams(commandParams); + executionCommand.setClusterName("c1"); + + HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); + expect(hrc.getRequestId()).andReturn(1L).anyTimes(); + expect(hrc.getStageId()).andReturn(2L).anyTimes(); + expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); + replay(cluster, hrc,yarnSiteConfig); + + FixYarnWebServiceUrl action = new FixYarnWebServiceUrl(); + clustersField.set(action, clusters); + + action.setExecutionCommand(executionCommand); + action.setHostRoleCommand(hrc); + + CommandReport report = action.execute(null); + assertNotNull(report); + + Cluster c = clusters.getCluster("c1"); + Config desiredYarnSiteConfig = c.getDesiredConfigByType(SOURCE_CONFIG_TYPE); + + Map<String, String> yarnSiteConfigMap = desiredYarnSiteConfig.getProperties(); + + assertTrue(yarnSiteConfigMap.containsKey(YARN_LOGSERVER_WEBSERVICE_URL)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_HTTP_POLICY)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPADDRESS)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPSADDRESS)); + String yarnLogServerWebServiceUrl = yarnSiteConfigMap.get(YARN_LOGSERVER_WEBSERVICE_URL); + + assertEquals("http://c6401.ambari.apache.org:8188/ws/v1/applicationhistory", yarnLogServerWebServiceUrl); + + } + + /** + * Test when http policy is set to HTTPS_ONLY + * @throws Exception + */ + @Test + public void testHttpsOnly() throws Exception { + + Map<String, String> mockProperties = new HashMap<String, String>() {{ + put(YARN_TIMELINE_WEBAPP_HTTPADDRESS, "c6401.ambari.apache.org:8188"); + put(YARN_TIMELINE_WEBAPP_HTTPSADDRESS, "c6401.ambari.apache.org:8190"); + put(YARN_HTTP_POLICY, "HTTPS_ONLY"); + put(YARN_LOGSERVER_WEBSERVICE_URL, "http://localhost:8188/ws/v1/applicationhistory"); + }}; + + Config yarnSiteConfig = EasyMock.createNiceMock(Config.class); + expect(yarnSiteConfig.getType()).andReturn("yarn-site").anyTimes(); + expect(yarnSiteConfig.getProperties()).andReturn(mockProperties).anyTimes(); + + expect(cluster.getDesiredConfigByType(SOURCE_CONFIG_TYPE)).andReturn(yarnSiteConfig).atLeastOnce(); + + Map<String, String> commandParams = new HashMap<String, String>(); + commandParams.put("clusterName", "c1"); + + ExecutionCommand executionCommand = new ExecutionCommand(); + executionCommand.setCommandParams(commandParams); + executionCommand.setClusterName("c1"); + + HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); + expect(hrc.getRequestId()).andReturn(1L).anyTimes(); + expect(hrc.getStageId()).andReturn(2L).anyTimes(); + expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); + replay(cluster, hrc,yarnSiteConfig); + + FixYarnWebServiceUrl action = new FixYarnWebServiceUrl(); + clustersField.set(action, clusters); + + action.setExecutionCommand(executionCommand); + action.setHostRoleCommand(hrc); + + CommandReport report = action.execute(null); + assertNotNull(report); + + Cluster c = clusters.getCluster("c1"); + Config desiredYarnSiteConfig = c.getDesiredConfigByType(SOURCE_CONFIG_TYPE); + + Map<String, String> yarnSiteConfigMap = desiredYarnSiteConfig.getProperties(); + + assertTrue(yarnSiteConfigMap.containsKey(YARN_LOGSERVER_WEBSERVICE_URL)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_HTTP_POLICY)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPADDRESS)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPSADDRESS)); + String yarnLogServerWebServiceUrl = yarnSiteConfigMap.get(YARN_LOGSERVER_WEBSERVICE_URL); + + assertEquals("https://c6401.ambari.apache.org:8190/ws/v1/applicationhistory", yarnLogServerWebServiceUrl); + + } + + /** + * Test when http policy is set to incorrect value + * @throws Exception + */ + @Test + public void testIncorrectValue() throws Exception { + + Map<String, String> mockProperties = new HashMap<String, String>() {{ + put(YARN_TIMELINE_WEBAPP_HTTPADDRESS, "c6401.ambari.apache.org:8188"); + put(YARN_TIMELINE_WEBAPP_HTTPSADDRESS, "c6401.ambari.apache.org:8190"); + put(YARN_HTTP_POLICY, "abc"); + put(YARN_LOGSERVER_WEBSERVICE_URL, "http://localhost:8188/ws/v1/applicationhistory"); + }}; + + Config yarnSiteConfig = EasyMock.createNiceMock(Config.class); + expect(yarnSiteConfig.getType()).andReturn("yarn-site").anyTimes(); + expect(yarnSiteConfig.getProperties()).andReturn(mockProperties).anyTimes(); + + expect(cluster.getDesiredConfigByType(SOURCE_CONFIG_TYPE)).andReturn(yarnSiteConfig).atLeastOnce(); + + Map<String, String> commandParams = new HashMap<String, String>(); + commandParams.put("clusterName", "c1"); + + ExecutionCommand executionCommand = new ExecutionCommand(); + executionCommand.setCommandParams(commandParams); + executionCommand.setClusterName("c1"); + + HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); + expect(hrc.getRequestId()).andReturn(1L).anyTimes(); + expect(hrc.getStageId()).andReturn(2L).anyTimes(); + expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); + replay(cluster, hrc,yarnSiteConfig); + + FixYarnWebServiceUrl action = new FixYarnWebServiceUrl(); + clustersField.set(action, clusters); + + action.setExecutionCommand(executionCommand); + action.setHostRoleCommand(hrc); + + CommandReport report = action.execute(null); + assertNotNull(report); + + Cluster c = clusters.getCluster("c1"); + Config desiredYarnSiteConfig = c.getDesiredConfigByType(SOURCE_CONFIG_TYPE); + + Map<String, String> yarnSiteConfigMap = desiredYarnSiteConfig.getProperties(); + + assertTrue(yarnSiteConfigMap.containsKey(YARN_LOGSERVER_WEBSERVICE_URL)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_HTTP_POLICY)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPADDRESS)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPSADDRESS)); + String yarnLogServerWebServiceUrl = yarnSiteConfigMap.get(YARN_LOGSERVER_WEBSERVICE_URL); + + assertEquals("http://localhost:8188/ws/v1/applicationhistory",yarnLogServerWebServiceUrl); + assertEquals(SOURCE_CONFIG_TYPE +"/" + YARN_HTTP_POLICY + " property contains an invalid value. It should be from [HTTP_ONLY,HTTPS_ONLY]", report.getStdOut()); + + } + + /** + * Test when some values are null + * @throws Exception + */ + @Test + public void testNullValues() throws Exception { + + Map<String, String> mockProperties = new HashMap<String, String>() {{ + put(YARN_TIMELINE_WEBAPP_HTTPADDRESS, null); + put(YARN_TIMELINE_WEBAPP_HTTPSADDRESS, "c6401.ambari.apache.org:8190"); + put(YARN_HTTP_POLICY, null); + put(YARN_LOGSERVER_WEBSERVICE_URL, "http://localhost:8188/ws/v1/applicationhistory"); + }}; + + Config yarnSiteConfig = EasyMock.createNiceMock(Config.class); + expect(yarnSiteConfig.getType()).andReturn("yarn-site").anyTimes(); + expect(yarnSiteConfig.getProperties()).andReturn(mockProperties).anyTimes(); + + expect(cluster.getDesiredConfigByType(SOURCE_CONFIG_TYPE)).andReturn(yarnSiteConfig).atLeastOnce(); + + + Map<String, String> commandParams = new HashMap<String, String>(); + commandParams.put("clusterName", "c1"); + + ExecutionCommand executionCommand = new ExecutionCommand(); + executionCommand.setCommandParams(commandParams); + executionCommand.setClusterName("c1"); + + HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); + expect(hrc.getRequestId()).andReturn(1L).anyTimes(); + expect(hrc.getStageId()).andReturn(2L).anyTimes(); + expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); + replay(cluster, hrc,yarnSiteConfig); + + FixYarnWebServiceUrl action = new FixYarnWebServiceUrl(); + clustersField.set(action, clusters); + + action.setExecutionCommand(executionCommand); + action.setHostRoleCommand(hrc); + + CommandReport report = action.execute(null); + assertNotNull(report); + + Cluster c = clusters.getCluster("c1"); + Config desiredYarnSiteConfig = c.getDesiredConfigByType(SOURCE_CONFIG_TYPE); + + Map<String, String> yarnSiteConfigMap = yarnSiteConfig.getProperties(); + yarnSiteConfigMap.put(YARN_TIMELINE_WEBAPP_HTTPADDRESS, ""); + + assertTrue(yarnSiteConfigMap.containsKey(YARN_LOGSERVER_WEBSERVICE_URL)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_HTTP_POLICY)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPADDRESS)); + assertTrue(yarnSiteConfigMap.containsKey(YARN_TIMELINE_WEBAPP_HTTPSADDRESS)); + String yarnLogServerWebServiceUrl = yarnSiteConfigMap.get(YARN_LOGSERVER_WEBSERVICE_URL); + + assertEquals("http://localhost:8188/ws/v1/applicationhistory", yarnLogServerWebServiceUrl); + assertEquals(SOURCE_CONFIG_TYPE + "/" +YARN_HTTP_POLICY +" property is null", report.getStdOut()); + + } + + + +}