You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by vb...@apache.org on 2015/10/29 12:55:14 UTC
ambari git commit: AMBARI-13617. Add unit tests for change hostname
functionality.(vbrodetskyi)
Repository: ambari
Updated Branches:
refs/heads/branch-2.1 2b171b02b -> 17ad5abd3
AMBARI-13617. Add unit tests for change hostname functionality.(vbrodetskyi)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/17ad5abd
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/17ad5abd
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/17ad5abd
Branch: refs/heads/branch-2.1
Commit: 17ad5abd303db52e9f4bcee36a13bb2aa20659f8
Parents: 2b171b0
Author: Vitaly Brodetskyi <vb...@hortonworks.com>
Authored: Thu Oct 29 13:54:30 2015 +0200
Committer: Vitaly Brodetskyi <vb...@hortonworks.com>
Committed: Thu Oct 29 13:54:30 2015 +0200
----------------------------------------------------------------------
.../ambari/server/update/HostUpdateHelper.java | 63 ++-
.../src/main/python/ambari_server/hostUpdate.py | 22 +-
.../server/update/HostUpdateHelperTest.java | 501 +++++++++++++++++++
.../src/test/python/TestAmbariServer.py | 49 +-
4 files changed, 590 insertions(+), 45 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/17ad5abd/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
index 672cd4e..ec4921d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/update/HostUpdateHelper.java
@@ -61,6 +61,7 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -118,6 +119,14 @@ public class HostUpdateHelper {
this.hostChangesFile = hostChangesFile;
}
+ public Map<String, Map<String, String>> getHostChangesFileMap() {
+ return hostChangesFileMap;
+ }
+
+ public void setHostChangesFileMap(Map<String, Map<String, String>> hostChangesFileMap) {
+ this.hostChangesFileMap = hostChangesFileMap;
+ }
+
/**
* Extension of main controller module
*/
@@ -138,8 +147,8 @@ public class HostUpdateHelper {
* Check cluster and hosts existence.
* Check on valid structure of json.
* */
- private void validateHostChanges() throws AmbariException {
- if (hostChangesFileMap == null && hostChangesFileMap.isEmpty()) {
+ void validateHostChanges() throws AmbariException {
+ if (hostChangesFileMap == null || hostChangesFileMap.isEmpty()) {
throw new AmbariException(String.format("File with host names changes is null or empty"));
}
@@ -181,11 +190,10 @@ public class HostUpdateHelper {
* Method updates all properties in all configs,
* which value contains hostname that should be updated
* */
- private void updateHostsInConfigurations() throws AmbariException {
+ void updateHostsInConfigurations() throws AmbariException {
ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
Clusters clusters = ambariManagementController.getClusters();
- ConfigHelper configHelper = injector.getInstance(ConfigHelper.class);
if (clusters != null) {
@@ -235,16 +243,18 @@ public class HostUpdateHelper {
}
}
+ /*
+ * Method replaces current hosts with new hosts in propertyValue
+ * */
private String replaceHosts(String propertyValue, List<String> currentHostNames, Map<String,String> hostMapping) {
- List<String> currentHostNamesWhichPropertyValueIncludes = new ArrayList<>();
- List<String> hostListForReplace = new ArrayList<>();
+ List<String> hostListForReplace;
String updatedPropertyValue = null;
- currentHostNamesWhichPropertyValueIncludes = getHostNamesWhichValueIncludes(currentHostNames, propertyValue);
+ hostListForReplace = getHostNamesWhichValueIncludes(currentHostNames, propertyValue);
- if (!currentHostNamesWhichPropertyValueIncludes.isEmpty() && hostMapping != null) {
- // filter current hosts which are included in other hosts
- hostListForReplace = filterHostNamesIncludedInOtherHostNames(currentHostNamesWhichPropertyValueIncludes);
+ if (!hostListForReplace.isEmpty() && hostMapping != null) {
+ // sort included hosts
+ Collections.sort(hostListForReplace, new StringComparator());
updatedPropertyValue = propertyValue;
// create map with replace codes, it will help us to replace every hostname only once
@@ -286,29 +296,12 @@ public class HostUpdateHelper {
}
/*
- * Method returns filtered list of host names
- * which are not includes each other
+ * String comparator. For sorting collection of strings from longer to shorter..
* */
- private List<String> filterHostNamesIncludedInOtherHostNames(List<String> hostNames) {
- if (hostNames != null && !hostNames.isEmpty()) {
- int includeCounter;
- List<String> hostNamesToExclude = new ArrayList<>();
- for (String hostNameToCheck : hostNames) {
- includeCounter = 0;
- for (String hostName : hostNames) {
- if (StringUtils.contains(hostName, hostNameToCheck)) {
- includeCounter++;
- }
- }
- if (includeCounter > 1) {
- hostNamesToExclude.add(hostNameToCheck);
- }
- }
+ public class StringComparator implements Comparator<String> {
- hostNames.removeAll(hostNamesToExclude);
- return hostNames;
- } else {
- return Collections.emptyList();
+ public int compare(String s1, String s2) {
+ return s2.length() - s1.length();
}
}
@@ -317,7 +310,7 @@ public class HostUpdateHelper {
* If enabled for someone, then we will throw exception
* and put message to log.
* */
- private void checkForSecurity() throws AmbariException {
+ void checkForSecurity() throws AmbariException {
AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
Clusters clusters = ambariManagementController.getClusters();
List<String> clustersInSecure = new ArrayList<>();
@@ -362,7 +355,7 @@ public class HostUpdateHelper {
/*
* Method updates host names in db for hosts table..
* */
- private void updateHostsInDB() {
+ void updateHostsInDB() {
ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
HostDAO hostDAO = injector.getInstance(HostDAO.class);
@@ -392,7 +385,7 @@ public class HostUpdateHelper {
* Method updates Current Alerts host name and
* regenerates hash for alert definitions(for alerts to be recreated)
* */
- private void updateHostsForAlertsInDB() {
+ void updateHostsForAlertsInDB() {
AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
AlertsDAO alertsDAO = injector.getInstance(AlertsDAO.class);
AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class);
@@ -434,7 +427,7 @@ public class HostUpdateHelper {
/*
* Method updates hosts for Topology Requests (Blue Prints logic)
* */
- private void updateHostsForTopologyRequests() {
+ void updateHostsForTopologyRequests() {
AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class);
TopologyRequestDAO topologyRequestDAO = injector.getInstance(TopologyRequestDAO.class);
TopologyHostRequestDAO topologyHostRequestDAO = injector.getInstance(TopologyHostRequestDAO.class);
http://git-wip-us.apache.org/repos/asf/ambari/blob/17ad5abd/ambari-server/src/main/python/ambari_server/hostUpdate.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/ambari_server/hostUpdate.py b/ambari-server/src/main/python/ambari_server/hostUpdate.py
index bfd6651..8acd27a 100644
--- a/ambari-server/src/main/python/ambari_server/hostUpdate.py
+++ b/ambari-server/src/main/python/ambari_server/hostUpdate.py
@@ -22,6 +22,10 @@ import os
import sys
from ambari_commons.exceptions import FatalException
+from ambari_server import serverConfiguration
+from ambari_server import dbConfiguration
+from ambari_server import setupSecurity
+from ambari_commons import os_utils
from ambari_server.serverConfiguration import configDefaults, get_java_exe_path, get_ambari_properties, read_ambari_user, \
parse_properties_file, JDBC_DATABASE_PROPERTY
from ambari_commons.logging_utils import print_info_msg, print_warning_msg, print_error_msg
@@ -69,7 +73,7 @@ def update_host_names(args, options):
if not os.access(host_mapping_file_path, os.R_OK):
raise FatalException(1, "File is not readable")
- jdk_path = get_java_exe_path()
+ jdk_path = serverConfiguration.get_java_exe_path()
if jdk_path is None:
print_error_msg("No JDK found, please run the \"setup\" "
@@ -77,22 +81,22 @@ def update_host_names(args, options):
"JDK manually to " + configDefaults.JDK_INSTALL_DIR)
sys.exit(1)
- properties = get_ambari_properties()
- parse_properties_file(options)
+ properties = serverConfiguration.get_ambari_properties()
+ serverConfiguration.parse_properties_file(options)
options.database_index = LINUX_DBMS_KEYS_LIST.index(properties[JDBC_DATABASE_PROPERTY])
- ensure_jdbc_driver_is_installed(options, get_ambari_properties())
+ dbConfiguration.ensure_jdbc_driver_is_installed(options, serverConfiguration.get_ambari_properties())
- serverClassPath = ServerClassPath(get_ambari_properties(), options)
+ serverClassPath = ServerClassPath(serverConfiguration.get_ambari_properties(), options)
class_path = serverClassPath.get_full_ambari_classpath_escaped_for_shell()
command = HOST_UPDATE_HELPER_CMD.format(jdk_path, class_path, host_mapping_file_path)
- ambari_user = read_ambari_user()
- current_user = ensure_can_start_under_current_user(ambari_user)
- environ = generate_env(options, ambari_user, current_user)
+ ambari_user = serverConfiguration.read_ambari_user()
+ current_user = setupSecurity.ensure_can_start_under_current_user(ambari_user)
+ environ = setupSecurity.generate_env(options, ambari_user, current_user)
- (retcode, stdout, stderr) = run_os_command(command, env=environ)
+ (retcode, stdout, stderr) = os_utils.run_os_command(command, env=environ)
print_info_msg("Return code from update host names command, retcode = " + str(retcode))
if retcode > 0:
http://git-wip-us.apache.org/repos/asf/ambari/blob/17ad5abd/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java b/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java
new file mode 100644
index 0000000..0755674
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/update/HostUpdateHelperTest.java
@@ -0,0 +1,501 @@
+/*
+ * 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.update;
+
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.AmbariManagementController;
+import org.apache.ambari.server.orm.DBAccessor;
+import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
+import org.apache.ambari.server.orm.dao.AlertDispatchDAO;
+import org.apache.ambari.server.orm.dao.AlertsDAO;
+import org.apache.ambari.server.orm.dao.ClusterDAO;
+import org.apache.ambari.server.orm.dao.HostDAO;
+import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
+import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
+import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
+import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
+import org.apache.ambari.server.orm.entities.ClusterEntity;
+import org.apache.ambari.server.orm.entities.HostEntity;
+import org.apache.ambari.server.orm.entities.StackEntity;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.Config;
+import org.apache.ambari.server.state.Host;
+import org.apache.ambari.server.state.stack.OsFamily;
+import org.easymock.EasyMockSupport;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+
+public class HostUpdateHelperTest {
+
+ @Test
+ public void testValidateHostChanges_Success() throws Exception {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
+ Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
+ Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class);
+ Host host1 = easyMockSupport.createNiceMock(Host.class);
+ Host host2 = easyMockSupport.createNiceMock(Host.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+ Map<String, String> hosts = new HashMap<>();
+ List<Host> clusterHosts = new ArrayList<>();
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
+ }
+ });
+
+ clusterHosts.add(host1);
+ clusterHosts.add(host2);
+
+ hosts.put("host1","host10");
+ hosts.put("host2","host11");
+
+ clusterHostsToChange.put("cl1", hosts);
+
+ expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
+ expect(mockClusters.getCluster("cl1")).andReturn(mockCluster).once();
+ expect(mockCluster.getHosts()).andReturn(clusterHosts).once();
+ expect(host1.getHostName()).andReturn("host1");
+ expect(host2.getHostName()).andReturn("host2");
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ hostUpdateHelper.validateHostChanges();
+ easyMockSupport.verifyAll();
+ }
+
+ @Test
+ public void testValidateHostChanges_InvalidHost() throws Exception {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
+ Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
+ Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class);
+ Host host1 = easyMockSupport.createNiceMock(Host.class);
+ Host host2 = easyMockSupport.createNiceMock(Host.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+ Map<String, String> hosts = new HashMap<>();
+ List<Host> clusterHosts = new ArrayList<>();
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
+ }
+ });
+
+ clusterHosts.add(host1);
+ clusterHosts.add(host2);
+
+ hosts.put("host1","host10");
+ hosts.put("host3","host11");
+
+ clusterHostsToChange.put("cl1", hosts);
+
+ expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
+ expect(mockClusters.getCluster("cl1")).andReturn(mockCluster).once();
+ expect(mockCluster.getHosts()).andReturn(clusterHosts).once();
+ expect(host1.getHostName()).andReturn("host1");
+ expect(host2.getHostName()).andReturn("host2");
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ try{
+ hostUpdateHelper.validateHostChanges();
+ } catch(AmbariException e) {
+ assert(e.getMessage().equals("Hostname(s): host3 was(were) not found."));
+ }
+
+ easyMockSupport.verifyAll();
+ }
+
+ @Test
+ public void testValidateHostChanges_InvalidCluster() throws Exception {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
+ Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
+ }
+ });
+
+ clusterHostsToChange.put("cl1", null);
+
+ expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
+ expect(mockClusters.getCluster("cl1")).andReturn(null).once();
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ try{
+ hostUpdateHelper.validateHostChanges();
+ } catch(AmbariException e) {
+ assert(e.getMessage().equals("Cluster cl1 was not found."));
+ }
+
+ easyMockSupport.verifyAll();
+ }
+
+ @Test
+ public void testValidateHostChanges_HostChangesNull() throws Exception {
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, null);
+
+ hostUpdateHelper.setHostChangesFileMap(null);
+
+ try{
+ hostUpdateHelper.validateHostChanges();
+ } catch(AmbariException e) {
+ assert(e.getMessage().equals("File with host names changes is null or empty"));
+ }
+ }
+
+ @Test
+ public void testUpdateHostsInConfigurations() throws AmbariException {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
+ final ClusterDAO mockClusterDAO = easyMockSupport.createNiceMock(ClusterDAO.class);
+ final EntityManager entityManager = createNiceMock(EntityManager.class);
+ final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
+ Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
+ Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class);
+ ClusterEntity mockClusterEntity1 = easyMockSupport.createNiceMock(ClusterEntity.class);
+ ClusterEntity mockClusterEntity2 = easyMockSupport.createNiceMock(ClusterEntity.class);
+ ClusterConfigEntity mockClusterConfigEntity1 = easyMockSupport.createNiceMock(ClusterConfigEntity.class);
+ ClusterConfigEntity mockClusterConfigEntity2 = easyMockSupport.createNiceMock(ClusterConfigEntity.class);
+ ClusterConfigEntity mockClusterConfigEntity3 = easyMockSupport.createNiceMock(ClusterConfigEntity.class);
+ ClusterConfigEntity mockClusterConfigEntity4 = easyMockSupport.createNiceMock(ClusterConfigEntity.class);
+ StackEntity mockStackEntity = easyMockSupport.createNiceMock(StackEntity.class);
+ ReadWriteLock mockReadWriteLock = easyMockSupport.createNiceMock(ReadWriteLock.class);
+ Lock mockLock = easyMockSupport.createNiceMock(Lock.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+ Map<String, String> hosts = new HashMap<>();
+ List<ClusterConfigEntity> clusterConfigEntities1 = new ArrayList<>();
+ List<ClusterConfigEntity> clusterConfigEntities2 = new ArrayList<>();
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
+ bind(DBAccessor.class).toInstance(dbAccessor);
+ bind(EntityManager.class).toInstance(entityManager);
+ bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+ bind(ClusterDAO.class).toInstance(mockClusterDAO);
+ }
+ });
+
+ hosts.put("host11","host55");
+ hosts.put("host5","host1");
+ hosts.put("host1","host5");
+ hosts.put("host55","host11");
+
+ clusterConfigEntities1.add(mockClusterConfigEntity1);
+ clusterConfigEntities1.add(mockClusterConfigEntity2);
+
+ clusterConfigEntities2.add(mockClusterConfigEntity3);
+ clusterConfigEntities2.add(mockClusterConfigEntity4);
+
+ clusterHostsToChange.put("cl1", hosts);
+
+ expect(mockClusterDAO.findByName("cl1")).andReturn(mockClusterEntity1).once();
+ expect(mockClusterDAO.findById(1L)).andReturn(mockClusterEntity2).atLeastOnce();
+
+ expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
+
+ expect(mockClusters.getCluster("cl1")).andReturn(mockCluster).once();
+ expect(mockCluster.getClusterGlobalLock()).andReturn(mockReadWriteLock).atLeastOnce();
+ expect(mockCluster.getClusterId()).andReturn(1L).atLeastOnce();
+
+ expect(mockReadWriteLock.writeLock()).andReturn(mockLock).atLeastOnce();
+
+ expect(mockClusterEntity1.getClusterConfigEntities()).andReturn(clusterConfigEntities1).atLeastOnce();
+ expect(mockClusterEntity2.getClusterConfigEntities()).andReturn(clusterConfigEntities2).atLeastOnce();
+
+ expect(mockClusterConfigEntity1.getStack()).andReturn(mockStackEntity).once();
+ expect(mockClusterConfigEntity1.getData()).andReturn("{\"testProperty1\" : \"testValue_host1\", " +
+ "\"testProperty2\" : \"testValue_host5\", \"testProperty3\" : \"testValue_host11\", " +
+ "\"testProperty4\" : \"testValue_host55\"}").atLeastOnce();
+ expect(mockClusterConfigEntity1.getTag()).andReturn("testTag1").atLeastOnce();
+ expect(mockClusterConfigEntity1.getType()).andReturn("testType1").atLeastOnce();
+ expect(mockClusterConfigEntity1.getVersion()).andReturn(1L).atLeastOnce();
+
+ expect(mockClusterConfigEntity2.getStack()).andReturn(mockStackEntity).once();
+ expect(mockClusterConfigEntity2.getData()).andReturn("{\"testProperty5\" : \"test_host1_test_host5_test_host11_test_host55\"}").atLeastOnce();
+ expect(mockClusterConfigEntity2.getTag()).andReturn("testTag2").atLeastOnce();
+ expect(mockClusterConfigEntity2.getType()).andReturn("testType2").atLeastOnce();
+ expect(mockClusterConfigEntity2.getVersion()).andReturn(2L).atLeastOnce();
+
+ expect(mockClusterConfigEntity3.getTag()).andReturn("testTag1").atLeastOnce();
+ expect(mockClusterConfigEntity3.getType()).andReturn("testType1").atLeastOnce();
+ expect(mockClusterConfigEntity3.getVersion()).andReturn(1L).atLeastOnce();
+
+ expect(mockClusterConfigEntity4.getTag()).andReturn("testTag2").atLeastOnce();
+ expect(mockClusterConfigEntity4.getType()).andReturn("testType2").atLeastOnce();
+ expect(mockClusterConfigEntity4.getVersion()).andReturn(2L).atLeastOnce();
+
+ mockClusterConfigEntity3.setData("{\"testProperty4\":\"testValue_host11\",\"testProperty3\":\"testValue_host55\"," +
+ "\"testProperty2\":\"testValue_host1\",\"testProperty1\":\"testValue_host5\"}");
+ expectLastCall();
+
+ mockClusterConfigEntity4.setData("{\"testProperty5\":\"test_host5_test_host1_test_host55_test_host11\"}");
+ expectLastCall();
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ hostUpdateHelper.updateHostsInConfigurations();
+ easyMockSupport.verifyAll();
+ }
+
+ @Test
+ public void testCheckForSecurity_SecureCluster() throws AmbariException {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
+ Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
+ Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class);
+ Config mockConfig = easyMockSupport.createNiceMock(Config.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+ Map<String, String> clusterEnvProperties = new HashMap<>();
+
+ clusterHostsToChange.put("cl1", null);
+ clusterEnvProperties.put("security_enabled", "true");
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
+ }
+ });
+
+ expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
+ expect(mockClusters.getCluster("cl1")).andReturn(mockCluster).once();
+ expect(mockCluster.getDesiredConfigByType("cluster-env")).andReturn(mockConfig).once();
+ expect(mockConfig.getProperties()).andReturn(clusterEnvProperties).once();
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ try{
+ hostUpdateHelper.checkForSecurity();
+ } catch(AmbariException e) {
+ assert(e.getMessage().equals("Cluster(s) cl1 from file, is(are) in secure mode. Please, turn off security mode."));
+ }
+ easyMockSupport.verifyAll();
+ }
+
+ @Test
+ public void testCheckForSecurity_NonSecureCluster() throws AmbariException {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
+ Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
+ Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class);
+ Config mockConfig = easyMockSupport.createNiceMock(Config.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+ Map<String, String> clusterEnvProperties = new HashMap<>();
+
+ clusterHostsToChange.put("cl1", null);
+ clusterEnvProperties.put("security_enabled", "false");
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
+ }
+ });
+
+ expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
+ expect(mockClusters.getCluster("cl1")).andReturn(mockCluster).once();
+ expect(mockCluster.getDesiredConfigByType("cluster-env")).andReturn(mockConfig).once();
+ expect(mockConfig.getProperties()).andReturn(clusterEnvProperties).once();
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ hostUpdateHelper.checkForSecurity();
+ easyMockSupport.verifyAll();
+ }
+
+ @Test
+ public void testUpdateHostsInDB() throws AmbariException {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final HostDAO mockHostDAO = easyMockSupport.createNiceMock(HostDAO.class);
+ final ClusterDAO mockClusterDAO = easyMockSupport.createNiceMock(ClusterDAO.class);
+ final EntityManager entityManager = createNiceMock(EntityManager.class);
+ final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
+ ClusterEntity mockClusterEntity = easyMockSupport.createNiceMock(ClusterEntity.class);
+ HostEntity mockHostEntity1 = easyMockSupport.createNiceMock(HostEntity.class);
+ HostEntity mockHostEntity2 = easyMockSupport.createNiceMock(HostEntity.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+ List<HostEntity> hostEntities = new ArrayList<>();
+ Map<String, String> hosts = new HashMap<>();
+
+ hosts.put("host1","host10");
+ hosts.put("host2","host11");
+
+ clusterHostsToChange.put("cl1", hosts);
+
+ hostEntities.add(mockHostEntity1);
+ hostEntities.add(mockHostEntity2);
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(DBAccessor.class).toInstance(dbAccessor);
+ bind(EntityManager.class).toInstance(entityManager);
+ bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+ bind(ClusterDAO.class).toInstance(mockClusterDAO);
+ bind(HostDAO.class).toInstance(mockHostDAO);
+ }
+ });
+
+ expect(mockClusterDAO.findByName("cl1")).andReturn(mockClusterEntity).once();
+ expect(mockClusterEntity.getHostEntities()).andReturn(hostEntities).once();
+ expect(mockHostEntity1.getHostName()).andReturn("host1").atLeastOnce();
+ expect(mockHostEntity2.getHostName()).andReturn("host2").atLeastOnce();
+
+ mockHostEntity1.setHostName("host10");
+ expectLastCall();
+
+ mockHostEntity2.setHostName("host11");
+ expectLastCall();
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ hostUpdateHelper.updateHostsInDB();
+ easyMockSupport.verifyAll();
+ }
+
+ @Test
+ public void testUpdateHostsForAlertsInDB() throws AmbariException {
+ EasyMockSupport easyMockSupport = new EasyMockSupport();
+ final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
+ final AlertsDAO mockAlertsDAO = easyMockSupport.createNiceMock(AlertsDAO.class);
+ final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class);
+ final AlertDispatchDAO mockAlertDispatchDAO = easyMockSupport.createNiceMock(AlertDispatchDAO.class);
+ final EntityManager entityManager = createNiceMock(EntityManager.class);
+ final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
+ final ClusterDAO mockClusterDAO = easyMockSupport.createNiceMock(ClusterDAO.class);
+ final Clusters mockClusters = easyMockSupport.createNiceMock(Clusters.class);
+ Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class);
+ AlertCurrentEntity mockAlertCurrentEntity1 = easyMockSupport.createNiceMock(AlertCurrentEntity.class);
+ AlertCurrentEntity mockAlertCurrentEntity2 = easyMockSupport.createNiceMock(AlertCurrentEntity.class);
+ AlertHistoryEntity mockAlertHistoryEntity1 = easyMockSupport.createNiceMock(AlertHistoryEntity.class);
+ AlertHistoryEntity mockAlertHistoryEntity2 = easyMockSupport.createNiceMock(AlertHistoryEntity.class);
+ AlertDefinitionEntity mockAlertDefinitionEntity = easyMockSupport.createNiceMock(AlertDefinitionEntity.class);
+ Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>();
+ List<AlertCurrentEntity> alertCurrentEntities = new ArrayList<>();
+ List<AlertDefinitionEntity> alertDefinitionEntities = new ArrayList<>();
+ Map<String, Cluster> clusterMap = new HashMap<>();
+ Map<String, String> hosts = new HashMap<>();
+
+ hosts.put("host1","host10");
+ hosts.put("host2","host11");
+
+ clusterHostsToChange.put("cl1", hosts);
+
+ clusterMap.put("cl1", mockCluster);
+
+ alertCurrentEntities.add(mockAlertCurrentEntity1);
+ alertCurrentEntities.add(mockAlertCurrentEntity2);
+
+ alertDefinitionEntities.add(mockAlertDefinitionEntity);
+
+ final Injector mockInjector = Guice.createInjector(new AbstractModule() {
+ @Override
+ protected void configure() {
+ bind(DBAccessor.class).toInstance(dbAccessor);
+ bind(EntityManager.class).toInstance(entityManager);
+ bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
+ bind(ClusterDAO.class).toInstance(mockClusterDAO);
+ bind(Clusters.class).toInstance(mockClusters);
+ bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
+ bind(AlertDispatchDAO.class).toInstance(mockAlertDispatchDAO);
+ bind(AlertsDAO.class).toInstance(mockAlertsDAO);
+ bind(AlertDefinitionDAO.class).toInstance(mockAlertDefinitionDAO);
+ }
+ });
+
+ expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
+ expect(mockClusters.getClusters()).andReturn(clusterMap).once();
+ expect(mockCluster.getClusterId()).andReturn(1L).once();
+ expect(mockAlertsDAO.findCurrentByCluster(1L)).andReturn(alertCurrentEntities).once();
+ expect(mockAlertCurrentEntity1.getAlertHistory()).andReturn(mockAlertHistoryEntity1).once();
+ expect(mockAlertCurrentEntity2.getAlertHistory()).andReturn(mockAlertHistoryEntity2).once();
+ expect(mockAlertHistoryEntity1.getHostName()).andReturn("host1").atLeastOnce();
+ expect(mockAlertHistoryEntity2.getHostName()).andReturn("host2").atLeastOnce();
+ expect(mockAlertDefinitionDAO.findAll(1L)).andReturn(alertDefinitionEntities).once();
+
+ mockAlertHistoryEntity1.setHostName("host10");
+ expectLastCall();
+
+ mockAlertHistoryEntity2.setHostName("host11");
+ expectLastCall();
+
+ mockAlertDefinitionEntity.setHash(anyString());
+ expectLastCall();
+
+ HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector);
+
+ hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange);
+
+ easyMockSupport.replayAll();
+ hostUpdateHelper.updateHostsForAlertsInDB();
+ easyMockSupport.verifyAll();
+ }
+
+
+}
+
http://git-wip-us.apache.org/repos/asf/ambari/blob/17ad5abd/ambari-server/src/test/python/TestAmbariServer.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestAmbariServer.py b/ambari-server/src/test/python/TestAmbariServer.py
index 034aed6..ebbd7de 100644
--- a/ambari-server/src/test/python/TestAmbariServer.py
+++ b/ambari-server/src/test/python/TestAmbariServer.py
@@ -62,7 +62,7 @@ with patch("platform.linux_distribution", return_value = os_distro_value):
from ambari_server.dbConfiguration_linux import PGConfig, LinuxDBMSConfig, OracleConfig
from ambari_server.properties import Properties
from ambari_server.resourceFilesKeeper import ResourceFilesKeeper, KeeperException
- from ambari_server.serverConfiguration import configDefaults, \
+ from ambari_server.serverConfiguration import configDefaults, get_java_exe_path, \
check_database_name_property, OS_FAMILY_PROPERTY, \
find_properties_file, get_ambari_properties, get_JAVA_HOME, \
parse_properties_file, read_ambari_user, update_ambari_properties, update_properties_2, write_property, find_jdk, \
@@ -93,6 +93,7 @@ with patch("platform.linux_distribution", return_value = os_distro_value):
read_password
from ambari_server_main import get_ulimit_open_files, ULIMIT_OPEN_FILES_KEY, ULIMIT_OPEN_FILES_DEFAULT
from ambari_server.serverClassPath import ServerClassPath
+ from ambari_server.hostUpdate import update_host_names
CURR_AMBARI_VERSION = "2.0.0"
@@ -6767,3 +6768,49 @@ MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV
self.assertRaises(FatalException, change_objects_owner, args)
print_error_msg_mock.assert_called_once_with("stderr:\nstderr")
pass
+
+
+ @patch.object(ServerClassPath, "get_full_ambari_classpath_escaped_for_shell", new = MagicMock(return_value = 'test' + os.pathsep + 'path12'))
+ @patch("ambari_commons.os_utils.run_os_command")
+ @patch("ambari_server.setupSecurity.generate_env")
+ @patch("ambari_server.setupSecurity.ensure_can_start_under_current_user")
+ @patch("ambari_server.serverConfiguration.read_ambari_user")
+ @patch("ambari_server.dbConfiguration.ensure_jdbc_driver_is_installed")
+ @patch("ambari_server.serverConfiguration.parse_properties_file")
+ @patch("ambari_server.serverConfiguration.get_ambari_properties")
+ @patch("ambari_server.serverConfiguration.get_java_exe_path")
+ @patch("os.access")
+ @patch("os.path.isfile")
+ @patch("sys.exit")
+ @patch("ambari_server.userInput.get_YN_input")
+ def test_update_host_names(self, getYNInput_mock, sysExitMock, isFileMock, osAccessMock, getJavaExePathMock,
+ getAmbariPropertiesMock, parsePropertiesFileMock, ensureDriverInstalledMock, readAmbariUserMock,
+ ensureCanStartUnderCurrentUserMock, generateEnvMock, runOSCommandMock):
+ properties = Properties()
+ properties.process_pair("server.jdbc.database", "embedded")
+
+ getYNInput_mock.return_value = False
+ isFileMock.return_value = True
+ osAccessMock.return_value = True
+ getJavaExePathMock.return_value = "/path/to/java"
+ getAmbariPropertiesMock.return_value = properties
+ readAmbariUserMock.return_value = "test_user"
+ ensureCanStartUnderCurrentUserMock.return_value = "test_user"
+ generateEnvMock.return_value = {}
+ runOSCommandMock.return_value = (0, "", "")
+
+ update_host_names(["update-host-names", "/testFileWithChanges"], properties)
+
+ self.assertEquals(len(sysExitMock.call_args_list), 3)
+ self.assertTrue(isFileMock.called)
+ self.assertTrue(osAccessMock.called)
+ self.assertTrue(getJavaExePathMock.called)
+ self.assertTrue(readAmbariUserMock.called)
+ self.assertTrue(ensureCanStartUnderCurrentUserMock.called)
+ self.assertTrue(generateEnvMock.called)
+
+ self.assertEquals(runOSCommandMock.call_args[0][0], '/path/to/java -cp test:path12 '
+ 'org.apache.ambari.server.update.HostUpdateHelper /testFileWithChanges > '
+ '/var/log/ambari-server/ambari-server.out 2>&1')
+
+