Best Python code snippet using lemoncheesecake
TestAmbariServer.py
Source:TestAmbariServer.py
1'''2Licensed to the Apache Software Foundation (ASF) under one3or more contributor license agreements. See the NOTICE file4distributed with this work for additional information5regarding copyright ownership. The ASF licenses this file6to you under the Apache License, Version 2.0 (the7"License"); you may not use this file except in compliance8with the License. You may obtain a copy of the License at9 http://www.apache.org/licenses/LICENSE-2.010Unless required by applicable law or agreed to in writing, software11distributed under the License is distributed on an "AS IS" BASIS,12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13See the License for the specific language governing permissions and14limitations under the License.15'''16from stacks.utils.RMFTestCase import *17import sys18import os19import datetime20import errno21import json22from mock.mock import patch, MagicMock, create_autospec, call23import operator24from optparse import OptionParser25import platform26import re27import shutil28import signal29import stat30import StringIO31import tempfile32from unittest import TestCase33from only_for_platform import get_platform, not_for_platform, only_for_platform, os_distro_value, PLATFORM_LINUX, PLATFORM_WINDOWS34if get_platform() != PLATFORM_WINDOWS:35 from pwd import getpwnam36# We have to use this import HACK because the filename contains a dash37with patch("platform.linux_distribution", return_value = os_distro_value):38 with patch("os.symlink"):39 with patch("__builtin__.open"):40 with patch("glob.glob", return_value = ['/etc/init.d/postgresql-9.3']):41 _ambari_server_ = __import__('ambari-server')42 from ambari_commons.firewall import Firewall43 from ambari_commons.os_check import OSCheck, OSConst44 from ambari_commons.os_family_impl import OsFamilyImpl, OsFamilyFuncImpl45 from ambari_commons.exceptions import FatalException, NonFatalException46 from ambari_commons.logging_utils import get_verbose, set_verbose, get_silent, set_silent, get_debug_mode, \47 print_info_msg, print_warning_msg, print_error_msg48 from ambari_commons.os_utils import run_os_command, search_file, set_file_permissions, remove_file, copy_file, \49 is_valid_filepath50 from ambari_server.dbConfiguration import DBMSConfigFactory, check_jdbc_drivers51 from ambari_server.dbConfiguration_linux import PGConfig, LinuxDBMSConfig, OracleConfig52 from ambari_server.properties import Properties53 from ambari_server.resourceFilesKeeper import ResourceFilesKeeper, KeeperException54 from ambari_server.serverConfiguration import configDefaults, \55 check_database_name_property, OS_FAMILY_PROPERTY, \56 find_properties_file, get_ambari_classpath, get_ambari_jars, get_ambari_properties, get_JAVA_HOME, get_share_jars, \57 parse_properties_file, read_ambari_user, update_ambari_properties, update_properties_2, write_property, find_jdk, \58 AMBARI_CONF_VAR, AMBARI_SERVER_LIB, JDBC_DATABASE_PROPERTY, JDBC_RCA_PASSWORD_FILE_PROPERTY, \59 PERSISTENCE_TYPE_PROPERTY, JDBC_URL_PROPERTY, get_conf_dir, JDBC_USER_NAME_PROPERTY, JDBC_PASSWORD_PROPERTY, \60 JDBC_DATABASE_NAME_PROPERTY, OS_TYPE_PROPERTY, validate_jdk, JDBC_POSTGRES_SCHEMA_PROPERTY, \61 RESOURCES_DIR_PROPERTY, JDBC_RCA_PASSWORD_ALIAS, JDBC_RCA_SCHEMA_PROPERTY, IS_LDAP_CONFIGURED, \62 SSL_API, SSL_API_PORT, CLIENT_API_PORT_PROPERTY,\63 JDBC_CONNECTION_POOL_TYPE, JDBC_CONNECTION_POOL_ACQUISITION_SIZE, \64 JDBC_CONNECTION_POOL_IDLE_TEST_INTERVAL, JDBC_CONNECTION_POOL_MAX_AGE, JDBC_CONNECTION_POOL_MAX_IDLE_TIME, \65 JDBC_CONNECTION_POOL_MAX_IDLE_TIME_EXCESS,\66 LDAP_MGR_PASSWORD_PROPERTY, LDAP_MGR_PASSWORD_ALIAS, JDBC_PASSWORD_FILENAME, NR_USER_PROPERTY, SECURITY_KEY_IS_PERSISTED, \67 SSL_TRUSTSTORE_PASSWORD_PROPERTY, SECURITY_IS_ENCRYPTION_ENABLED, SSL_TRUSTSTORE_PASSWORD_ALIAS, \68 SECURITY_MASTER_KEY_LOCATION, SECURITY_KEYS_DIR, LDAP_PRIMARY_URL_PROPERTY, store_password_file, \69 get_pass_file_path, GET_FQDN_SERVICE_URL, JDBC_USE_INTEGRATED_AUTH_PROPERTY, SECURITY_KEY_ENV_VAR_NAME, \70 JAVA_HOME_PROPERTY, JDK_NAME_PROPERTY, JCE_NAME_PROPERTY71 from ambari_server.serverUtils import is_server_runing, refresh_stack_hash72 from ambari_server.serverSetup import check_selinux, check_ambari_user, proceedJDBCProperties, SE_STATUS_DISABLED, SE_MODE_ENFORCING, configure_os_settings, \73 download_and_install_jdk, prompt_db_properties, setup, \74 AmbariUserChecks, AmbariUserChecksLinux, AmbariUserChecksWindows, JDKSetup, reset, setup_jce_policy, expand_jce_zip_file75 from ambari_server.serverUpgrade import upgrade, upgrade_local_repo, change_objects_owner, upgrade_stack, \76 run_stack_upgrade, run_metainfo_upgrade, run_schema_upgrade, move_user_custom_actions77 from ambari_server.setupHttps import is_valid_https_port, setup_https, import_cert_and_key_action, get_fqdn, \78 generate_random_string, get_cert_info, COMMON_NAME_ATTR, is_valid_cert_exp, NOT_AFTER_ATTR, NOT_BEFORE_ATTR, \79 SSL_DATE_FORMAT, import_cert_and_key, is_valid_cert_host, setup_truststore, \80 SRVR_ONE_WAY_SSL_PORT_PROPERTY, SRVR_TWO_WAY_SSL_PORT_PROPERTY, GANGLIA_HTTPS81 from ambari_server.setupSecurity import adjust_directory_permissions, get_alias_string, get_ldap_event_spec_names, sync_ldap, LdapSyncOptions, \82 configure_ldap_password, setup_ldap, REGEX_HOSTNAME_PORT, REGEX_TRUE_FALSE, REGEX_ANYTHING, setup_master_key, \83 setup_ambari_krb5_jaas, ensure_can_start_under_current_user, generate_env84 from ambari_server.userInput import get_YN_input, get_choice_string_input, get_validated_string_input, \85 read_password86 from ambari_server_main import get_ulimit_open_files, ULIMIT_OPEN_FILES_KEY, ULIMIT_OPEN_FILES_DEFAULT87CURR_AMBARI_VERSION = "2.0.0"88@patch("ambari_server.dbConfiguration_linux.get_postgre_hba_dir", new = MagicMock(return_value = "/var/lib/pgsql/data"))89@patch("ambari_server.dbConfiguration_linux.get_postgre_running_status", new = MagicMock(return_value = "running"))90class TestAmbariServer(TestCase):91 def setUp(self):92 out = StringIO.StringIO()93 sys.stdout = out94 def tearDown(self):95 sys.stdout = sys.__stdout__96 @not_for_platform(PLATFORM_WINDOWS)97 @patch("ambari_server.dbConfiguration_linux.run_os_command")98 def test_configure_pg_hba_ambaridb_users(self, run_os_command_method):99 # Prepare mocks100 run_os_command_method.return_value = (0, "", "")101 database_username = "ffdf"102 tf1 = tempfile.NamedTemporaryFile()103 # Run test104 PGConfig._configure_pg_hba_ambaridb_users(tf1.name, database_username)105 # Check results106 self.assertTrue(run_os_command_method.called)107 string_expected = self.get_file_string(self.get_samples_dir("configure_pg_hba_ambaridb_users1"))108 string_actual = self.get_file_string(tf1.name)109 self.assertEquals(string_expected, string_actual)110 pass111 @not_for_platform(PLATFORM_WINDOWS)112 @patch("__builtin__.raw_input")113 def test_servicename_regex(self, raw_input_method):114 ''' Test to make sure the service name can contain digits '''115 set_silent(False)116 raw_input_method.return_value = "OT100"117 result = OracleConfig._get_validated_service_name("ambari", 1)118 self.assertEqual("OT100", result, "Not accepting digits")119 pass120 @not_for_platform(PLATFORM_WINDOWS)121 @patch("__builtin__.raw_input")122 def test_dbname_regex(self, raw_input_method):123 ''' Test to make sure the service name can contain digits '''124 set_silent(False)125 raw_input_method.return_value = "OT100"126 result = LinuxDBMSConfig._get_validated_db_name("Database", "ambari")127 self.assertEqual("OT100", result, "Not accepting digits")128 pass129 @not_for_platform(PLATFORM_WINDOWS)130 def test_configure_pg_hba_postgres_user(self):131 tf1 = tempfile.NamedTemporaryFile()132 PGConfig.PG_HBA_CONF_FILE = tf1.name133 with open(PGConfig.PG_HBA_CONF_FILE, 'w') as fout:134 fout.write("\n")135 fout.write("local all all md5\n")136 fout.write("host all all 0.0.0.0/0 md5\n")137 fout.write("host all all ::/0 md5\n")138 PGConfig._configure_pg_hba_postgres_user()139 expected = self.get_file_string(self.get_samples_dir(140 "configure_pg_hba_ambaridb_users2"))141 result = self.get_file_string(PGConfig.PG_HBA_CONF_FILE)142 self.assertEqual(expected, result, "pg_hba_conf not processed")143 mode = oct(os.stat(PGConfig.PG_HBA_CONF_FILE)[stat.ST_MODE])144 str_mode = str(mode)[-4:]145 self.assertEqual("0644", str_mode, "Wrong file permissions")146 pass147 @patch("__builtin__.raw_input")148 def test_get_choice_string_input(self, raw_input_method):149 prompt = "blablabla"150 default = "default blablabla"151 firstChoice = set(['yes', 'ye', 'y'])152 secondChoice = set(['no', 'n'])153 # test first input154 raw_input_method.return_value = "Y"155 result = get_choice_string_input(prompt, default,156 firstChoice, secondChoice)157 self.assertEquals(result, True)158 raw_input_method.reset_mock()159 # test second input160 raw_input_method.return_value = "N"161 result = get_choice_string_input(prompt, default,162 firstChoice, secondChoice)163 self.assertEquals(result, False)164 raw_input_method.reset_mock()165 # test enter pressed166 raw_input_method.return_value = ""167 result = get_choice_string_input(prompt, default,168 firstChoice, secondChoice)169 self.assertEquals(result, default)170 raw_input_method.reset_mock()171 # test wrong input172 list_of_return_values = ['yes', 'dsad', 'fdsfds']173 def side_effect(list):174 return list_of_return_values.pop()175 raw_input_method.side_effect = side_effect176 result = get_choice_string_input(prompt, default,177 firstChoice, secondChoice)178 self.assertEquals(result, True)179 self.assertEquals(raw_input_method.call_count, 3)180 pass181 @patch("re.search")182 @patch("__builtin__.raw_input")183 @patch("getpass.getpass")184 def test_get_validated_string_input(self, get_pass_method,185 raw_input_method, re_search_method):186 prompt = "blabla"187 default = "default_pass"188 pattern = "pattern_pp"189 description = "blabla2"190 # check password input191 self.assertFalse(False, get_silent())192 is_pass = True193 get_pass_method.return_value = "dfdsfdsfds"194 result = get_validated_string_input(prompt, default,195 pattern, description, is_pass)196 self.assertEquals(get_pass_method.return_value, result)197 get_pass_method.assure_called_once(prompt)198 self.assertFalse(raw_input_method.called)199 # check raw input200 get_pass_method.reset_mock()201 raw_input_method.reset_mock()202 is_pass = False203 raw_input_method.return_value = "dkf90ewuf0"204 result = get_validated_string_input(prompt, default,205 pattern, description, is_pass)206 self.assertEquals(raw_input_method.return_value, result)207 self.assertFalse(get_pass_method.called)208 raw_input_method.assure_called_once(prompt)209 pass210 @not_for_platform(PLATFORM_WINDOWS)211 def test_get_pass_file_path(self):212 result = get_pass_file_path("/etc/ambari/conf_file", JDBC_PASSWORD_FILENAME)213 self.assertEquals("/etc/ambari/password.dat", result)214 pass215 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))216 @patch.object(_ambari_server_, "setup_security")217 @patch("optparse.OptionParser")218 def test_main_test_setup_security(self, OptionParserMock,219 setup_security_method):220 opm = OptionParserMock.return_value221 options = MagicMock()222 args = ["setup-security"]223 opm.parse_args.return_value = (options, args)224 options.dbms = None225 options.sid_or_sname = "sid"226 setup_security_method.return_value = None227 _ambari_server_.mainBody()228 _ambari_server_.mainBody()229 self.assertTrue(setup_security_method.called)230 self.assertFalse(False, get_verbose())231 self.assertFalse(False, get_silent())232 pass233 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))234 @patch.object(_ambari_server_, "setup_ambari_krb5_jaas")235 @patch.object(_ambari_server_, "setup_master_key")236 @patch.object(_ambari_server_, "setup_truststore")237 @patch.object(_ambari_server_, "setup_https")238 @patch.object(_ambari_server_, "get_validated_string_input")239 def test_setup_security(self, get_validated_string_input_mock, setup_https_mock,240 setup_truststore_mock, setup_master_key_mock,241 setup_ambari_krb5_jaas_mock):242 args = {}243 get_validated_string_input_mock.return_value = '1'244 _ambari_server_.setup_security(args)245 self.assertTrue(setup_https_mock.called)246 get_validated_string_input_mock.return_value = '2'247 _ambari_server_.setup_security(args)248 self.assertTrue(setup_master_key_mock.called)249 get_validated_string_input_mock.return_value = '3'250 _ambari_server_.setup_security(args)251 self.assertTrue(setup_ambari_krb5_jaas_mock.called)252 get_validated_string_input_mock.return_value = '4'253 _ambari_server_.setup_security(args)254 self.assertTrue(setup_truststore_mock.called)255 get_validated_string_input_mock.return_value = '5'256 _ambari_server_.setup_security(args)257 self.assertTrue(setup_truststore_mock.called)258 pass259 @patch("re.sub")260 @patch("fileinput.FileInput")261 @patch("ambari_server.setupSecurity.get_validated_string_input")262 @patch("ambari_server.setupSecurity.search_file")263 @patch("os.path.exists")264 def test_setup_ambari_krb5_jaas(self, exists_mock, search_mock,265 get_validated_string_input_mock,266 fileinput_mock, re_sub_mock):267 search_mock.return_value = 'filepath'268 exists_mock.return_value = False269 # Negative case270 try:271 setup_ambari_krb5_jaas()272 self.fail("Should throw exception")273 except NonFatalException as fe:274 # Expected275 self.assertTrue("No jaas config file found at location" in fe.reason)276 pass277 # Positive case278 exists_mock.reset_mock()279 exists_mock.return_value = True280 get_validated_string_input_mock.side_effect = ['aaa@aaa.cnn',281 'pathtokeytab']282 fileinput_mock.return_value = [ 'keyTab=xyz', 'principal=xyz' ]283 setup_ambari_krb5_jaas()284 self.assertTrue(fileinput_mock.called)285 self.assertTrue(re_sub_mock.called)286 self.assertTrue(re_sub_mock.call_args_list, [('aaa@aaa.cnn'),287 ('pathtokeytab')])288 pass289 @patch("sys.exit")290 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))291 @patch.object(_ambari_server_, "setup")292 @patch.object(_ambari_server_, "start")293 @patch.object(_ambari_server_, "stop")294 @patch.object(_ambari_server_, "reset")295 @patch("optparse.OptionParser")296 def test_main_test_setup(self, OptionParserMock, reset_method, stop_method,297 start_method, setup_method, exit_mock):298 opm = OptionParserMock.return_value299 options = MagicMock()300 args = ["setup"]301 opm.parse_args.return_value = (options, args)302 options.dbms = None303 options.sid_or_sname = "sid"304 _ambari_server_.mainBody()305 self.assertTrue(setup_method.called)306 self.assertFalse(start_method.called)307 self.assertFalse(stop_method.called)308 self.assertFalse(reset_method.called)309 self.assertFalse(False, get_verbose())310 self.assertFalse(False, get_silent())311 setup_method.reset_mock()312 start_method.reset_mock()313 stop_method.reset_mock()314 reset_method.reset_mock()315 exit_mock.reset_mock()316 args = ["setup", "-v"]317 options = MagicMock()318 opm.parse_args.return_value = (options, args)319 options.dbms = None320 options.sid_or_sname = "sid"321 setup_method.side_effect = Exception("Unexpected error")322 try:323 _ambari_server_.mainBody()324 except Exception:325 self.assertTrue(True)326 self.assertTrue(setup_method.called)327 self.assertFalse(start_method.called)328 self.assertFalse(stop_method.called)329 self.assertFalse(reset_method.called)330 self.assertTrue(get_verbose())331 setup_method.reset_mock()332 start_method.reset_mock()333 stop_method.reset_mock()334 reset_method.reset_mock()335 exit_mock.reset_mock()336 args = ["setup"]337 options = MagicMock()338 opm.parse_args.return_value = (options, args)339 options.dbms = None340 options.sid_or_sname = "sid"341 options.verbose = False342 setup_method.side_effect = Exception("Unexpected error")343 _ambari_server_.mainBody()344 self.assertTrue(exit_mock.called)345 self.assertTrue(setup_method.called)346 self.assertFalse(start_method.called)347 self.assertFalse(stop_method.called)348 self.assertFalse(reset_method.called)349 self.assertFalse(get_verbose())350 pass351 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))352 @patch.object(_ambari_server_, "setup")353 @patch.object(_ambari_server_, "start")354 @patch.object(_ambari_server_, "stop")355 @patch.object(_ambari_server_, "reset")356 @patch("optparse.OptionParser")357 def test_main_test_start(self, optionParserMock, reset_method, stop_method,358 start_method, setup_method):359 opm = optionParserMock.return_value360 options = MagicMock()361 args = ["setup"]362 opm.parse_args.return_value = (options, args)363 options.dbms = None364 options.sid_or_sname = "sname"365 _ambari_server_.mainBody()366 self.assertTrue(setup_method.called)367 self.assertFalse(start_method.called)368 self.assertFalse(stop_method.called)369 self.assertFalse(reset_method.called)370 self.assertFalse(False, get_verbose())371 self.assertFalse(False, get_silent())372 pass373 @not_for_platform(PLATFORM_WINDOWS)374 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))375 @patch.object(_ambari_server_, "setup")376 @patch.object(_ambari_server_, "start")377 @patch.object(_ambari_server_, "stop")378 @patch.object(_ambari_server_, "reset")379 def test_main_test_start_debug_short(self, reset_method, stop_method,380 start_method, setup_method):381 temp_args = sys.argv382 try:383 sys.argv = ["ambari-server", "start", "-g"]384 _ambari_server_.mainBody()385 self.assertFalse(setup_method.called)386 self.assertTrue(start_method.called)387 self.assertFalse(stop_method.called)388 self.assertFalse(reset_method.called)389 self.assertTrue(get_debug_mode())390 finally:391 sys.argv = temp_args392 pass393 @only_for_platform(PLATFORM_WINDOWS)394 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))395 @patch.object(_ambari_server_, "setup")396 @patch.object(_ambari_server_, "start")397 @patch.object(_ambari_server_, "stop")398 @patch.object(_ambari_server_, "reset")399 def test_main_test_start_debug_short(self, reset_method, stop_method,400 start_method, setup_method):401 temp_args = sys.argv402 try:403 sys.argv = ["ambari-server", "pstart", "-g"]404 _ambari_server_.mainBody()405 self.assertFalse(setup_method.called)406 self.assertTrue(start_method.called)407 self.assertFalse(stop_method.called)408 self.assertFalse(reset_method.called)409 self.assertTrue(get_debug_mode())410 finally:411 sys.argv = temp_args412 pass413 @not_for_platform(PLATFORM_WINDOWS)414 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))415 @patch.object(_ambari_server_, "setup")416 @patch.object(_ambari_server_, "start")417 @patch.object(_ambari_server_, "stop")418 @patch.object(_ambari_server_, "reset")419 def test_main_test_start_debug_long(self, reset_method, stop_method,420 start_method, setup_method):421 temp_args = sys.argv422 try:423 sys.argv = ["ambari-server", "start", "--debug"]424 _ambari_server_.mainBody()425 self.assertFalse(setup_method.called)426 self.assertTrue(start_method.called)427 self.assertFalse(stop_method.called)428 self.assertFalse(reset_method.called)429 self.assertTrue(get_debug_mode())430 finally:431 sys.argv = temp_args432 pass433 @only_for_platform(PLATFORM_WINDOWS)434 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))435 @patch.object(_ambari_server_, "setup")436 @patch.object(_ambari_server_, "start")437 @patch.object(_ambari_server_, "stop")438 @patch.object(_ambari_server_, "reset")439 def test_main_test_start_debug_long(self, reset_method, stop_method,440 start_method, setup_method):441 temp_args = sys.argv442 try:443 sys.argv = ["ambari-server", "pstart", "--debug"]444 _ambari_server_.mainBody()445 self.assertFalse(setup_method.called)446 self.assertTrue(start_method.called)447 self.assertFalse(stop_method.called)448 self.assertFalse(reset_method.called)449 self.assertTrue(get_debug_mode())450 finally:451 sys.argv = temp_args452 pass453 #Backup is not yet supported on Windows454 @not_for_platform(PLATFORM_WINDOWS)455 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))456 @patch.object(_ambari_server_, "setup")457 @patch.object(_ambari_server_, "start")458 @patch.object(_ambari_server_, "stop")459 @patch.object(_ambari_server_, "reset")460 @patch.object(_ambari_server_, "backup")461 @patch.object(_ambari_server_, "restore")462 @patch("optparse.OptionParser")463 def test_main_test_backup(self, optionParserMock, restore_mock, backup_mock, reset_method, stop_method,464 start_method, setup_method):465 opm = optionParserMock.return_value466 options = MagicMock()467 args = ["backup"]468 opm.parse_args.return_value = (options, args)469 options.dbms = None470 options.sid_or_sname = "sname"471 _ambari_server_.mainBody()472 self.assertTrue(backup_mock.called)473 self.assertFalse(restore_mock.called)474 self.assertFalse(setup_method.called)475 self.assertFalse(start_method.called)476 self.assertFalse(stop_method.called)477 self.assertFalse(reset_method.called)478 self.assertFalse(False, get_verbose())479 self.assertFalse(False, get_silent())480 pass481 #Restore is not yet supported on Windows482 @not_for_platform(PLATFORM_WINDOWS)483 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))484 @patch.object(_ambari_server_, "setup")485 @patch.object(_ambari_server_, "start")486 @patch.object(_ambari_server_, "stop")487 @patch.object(_ambari_server_, "reset")488 @patch.object(_ambari_server_, "backup")489 @patch.object(_ambari_server_, "restore")490 @patch("optparse.OptionParser")491 def test_main_test_restore(self, optionParserMock, restore_mock, backup_mock, reset_method, stop_method,492 start_method, setup_method):493 opm = optionParserMock.return_value494 options = MagicMock()495 args = ["restore"]496 opm.parse_args.return_value = (options, args)497 options.dbms = None498 options.sid_or_sname = "sname"499 _ambari_server_.mainBody()500 self.assertTrue(restore_mock.called)501 self.assertFalse(backup_mock.called)502 self.assertFalse(setup_method.called)503 self.assertFalse(start_method.called)504 self.assertFalse(stop_method.called)505 self.assertFalse(reset_method.called)506 self.assertFalse(False, get_verbose())507 self.assertFalse(False, get_silent())508 pass509 @not_for_platform(PLATFORM_WINDOWS)510 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))511 @patch.object(_ambari_server_, "setup")512 @patch.object(_ambari_server_, "start")513 @patch.object(_ambari_server_, "is_server_runing")514 @patch.object(_ambari_server_, "reset")515 @patch("optparse.OptionParser")516 def test_main_test_stop(self, optionParserMock, reset_method, is_server_runing_method,517 start_method, setup_method):518 opm = optionParserMock.return_value519 options = MagicMock()520 del options.exit_message521 args = ["stop"]522 opm.parse_args.return_value = (options, args)523 is_server_runing_method.return_value = (False, None)524 options.dbms = None525 options.sid_or_sname = "sid"526 _ambari_server_.mainBody()527 self.assertFalse(setup_method.called)528 self.assertFalse(start_method.called)529 self.assertTrue(is_server_runing_method.called)530 self.assertFalse(reset_method.called)531 self.assertFalse(False, get_verbose())532 self.assertFalse(False, get_silent())533 self.assertTrue(options.exit_message is None)534 pass535 @only_for_platform(PLATFORM_WINDOWS)536 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))537 @patch.object(_ambari_server_, "setup")538 @patch.object(_ambari_server_, "start")539 @patch("os_windows.win32serviceutil.WaitForServiceStatus")540 @patch("os_windows.win32serviceutil.StopService")541 @patch("os_windows.win32serviceutil.StopServiceWithDeps")542 @patch.object(_ambari_server_, "reset")543 def test_main_test_stop(self, reset_method, service_stop_w_deps_method,544 service_stop_method, service_status_wait_method,545 start_method, setup_method):546 temp_args = sys.argv547 try:548 sys.argv = ["ambari-server", "stop"]549 _ambari_server_.mainBody()550 self.assertFalse(setup_method.called)551 self.assertFalse(start_method.called)552 self.assertTrue(service_stop_w_deps_method.called)553 self.assertTrue(service_status_wait_method.called)554 self.assertFalse(reset_method.called)555 self.assertFalse(False, get_verbose())556 self.assertFalse(False, get_silent())557 finally:558 sys.argv = temp_args559 pass560 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))561 @patch.object(_ambari_server_, "setup")562 @patch.object(_ambari_server_, "start")563 @patch.object(_ambari_server_, "stop")564 @patch.object(_ambari_server_, "reset")565 @patch("optparse.OptionParser")566 def test_main_test_reset(self, optionParserMock, reset_method, stop_method,567 start_method, setup_method):568 opm = optionParserMock.return_value569 options = MagicMock()570 args = ["reset"]571 opm.parse_args.return_value = (options, args)572 options.dbms = None573 options.sid_or_sname = "sid"574 _ambari_server_.mainBody()575 self.assertFalse(setup_method.called)576 self.assertFalse(start_method.called)577 self.assertFalse(stop_method.called)578 self.assertTrue(reset_method.called)579 self.assertFalse(False, get_verbose())580 self.assertFalse(False, get_silent())581 pass582 @not_for_platform(PLATFORM_WINDOWS)583 def test_configure_postgresql_conf(self):584 tf1 = tempfile.NamedTemporaryFile()585 PGConfig.POSTGRESQL_CONF_FILE = tf1.name586 with open(PGConfig.POSTGRESQL_CONF_FILE, 'w') as f:587 f.write("#listen_addresses = '127.0.0.1' #\n")588 f.write("#listen_addresses = '127.0.0.1'")589 PGConfig._configure_postgresql_conf()590 expected = self.get_file_string(self.get_samples_dir(591 "configure_postgresql_conf1"))592 result = self.get_file_string(PGConfig.POSTGRESQL_CONF_FILE)593 self.assertEqual(expected, result, "postgresql.conf not updated")594 mode = oct(os.stat(PGConfig.POSTGRESQL_CONF_FILE)[stat.ST_MODE])595 str_mode = str(mode)[-4:]596 self.assertEqual("0644", str_mode, "Wrong file permissions")597 pass598 @not_for_platform(PLATFORM_WINDOWS)599 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))600 @patch.object(PGConfig, "_restart_postgres")601 @patch.object(PGConfig, "_get_postgre_status")602 @patch.object(PGConfig, "_configure_postgresql_conf")603 @patch("ambari_server.dbConfiguration_linux.run_os_command")604 def test_configure_postgres(self,605 run_os_command_mock,606 configure_postgresql_conf_mock,607 get_postgre_status_mock,608 restart_postgres_mock):609 args = MagicMock()610 properties = Properties()611 args.database_index = 0612 del args.dbms613 del args.database_host614 del args.database_port615 del args.database_name616 del args.database_username617 del args.database_password618 del args.silent619 factory = DBMSConfigFactory()620 dbConfig = factory.create(args, properties)621 self.assertTrue(dbConfig.dbms, "postgres")622 self.assertTrue(dbConfig.persistence_type, "local")623 tf1 = tempfile.NamedTemporaryFile()624 tf2 = tempfile.NamedTemporaryFile()625 PGConfig.PG_HBA_CONF_FILE = tf1.name626 PGConfig.PG_HBA_CONF_FILE_BACKUP = tf2.name627 out = StringIO.StringIO()628 sys.stdout = out629 retcode, out1, err = dbConfig._configure_postgres()630 sys.stdout = sys.__stdout__631 self.assertEqual(0, retcode)632 self.assertEqual("Backup for pg_hba found, reconfiguration not required\n",633 out.getvalue())634 tf2.close()635 get_postgre_status_mock.return_value = PGConfig.PG_STATUS_RUNNING, 0, "", ""636 run_os_command_mock.return_value = 0, "", ""637 restart_postgres_mock.return_value = 0, "", ""638 rcode, out, err = dbConfig._configure_postgres()639 self.assertTrue(os.path.isfile(PGConfig.PG_HBA_CONF_FILE_BACKUP),640 "postgresql.conf backup not created")641 self.assertTrue(run_os_command_mock.called)642 mode = oct(os.stat(PGConfig.PG_HBA_CONF_FILE)[stat.ST_MODE])643 str_mode = str(mode)[-4:]644 self.assertEqual("0644", str_mode, "Wrong file permissions")645 self.assertTrue(configure_postgresql_conf_mock.called)646 self.assertEqual(0, rcode)647 os.unlink(PGConfig.PG_HBA_CONF_FILE_BACKUP)648 get_postgre_status_mock.return_value = "stopped", 0, "", ""649 rcode, out, err = dbConfig._configure_postgres()650 self.assertEqual(0, rcode)651 os.unlink(PGConfig.PG_HBA_CONF_FILE_BACKUP)652 sys.stdout = sys.__stdout__653 pass654 @not_for_platform(PLATFORM_WINDOWS)655 @patch("time.sleep")656 @patch("subprocess.Popen")657 @patch("ambari_server.dbConfiguration_linux.run_os_command")658 @patch.object(PGConfig, "_get_postgre_status")659 @patch("ambari_server.dbConfiguration_linux.print_info_msg")660 def test_restart_postgres(self, printInfoMsg_mock, get_postgre_status_mock,661 run_os_command_mock, popenMock, sleepMock):662 p = MagicMock()663 p.poll.return_value = 0664 popenMock.return_value = p665 retcode, out, err = PGConfig._restart_postgres()666 self.assertEqual(0, retcode)667 p.poll.return_value = None668 get_postgre_status_mock.return_value = "stopped", 0, "", ""669 run_os_command_mock.return_value = (1, None, None)670 retcode, out, err = PGConfig._restart_postgres()671 self.assertEqual(1, retcode)672 pass673 @not_for_platform(PLATFORM_WINDOWS)674 @patch("shlex.split")675 @patch("subprocess.Popen")676 @patch("ambari_commons.os_linux.print_info_msg")677 def test_run_os_command(self, printInfoMsg_mock, popenMock, splitMock):678 p = MagicMock()679 p.communicate.return_value = (None, None)680 p.returncode = 3681 popenMock.return_value = p682 # with list arg683 cmd = ["exec", "arg"]684 run_os_command(cmd)685 self.assertFalse(splitMock.called)686 # with str arg687 resp = run_os_command("runme")688 self.assertEqual(3, resp[0])689 self.assertTrue(splitMock.called)690 pass691 @only_for_platform(PLATFORM_WINDOWS)692 @patch("shlex.split")693 @patch("subprocess.Popen")694 @patch("ambari_commons.os_windows.print_info_msg")695 def test_run_os_command(self, printInfoMsg_mock, popenMock, splitMock):696 p = MagicMock()697 p.communicate.return_value = (None, None)698 p.returncode = 3699 popenMock.return_value = p700 # with list arg701 cmd = ["exec", "arg"]702 run_os_command(cmd)703 self.assertFalse(splitMock.called)704 # with str arg705 resp = run_os_command("runme")706 self.assertEqual(3, resp[0])707 self.assertTrue(splitMock.called)708 pass709 @not_for_platform(PLATFORM_WINDOWS)710 @patch("ambari_server.serverConfiguration.get_conf_dir")711 @patch("ambari_server.serverConfiguration.search_file")712 def test_write_property(self, search_file_mock, get_conf_dir_mock):713 expected_content = "key1=val1\n"714 tf1 = tempfile.NamedTemporaryFile()715 search_file_mock.return_value = tf1.name716 write_property("key1", "val1")717 result = tf1.read()718 self.assertTrue(expected_content in result)719 pass720 @only_for_platform(PLATFORM_WINDOWS)721 @patch("ambari_server.serverConfiguration.get_conf_dir")722 @patch("ambari_server.serverConfiguration.search_file")723 def test_write_property(self, search_file_mock, get_conf_dir_mock):724 expected_content = "key1=val1\n"725 tf1 = tempfile.NamedTemporaryFile("r+b", delete=False)726 search_file_mock.return_value = tf1.name727 tf1.close()728 write_property("key1", "val1")729 hf1 = open(tf1.name, "r")730 try:731 result = hf1.read()732 self.assertTrue(expected_content in result)733 finally:734 hf1.close()735 os.unlink(tf1.name)736 pass737 @not_for_platform(PLATFORM_WINDOWS)738 @patch("ambari_server.dbConfiguration.decrypt_password_for_alias")739 @patch("ambari_server.dbConfiguration_linux.run_os_command")740 def test_setup_db(self, run_os_command_mock,741 decrypt_password_for_alias_mock):742 args = MagicMock()743 del args.database_index744 del args.dbms745 del args.database_host746 del args.database_port747 del args.database_name748 del args.database_username749 del args.database_password750 properties = Properties()751 properties.process_pair(JDBC_PASSWORD_PROPERTY, get_alias_string("mypwdalias"))752 decrypt_password_for_alias_mock.return_value = "password"753 dbms = PGConfig(args, properties, "local")754 self.assertTrue(decrypt_password_for_alias_mock.called)755 run_os_command_mock.return_value = (0, None, None)756 result = dbms._setup_db()757 self.assertTrue(run_os_command_mock.called)758 self.assertEqual((0, None, None), result)759 pass760 @not_for_platform(PLATFORM_WINDOWS)761 @patch("ambari_server.dbConfiguration.decrypt_password_for_alias")762 @patch("time.sleep")763 @patch("ambari_server.dbConfiguration_linux.run_os_command")764 def test_setup_db_connect_attempts_fail(self, run_os_command_mock,765 sleep_mock, decrypt_password_for_alias_mock):766 args = MagicMock()767 del args.database_index768 del args.dbms769 del args.database_host770 del args.database_port771 del args.database_name772 del args.database_username773 del args.database_password774 properties = Properties()775 decrypt_password_for_alias_mock.return_value = "password"776 dbms = PGConfig(args, properties, "local")777 run_os_command_mock.side_effect = [(1, "error", "error"), (1, "error", "error"),778 (1, "error", "error")]779 result = dbms._setup_db()780 self.assertTrue(run_os_command_mock.called)781 self.assertEqual((1, 'error', 'error') , result)782 self.assertEqual(2, sleep_mock.call_count)783 pass784 @not_for_platform(PLATFORM_WINDOWS)785 @patch("ambari_server.dbConfiguration.decrypt_password_for_alias")786 @patch("time.sleep")787 @patch("ambari_server.dbConfiguration_linux.run_os_command")788 def test_setup_db_connect_attempts_success(self, run_os_command_mock,789 sleep_mock, decrypt_password_for_alias_mock):790 args = MagicMock()791 del args.database_index792 del args.dbms793 del args.database_host794 del args.database_port795 del args.database_name796 del args.database_username797 del args.database_password798 properties = Properties()799 decrypt_password_for_alias_mock.return_value = "password"800 dbms = PGConfig(args, properties, "local")801 run_os_command_mock.side_effect = [(1, "error", "error"), (0, None, None),802 (0, None, None)]803 result = dbms._setup_db()804 self.assertTrue(run_os_command_mock.called)805 self.assertEqual((0, None, None) , result)806 self.assertEqual(1, sleep_mock.call_count)807 pass808 @not_for_platform(PLATFORM_WINDOWS)809 @patch("ambari_server.serverSetup.get_YN_input")810 @patch("ambari_server.serverSetup.run_os_command")811 def test_check_selinux(self, run_os_command_mock, getYNInput_mock):812 run_os_command_mock.return_value = (0, SE_STATUS_DISABLED,813 None)814 rcode = check_selinux()815 self.assertEqual(0, rcode)816 getYNInput_mock.return_value = True817 run_os_command_mock.return_value = (0, "enabled "818 + SE_MODE_ENFORCING,819 None)820 rcode = check_selinux()821 self.assertEqual(0, rcode)822 self.assertTrue(run_os_command_mock.called)823 self.assertTrue(getYNInput_mock.called)824 pass825 @not_for_platform(PLATFORM_WINDOWS)826 @patch("ambari_server.serverConfiguration.print_info_msg")827 def test_get_ambari_jars(self, printInfoMsg_mock):828 env = "/ambari/jars"829 os.environ[AMBARI_SERVER_LIB] = env830 result = get_ambari_jars()831 self.assertEqual(env, result)832 del os.environ[AMBARI_SERVER_LIB]833 result = get_ambari_jars()834 self.assertEqual("/usr/lib/ambari-server", result)835 self.assertTrue(printInfoMsg_mock.called)836 pass837 @only_for_platform(PLATFORM_WINDOWS)838 @patch("ambari_server.serverConfiguration.print_info_msg")839 def test_get_ambari_jars(self, printInfoMsg_mock):840 env = "\\ambari\\jars"841 os.environ[AMBARI_SERVER_LIB] = env842 result = get_ambari_jars()843 self.assertEqual(env, result)844 del os.environ[AMBARI_SERVER_LIB]845 result = get_ambari_jars()846 self.assertEqual("lib", result)847 self.assertTrue(printInfoMsg_mock.called)848 pass849 @patch("glob.glob")850 @patch("ambari_server.serverConfiguration.print_info_msg")851 def test_get_share_jars(self, printInfoMsg_mock, globMock):852 globMock.return_value = ["one", "two"]853 expected = "one" + os.pathsep + "two" + os.pathsep + \854 "one" + os.pathsep + "two" + os.pathsep + \855 "one" + os.pathsep + "two"856 result = get_share_jars()857 self.assertEqual(expected, result)858 globMock.return_value = []859 expected = ""860 result = get_share_jars()861 self.assertEqual(expected, result)862 pass863 @patch("glob.glob")864 @patch("ambari_server.serverConfiguration.print_info_msg")865 @patch("ambari_server.serverConfiguration.get_ambari_properties")866 def test_get_ambari_classpath(self, get_ambari_properties_mock, printInfoMsg_mock, globMock):867 globMock.return_value = ["one"]868 result = get_ambari_classpath()869 self.assertTrue(get_ambari_jars() in result)870 self.assertTrue(get_share_jars() in result)871 globMock.return_value = []872 result = get_ambari_classpath()873 self.assertTrue(get_ambari_jars() in result)874 self.assertFalse(":" in result[2:])875 pass876 @not_for_platform(PLATFORM_WINDOWS)877 @patch("ambari_server.serverConfiguration.print_info_msg")878 def test_get_conf_dir(self, printInfoMsg_mock):879 env = "/dummy/ambari/conf"880 os.environ[AMBARI_CONF_VAR] = env881 result = get_conf_dir()882 self.assertEqual(env, result)883 del os.environ[AMBARI_CONF_VAR]884 result = get_conf_dir()885 self.assertEqual("/etc/ambari-server/conf", result)886 pass887 @only_for_platform(PLATFORM_WINDOWS)888 @patch("ambari_server.serverConfiguration.print_info_msg")889 def test_get_conf_dir(self, printInfoMsg_mock):890 env = "\\dummy\\ambari\\conf"891 os.environ[AMBARI_CONF_VAR] = env892 result = get_conf_dir()893 self.assertEqual(env, result)894 del os.environ[AMBARI_CONF_VAR]895 result = get_conf_dir()896 self.assertEqual("conf", result)897 pass898 def test_search_file(self):899 path = os.path.dirname(__file__)900 result = search_file(__file__, path)901 expected = os.path.abspath(__file__)902 self.assertEqual(expected, result)903 result = search_file("non_existent_file", path)904 self.assertEqual(None, result)905 pass906 @patch("ambari_server.serverConfiguration.search_file")907 def test_find_properties_file(self, search_file_mock):908 # Testing case when file is not found909 search_file_mock.return_value = None910 try:911 find_properties_file()912 self.fail("File not found'")913 except FatalException:914 # Expected915 pass916 self.assertTrue(search_file_mock.called)917 # Testing case when file is found918 value = MagicMock()919 search_file_mock.return_value = value920 result = find_properties_file()921 self.assertTrue(result is value)922 pass923 @patch("ambari_server.serverConfiguration.get_ambari_properties")924 @patch("ambari_server.serverConfiguration.Properties")925 def test_read_ambari_user(self, properties_mock, get_ambari_properties_mock):926 # Testing with defined user927 properties_mock.__getitem__.return_value = "dummy_user"928 get_ambari_properties_mock.return_value = properties_mock929 user = read_ambari_user()930 self.assertEquals(user, "dummy_user")931 # Testing with undefined user932 properties_mock.__getitem__.return_value = None933 user = read_ambari_user()934 self.assertEquals(user, None)935 pass936 @patch("ambari_server.setupSecurity.get_file_owner")937 @patch("ambari_server.setupSecurity.get_ambari_repo_file_full_name")938 @patch("os.path.exists")939 @patch("ambari_server.setupSecurity.set_file_permissions")940 @patch("ambari_server.setupSecurity.get_ambari_properties")941 @patch("ambari_server.setupSecurity.get_resources_location")942 @patch("ambari_server.setupSecurity.get_value_from_properties")943 @patch("os.mkdir")944 @patch("shutil.rmtree")945 @patch("ambari_commons.os_utils.print_info_msg")946 @patch("ambari_server.setupSecurity.change_owner")947 def test_adjust_directory_permissions(self, change_owner_mock, print_info_msg_mock, rmtree_mock, mkdir_mock,948 get_value_from_properties_mock, get_resources_location_mock,949 get_ambari_properties_mock, set_file_permissions_mock, exists_mock,950 get_ambari_repo_file_full_name_mock, get_file_owner_mock):951 # Testing boostrap dir wipe952 properties_mock = Properties()953 properties_mock.process_pair(JDK_NAME_PROPERTY, "dummy_jdk")954 properties_mock.process_pair(JCE_NAME_PROPERTY, "dummy_jce")955 properties_mock.process_pair(JAVA_HOME_PROPERTY, "dummy_java_home")956 get_ambari_properties_mock.return_value = properties_mock957 get_value_from_properties_mock.return_value = "dummy_bootstrap_dir"958 get_resources_location_mock.return_value = "dummy_resources_dir"959 exists_mock.return_value = False960 adjust_directory_permissions("user")961 self.assertEquals(rmtree_mock.call_args_list[0][0][0], os.path.join(os.getcwd(), "dummy_bootstrap_dir"))962 self.assertTrue(mkdir_mock.called)963 set_file_permissions_mock.reset_mock()964 change_owner_mock.reset_mock()965 # Test recursive calls966 old_adjust_owner_list = configDefaults.NR_ADJUST_OWNERSHIP_LIST967 old_change_owner_list = configDefaults.NR_CHANGE_OWNERSHIP_LIST968 try:969 configDefaults.NR_ADJUST_OWNERSHIP_LIST = [970 ( "/etc/ambari-server/conf", "755", "{0}", True ),971 ( "/etc/ambari-server/conf/ambari.properties", "644", "{0}", False )972 ]973 configDefaults.NR_CHANGE_OWNERSHIP_LIST = [974 ( "/etc/ambari-server", "{0}", True )975 ]976 adjust_directory_permissions("user")977 self.assertTrue(len(set_file_permissions_mock.call_args_list) ==978 len(configDefaults.NR_ADJUST_OWNERSHIP_LIST))979 self.assertEquals(set_file_permissions_mock.call_args_list[0][0][3], True)980 self.assertEquals(set_file_permissions_mock.call_args_list[1][0][3], False)981 self.assertTrue(len(change_owner_mock.call_args_list) ==982 len(configDefaults.NR_CHANGE_OWNERSHIP_LIST))983 self.assertEquals(change_owner_mock.call_args_list[0][0][2], True)984 finally:985 configDefaults.NR_ADJUST_OWNERSHIP_LIST = old_adjust_owner_list986 configDefaults.NR_CHANGE_OWNERSHIP_LIST = old_change_owner_list987 pass988 #989 # Test ambari repo file permission change call990 #991 # Reset the set_file_permissions() mock function992 set_file_permissions_mock.reset_mock()993 # Save the existing permissions list994 old_adjust_owner_list = configDefaults.NR_ADJUST_OWNERSHIP_LIST995 # Set up the mock function for os_utils.get_ambari_repo_file_full_name()996 get_ambari_repo_file_full_name_mock.return_value = "ambari.dummy.repo"997 # Set up the mock function for os_utils.get_file_owner()998 get_file_owner_mock.return_value = "dummy.root"999 try:1000 # Clear the list of files whose permissions are to be changed1001 configDefaults.NR_ADJUST_OWNERSHIP_LIST = [1002 ]1003 # Call the function to be tested.1004 adjust_directory_permissions("dummy_user")1005 # Assert that set_file_permissions() was called1006 self.assertTrue(set_file_permissions_mock.called)1007 # One of the entries in NR_ADJUST_OWNERSHIP_LIST should be the full path to the ambari repo file.1008 # These are the expected values:1009 ambari_repo_file_entry = (1010 get_ambari_repo_file_full_name_mock(),1011 '644',1012 get_file_owner_mock(),1013 False1014 )1015 # Assert the arguments to the call set_file_permissions() - got from NR_ADJUST_OWNERSHIP_LIST1016 # Flag to ensure we found our entry in the set_file_permissions() call1017 entry_found = False1018 for args_entry in set_file_permissions_mock.call_args_list:1019 if args_entry[0][0] == ambari_repo_file_entry[0]: # File name1020 # ambari repo file name matched; assert the rest of the entries1021 self.assertEquals(args_entry[0][1], ambari_repo_file_entry[1]) # Permissions1022 self.assertEquals(args_entry[0][2], ambari_repo_file_entry[2]) # File owner1023 self.assertEquals(args_entry[0][3], ambari_repo_file_entry[3]) # Non-recursive1024 entry_found = True1025 break1026 # Ensure that the ambari repo file entry was found1027 self.assertTrue(entry_found)1028 finally:1029 # Restore the permissions list1030 configDefaults.NR_ADJUST_OWNERSHIP_LIST = old_adjust_owner_list1031 pass1032 @not_for_platform(PLATFORM_WINDOWS)1033 @patch("os.path.exists")1034 @patch("ambari_commons.os_linux.os_run_os_command")1035 @patch("ambari_commons.os_linux.print_warning_msg")1036 @patch("ambari_commons.os_utils.print_info_msg")1037 def test_set_file_permissions(self, print_info_msg_mock, print_warning_msg_mock,1038 run_os_command_mock, exists_mock):1039 # Testing not existent file scenario1040 exists_mock.return_value = False1041 set_file_permissions("dummy-file", "dummy-mod",1042 "dummy-user", False)1043 self.assertFalse(run_os_command_mock.called)1044 self.assertTrue(print_info_msg_mock.called)1045 run_os_command_mock.reset_mock()1046 print_warning_msg_mock.reset_mock()1047 # Testing OK scenario1048 exists_mock.return_value = True1049 run_os_command_mock.side_effect = [(0, "", ""), (0, "", "")]1050 set_file_permissions("dummy-file", "dummy-mod",1051 "dummy-user", False)1052 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)1053 self.assertFalse(print_warning_msg_mock.called)1054 run_os_command_mock.reset_mock()1055 print_warning_msg_mock.reset_mock()1056 # Testing first command fail1057 run_os_command_mock.side_effect = [(1, "", ""), (0, "", "")]1058 set_file_permissions("dummy-file", "dummy-mod",1059 "dummy-user", False)1060 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)1061 self.assertTrue(print_warning_msg_mock.called)1062 run_os_command_mock.reset_mock()1063 print_warning_msg_mock.reset_mock()1064 # Testing second command fail1065 run_os_command_mock.side_effect = [(0, "", ""), (1, "", "")]1066 set_file_permissions("dummy-file", "dummy-mod",1067 "dummy-user", False)1068 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)1069 self.assertTrue(print_warning_msg_mock.called)1070 run_os_command_mock.reset_mock()1071 print_warning_msg_mock.reset_mock()1072 # Testing recursive operation1073 exists_mock.return_value = True1074 run_os_command_mock.side_effect = [(0, "", ""), (0, "", "")]1075 set_file_permissions("dummy-file", "dummy-mod",1076 "dummy-user", True)1077 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)1078 self.assertTrue("-R" in run_os_command_mock.call_args_list[0][0][0])1079 self.assertTrue("-R" in run_os_command_mock.call_args_list[1][0][0])1080 self.assertFalse(print_warning_msg_mock.called)1081 run_os_command_mock.reset_mock()1082 print_warning_msg_mock.reset_mock()1083 # Testing non-recursive operation1084 exists_mock.return_value = True1085 run_os_command_mock.side_effect = [(0, "", ""), (0, "", "")]1086 set_file_permissions("dummy-file", "dummy-mod",1087 "dummy-user", False)1088 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)1089 self.assertFalse("-R" in run_os_command_mock.call_args_list[0][0][0])1090 self.assertFalse("-R" in run_os_command_mock.call_args_list[1][0][0])1091 self.assertFalse(print_warning_msg_mock.called)1092 run_os_command_mock.reset_mock()1093 print_warning_msg_mock.reset_mock()1094 pass1095 @not_for_platform(PLATFORM_WINDOWS)1096 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))1097 @patch("ambari_server.serverSetup.get_validated_string_input")1098 @patch("ambari_server.serverSetup.print_info_msg")1099 @patch("ambari_server.serverSetup.print_warning_msg")1100 @patch("ambari_server.serverSetup.run_os_command")1101 def test_create_custom_user(self, run_os_command_mock, print_warning_msg_mock,1102 print_info_msg_mock, get_validated_string_input_mock):1103 options = MagicMock()1104 user = "dummy-user"1105 get_validated_string_input_mock.return_value = user1106 userChecks = AmbariUserChecks(options)1107 # Testing scenario: absent user1108 run_os_command_mock.side_effect = [(0, "", "")]1109 result = userChecks._create_custom_user()1110 self.assertFalse(print_warning_msg_mock.called)1111 self.assertEquals(result, 0)1112 self.assertEquals(userChecks.user, user)1113 print_info_msg_mock.reset_mock()1114 print_warning_msg_mock.reset_mock()1115 run_os_command_mock.reset_mock()1116 # Testing scenario: existing user1117 run_os_command_mock.side_effect = [(9, "", "")]1118 result = userChecks._create_custom_user()1119 self.assertTrue("User dummy-user already exists" in str(print_info_msg_mock.call_args_list[1][0]))1120 self.assertEquals(result, 0)1121 self.assertEquals(userChecks.user, user)1122 print_info_msg_mock.reset_mock()1123 print_warning_msg_mock.reset_mock()1124 run_os_command_mock.reset_mock()1125 # Testing scenario: os command fail1126 run_os_command_mock.side_effect = [(1, "", "")]1127 result = userChecks._create_custom_user()1128 self.assertTrue(print_warning_msg_mock.called)1129 self.assertEquals(result, 1)1130 pass1131 @only_for_platform(PLATFORM_WINDOWS)1132 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))1133 @patch("win32security.LsaAddAccountRights")1134 @patch("win32security.LookupAccountName")1135 @patch("win32net.NetUserAdd")1136 @patch("win32net.NetUserGetInfo")1137 @patch("win32security.LsaOpenPolicy")1138 @patch("win32net.NetGetDCName")1139 @patch("ambari_server.serverSetup.get_validated_string_input")1140 @patch("ambari_server.serverSetup.print_info_msg")1141 @patch("ambari_server.serverSetup.print_warning_msg")1142 def test_create_custom_user(self, print_warning_msg_mock,1143 print_info_msg_mock, get_validated_string_input_mock,1144 net_get_dc_name_mock, lsa_open_policy_mock,1145 net_user_get_info_mock, net_user_add_mock,1146 lookup_account_name_mock, lsa_add_account_rights_mock):1147 def _reset_mocks():1148 get_validated_string_input_mock.reset_mock()1149 print_info_msg_mock.reset_mock()1150 print_warning_msg_mock.reset_mock()1151 net_get_dc_name_mock.reset_mock()1152 net_user_get_info_mock.reset_mock()1153 net_user_add_mock.reset_mock()1154 lookup_account_name_mock.reset_mock()1155 lsa_add_account_rights_mock.reset_mock()1156 pass1157 options = MagicMock()1158 user = "dummy-user"1159 get_validated_string_input_mock.return_value = user1160 userChecks = AmbariUserChecks(options)1161 # Testing scenario: absent user1162 def user_not_found(*args, **keywargs):1163 import pywintypes1164 raise pywintypes.error(2221)1165 net_user_get_info_mock.side_effect = user_not_found1166 result = userChecks._create_custom_user()1167 self.assertTrue(print_warning_msg_mock.called)1168 self.assertTrue(net_user_add_mock.called)1169 self.assertEqual(str(net_user_add_mock.call_args_list[0][0]), str((None, 1, {'comment': 'Ambari user', 'password': 'dummy-user', 'flags': 513, 'name': 'dummy-user', 'priv': 1})))1170 self.assertEquals(result, 0)1171 self.assertEquals(userChecks.user, ".\\" + user)1172 _reset_mocks()1173 # Testing scenario: existing user1174 net_user_get_info_mock.side_effect = None1175 net_user_get_info_mock.return_value = { "name":"dummy_user" }1176 #lookup_account_name_mock1177 #lsa_add_account_rights_mock1178 result = userChecks._create_custom_user()1179 self.assertTrue("User dummy-user already exists" in print_info_msg_mock.call_args_list[0][0][0])1180 self.assertEquals(result, 0)1181 self.assertEquals(userChecks.user, ".\\" + user)1182 self.assertFalse(net_user_add_mock.called)1183 _reset_mocks()1184 # Testing scenario: new domain user1185 get_validated_string_input_mock.side_effect = ["dummy_domain\\dummy_user", "newpassword"]1186 net_get_dc_name_mock.return_value = "dummy_dc"1187 net_user_get_info_mock.side_effect = user_not_found1188 result = userChecks._create_custom_user()1189 self.assertTrue(net_get_dc_name_mock.called)1190 self.assertEqual(str(net_get_dc_name_mock.call_args_list[0][0]), str((None, "dummy_domain")))1191 self.assertTrue(net_user_add_mock.called)1192 self.assertEqual(str(net_user_add_mock.call_args_list[0][0]), str(('dummy_dc', 1, {'comment': 'Ambari user', 'password': 'newpassword', 'flags': 513, 'name': 'dummy_user', 'priv': 1})))1193 self.assertEquals(result, 0)1194 self.assertEquals(userChecks.user, "dummy_domain\\dummy_user")1195 _reset_mocks()1196 # Testing scenario: existing domain user1197 get_validated_string_input_mock.side_effect = ["dummy_domain\\dummy_user", "newpassword"]1198 net_user_get_info_mock.side_effect = None1199 net_user_get_info_mock.return_value = { "name":"dummy_domain\\dummy_user" }1200 result = userChecks._create_custom_user()1201 self.assertTrue("User dummy_domain\\dummy_user already exists" in print_info_msg_mock.call_args_list[0][0][0])1202 self.assertTrue(net_get_dc_name_mock.called)1203 self.assertEqual(str(net_get_dc_name_mock.call_args_list[0][0]), str((None, "dummy_domain")))1204 self.assertFalse(net_user_add_mock.called)1205 self.assertEquals(result, 0)1206 self.assertEquals(userChecks.user, "dummy_domain\\dummy_user")1207 pass1208 @not_for_platform(PLATFORM_WINDOWS)1209 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))1210 @patch("ambari_server.serverSetup.read_ambari_user")1211 @patch("ambari_server.serverSetup.get_YN_input")1212 @patch("ambari_server.serverSetup.get_validated_string_input")1213 @patch("ambari_server.serverSetup.adjust_directory_permissions")1214 @patch("ambari_server.serverSetup.run_os_command")1215 @patch("ambari_server.serverSetup.print_error_msg")1216 @patch("ambari_server.serverSetup.print_warning_msg")1217 @patch("ambari_server.serverSetup.print_info_msg")1218 def test_check_ambari_user(self, print_info_msg_mock, print_warning_msg_mock, print_error_msg_mock,1219 run_os_command_mock, adjust_directory_permissions_mock,1220 get_validated_string_input_mock, get_YN_input_mock, read_ambari_user_mock):1221 def _reset_mocks():1222 get_YN_input_mock.reset_mock()1223 get_validated_string_input_mock.reset_mock()1224 run_os_command_mock.reset_mock()1225 adjust_directory_permissions_mock.reset_mock()1226 pass1227 options = MagicMock()1228 run_os_command_mock.return_value = (0, "", "")1229 # Scenario: user is already defined, user does not want to reconfigure it1230 read_ambari_user_mock.return_value = "dummy-user"1231 get_YN_input_mock.return_value = False1232 result = check_ambari_user(options)1233 self.assertTrue(get_YN_input_mock.called)1234 self.assertFalse(get_validated_string_input_mock.called)1235 self.assertFalse(run_os_command_mock.called)1236 self.assertTrue(adjust_directory_permissions_mock.called)1237 self.assertEqual(result[0], 0)1238 _reset_mocks()1239 # Scenario: user is already defined, but user wants to reconfigure it1240 read_ambari_user_mock.return_value = "dummy-user"1241 get_validated_string_input_mock.return_value = "new-dummy-user"1242 get_YN_input_mock.return_value = True1243 result = check_ambari_user(options)1244 self.assertTrue(get_YN_input_mock.called)1245 self.assertTrue(result[2] == "new-dummy-user")1246 self.assertTrue(get_validated_string_input_mock.called)1247 self.assertTrue(adjust_directory_permissions_mock.called)1248 self.assertEqual(result[0], 0)1249 _reset_mocks()1250 # Negative scenario: user is already defined, but user wants1251 # to reconfigure it, user creation failed1252 read_ambari_user_mock.return_value = "dummy-user"1253 run_os_command_mock.return_value = (1, "", "")1254 get_YN_input_mock.return_value = True1255 result = check_ambari_user(options)1256 self.assertTrue(get_YN_input_mock.called)1257 self.assertTrue(get_validated_string_input_mock.called)1258 self.assertTrue(run_os_command_mock.called)1259 self.assertFalse(adjust_directory_permissions_mock.called)1260 self.assertEqual(result[0], 1)1261 _reset_mocks()1262 # Scenario: user is not defined (setup process)1263 read_ambari_user_mock.return_value = None1264 get_YN_input_mock.return_value = True1265 get_validated_string_input_mock.return_value = "dummy-user"1266 run_os_command_mock.return_value = (0, "", "")1267 result = check_ambari_user(options)1268 self.assertTrue(get_YN_input_mock.called)1269 self.assertTrue(get_validated_string_input_mock.called)1270 self.assertTrue(run_os_command_mock.called)1271 self.assertTrue(result[2] == "dummy-user")1272 self.assertTrue(adjust_directory_permissions_mock.called)1273 self.assertEqual(result[0], 0)1274 _reset_mocks()1275 # Scenario: user is not defined (setup process), user creation failed1276 read_ambari_user_mock.return_value = None1277 get_YN_input_mock.return_value = True1278 run_os_command_mock.return_value = (1, "", "")1279 result = check_ambari_user(options)1280 self.assertTrue(get_YN_input_mock.called)1281 self.assertTrue(get_validated_string_input_mock.called)1282 self.assertTrue(run_os_command_mock.called)1283 self.assertFalse(adjust_directory_permissions_mock.called)1284 self.assertEqual(result[0], 1)1285 _reset_mocks()1286 # negative scenario: user is not defined (setup process), user creation failed1287 read_ambari_user_mock.return_value = None1288 get_YN_input_mock.return_value = True1289 run_os_command_mock.return_value = (1, "", "")1290 result = check_ambari_user(options)1291 self.assertTrue(get_YN_input_mock.called)1292 self.assertTrue(get_validated_string_input_mock.called)1293 self.assertTrue(run_os_command_mock.called)1294 self.assertFalse(adjust_directory_permissions_mock.called)1295 self.assertEqual(result[0], 1)1296 _reset_mocks()1297 # Scenario: user is not defined and left to be root1298 read_ambari_user_mock.return_value = None1299 get_YN_input_mock.return_value = False1300 result = check_ambari_user(options)1301 self.assertTrue(get_YN_input_mock.called)1302 self.assertFalse(get_validated_string_input_mock.called)1303 self.assertFalse(run_os_command_mock.called)1304 self.assertTrue(result[2] == "root")1305 self.assertTrue(adjust_directory_permissions_mock.called)1306 self.assertEqual(result[0], 0)1307 pass1308 @only_for_platform(PLATFORM_WINDOWS)1309 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))1310 @patch("ambari_commons.os_windows.UserHelper.add_user_privilege")1311 @patch("ambari_commons.os_windows.UserHelper.create_user")1312 @patch("ambari_commons.os_windows.UserHelper.find_user")1313 @patch("ambari_server.serverSetup.read_ambari_user")1314 @patch("ambari_server.serverSetup.get_YN_input")1315 @patch("ambari_server.serverSetup.get_validated_string_input")1316 @patch("ambari_server.serverSetup.adjust_directory_permissions")1317 @patch("ambari_server.serverSetup.run_os_command")1318 @patch("ambari_server.serverSetup.print_error_msg")1319 @patch("ambari_server.serverSetup.print_warning_msg")1320 @patch("ambari_server.serverSetup.print_info_msg")1321 def test_check_ambari_user(self, print_info_msg_mock, print_warning_msg_mock, print_error_msg_mock,1322 run_os_command_mock, adjust_directory_permissions_mock,1323 get_validated_string_input_mock, get_YN_input_mock, read_ambari_user_mock,1324 find_user_mock, create_user_mock, add_user_privilege_mock):1325 def _reset_mocks():1326 get_YN_input_mock.reset_mock()1327 get_validated_string_input_mock.reset_mock()1328 find_user_mock.reset_mock()1329 create_user_mock.reset_mock()1330 adjust_directory_permissions_mock.reset_mock()1331 pass1332 options = MagicMock()1333 options.svc_user = None1334 options.svc_password = None1335 run_os_command_mock.return_value = (0, "", "")1336 # Scenario: user is already defined, user does not want to reconfigure it1337 read_ambari_user_mock.return_value = "dummy-user"1338 get_YN_input_mock.return_value = False1339 result = check_ambari_user(options)1340 self.assertTrue(get_YN_input_mock.called)1341 self.assertFalse(get_validated_string_input_mock.called)1342 self.assertFalse(find_user_mock.called)1343 self.assertFalse(create_user_mock.called)1344 self.assertTrue(adjust_directory_permissions_mock.called)1345 self.assertEqual(result[0], 0)1346 _reset_mocks()1347 # Scenario: user is already defined, but user wants to reconfigure it1348 read_ambari_user_mock.return_value = "dummy-user"1349 get_validated_string_input_mock.side_effect = ["new-dummy-user", "new_password"]1350 get_YN_input_mock.return_value = True1351 find_user_mock.return_value = False1352 create_user_mock.return_value = (0, "User created")1353 add_user_privilege_mock.return_value = (0, "Privilege added")1354 result = check_ambari_user(options)1355 self.assertTrue(get_YN_input_mock.called)1356 self.assertEqual(result[2], ".\\new-dummy-user")1357 self.assertTrue(get_validated_string_input_mock.called)1358 self.assertTrue(adjust_directory_permissions_mock.called)1359 self.assertTrue(find_user_mock.called)1360 self.assertTrue(create_user_mock.called)1361 self.assertTrue(add_user_privilege_mock.called)1362 self.assertEqual(result[0], 0)1363 _reset_mocks()1364 # Negative scenario: user is already defined, but user wants1365 # to reconfigure it, user creation failed1366 read_ambari_user_mock.return_value = "dummy-user"1367 get_validated_string_input_mock.side_effect = ["new-dummy-user", "new_password"]1368 find_user_mock.return_value = False1369 create_user_mock.return_value = (-1, "Failed")1370 get_YN_input_mock.return_value = True1371 result = check_ambari_user(options)1372 self.assertTrue(get_YN_input_mock.called)1373 self.assertTrue(get_validated_string_input_mock.called)1374 self.assertTrue(create_user_mock.called)1375 self.assertFalse(adjust_directory_permissions_mock.called)1376 self.assertEqual(result[0], -1)1377 _reset_mocks()1378 # Scenario: user is not defined (setup process)1379 read_ambari_user_mock.return_value = None1380 get_YN_input_mock.return_value = True1381 get_validated_string_input_mock.side_effect = ["dummy-user", "new_password"]1382 create_user_mock.return_value = (0, "User created")1383 result = check_ambari_user(options)1384 self.assertTrue(get_YN_input_mock.called)1385 self.assertTrue(get_validated_string_input_mock.called)1386 self.assertTrue(create_user_mock.called)1387 self.assertTrue(result[2] == ".\\dummy-user")1388 self.assertTrue(adjust_directory_permissions_mock.called)1389 self.assertEqual(result[0], 0)1390 _reset_mocks()1391 # Scenario: user is not defined, use system account (setup process)1392 read_ambari_user_mock.return_value = None1393 get_YN_input_mock.return_value = True1394 get_validated_string_input_mock.side_effect = ["NT AUTHORITY\\SYSTEM"]1395 create_user_mock.return_value = (0, "User created")1396 result = check_ambari_user(options)1397 self.assertTrue(get_YN_input_mock.called)1398 self.assertTrue(get_validated_string_input_mock.called)1399 self.assertEqual(get_validated_string_input_mock.call_count, 1)1400 self.assertFalse(find_user_mock.called)1401 self.assertFalse(create_user_mock.called)1402 self.assertTrue(result[2] == "NT AUTHORITY\\SYSTEM")1403 self.assertTrue(adjust_directory_permissions_mock.called)1404 self.assertEqual(result[0], 0)1405 _reset_mocks()1406 # Scenario: user is not defined (setup process), user creation failed1407 read_ambari_user_mock.return_value = None1408 get_YN_input_mock.return_value = True1409 get_validated_string_input_mock.side_effect = ["new-dummy-user", "new_password"]1410 find_user_mock.return_value = False1411 create_user_mock.return_value = (-1, "Failed")1412 result = check_ambari_user(options)1413 self.assertTrue(get_YN_input_mock.called)1414 self.assertTrue(get_validated_string_input_mock.called)1415 self.assertTrue(create_user_mock.called)1416 self.assertFalse(adjust_directory_permissions_mock.called)1417 self.assertEqual(result[0], -1)1418 _reset_mocks()1419 # Scenario: user is not defined and left to be the default1420 read_ambari_user_mock.return_value = None1421 get_YN_input_mock.return_value = False1422 result = check_ambari_user(options)1423 self.assertTrue(get_YN_input_mock.called)1424 self.assertFalse(get_validated_string_input_mock.called)1425 self.assertFalse(run_os_command_mock.called)1426 self.assertTrue(result[2] == "NT AUTHORITY\\SYSTEM")1427 self.assertTrue(adjust_directory_permissions_mock.called)1428 self.assertEqual(result[0], 0)1429 pass1430 @patch("ambari_server.serverConfiguration.search_file")1431 @patch("__builtin__.open")1432 @patch("ambari_server.serverConfiguration.read_ambari_user")1433 @patch("ambari_server.serverConfiguration.set_file_permissions")1434 def test_store_password_file(self, set_file_permissions_mock,1435 read_ambari_user_mock, open_mock, search_file_mock):1436 search_file_mock.return_value = "/etc/ambari-server/conf/ambari.properties"1437 open_mock.return_value = MagicMock()1438 store_password_file("password", "passfile")1439 self.assertTrue(set_file_permissions_mock.called)1440 pass1441 @patch("subprocess.Popen")1442 @patch.object(OSCheck, "get_os_family")1443 @patch.object(OSCheck, "get_os_type")1444 @patch.object(OSCheck, "get_os_major_version")1445 def test_check_firewall_is_running(self, get_os_major_version_mock, get_os_type_mock, get_os_family_mock, popen_mock):1446 get_os_major_version_mock.return_value = 181447 get_os_type_mock.return_value = OSConst.OS_FEDORA1448 get_os_family_mock.return_value = OSConst.REDHAT_FAMILY1449 firewall_obj = Firewall().getFirewallObject()1450 p = MagicMock()1451 p.communicate.return_value = ("active", "err")1452 p.returncode = 01453 popen_mock.return_value = p1454 self.assertEqual("Fedora18FirewallChecks", firewall_obj.__class__.__name__)1455 self.assertTrue(firewall_obj.check_firewall())1456 p.communicate.return_value = ("", "err")1457 p.returncode = 31458 self.assertFalse(firewall_obj.check_firewall())1459 self.assertEqual("err", firewall_obj.stderrdata)1460 get_os_type_mock.return_value = OSConst.OS_UBUNTU1461 get_os_family_mock.return_value = OSConst.UBUNTU_FAMILY1462 firewall_obj = Firewall().getFirewallObject()1463 p.communicate.return_value = ("Status: active", "err")1464 p.returncode = 01465 self.assertEqual("UbuntuFirewallChecks", firewall_obj.__class__.__name__)1466 self.assertTrue(firewall_obj.check_firewall())1467 p.communicate.return_value = ("Status: inactive", "err")1468 p.returncode = 01469 self.assertFalse(firewall_obj.check_firewall())1470 self.assertEqual("err", firewall_obj.stderrdata)1471 get_os_type_mock.return_value = ""1472 get_os_family_mock.return_value = OSConst.SUSE_FAMILY1473 firewall_obj = Firewall().getFirewallObject()1474 p.communicate.return_value = ("### iptables", "err")1475 p.returncode = 01476 self.assertEqual("SuseFirewallChecks", firewall_obj.__class__.__name__)1477 self.assertTrue(firewall_obj.check_firewall())1478 p.communicate.return_value = ("SuSEfirewall2 not active", "err")1479 p.returncode = 01480 self.assertFalse(firewall_obj.check_firewall())1481 self.assertEqual("err", firewall_obj.stderrdata)1482 get_os_type_mock.return_value = ""1483 get_os_family_mock.return_value = OSConst.REDHAT_FAMILY1484 firewall_obj = Firewall().getFirewallObject()1485 p.communicate.return_value = ("Table: filter", "err")1486 p.returncode = 01487 self.assertEqual("FirewallChecks", firewall_obj.__class__.__name__)1488 self.assertTrue(firewall_obj.check_firewall())1489 p.communicate.return_value = ("", "err")1490 p.returncode = 31491 self.assertFalse(firewall_obj.check_firewall())1492 self.assertEqual("err", firewall_obj.stderrdata)1493 pass1494 @patch("ambari_server.setupHttps.get_validated_filepath_input")1495 @patch("ambari_server.setupHttps.get_validated_string_input")1496 @patch("ambari_server.setupHttps.run_os_command")1497 @patch("ambari_server.setupHttps.get_truststore_type")1498 @patch("__builtin__.open")1499 @patch("ambari_server.setupHttps.find_properties_file")1500 @patch("ambari_server.setupHttps.run_component_https_cmd")1501 @patch("ambari_server.setupHttps.get_delete_cert_command")1502 @patch("ambari_server.setupHttps.get_truststore_password")1503 @patch("ambari_server.setupHttps.get_truststore_path")1504 @patch("ambari_server.setupHttps.get_YN_input")1505 @patch("ambari_server.setupHttps.get_ambari_properties")1506 @patch("ambari_server.setupHttps.find_jdk")1507 def test_setup_truststore(self, find_jdk_mock, get_ambari_properties_mock, get_YN_input_mock,1508 get_truststore_path_mock, get_truststore_password_mock,1509 get_delete_cert_command_mock, run_component_https_cmd_mock,1510 find_properties_file_mock, open_mock,1511 get_truststore_type_mock, run_os_command_mock,1512 get_validated_string_input_mock,1513 get_validated_filepath_input_mock):1514 out = StringIO.StringIO()1515 sys.stdout = out1516 component = "component"1517 command = "command"1518 property = "use_ssl"1519 alias = "alias"1520 #Silent mode1521 set_silent(True)1522 setup_truststore()1523 self.assertEqual('setup-security is not enabled in silent mode.\n', out.getvalue())1524 sys.stdout = sys.__stdout__1525 #Verbouse mode and jdk_path is None1526 set_silent(False)1527 p = get_ambari_properties_mock.return_value1528 # Dont disable ssl1529 get_YN_input_mock.side_effect = [False]1530 get_validated_string_input_mock.return_value = "alias"1531 setup_truststore()1532 self.assertTrue(get_YN_input_mock.called)1533 p.get_property.reset_mock()1534 get_YN_input_mock.reset_mock()1535 # Cant find jdk1536 find_jdk_mock.return_value = None1537 try:1538 setup_truststore()1539 self.fail("Should throw exception")1540 except FatalException as fe:1541 # Expected1542 self.assertTrue('No JDK found, please run the "ambari-server setup" command to install a' +1543 ' JDK automatically or install any JDK manually to ' in fe.reason)1544 pass1545 #Verbouse mode and jdk_path is not None (use_https = true)1546 find_jdk_mock.return_value = "/jdk_path"1547 p.get_property.side_effect = ["true"]1548 get_YN_input_mock.side_effect = [True,True]1549 get_truststore_path_mock.return_value = "/truststore_path"1550 get_truststore_password_mock.return_value = "/truststore_password"1551 get_delete_cert_command_mock.return_value = "rm -f"1552 setup_truststore(True)1553 self.assertTrue(get_truststore_path_mock.called)1554 self.assertTrue(get_truststore_password_mock.called)1555 self.assertTrue(get_delete_cert_command_mock.called)1556 self.assertTrue(find_properties_file_mock.called)1557 self.assertTrue(open_mock.called)1558 self.assertTrue(p.store.called)1559 self.assertTrue(run_component_https_cmd_mock.called)1560 p.process_pair.reset_mock()1561 get_truststore_path_mock.reset_mock()1562 get_truststore_password_mock.reset_mock()1563 get_delete_cert_command_mock.reset_mock()1564 find_properties_file_mock.reset_mock()1565 open_mock.reset_mock()1566 p.store.reset_mock()1567 #Verbouse mode and jdk_path is not None (use_https = false) and import cert1568 p.get_property.side_effect = ["false"]1569 get_YN_input_mock.side_effect = [True,True]1570 setup_truststore(True)1571 self.assertTrue(get_truststore_type_mock.called)1572 self.assertTrue(get_truststore_path_mock.called)1573 self.assertTrue(get_truststore_password_mock.called)1574 self.assertTrue(get_delete_cert_command_mock.called)1575 self.assertTrue(find_properties_file_mock.called)1576 self.assertTrue(open_mock.called)1577 self.assertTrue(p.store.called)1578 self.assertTrue(run_component_https_cmd_mock.called)1579 self.assertTrue(run_os_command_mock.called)1580 self.assertTrue(get_validated_filepath_input_mock.called)1581 p.process_pair.reset_mock()1582 get_truststore_type_mock.reset_mock()1583 get_truststore_path_mock.reset_mock()1584 get_truststore_password_mock.reset_mock()1585 get_delete_cert_command_mock.reset_mock()1586 find_properties_file_mock.reset_mock()1587 open_mock.reset_mock()1588 p.store.reset_mock()1589 run_os_command_mock.reset_mock()1590 get_validated_filepath_input_mock.reset_mock()1591 pass1592 @patch("ambari_server.setupHttps.adjust_directory_permissions")1593 @patch("ambari_server.setupHttps.read_ambari_user")1594 @patch("ambari_server.setupHttps.get_validated_string_input")1595 @patch("ambari_server.setupHttps.find_properties_file")1596 @patch("ambari_server.setupHttps.get_ambari_properties")1597 @patch("ambari_server.setupHttps.import_cert_and_key_action")1598 @patch("ambari_server.setupHttps.get_YN_input")1599 @patch("__builtin__.open")1600 @patch("ambari_server.setupHttps.is_root")1601 @patch("ambari_server.setupHttps.is_valid_cert_host")1602 @patch("ambari_server.setupHttps.is_valid_cert_exp")1603 def test_setup_https(self, is_valid_cert_exp_mock, is_valid_cert_host_mock, \1604 is_root_mock, open_Mock, get_YN_input_mock, \1605 import_cert_and_key_action_mock,1606 get_ambari_properties_mock, \1607 find_properties_file_mock, \1608 get_validated_string_input_mock, read_ambari_user_method, \1609 adjust_directory_permissions_mock):1610 is_valid_cert_exp_mock.return_value = True1611 is_valid_cert_host_mock.return_value = True1612 args = MagicMock()1613 open_Mock.return_value = file1614 p = get_ambari_properties_mock.return_value1615 # Testing call under non-root1616 is_root_mock.return_value = False1617 try:1618 setup_https(args)1619 self.fail("Should throw exception")1620 except FatalException as fe:1621 # Expected1622 self.assertTrue("root-level" in fe.reason)1623 pass1624 # Testing call under root1625 is_root_mock.return_value = True1626 read_ambari_user_method.return_value = "user"1627 #Case #1: if client ssl is on and user didnt choose1628 #disable ssl option and choose import certs and keys1629 p.get_property.side_effect = ["key_dir", "5555", "6666", "true"]1630 get_YN_input_mock.side_effect = [False, True]1631 get_validated_string_input_mock.side_effect = ["4444"]1632 get_property_expected = "[call('security.server.keys_dir'),\n" + \1633 " call('client.api.ssl.port'),\n" + \1634 " call('client.api.ssl.port'),\n call('api.ssl')]"1635 process_pair_expected = "[call('client.api.ssl.port', '4444')]"1636 set_silent(False)1637 setup_https(args)1638 self.assertTrue(p.process_pair.called)1639 self.assertTrue(p.get_property.call_count == 4)1640 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)1641 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)1642 self.assertTrue(p.store.called)1643 self.assertTrue(import_cert_and_key_action_mock.called)1644 p.process_pair.reset_mock()1645 p.get_property.reset_mock()1646 p.store.reset_mock()1647 import_cert_and_key_action_mock.reset_mock()1648 #Case #2: if client ssl is on and user choose to disable ssl option1649 p.get_property.side_effect = ["key_dir", "", "true"]1650 get_YN_input_mock.side_effect = [True]1651 get_validated_string_input_mock.side_effect = ["4444"]1652 get_property_expected = "[call('security.server.keys_dir'),\n" + \1653 " call('client.api.ssl.port'),\n call('api.ssl')]"1654 process_pair_expected = "[call('api.ssl', 'false')]"1655 setup_https(args)1656 self.assertTrue(p.process_pair.called)1657 self.assertTrue(p.get_property.call_count == 3)1658 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)1659 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)1660 self.assertTrue(p.store.called)1661 self.assertFalse(import_cert_and_key_action_mock.called)1662 p.process_pair.reset_mock()1663 p.get_property.reset_mock()1664 p.store.reset_mock()1665 import_cert_and_key_action_mock.reset_mock()1666 #Case #3: if client ssl is off and user choose option1667 #to import cert and keys1668 p.get_property.side_effect = ["key_dir", "", None]1669 get_YN_input_mock.side_effect = [True, True]1670 get_validated_string_input_mock.side_effect = ["4444"]1671 get_property_expected = "[call('security.server.keys_dir'),\n" + \1672 " call('client.api.ssl.port'),\n call('api.ssl')]"1673 process_pair_expected = "[call('client.api.ssl.port', '4444')]"1674 setup_https(args)1675 self.assertTrue(p.process_pair.called)1676 self.assertTrue(p.get_property.call_count == 3)1677 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)1678 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)1679 self.assertTrue(p.store.called)1680 self.assertTrue(import_cert_and_key_action_mock.called)1681 p.process_pair.reset_mock()1682 p.get_property.reset_mock()1683 p.store.reset_mock()1684 import_cert_and_key_action_mock.reset_mock()1685 #Case #4: if client ssl is off and1686 #user did not choose option to import cert and keys1687 p.get_property.side_effect = ["key_dir", "", None]1688 get_YN_input_mock.side_effect = [False]1689 get_validated_string_input_mock.side_effect = ["4444"]1690 get_property_expected = "[call('security.server.keys_dir'),\n" + \1691 " call('client.api.ssl.port'),\n call('api.ssl')]"1692 process_pair_expected = "[]"1693 setup_https(args)1694 self.assertFalse(p.process_pair.called)1695 self.assertTrue(p.get_property.call_count == 3)1696 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)1697 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)1698 self.assertFalse(p.store.called)1699 self.assertFalse(import_cert_and_key_action_mock.called)1700 p.process_pair.reset_mock()1701 p.get_property.reset_mock()1702 p.store.reset_mock()1703 import_cert_and_key_action_mock.reset_mock()1704 #Case #5: if cert must be imported but didnt imported1705 p.get_property.side_effect = ["key_dir", "", "false"]1706 get_YN_input_mock.side_effect = [True]1707 import_cert_and_key_action_mock.side_effect = [False]1708 get_validated_string_input_mock.side_effect = ["4444"]1709 get_property_expected = "[call('security.server.keys_dir'),\n" + \1710 " call('client.api.ssl.port'),\n call('api.ssl')]"1711 process_pair_expected = "[call('client.api.ssl.port', '4444')]"1712 self.assertFalse(setup_https(args))1713 self.assertTrue(p.process_pair.called)1714 self.assertTrue(p.get_property.call_count == 3)1715 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)1716 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)1717 self.assertFalse(p.store.called)1718 self.assertTrue(import_cert_and_key_action_mock.called)1719 p.process_pair.reset_mock()1720 p.get_property.reset_mock()1721 p.store.reset_mock()1722 import_cert_and_key_action_mock.reset_mock()1723 #Case #6: if silent mode is enabled1724 set_silent(True)1725 try:1726 setup_https(args)1727 self.fail("Should throw exception")1728 except NonFatalException as fe:1729 self.assertTrue("setup-https is not enabled in silent mode" in fe.reason)1730 p.process_pair.reset_mock()1731 p.get_property.reset_mock()1732 p.store.reset_mock()1733 import_cert_and_key_action_mock.reset_mock()1734 #Case #7: read property throw exception1735 set_silent(False)1736 find_properties_file_mock.return_value = "propertyFile"1737 p.get_property.side_effect = KeyError("Failed to read property")1738 try:1739 setup_https(args)1740 self.fail("Should throw exception")1741 except FatalException as fe:1742 self.assertTrue("Failed to read property" in fe.reason)1743 pass1744 @patch("ambari_server.setupHttps.import_cert_and_key")1745 def test_import_cert_and_key_action(self, import_cert_and_key_mock):1746 import_cert_and_key_mock.return_value = True1747 properties = MagicMock()1748 properties.get_property.side_effect = ["key_dir", "5555", "6666", "true"]1749 properties.process_pair = MagicMock()1750 expect_process_pair = "[call('client.api.ssl.cert_name', 'https.crt'),\n" + \1751 " call('client.api.ssl.key_name', 'https.key'),\n" + \1752 " call('api.ssl', 'true')]"1753 import_cert_and_key_action("key_dir", properties)1754 self.assertEqual(str(properties.process_pair.call_args_list), \1755 expect_process_pair)1756 pass1757 @patch("ambari_server.setupHttps.remove_file")1758 @patch("ambari_server.setupHttps.copy_file")1759 @patch("ambari_server.setupHttps.read_ambari_user")1760 @patch("ambari_server.setupHttps.set_file_permissions")1761 @patch("ambari_server.setupHttps.import_file_to_keystore")1762 @patch("__builtin__.open")1763 @patch("ambari_server.setupHttps.run_os_command")1764 @patch("os.path.join")1765 @patch("os.path.isfile")1766 @patch("__builtin__.raw_input")1767 @patch("ambari_server.setupHttps.get_validated_string_input")1768 @patch("ambari_server.setupHttps.is_valid_cert_host")1769 @patch("ambari_server.setupHttps.is_valid_cert_exp")1770 def test_import_cert_and_key(self, is_valid_cert_exp_mock, \1771 is_valid_cert_host_mock, \1772 get_validated_string_input_mock, \1773 raw_input_mock, \1774 os_path_isfile_mock, \1775 os_path_join_mock, run_os_command_mock, \1776 open_mock, import_file_to_keystore_mock, \1777 set_file_permissions_mock, read_ambari_user_mock, copy_file_mock, \1778 remove_file_mock):1779 is_valid_cert_exp_mock.return_value = True1780 is_valid_cert_host_mock.return_value = True1781 os_path_isfile_mock.return_value = True1782 get_validated_string_input_mock.return_value = "password"1783 raw_input_mock.side_effect = \1784 ["cert_file_path", "key_file_path"]1785 os_path_join_mock.side_effect = ["keystore_file_path", "keystore_file_path_tmp", \1786 "pass_file_path", "pass_file_path_tmp", \1787 "passin_file_path", "password_file_path", \1788 "keystore_cert_file_path", \1789 "keystore_cert_key_file_path", ]1790 run_os_command_mock.return_value = (0, "", "")1791 om = open_mock.return_value1792 expect_import_file_to_keystore = "[call('keystore_file_path_tmp'," + \1793 " 'keystore_file_path'),\n" + \1794 " call('pass_file_path_tmp'," + \1795 " 'pass_file_path'),\n" + \1796 " call('cert_file_path'," + \1797 " 'keystore_cert_file_path'),\n" + \1798 " call('key_file_path'," + \1799 " 'keystore_cert_key_file_path')]"1800 import_cert_and_key("key_dir")1801 self.assertTrue(raw_input_mock.call_count == 2)1802 self.assertTrue(get_validated_string_input_mock.called)1803 self.assertEqual(os_path_join_mock.call_count, 8)1804 self.assertTrue(set_file_permissions_mock.call_count == 1)1805 self.assertEqual(str(import_file_to_keystore_mock.call_args_list), \1806 expect_import_file_to_keystore)1807 pass1808 @patch("ambari_server.setupHttps.remove_file")1809 @patch("ambari_server.setupHttps.copy_file")1810 @patch("ambari_server.setupHttps.generate_random_string")1811 @patch("ambari_server.setupHttps.read_ambari_user")1812 @patch("ambari_server.setupHttps.set_file_permissions")1813 @patch("ambari_server.setupHttps.import_file_to_keystore")1814 @patch("__builtin__.open")1815 @patch("ambari_server.setupHttps.run_os_command")1816 @patch("os.path.join")1817 @patch("ambari_server.setupHttps.get_validated_filepath_input")1818 @patch("ambari_server.setupHttps.get_validated_string_input")1819 @patch("ambari_server.setupHttps.is_valid_cert_host")1820 @patch("ambari_server.setupHttps.is_valid_cert_exp")1821 def test_import_cert_and_key_with_empty_password(self, \1822 is_valid_cert_exp_mock, is_valid_cert_host_mock,1823 get_validated_string_input_mock, get_validated_filepath_input_mock, \1824 os_path_join_mock, run_os_command_mock, open_mock, \1825 import_file_to_keystore_mock, set_file_permissions_mock,1826 read_ambari_user_mock, generate_random_string_mock, copy_file_mock, \1827 remove_file_mock):1828 is_valid_cert_exp_mock.return_value = True1829 is_valid_cert_host_mock.return_value = True1830 get_validated_string_input_mock.return_value = ""1831 get_validated_filepath_input_mock.side_effect = \1832 ["cert_file_path", "key_file_path"]1833 os_path_join_mock.side_effect = ["keystore_file_path", "keystore_file_path_tmp", \1834 "pass_file_path", "pass_file_path_tmp", \1835 "passin_file_path", "password_file_path", \1836 "keystore_cert_file_path", \1837 "keystore_cert_key_file_path", ]1838 run_os_command_mock.return_value = (0, "", "")1839 expect_import_file_to_keystore = "[call('keystore_file_path_tmp'," + \1840 " 'keystore_file_path'),\n" + \1841 " call('pass_file_path_tmp'," + \1842 " 'pass_file_path'),\n" + \1843 " call('cert_file_path'," + \1844 " 'keystore_cert_file_path'),\n" + \1845 " call('key_file_path.secured'," + \1846 " 'keystore_cert_key_file_path')]"1847 import_cert_and_key("key_dir")1848 self.assertEquals(get_validated_filepath_input_mock.call_count, 2)1849 self.assertTrue(get_validated_string_input_mock.called)1850 self.assertEquals(os_path_join_mock.call_count, 8)1851 self.assertEquals(set_file_permissions_mock.call_count, 1)1852 self.assertEqual(str(import_file_to_keystore_mock.call_args_list), \1853 expect_import_file_to_keystore)1854 self.assertTrue(generate_random_string_mock.called)1855 pass1856 @patch("__builtin__.open")1857 @patch("ambari_server.setupHttps.copy_file")1858 @patch("ambari_server.setupHttps.is_root")1859 @patch("ambari_server.setupHttps.read_ambari_user")1860 @patch("ambari_server.setupHttps.set_file_permissions")1861 @patch("ambari_server.setupHttps.import_file_to_keystore")1862 @patch("ambari_server.setupHttps.run_os_command")1863 @patch("os.path.join")1864 @patch("ambari_server.setupHttps.get_validated_filepath_input")1865 @patch("ambari_server.setupHttps.get_validated_string_input")1866 def test_import_cert_and_key_with_incorrect_password(self,1867 get_validated_string_input_mock, \1868 get_validated_filepath_input_mock, \1869 os_path_join_mock, \1870 run_os_command_mock, \1871 import_file_to_keystore_mock, \1872 set_file_permissions_mock, \1873 read_ambari_user_mock, \1874 is_root_mock, \1875 copy_file_mock, \1876 open_mock):1877 get_validated_string_input_mock.return_value = "incorrect_password"1878 get_validated_filepath_input_mock.return_value = 'filename'1879 open_mock.return_value = MagicMock()1880 os_path_join_mock.return_value = ''1881 is_root_mock.return_value = True1882 #provided password doesn't match, openssl command returns an error1883 run_os_command_mock.return_value = (1, "", "Some error message")1884 self.assertFalse(import_cert_and_key_action(*["key_dir", None]))1885 self.assertFalse(import_cert_and_key("key_dir"))1886 pass1887 def test_is_valid_cert_exp(self):1888 #No data in certInfo1889 certInfo = {}1890 is_valid = is_valid_cert_exp(certInfo)1891 self.assertFalse(is_valid)1892 #Issued in future1893 issuedOn = (datetime.datetime.now() + datetime.timedelta(hours=1000)).strftime(SSL_DATE_FORMAT)1894 expiresOn = (datetime.datetime.now() + datetime.timedelta(hours=2000)).strftime(SSL_DATE_FORMAT)1895 certInfo = {NOT_BEFORE_ATTR: issuedOn,1896 NOT_AFTER_ATTR: expiresOn}1897 is_valid = is_valid_cert_exp(certInfo)1898 self.assertFalse(is_valid)1899 #Was expired1900 issuedOn = (datetime.datetime.now() - datetime.timedelta(hours=2000)).strftime(SSL_DATE_FORMAT)1901 expiresOn = (datetime.datetime.now() - datetime.timedelta(hours=1000)).strftime(SSL_DATE_FORMAT)1902 certInfo = {NOT_BEFORE_ATTR: issuedOn,1903 NOT_AFTER_ATTR: expiresOn}1904 is_valid = is_valid_cert_exp(certInfo)1905 self.assertFalse(is_valid)1906 #Valid1907 issuedOn = (datetime.datetime.now() - datetime.timedelta(hours=2000)).strftime(SSL_DATE_FORMAT)1908 expiresOn = (datetime.datetime.now() + datetime.timedelta(hours=1000)).strftime(SSL_DATE_FORMAT)1909 certInfo = {NOT_BEFORE_ATTR: issuedOn,1910 NOT_AFTER_ATTR: expiresOn}1911 is_valid = is_valid_cert_exp(certInfo)1912 self.assertTrue(is_valid)1913 pass1914 @patch("ambari_server.setupHttps.get_fqdn")1915 def test_is_valid_cert_host(self, get_fqdn_mock):1916 #No data in certInfo1917 certInfo = {}1918 is_valid = is_valid_cert_host(certInfo)1919 self.assertFalse(is_valid)1920 #Failed to get FQDN1921 get_fqdn_mock.return_value = None1922 is_valid = is_valid_cert_host(certInfo)1923 self.assertFalse(is_valid)1924 #FQDN and Common name in certificated don't correspond1925 get_fqdn_mock.return_value = 'host1'1926 certInfo = {COMMON_NAME_ATTR: 'host2'}1927 is_valid = is_valid_cert_host(certInfo)1928 self.assertFalse(is_valid)1929 #FQDN and Common name in certificated correspond1930 get_fqdn_mock.return_value = 'host1'1931 certInfo = {COMMON_NAME_ATTR: 'host1'}1932 is_valid = is_valid_cert_host(certInfo)1933 self.assertTrue(is_valid)1934 pass1935 @patch("ambari_server.setupHttps.get_ambari_properties")1936 def test_is_valid_https_port(self, get_ambari_properties_mock):1937 #No ambari.properties1938 get_ambari_properties_mock.return_value = -11939 is_valid = is_valid_https_port(1111)1940 self.assertEqual(is_valid, False)1941 #User entered port used by one way auth1942 portOneWay = "1111"1943 portTwoWay = "2222"1944 validPort = "3333"1945 get_ambari_properties_mock.return_value = {SRVR_ONE_WAY_SSL_PORT_PROPERTY: portOneWay,1946 SRVR_TWO_WAY_SSL_PORT_PROPERTY: portTwoWay}1947 is_valid = is_valid_https_port(portOneWay)1948 self.assertEqual(is_valid, False)1949 #User entered port used by two way auth1950 is_valid = is_valid_https_port(portTwoWay)1951 self.assertEqual(is_valid, False)1952 #User entered valid port1953 get_ambari_properties_mock.return_value = {SRVR_ONE_WAY_SSL_PORT_PROPERTY: portOneWay,1954 SRVR_TWO_WAY_SSL_PORT_PROPERTY: portTwoWay}1955 is_valid = is_valid_https_port(validPort)1956 self.assertEqual(is_valid, True)1957 pass1958 @patch("socket.getfqdn")1959 @patch("urllib2.urlopen")1960 @patch("ambari_server.setupHttps.get_ambari_properties")1961 def test_get_fqdn(self, get_ambari_properties_mock, url_open_mock, getfqdn_mock):1962 #No ambari.properties1963 get_ambari_properties_mock.return_value = -11964 fqdn = get_fqdn()1965 self.assertEqual(fqdn, None)1966 #Check mbari_server.GET_FQDN_SERVICE_URL property name (AMBARI-2612)1967 #property name should be server.fqdn.service.url1968 self.assertEqual(GET_FQDN_SERVICE_URL, "server.fqdn.service.url")1969 #Read FQDN from service1970 p = MagicMock()1971 p[GET_FQDN_SERVICE_URL] = 'someurl'1972 get_ambari_properties_mock.return_value = p1973 u = MagicMock()1974 host = 'host1.domain.com'1975 u.read.return_value = host1976 url_open_mock.return_value = u1977 fqdn = get_fqdn()1978 self.assertEqual(fqdn, host)1979 #Failed to read FQDN from service, getting from socket1980 u.reset_mock()1981 u.side_effect = Exception("Failed to read FQDN from service")1982 getfqdn_mock.return_value = host1983 fqdn = get_fqdn()1984 self.assertEqual(fqdn, host)1985 pass1986 def test_get_ulimit_open_files(self):1987 # 1 - No ambari.properties1988 p = Properties()1989 open_files = get_ulimit_open_files(p)1990 self.assertEqual(open_files, ULIMIT_OPEN_FILES_DEFAULT)1991 # 2 - With ambari.properties - ok1992 prop_value = 650001993 p.process_pair(ULIMIT_OPEN_FILES_KEY, str(prop_value))1994 open_files = get_ulimit_open_files(p)1995 self.assertEqual(open_files, 65000)1996 # 2 - With ambari.properties - default1997 tf1 = tempfile.NamedTemporaryFile()1998 prop_value = 01999 p.process_pair(ULIMIT_OPEN_FILES_KEY, str(prop_value))2000 open_files = get_ulimit_open_files(p)2001 self.assertEqual(open_files, ULIMIT_OPEN_FILES_DEFAULT)2002 pass2003 @patch("ambari_server.setupHttps.run_os_command")2004 def test_get_cert_info(self, run_os_command_mock):2005 # Error running openssl command2006 path = 'path/to/certificate'2007 run_os_command_mock.return_value = -1, None, None2008 cert_info = get_cert_info(path)2009 self.assertEqual(cert_info, None)2010 #Empty result of openssl command2011 run_os_command_mock.return_value = 0, None, None2012 cert_info = get_cert_info(path)2013 self.assertEqual(cert_info, None)2014 #Positive scenario2015 notAfter = 'Jul 3 14:12:57 2014 GMT'2016 notBefore = 'Jul 3 14:12:57 2013 GMT'2017 attr1_key = 'A'2018 attr1_value = 'foo'2019 attr2_key = 'B'2020 attr2_value = 'bar'2021 attr3_key = 'CN'2022 attr3_value = 'host.domain.com'2023 subject_pattern = '/{attr1_key}={attr1_value}/{attr2_key}={attr2_value}/{attr3_key}={attr3_value}'2024 subject = subject_pattern.format(attr1_key=attr1_key, attr1_value=attr1_value,2025 attr2_key=attr2_key, attr2_value=attr2_value,2026 attr3_key=attr3_key, attr3_value=attr3_value)2027 out_pattern = \2028 "notAfter={notAfter}" + os.linesep + \2029 "notBefore={notBefore}" + os.linesep + \2030 "subject={subject}" + os.linesep + \2031 "-----BEGIN CERTIFICATE-----" + os.linesep + \2032 "MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV" + os.linesep + \2033 "..." + os.linesep + \2034 "5lqd8XxOGSYoMOf+70BLN2sB" + os.linesep + \2035 "-----END CERTIFICATE-----" + os.linesep + \2036 ""2037 out = out_pattern.format(notAfter=notAfter, notBefore=notBefore, subject=subject)2038 run_os_command_mock.return_value = 0, out, None2039 cert_info = get_cert_info(path)2040 self.assertEqual(cert_info['notAfter'], notAfter)2041 self.assertEqual(cert_info['notBefore'], notBefore)2042 self.assertEqual(cert_info['subject'], subject)2043 self.assertEqual(cert_info[attr1_key], attr1_value)2044 self.assertEqual(cert_info[attr2_key], attr2_value)2045 self.assertEqual(cert_info[attr3_key], attr3_value)2046 pass2047 @patch("__builtin__.raw_input")2048 def test_get_validated_string_input(self, raw_input_mock):2049 prompt = 'prompt'2050 default_value = 'default'2051 description = 'desc'2052 validator = MagicMock()2053 validator.return_value = True2054 inputed_value1 = 'val1'2055 inputed_value2 = 'val2'2056 raw_input_mock.return_value = inputed_value12057 input = get_validated_string_input(prompt, default_value, None,2058 description, False, False, validator)2059 self.assertTrue(validator.called)2060 self.assertEqual(inputed_value1, input)2061 validator.side_effect = [False, True]2062 raw_input_mock.side_effect = [inputed_value1, inputed_value2]2063 input = get_validated_string_input(prompt, default_value, None,2064 description, False, False, validator)2065 self.assertEqual(inputed_value2, input)2066 pass2067 @not_for_platform(PLATFORM_WINDOWS)2068 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2069 @patch("ambari_server.serverUtils.run_os_command")2070 @patch("__builtin__.open")2071 @patch("os.path.exists")2072 def test_is_server_runing(self, os_path_exists_mock, open_mock, \2073 run_os_command_mock):2074 os_path_exists_mock.return_value = True2075 f = open_mock.return_value2076 f.readline.return_value = "111"2077 run_os_command_mock.return_value = 0, "", ""2078 status, pid = is_server_runing()2079 self.assertTrue(status)2080 self.assertEqual(111, pid)2081 os_path_exists_mock.return_value = False2082 status, pid = is_server_runing()2083 self.assertFalse(status)2084 pass2085 @only_for_platform(PLATFORM_WINDOWS)2086 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2087 @patch("os_windows.win32serviceutil.QueryServiceStatus")2088 def test_is_server_runing(self, query_service_status_mock):2089 query_service_status_mock.return_value = ("", 4)2090 status, desc = is_server_runing()2091 self.assertTrue(status)2092 self.assertEqual("", desc)2093 query_service_status_mock.return_value = ("", 1)2094 status, desc = is_server_runing()2095 self.assertFalse(status)2096 self.assertEqual("stopped", desc)2097 pass2098 @not_for_platform(PLATFORM_WINDOWS)2099 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2100 @patch("ambari_server.serverUtils.run_os_command")2101 @patch("__builtin__.open")2102 @patch("os.path.exists")2103 def test_is_server_runing_bad_file(self, os_path_exists_mock, open_mock, \2104 run_os_command_mock):2105 os_path_exists_mock.return_value = True2106 f = open_mock.return_value2107 f.readline.return_value = "" # empty file content2108 run_os_command_mock.return_value = 0, "", ""2109 self.assertRaises(NonFatalException, is_server_runing)2110 open_mock.side_effect = IOError('[Errno 13] Permission denied: /var/run/ambari-server/ambari-server.pid')2111 self.assertRaises(FatalException, is_server_runing)2112 pass2113 @not_for_platform(PLATFORM_WINDOWS)2114 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2115 @patch("os.path.exists")2116 @patch("os.makedirs")2117 @patch("os.chdir")2118 @patch("ambari_server.serverSetup.run_os_command")2119 def test_install_jdk(self, run_os_command_mock, os_chdir_mock, os_makedirs_mock, os_path_exists_mock):2120 run_os_command_mock.return_value = 1, "", ""2121 os_path_exists_mock.return_value = False2122 failed = False2123 try:2124 jdkSetup = JDKSetup()2125 jdkSetup._install_jdk(MagicMock(), MagicMock())2126 self.fail("Exception was not rised!")2127 except FatalException:2128 failed = True2129 self.assertTrue(failed)2130 pass2131 @only_for_platform(PLATFORM_WINDOWS)2132 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2133 @patch("os.path.exists")2134 @patch("os.makedirs")2135 @patch("os.chdir")2136 @patch("ambari_server.serverSetup.run_os_command")2137 def test_install_jdk(self, run_os_command_mock, os_chdir_mock, os_makedirs_mock, os_path_exists_mock):2138 jdk_cfg = MagicMock()2139 jdk_cfg.inst_dir = "java_home_dir"2140 run_os_command_mock.return_value = 1, "", ""2141 os_path_exists_mock.return_value = False2142 failed = False2143 try:2144 jdkSetup = JDKSetup()2145 jdkSetup._install_jdk("jdk.exe", jdk_cfg)2146 self.fail("Exception was not rised!")2147 except FatalException:2148 failed = True2149 self.assertTrue(failed)2150 pass2151 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2152 @patch("ambari_server.serverSetup.read_ambari_user")2153 @patch("os.stat")2154 @patch("os.path.isfile")2155 @patch("os.path.exists")2156 @patch("os.chdir")2157 @patch("os.makedirs")2158 @patch("ambari_server.serverSetup.JDKSetupLinux.adjust_jce_permissions")2159 @patch("ambari_server.serverSetup.expand_jce_zip_file")2160 @patch("ambari_server.serverSetup.force_download_file")2161 @patch("ambari_server.serverSetup.get_YN_input")2162 @patch("ambari_server.serverSetup.run_os_command")2163 @patch("ambari_server.serverSetup.update_properties")2164 @patch("ambari_server.serverSetup.get_validated_string_input")2165 @patch("ambari_server.serverSetup.print_info_msg")2166 @patch("ambari_server.serverSetup.validate_jdk")2167 @patch("ambari_server.serverSetup.get_JAVA_HOME")2168 @patch("ambari_server.serverSetup.get_resources_location")2169 @patch("ambari_server.serverSetup.get_ambari_properties")2170 @patch("shutil.copyfile")2171 @patch("sys.exit")2172 def test_download_jdk(self, exit_mock, copyfile_mock, get_ambari_properties_mock, get_resources_location_mock, get_JAVA_HOME_mock, \2173 validate_jdk_mock, print_info_msg_mock, get_validated_string_input_mock, update_properties_mock, \2174 run_os_command_mock, get_YN_input_mock, force_download_file_mock, expand_jce_zip_file_mock,2175 adjust_jce_permissions_mock, os_makedirs_mock,2176 os_chdir_mock, path_existsMock, path_isfileMock, statMock, read_ambari_user_mock):2177 @OsFamilyFuncImpl(OSConst.WINSRV_FAMILY)2178 def _init_test_jdk_mocks():2179 jdk1_url = "http://somewhere/myjdk.exe"2180 res_location = "resources"2181 p = Properties()2182 p.process_pair("java.releases", "jdk1")2183 p.process_pair("jdk1.desc", "JDK name")2184 p.process_pair("jdk1.url", "http://somewhere/myjdk.exe")2185 p.process_pair("jdk1.dest-file", "myjdk.exe")2186 p.process_pair("jdk1.jcpol-url", "http://somewhere/some-jcpol.zip")2187 p.process_pair("jdk1.jcpol-file", "some-jcpol.zip")2188 p.process_pair("jdk1.home", "C:\\jdk1")2189 p.process_pair("jdk1.re", "(jdk.*)/jre")2190 pem_side_effect1 = [False, True, False]2191 return p, jdk1_url, res_location, pem_side_effect12192 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)2193 def _init_test_jdk_mocks():2194 jdk1_url = "http://somewhere/somewhere.tar.gz"2195 res_location = MagicMock()2196 p = Properties()2197 p.process_pair("java.releases", "jdk1")2198 p.process_pair("jdk1.desc", "JDK name")2199 p.process_pair("jdk1.url", jdk1_url)2200 p.process_pair("jdk1.dest-file", "somewhere.tar.gz")2201 p.process_pair("jdk1.jcpol-url", "http://somewhere/some-jcpol.tar.gz")2202 p.process_pair("jdk1.jcpol-file", "some-jcpol.tar.gz")2203 p.process_pair("jdk1.home", "/jdk1")2204 p.process_pair("jdk1.re", "(jdk.*)/jre")2205 pem_side_effect1 = [True, False, True, False]2206 return p, jdk1_url, res_location, pem_side_effect12207 args = MagicMock()2208 args.java_home = "somewhere"2209 args.silent = False2210 p, jdk1_url, res_location, pem_side_effect1 = _init_test_jdk_mocks()2211 validate_jdk_mock.return_value = False2212 path_existsMock.return_value = False2213 get_resources_location_mock.return_value = res_location2214 get_JAVA_HOME_mock.return_value = False2215 read_ambari_user_mock.return_value = "ambari"2216 get_ambari_properties_mock.return_value = p2217 # Test case: ambari.properties not found2218 try:2219 download_and_install_jdk(args)2220 self.fail("Should throw exception because of not found ambari.properties")2221 except FatalException:2222 # Expected2223 self.assertTrue(get_ambari_properties_mock.called)2224 pass2225 # Test case: JDK already exists2226 args.java_home = None2227 args.jdk_location = None2228 get_JAVA_HOME_mock.return_value = "some_jdk"2229 validate_jdk_mock.return_value = True2230 get_YN_input_mock.return_value = False2231 path_existsMock.return_value = False2232 run_os_command_mock.return_value = 0, "", ""2233 rcode = download_and_install_jdk(args)2234 self.assertEqual(0, rcode)2235 # Test case: java home setup2236 args.java_home = "somewhere"2237 validate_jdk_mock.return_value = True2238 path_existsMock.return_value = False2239 get_JAVA_HOME_mock.return_value = None2240 rcode = download_and_install_jdk(args)2241 self.assertEqual(0, rcode)2242 self.assertTrue(update_properties_mock.called)2243 # Test case: JDK file does not exist, property not defined2244 validate_jdk_mock.return_value = False2245 path_existsMock.return_value = False2246 get_ambari_properties_mock.return_value = p2247 p.removeProp("jdk1.url")2248 try:2249 download_and_install_jdk(args)2250 self.fail("Should throw exception")2251 except FatalException:2252 # Expected2253 pass2254 # Test case: JDK file does not exist, HTTP response does not2255 # contain Content-Length2256 p.process_pair("jdk1.url", jdk1_url)2257 validate_jdk_mock.return_value = False2258 path_existsMock.return_value = False2259 get_YN_input_mock.return_value = True2260 get_validated_string_input_mock.return_value = "1"2261 run_os_command_mock.return_value = (0, "Wrong out", None)2262 try:2263 download_and_install_jdk(args)2264 self.fail("Should throw exception")2265 except FatalException:2266 # Expected2267 pass2268 # Successful JDK download2269 args.java_home = None2270 validate_jdk_mock.return_value = False2271 path_existsMock.reset_mock()2272 path_existsMock.side_effect = [False, False, False]2273 path_isfileMock.return_value = False2274 args.jdk_location = None2275 run_os_command_mock.return_value = (0, "Creating jdk1/jre", None)2276 statResult = MagicMock()2277 statResult.st_size = 320002278 statMock.return_value = statResult2279 try:2280 rcode = download_and_install_jdk(args)2281 except Exception, e:2282 raise2283 self.assertEqual(0, rcode)2284 # Test case: not accept the license"2285 get_YN_input_mock.return_value = False2286 path_existsMock.reset_mock()2287 path_existsMock.side_effect = [False, False, True, False, True, False]2288 download_and_install_jdk(args)2289 self.assertTrue(exit_mock.called)2290 # Test case: jdk is already installed, ensure that JCE check is skipped if -j option is not supplied.2291 args.jdk_location = None2292 get_JAVA_HOME_mock.return_value = "some_jdk"2293 validate_jdk_mock.return_value = True2294 get_YN_input_mock.return_value = False2295 path_existsMock.reset_mock()2296 path_existsMock.side_effect = pem_side_effect12297 force_download_file_mock.reset_mock()2298 with patch("ambari_server.serverSetup.JDKSetup._download_jce_policy") as download_jce_policy_mock:2299 rcode = download_and_install_jdk(args)2300 self.assertFalse(download_jce_policy_mock.called)2301 self.assertFalse(force_download_file_mock.called)2302 # Test case: Update JAVA_HOME location using command: ambari-server setup -j %NEW_LOCATION%2303 update_properties_mock.reset_mock()2304 args.java_home = "somewhere"2305 validate_jdk_mock.return_value = True2306 path_existsMock.reset_mock()2307 path_existsMock.side_effect = pem_side_effect12308 get_JAVA_HOME_mock.return_value = "some_jdk"2309 path_isfileMock.return_value = True2310 download_and_install_jdk(args)2311 self.assertTrue(update_properties_mock.call_count == 1)2312 # Test case: Negative test case JAVA_HOME location should not be updated if -j option is supplied and2313 # jce_policy file already exists in resources dir.2314 #write_property_mock.reset_mock()2315 #args.java_home = "somewhere"2316 #path_existsMock.side_effect = None2317 #path_existsMock.return_value = True2318 #get_JAVA_HOME_mock.return_value = "some_jdk"2319 #try:2320 # download_and_install_jdk(args)2321 # self.fail("Should throw exception")2322 #except FatalException as fe:2323 # Expected2324 # self.assertFalse(write_property_mock.called)2325 # Test case: Setup ambari-server first time, Custom JDK selected, JDK exists2326 args.java_home = None2327 args.jdk_location = None2328 validate_jdk_mock.return_value = False2329 update_properties_mock.reset_mock()2330 path_existsMock.reset_mock()2331 path_existsMock.side_effect = [True, True, True, True]2332 get_validated_string_input_mock.return_value = "2"2333 get_JAVA_HOME_mock.return_value = None2334 rcode = download_and_install_jdk(args)2335 self.assertEqual(0, rcode)2336 self.assertTrue(update_properties_mock.called)2337 # Test case: Setup ambari-server first time, Custom JDK selected, JDK not exists2338 update_properties_mock.reset_mock()2339 validate_jdk_mock.return_value = False2340 path_existsMock.reset_mock()2341 path_existsMock.side_effect = pem_side_effect12342 get_validated_string_input_mock.return_value = "2"2343 get_JAVA_HOME_mock.return_value = None2344 try:2345 download_and_install_jdk(args)2346 self.fail("Should throw exception")2347 except FatalException as fe:2348 # Expected2349 pass2350 # Test when custom java home exists but java binary file doesn't exist2351 args.java_home = None2352 validate_jdk_mock.return_value = False2353 path_isfileMock.return_value = False2354 update_properties_mock.reset_mock()2355 path_existsMock.reset_mock()2356 path_existsMock.side_effect = pem_side_effect12357 get_validated_string_input_mock.return_value = "2"2358 get_JAVA_HOME_mock.return_value = None2359 flag = False2360 try:2361 download_and_install_jdk(args)2362 self.fail("Should throw exception")2363 except FatalException as fe:2364 # Expected2365 flag = True2366 pass2367 self.assertTrue(flag)2368 #Test case: Setup ambari-server with java home passed. Path to java home doesn't exist2369 args.java_home = "somewhere"2370 validate_jdk_mock.return_value = False2371 path_existsMock.reset_mock()2372 path_existsMock.side_effect = pem_side_effect12373 try:2374 download_and_install_jdk(args)2375 self.fail("Should throw exception")2376 except FatalException as fe:2377 self.assertTrue("Path to java home somewhere or java binary file does not exists" in fe.reason)2378 pass2379 pass2380 @not_for_platform(PLATFORM_WINDOWS)2381 @patch("ambari_server.dbConfiguration_linux.run_os_command")2382 def test_get_postgre_status(self, run_os_command_mock):2383 run_os_command_mock.return_value = (1, "running", None)2384 pg_status, retcode, out, err = PGConfig._get_postgre_status()2385 self.assertEqual("running", pg_status)2386 run_os_command_mock.return_value = (1, "wrong", None)2387 pg_status, retcode, out, err = PGConfig._get_postgre_status()2388 self.assertEqual(None, pg_status)2389 pass2390 @not_for_platform(PLATFORM_WINDOWS)2391 @patch("time.sleep")2392 @patch("subprocess.Popen")2393 @patch("ambari_server.dbConfiguration_linux.run_os_command")2394 @patch.object(PGConfig, "_get_postgre_status")2395 def test_check_postgre_up(self, get_postgre_status_mock, run_os_command_mock,2396 popen_mock, sleep_mock):2397 from ambari_server import serverConfiguration2398 p = MagicMock()2399 p.communicate.return_value = (None, None)2400 p.returncode = 02401 popen_mock.return_value = p2402 get_postgre_status_mock.return_value = "running", 0, "", ""2403 serverConfiguration.OS_TYPE = OSConst.OS_REDHAT2404 p.poll.return_value = 02405 run_os_command_mock.return_value = (0, None, None)2406 pg_status, retcode, out, err = PGConfig._check_postgre_up()2407 self.assertEqual(0, retcode)2408 serverConfiguration.OS_TYPE = OSConst.OS_SUSE2409 run_os_command_mock.return_value = (0, None, None)2410 p.poll.return_value = 02411 get_postgre_status_mock.return_value = "stopped", 0, "", ""2412 pg_status, retcode, out, err = PGConfig._check_postgre_up()2413 self.assertEqual(0, retcode)2414 pass2415 @patch("platform.linux_distribution")2416 @patch("platform.system")2417 @patch("ambari_commons.logging_utils.print_info_msg")2418 @patch("ambari_commons.logging_utils.print_error_msg")2419 @patch("ambari_server.serverSetup.get_ambari_properties")2420 @patch("ambari_server.serverSetup.write_property")2421 @patch("ambari_server.serverConfiguration.get_conf_dir")2422 def test_configure_os_settings(self, get_conf_dir_mock, write_property_mock, get_ambari_properties_mock,2423 print_error_msg_mock, print_info_msg_mock,2424 systemMock, distMock):2425 get_ambari_properties_mock.return_value = -12426 rcode = configure_os_settings()2427 self.assertEqual(-1, rcode)2428 p = MagicMock()2429 p[OS_TYPE_PROPERTY] = 'somevalue'2430 get_ambari_properties_mock.return_value = p2431 rcode = configure_os_settings()2432 self.assertEqual(0, rcode)2433 p.__getitem__.return_value = ""2434 rcode = configure_os_settings()2435 self.assertEqual(0, rcode)2436 self.assertTrue(write_property_mock.called)2437 self.assertEqual(2, write_property_mock.call_count)2438 self.assertEquals(write_property_mock.call_args_list[0][0][0], "server.os_family")2439 self.assertEquals(write_property_mock.call_args_list[1][0][0], "server.os_type")2440 pass2441 @patch("__builtin__.open")2442 @patch("ambari_server.serverConfiguration.Properties")2443 @patch("ambari_server.serverConfiguration.search_file")2444 @patch("ambari_server.serverConfiguration.get_conf_dir")2445 def test_get_JAVA_HOME(self, get_conf_dir_mock, search_file_mock,2446 Properties_mock, openMock):2447 openMock.side_effect = Exception("exception")2448 result = get_JAVA_HOME()2449 self.assertEqual(None, result)2450 expected = os.path.dirname(__file__)2451 p = MagicMock()2452 p.__getitem__.return_value = expected2453 openMock.side_effect = None2454 Properties_mock.return_value = p2455 result = get_JAVA_HOME()2456 self.assertEqual(expected, result)2457 pass2458 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2459 def test_prompt_db_properties_default(self):2460 args = MagicMock()2461 args.must_set_database_options = False2462 del args.database_index2463 del args.dbms2464 del args.database_host2465 del args.database_port2466 del args.database_name2467 del args.database_username2468 del args.database_password2469 del args.persistence_type2470 prompt_db_properties(args)2471 self.assertEqual(args.database_index, 0)2472 pass2473 @not_for_platform(PLATFORM_WINDOWS)2474 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2475 @patch.object(LinuxDBMSConfig, "_setup_remote_server")2476 @patch("ambari_server.dbConfiguration_linux.print_info_msg")2477 @patch("ambari_server.dbConfiguration_linux.read_password")2478 @patch("ambari_server.dbConfiguration_linux.get_validated_string_input")2479 @patch("ambari_server.dbConfiguration.get_validated_string_input")2480 @patch("ambari_server.serverSetup.get_YN_input")2481 def test_prompt_db_properties_oracle_sname(self, gyni_mock, gvsi_mock, gvsi_2_mock, rp_mock, print_info_msg_mock, srs_mock):2482 gyni_mock.return_value = True2483 list_of_return_values = ["ambari-server", "ambari", "1", "1521", "localhost", "2"]2484 def side_effect(*args, **kwargs):2485 return list_of_return_values.pop()2486 gvsi_mock.side_effect = side_effect2487 gvsi_2_mock.side_effect = side_effect2488 rp_mock.return_value = "password"2489 args = MagicMock()2490 args.must_set_database_options = True2491 del args.database_index2492 del args.dbms2493 del args.database_host2494 del args.database_port2495 del args.database_name2496 del args.database_username2497 del args.database_password2498 del args.persistence_type2499 del args.sid_or_sname2500 del args.jdbc_url2501 set_silent(False)2502 prompt_db_properties(args)2503 self.assertEqual(args.database_index, 1)2504 props = Properties()2505 factory = DBMSConfigFactory()2506 dbmsConfig = factory.create(args, props)2507 self.assertEqual(dbmsConfig.dbms, "oracle")2508 self.assertEqual(dbmsConfig.database_port, "1521")2509 self.assertEqual(dbmsConfig.database_host, "localhost")2510 self.assertEqual(dbmsConfig.database_name, "ambari")2511 self.assertEqual(dbmsConfig.database_username, "ambari")2512 self.assertEqual(dbmsConfig.database_password, "bigdata")2513 self.assertEqual(dbmsConfig.sid_or_sname, "sid")2514 dbmsConfig.configure_database(props)2515 self.assertEqual(dbmsConfig.database_username, "ambari-server")2516 self.assertEqual(dbmsConfig.sid_or_sname, "sname")2517 pass2518 @not_for_platform(PLATFORM_WINDOWS)2519 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2520 @patch.object(LinuxDBMSConfig, "_setup_remote_server")2521 @patch("ambari_server.dbConfiguration_linux.print_info_msg")2522 @patch("ambari_server.dbConfiguration_linux.read_password")2523 @patch("ambari_server.dbConfiguration_linux.get_validated_string_input")2524 @patch("ambari_server.dbConfiguration.get_validated_string_input")2525 @patch("ambari_server.serverSetup.get_YN_input")2526 def test_prompt_db_properties_oracle_sid(self, gyni_mock, gvsi_mock, gvsi_2_mock, rp_mock, print_info_msg_mock, srs_mock):2527 gyni_mock.return_value = True2528 list_of_return_values = ["ambari-server", "ambari", "2", "1521", "localhost", "2"]2529 def side_effect(*args, **kwargs):2530 return list_of_return_values.pop()2531 gvsi_mock.side_effect = side_effect2532 gvsi_2_mock.side_effect = side_effect2533 rp_mock.return_value = "password"2534 args = MagicMock()2535 args.must_set_database_options = True2536 del args.database_index2537 del args.dbms2538 del args.database_host2539 del args.database_port2540 del args.database_name2541 del args.database_username2542 del args.database_password2543 del args.persistence_type2544 del args.sid_or_sname2545 del args.jdbc_url2546 set_silent(False)2547 prompt_db_properties(args)2548 self.assertEqual(args.database_index, 1)2549 props = Properties()2550 factory = DBMSConfigFactory()2551 dbmsConfig = factory.create(args, props)2552 self.assertEqual(dbmsConfig.dbms, "oracle")2553 self.assertEqual(dbmsConfig.database_port, "1521")2554 self.assertEqual(dbmsConfig.database_host, "localhost")2555 self.assertEqual(dbmsConfig.database_name, "ambari")2556 self.assertEqual(dbmsConfig.database_username, "ambari")2557 self.assertEqual(dbmsConfig.database_password, "bigdata")2558 dbmsConfig.configure_database(props)2559 self.assertEqual(dbmsConfig.database_username, "ambari-server")2560 self.assertEqual(dbmsConfig.database_password, "password")2561 self.assertEqual(dbmsConfig.sid_or_sname, "sid")2562 pass2563 @not_for_platform(PLATFORM_WINDOWS)2564 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2565 @patch.object(PGConfig, "_setup_local_server")2566 @patch("ambari_server.dbConfiguration_linux.print_info_msg")2567 @patch("ambari_server.dbConfiguration_linux.read_password")2568 @patch("ambari_server.dbConfiguration_linux.get_validated_string_input")2569 @patch("ambari_server.dbConfiguration.get_validated_string_input")2570 @patch("ambari_server.serverSetup.get_YN_input")2571 def test_prompt_db_properties_postgre_adv(self, gyni_mock, gvsi_mock, gvsi_2_mock, rp_mock, print_info_msg_mock, sls_mock):2572 gyni_mock.return_value = True2573 list_of_return_values = ["ambari-server", "ambari", "ambari", "1"]2574 def side_effect(*args, **kwargs):2575 return list_of_return_values.pop()2576 gvsi_mock.side_effect = side_effect2577 gvsi_2_mock.side_effect = side_effect2578 rp_mock.return_value = "password"2579 args = MagicMock()2580 args.must_set_database_options = True2581 del args.database_index2582 del args.dbms2583 del args.database_host2584 del args.database_port2585 del args.database_name2586 del args.database_username2587 del args.database_password2588 del args.persistence_type2589 set_silent(False)2590 prompt_db_properties(args)2591 self.assertEqual(args.database_index, 0)2592 props = Properties()2593 factory = DBMSConfigFactory()2594 dbmsConfig = factory.create(args, props)2595 self.assertEqual(dbmsConfig.dbms, "postgres")2596 self.assertEqual(dbmsConfig.database_port, "5432")2597 self.assertEqual(dbmsConfig.database_host, "localhost")2598 self.assertEqual(dbmsConfig.database_name, "ambari")2599 self.assertEqual(dbmsConfig.database_username, "ambari")2600 self.assertEqual(dbmsConfig.database_password, "bigdata")2601 dbmsConfig.configure_database(props)2602 self.assertEqual(dbmsConfig.database_username, "ambari-server")2603 self.assertEqual(dbmsConfig.database_password, "password")2604 self.assertEqual(dbmsConfig.sid_or_sname, "sid")2605 pass2606 @not_for_platform(PLATFORM_WINDOWS)2607 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2608 @patch("ambari_server.dbConfiguration_linux.store_password_file")2609 @patch("ambari_server.dbConfiguration_linux.read_password")2610 @patch("ambari_server.dbConfiguration_linux.get_validated_string_input")2611 @patch("ambari_server.dbConfiguration_linux.get_YN_input")2612 def test_prompt_db_properties_for_each_database_type(self, gyni_mock, gvsi_mock, rp_mock, spf_mock):2613 """2614 :return: Validates that installation for each database type correctly stores the database type, database name,2615 and optionally the postgres schema name.2616 """2617 from ambari_server import serverConfiguration2618 gyni_mock.return_value = True2619 rp_mock.return_value = "password"2620 spf_mock.return_value = "encrypted password"2621 # Values to use while installing several database types2622 hostname = "localhost"2623 db_name = "db_ambari"2624 postgres_schema = "sc_ambari"2625 port = "1234"2626 oracle_service = "1"2627 oracle_service_name = "ambari"2628 user_name = "ambari"2629 # Input values2630 postgres_embedded_values = [db_name, postgres_schema, hostname]2631 oracle_values = [hostname, port, oracle_service, oracle_service_name, user_name]2632 mysql_values = [hostname, port, db_name, user_name]2633 postgres_external_values = [hostname, port, db_name, postgres_schema, user_name]2634 mssql_values = [hostname, port, db_name, user_name]2635 list_of_return_values = postgres_embedded_values + oracle_values + mysql_values + postgres_external_values + mssql_values2636 list_of_return_values = list_of_return_values[::-1] # Reverse the list since the input will be popped2637 def side_effect(*args, **kwargs):2638 return list_of_return_values.pop()2639 gvsi_mock.side_effect = side_effect2640 if AMBARI_CONF_VAR in os.environ:2641 del os.environ[AMBARI_CONF_VAR]2642 tempdir = tempfile.gettempdir()2643 os.environ[AMBARI_CONF_VAR] = tempdir2644 prop_file = os.path.join(tempdir, "ambari.properties")2645 for i in range(0, 5):2646 # Use the expected path of the ambari.properties file to delete it if it exists, and then create a new one2647 # during each use case.2648 if os.path.exists(prop_file):2649 os.remove(prop_file)2650 with open(prop_file, "w") as f:2651 f.write("server.jdbc.database_name=oldDBName")2652 f.close()2653 serverConfiguration.AMBARI_PROPERTIES_FILE = prop_file2654 args = MagicMock()2655 properties = Properties()2656 args.database_index = i2657 args.silent = False2658 del args.dbms2659 del args.database_host2660 del args.database_port2661 del args.database_name2662 del args.database_username2663 del args.database_password2664 del args.sid_or_sname2665 del args.jdbc_url2666 factory = DBMSConfigFactory()2667 dbConfig = factory.create(args, properties)2668 dbConfig._prompt_db_properties()2669 if dbConfig._is_local_database():2670 dbConfig._setup_local_server(properties)2671 else:2672 dbConfig._setup_remote_server(properties)2673 if i == 0:2674 # Postgres Embedded2675 self.assertEqual(properties[JDBC_DATABASE_PROPERTY], "postgres")2676 self.assertEqual(properties[JDBC_DATABASE_NAME_PROPERTY], db_name)2677 self.assertEqual(properties[JDBC_POSTGRES_SCHEMA_PROPERTY], postgres_schema)2678 self.assertEqual(properties[PERSISTENCE_TYPE_PROPERTY], "local")2679 elif i == 1:2680 # Oracle2681 self.assertEqual(properties[JDBC_DATABASE_PROPERTY], "oracle")2682 self.assertFalse(JDBC_POSTGRES_SCHEMA_PROPERTY in properties.propertyNames())2683 self.assertEqual(properties[PERSISTENCE_TYPE_PROPERTY], "remote")2684 elif i == 2:2685 # MySQL2686 self.assertEqual(properties[JDBC_DATABASE_PROPERTY], "mysql")2687 self.assertFalse(JDBC_POSTGRES_SCHEMA_PROPERTY in properties.propertyNames())2688 self.assertEqual(properties[PERSISTENCE_TYPE_PROPERTY], "remote")2689 elif i == 3:2690 # Postgres External2691 self.assertEqual(properties[JDBC_DATABASE_PROPERTY], "postgres")2692 self.assertEqual(properties[JDBC_DATABASE_NAME_PROPERTY], db_name)2693 self.assertEqual(properties[JDBC_POSTGRES_SCHEMA_PROPERTY], postgres_schema)2694 self.assertEqual(properties[PERSISTENCE_TYPE_PROPERTY], "remote")2695 elif i == 4:2696 # MSSQL2697 self.assertEqual(properties[JDBC_DATABASE_PROPERTY], "mssql")2698 self.assertFalse(JDBC_POSTGRES_SCHEMA_PROPERTY in properties.propertyNames())2699 self.assertEqual(properties[PERSISTENCE_TYPE_PROPERTY], "remote")2700 pass2701 @patch.object(os.path, "exists")2702 @patch.object(os.path, "isfile")2703 def test_validate_jdk(self, isfile_mock, exists_mock):2704 exists_mock.side_effect = [False]2705 result = validate_jdk("path")2706 self.assertFalse(result)2707 exists_mock.side_effect = [True, False]2708 result = validate_jdk("path")2709 self.assertFalse(result)2710 exists_mock.side_effect = [True, True]2711 isfile_mock.return_value = False2712 result = validate_jdk("path")2713 self.assertFalse(result)2714 exists_mock.side_effect = [True, True]2715 isfile_mock.return_value = True2716 result = validate_jdk("path")2717 self.assertTrue(result)2718 pass2719 @patch("glob.glob")2720 @patch("ambari_server.serverConfiguration.get_JAVA_HOME")2721 @patch("ambari_server.serverConfiguration.validate_jdk")2722 def test_find_jdk(self, validate_jdk_mock, get_JAVA_HOME_mock, globMock):2723 get_JAVA_HOME_mock.return_value = "somewhere"2724 validate_jdk_mock.return_value = True2725 result = find_jdk()2726 self.assertEqual("somewhere", result)2727 get_JAVA_HOME_mock.return_value = None2728 globMock.return_value = []2729 result = find_jdk()2730 self.assertEqual(None, result)2731 globMock.return_value = ["one", "two"]2732 result = find_jdk()2733 self.assertNotEqual(None, result)2734 globMock.return_value = ["one", "two"]2735 validate_jdk_mock.side_effect = [False, True]2736 result = find_jdk()2737 self.assertEqual(result, "one")2738 pass2739 @patch("os.path.exists")2740 @patch("zipfile.ZipFile")2741 @patch("os.path.split")2742 @patch("os.listdir")2743 @patch("ambari_server.serverSetup.copy_files")2744 @patch("shutil.rmtree")2745 def test_unpack_jce_policy(self, rmtree_mock, copy_files_mock, os_listdir_mock, os_path_split_mock, zipfile_mock, exists_mock):2746 # Testing the case when the zip file doesn't contains any folder2747 exists_mock.return_value = True2748 zipfile = MagicMock()2749 zipfile_mock.return_value = zipfile2750 zip_members = ["US_export_policy.jar", "local_policy.jar", "README.txt"]2751 zipfile.namelist.return_value = zip_members2752 os_path_split_mock.return_value = [""]2753 expand_jce_zip_file("", "")2754 self.assertTrue(exists_mock.called)2755 self.assertTrue(zipfile_mock.called)2756 self.assertTrue(os_path_split_mock.called)2757 # Testing the case when the zip file contains a folder2758 unziped_jce_path = "jce"2759 os_path_split_mock.return_value = unziped_jce_path2760 expand_jce_zip_file("", "")2761 self.assertTrue(exists_mock.called)2762 self.assertTrue(zipfile_mock.called)2763 self.assertTrue(os_listdir_mock.called)2764 self.assertTrue(copy_files_mock.called)2765 self.assertTrue(rmtree_mock.called)2766 # Testing when the jdk_security_path or jce_zip_path doesn't exist2767 exists_mock.return_value = False2768 try:2769 expand_jce_zip_file("", "")2770 except FatalException:2771 self.assertTrue(True)2772 exists_mock.return_value = True2773 # Testing when zipfile fail with an error2774 zipfile_mock.side_effect = FatalException(1,"Extract error")2775 try:2776 expand_jce_zip_file("", "")2777 except FatalException:2778 self.assertTrue(True)2779 @patch("os.path.exists")2780 @patch("shutil.copy")2781 @patch("os.path.split")2782 @patch("ambari_server.serverSetup.update_properties")2783 @patch.object(JDKSetup, "unpack_jce_policy")2784 @patch("ambari_server.serverSetup.get_ambari_properties")2785 @patch("ambari_commons.os_utils.search_file")2786 @patch("__builtin__.open")2787 def test_setup_jce_policy(self, open_mock, search_file_mock, get_ambari_properties_mock, unpack_jce_policy_mock,2788 update_properties_mock, path_split_mock, shutil_copy_mock, exists_mock):2789 exists_mock.return_value = True2790 properties = Properties()2791 properties.process_pair(JAVA_HOME_PROPERTY, "/java_home")2792 unpack_jce_policy_mock.return_value = 02793 get_ambari_properties_mock.return_value = properties2794 conf_file = 'etc/ambari-server/conf/ambari.properties'2795 search_file_mock.return_value = conf_file2796 path_split_mock.return_value = ["/path/to", "JCEPolicy.zip"]2797 args = ['setup-jce', '/path/to/JCEPolicy.zip']2798 setup_jce_policy(args)2799 shutil_copy_mock.assert_called_with(args[1], configDefaults.SERVER_RESOURCES_DIR)2800 self.assertTrue(unpack_jce_policy_mock.called)2801 self.assertTrue(get_ambari_properties_mock.called)2802 self.assertTrue(update_properties_mock.called)2803 # Testing that if the source and the destination is the same will not try to copy the file2804 path_split_mock.return_value = [configDefaults.SERVER_RESOURCES_DIR, "JCEPolicy.zip"]2805 shutil_copy_mock.reset_mock()2806 setup_jce_policy(args)2807 self.assertFalse(shutil_copy_mock.called)2808 self.assertTrue(unpack_jce_policy_mock.called)2809 self.assertTrue(get_ambari_properties_mock.called)2810 self.assertTrue(update_properties_mock.called)2811 path_split_mock.return_value = ["/path/to", "JCEPolicy.zip"]2812 # Testing with bad path2813 exists_mock.return_value = False2814 try:2815 setup_jce_policy(args)2816 except FatalException:2817 self.assertTrue(True)2818 exists_mock.return_value = True2819 # Testing with an error produced by shutil.copy2820 shutil_copy_mock.reset_mock()2821 shutil_copy_mock.side_effect = FatalException(1, "Error trying to copy the file.")2822 try:2823 setup_jce_policy(args)2824 except FatalException:2825 self.assertTrue(True)2826 # Testing with an error produced by Properties.store function2827 update_properties_mock.side_effect = Exception("Invalid file.")2828 try:2829 setup_jce_policy(args)2830 except Exception:2831 self.assertTrue(True)2832 update_properties_mock.reset_mock()2833 # Testing with an error produced by unpack_jce_policy2834 unpack_jce_policy_mock.side_effect = FatalException(1, "Can not install JCE policy")2835 try:2836 setup_jce_policy(args)2837 except FatalException:2838 self.assertTrue(True)2839 pass2840 @not_for_platform(PLATFORM_WINDOWS)2841 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))2842 @patch("ambari_commons.firewall.run_os_command")2843 @patch("os.path.exists")2844 @patch("os.path.isfile")2845 @patch("ambari_commons.os_utils.remove_file")2846 @patch("ambari_server.dbConfiguration_linux.LinuxDBMSConfig.ensure_jdbc_driver_installed")2847 @patch("ambari_server.dbConfiguration_linux.get_YN_input")2848 @patch("ambari_server.serverSetup.update_properties")2849 @patch("ambari_server.dbConfiguration_linux.get_ambari_properties")2850 @patch("ambari_server.dbConfiguration_linux.store_password_file")2851 @patch("ambari_server.dbConfiguration_linux.run_os_command")2852 @patch("ambari_server.dbConfiguration_linux.PGConfig._configure_postgres")2853 @patch("ambari_server.dbConfiguration_linux.PGConfig._check_postgre_up")2854 @patch("ambari_server.dbConfiguration_linux.PGConfig._is_jdbc_user_changed")2855 @patch("ambari_server.serverSetup.verify_setup_allowed")2856 @patch("ambari_server.dbConfiguration_linux.read_password")2857 @patch("ambari_server.dbConfiguration_linux.get_validated_string_input")2858 @patch("ambari_server.dbConfiguration.get_validated_string_input")2859 @patch("ambari_server.serverSetup.get_YN_input")2860 @patch("ambari_server.serverSetup.get_ambari_properties")2861 @patch("ambari_server.serverSetup.configure_os_settings")2862 @patch("ambari_server.serverSetup.download_and_install_jdk")2863 @patch("ambari_server.serverSetup.check_ambari_user")2864 @patch("ambari_server.serverSetup.check_jdbc_drivers")2865 @patch("ambari_server.serverSetup.disable_security_enhancements")2866 @patch("ambari_server.serverSetup.is_root")2867 @patch("ambari_server.serverSetup.proceedJDBCProperties")2868 @patch("ambari_server.serverSetup.extract_views")2869 @patch("ambari_server.serverSetup.adjust_directory_permissions")2870 @patch("ambari_server.serverSetup.service_setup")2871 @patch("ambari_server.serverSetup.read_ambari_user")2872 @patch("ambari_server.serverSetup.expand_jce_zip_file")2873 def test_setup(self, expand_jce_zip_file_mock, read_ambari_user_mock,2874 service_setup_mock, adjust_dirs_mock, extract_views_mock, proceedJDBCProperties_mock, is_root_mock,2875 disable_security_enhancements_mock, check_jdbc_drivers_mock, check_ambari_user_mock,2876 download_jdk_mock, configure_os_settings_mock, get_ambari_properties_mock,2877 get_YN_input_mock, gvsi_mock, gvsi_1_mock,2878 read_password_mock, verify_setup_allowed_method, is_jdbc_user_changed_mock, check_postgre_up_mock,2879 configure_postgres_mock, run_os_command_1_mock,2880 store_password_file_mock, get_ambari_properties_1_mock, update_properties_mock,2881 get_YN_input_1_mock, ensure_jdbc_driver_installed_mock,2882 remove_file_mock, isfile_mock, exists_mock,2883 run_os_command_mock):2884 hostname = "localhost"2885 db_name = "db_ambari"2886 postgres_schema = "sc_ambari"2887 port = "1234"2888 oracle_service = "1"2889 oracle_service_name = "ambari"2890 user_name = "ambari"2891 args = MagicMock()2892 del args.dbms2893 del args.database_index2894 del args.database_host2895 del args.database_port2896 del args.database_name2897 del args.database_username2898 del args.database_password2899 args.silent = False2900 failed = False2901 properties = Properties()2902 get_YN_input_mock.return_value = False2903 isfile_mock.return_value = False2904 verify_setup_allowed_method.return_value = 02905 exists_mock.return_value = False2906 remove_file_mock.return_value = 02907 run_os_command_mock.return_value = 3,"",""2908 extract_views_mock.return_value = 02909 read_ambari_user_mock.return_value = "ambari"2910 read_password_mock.return_value = "bigdata2"2911 get_ambari_properties_mock.return_value = properties2912 get_ambari_properties_1_mock.return_value = properties2913 store_password_file_mock.return_value = "encrypted_bigdata2"2914 ensure_jdbc_driver_installed_mock.return_value = True2915 check_postgre_up_mock.return_value = (PGConfig.PG_STATUS_RUNNING, 0, "", "")2916 configure_postgres_mock.return_value = (0, "", "")2917 run_os_command_1_mock.return_value = (0, "", "")2918 expand_jce_zip_file_mock.return_value = 02919 def reset_mocks():2920 is_jdbc_user_changed_mock.reset_mock()2921 is_root_mock.reset_mock()2922 disable_security_enhancements_mock.reset_mock()2923 check_jdbc_drivers_mock.reset_mock()2924 check_ambari_user_mock.reset_mock()2925 run_os_command_mock.reset_mock()2926 configure_os_settings_mock.reset_mock()2927 run_os_command_1_mock.reset_mock()2928 get_YN_input_1_mock.reset_mock()2929 update_properties_mock.reset_mock()2930 args = MagicMock()2931 del args.dbms2932 del args.database_index2933 del args.database_host2934 del args.database_port2935 del args.database_name2936 del args.database_username2937 del args.database_password2938 del args.persistence_type2939 del args.sid_or_sname2940 del args.jdbc_url2941 args.jdbc_driver= None2942 args.jdbc_db = None2943 args.silent = False2944 return args2945 # Testing call under non-root2946 is_root_mock.return_value = False2947 try:2948 setup(args)2949 self.fail("Should throw exception")2950 except FatalException as fe:2951 # Expected2952 self.assertTrue("root-level" in fe.reason)2953 pass2954 args = reset_mocks()2955 # Testing calls under root2956 # remote case2957 is_root_mock.return_value = True2958 disable_security_enhancements_mock.return_value = (0, "")2959 check_ambari_user_mock.return_value = (0, False, 'user', None)2960 check_jdbc_drivers_mock.return_value = 02961 download_jdk_mock.return_value = 02962 configure_os_settings_mock.return_value = 02963 result = setup(args)2964 self.assertEqual(None, result)2965 self.assertTrue(check_ambari_user_mock.called)2966 self.assertEqual(1, run_os_command_mock.call_count)2967 #Local case2968 args = reset_mocks()2969 # Input values2970 db_selection_values = ["1"]2971 postgres_values = [db_name, postgres_schema, hostname]2972 postgres_values = postgres_values[::-1] # Reverse the list since the input will be popped2973 def side_effect(*args, **kwargs):2974 return db_selection_values.pop()2975 gvsi_mock.side_effect = side_effect2976 def side_effect_1(*args, **kwargs):2977 return postgres_values.pop()2978 gvsi_1_mock.side_effect = side_effect_12979 get_YN_input_mock.return_value = True2980 # is_local_database_mock.return_value = True2981 is_jdbc_user_changed_mock.return_value = False2982 try:2983 result = setup(args)2984 except FatalException:2985 self.fail("Setup should be successful")2986 self.assertEqual(None, result)2987 self.assertTrue(is_jdbc_user_changed_mock.called)2988 self.assertTrue(update_properties_mock.called)2989 self.assertTrue(run_os_command_1_mock.called)2990 self.assertFalse(remove_file_mock.called)2991 self.assertTrue("Ambari-DDL-Postgres-EMBEDDED-CREATE.sql" in run_os_command_1_mock.call_args[0][0][3])2992 #if DB user name was changed2993 args = reset_mocks()2994 # is_local_database_mock.return_value = True2995 is_jdbc_user_changed_mock.return_value = True2996 db_selection_values = ["1"]2997 postgres_values = [db_name, postgres_schema, hostname]2998 postgres_values = postgres_values[::-1] # Reverse the list since the input will be popped2999 try:3000 result = setup(args)3001 except FatalException:3002 self.fail("Setup should be successful")3003 self.assertEqual(None, result)3004 self.assertTrue(is_jdbc_user_changed_mock.called)3005 self.assertTrue(update_properties_mock.called)3006 self.assertTrue(run_os_command_1_mock.called)3007 self.assertFalse(remove_file_mock.called)3008 #negative case3009 args = reset_mocks()3010 # Use remote database3011 get_YN_input_1_mock.return_value = False3012 db_selection_values = ["4"]3013 postgres_values = [hostname, port, db_name, postgres_schema, user_name]3014 postgres_values = postgres_values[::-1] # Reverse the list since the input will be popped3015 try:3016 result = setup(args)3017 self.fail("Should throw exception")3018 except NonFatalException as fe:3019 self.assertTrue("Remote database setup aborted." in fe.reason)3020 self.assertFalse(run_os_command_1_mock.called)3021 # test not run setup if ambari-server setup executed with jdbc properties3022 args = reset_mocks()3023 # is_server_runing_mock.return_value = (False, 1)3024 args.jdbc_driver= "path/to/driver"3025 args.jdbc_db = "test_db_name"3026 setup(args)3027 self.assertTrue(proceedJDBCProperties_mock.called)3028 self.assertFalse(disable_security_enhancements_mock.called)3029 self.assertFalse(check_ambari_user_mock.called)3030 pass3031 @only_for_platform(PLATFORM_WINDOWS)3032 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3033 @patch("ambari_commons.firewall.run_os_command")3034 @patch("os.path.exists")3035 @patch("os.path.isfile")3036 @patch("ambari_commons.os_utils.remove_file")3037 @patch("ambari_server.dbConfiguration_windows.MSSQLConfig.ensure_jdbc_driver_installed")3038 @patch("ambari_server.serverSetup.update_properties")3039 @patch("ambari_server.dbConfiguration_windows.store_password_file")3040 @patch("ambari_server.dbConfiguration_windows.run_os_command")3041 @patch("ambari_server.serverSetup.verify_setup_allowed")3042 @patch("ambari_server.dbConfiguration_windows.get_validated_string_input")3043 @patch("ambari_server.dbConfiguration.get_validated_string_input")3044 @patch("ambari_server.serverSetup.get_YN_input")3045 @patch("ambari_server.serverSetup.get_ambari_properties")3046 @patch("ambari_server.serverSetup.configure_os_settings")3047 @patch("ambari_server.serverSetup.download_and_install_jdk")3048 @patch("ambari_server.serverSetup.check_firewall")3049 @patch("ambari_server.serverSetup.check_ambari_user")3050 @patch("ambari_server.serverSetup.check_jdbc_drivers")3051 @patch("ambari_server.serverSetup.disable_security_enhancements")3052 @patch("ambari_server.serverSetup.is_root")3053 @patch("ambari_server.serverSetup.proceedJDBCProperties")3054 @patch("ambari_server.serverSetup.extract_views")3055 @patch("ambari_server.serverSetup.adjust_directory_permissions")3056 @patch("ambari_server.serverSetup.service_setup")3057 @patch("ambari_server.serverSetup.read_ambari_user")3058 @patch("ambari_server.serverSetup.expand_jce_zip_file")3059 def test_setup(self, expand_jce_zip_file_mock, read_ambari_user_mock,3060 service_setup_mock, adjust_dirs_mock, extract_views_mock, proceedJDBCProperties_mock, is_root_mock,3061 disable_security_enhancements_mock, check_jdbc_drivers_mock, check_ambari_user_mock, check_firewall_mock,3062 download_jdk_mock, configure_os_settings_mock, get_ambari_properties_mock,3063 get_YN_input_mock, gvsi_mock, gvsi_1_mock,3064 verify_setup_allowed_method, run_os_command_1_mock,3065 store_password_file_mock, update_properties_mock,3066 ensure_jdbc_driver_installed_mock,3067 remove_file_mock, isfile_mock, exists_mock,3068 run_os_command_mock):3069 hostname = "localhost"3070 db_name = "db_ambari"3071 port = "1433"3072 user_name = "ambari"3073 password = "bigdata2"3074 failed = False3075 properties = Properties()3076 get_YN_input_mock.return_value = False3077 isfile_mock.return_value = False3078 verify_setup_allowed_method.return_value = 03079 exists_mock.return_value = False3080 remove_file_mock.return_value = 03081 run_os_command_mock.return_value = 3,"",""3082 extract_views_mock.return_value = 03083 read_ambari_user_mock.return_value = "ambari"3084 #read_password_mock.return_value = "bigdata2"3085 get_ambari_properties_mock.return_value = properties3086 store_password_file_mock.return_value = "encrypted_bigdata2"3087 ensure_jdbc_driver_installed_mock.return_value = True3088 run_os_command_1_mock.return_value = (0, "", "")3089 expand_jce_zip_file_mock.return_value = 03090 def reset_mocks():3091 is_root_mock.reset_mock()3092 disable_security_enhancements_mock.reset_mock()3093 check_jdbc_drivers_mock.reset_mock()3094 check_ambari_user_mock.reset_mock()3095 run_os_command_mock.reset_mock()3096 configure_os_settings_mock.reset_mock()3097 run_os_command_1_mock.reset_mock()3098 update_properties_mock.reset_mock()3099 args = MagicMock()3100 del args.dbms3101 del args.database_index3102 del args.database_host3103 del args.database_port3104 del args.database_name3105 del args.database_username3106 del args.database_password3107 del args.default_database_host3108 del args.persistence_type3109 del args.init_db_script_file3110 del args.cleanup_db_script_file3111 del args.sid_or_sname3112 del args.jdbc_url3113 args.jdbc_driver= None3114 args.jdbc_db = None3115 args.silent = False3116 args.must_set_database_options = True3117 return args3118 args = reset_mocks()3119 # Testing call under non-root3120 is_root_mock.return_value = False3121 try:3122 setup(args)3123 self.fail("Should throw exception")3124 except FatalException as fe:3125 # Expected3126 self.assertTrue("administrator-level" in fe.reason)3127 pass3128 args = reset_mocks()3129 # Testing calls under root3130 is_root_mock.return_value = True3131 disable_security_enhancements_mock.return_value = (0, "")3132 check_ambari_user_mock.return_value = (0, False, 'user', None)3133 check_jdbc_drivers_mock.return_value = 03134 download_jdk_mock.return_value = 03135 configure_os_settings_mock.return_value = 03136 result = setup(args)3137 self.assertEqual(None, result)3138 self.assertTrue(check_ambari_user_mock.called)3139 self.assertEqual(2, run_os_command_1_mock.call_count)3140 #negative case3141 args = reset_mocks()3142 # Use Windows authentication3143 get_YN_input_mock.return_value = True3144 gvsi_1_mock.side_effect = [hostname, "1"]3145 try:3146 result = setup(args)3147 except Exception:3148 self.fail("Shouldn't throw exception")3149 self.assertTrue(run_os_command_1_mock.called)3150 # Use SQL Server authentication3151 get_YN_input_mock.return_value = True3152 gvsi_1_mock.side_effect = [hostname, "2", user_name, password]3153 try:3154 result = setup(args)3155 except Exception:3156 self.fail("Shouldn't throw exception")3157 self.assertTrue(run_os_command_1_mock.called)3158 # test not run setup if ambari-server setup executed with jdbc properties3159 args = reset_mocks()3160 args.jdbc_driver= "path/to/driver"3161 args.jdbc_db = "test_db_name"3162 setup(args)3163 self.assertTrue(proceedJDBCProperties_mock.called)3164 self.assertFalse(disable_security_enhancements_mock.called)3165 self.assertFalse(check_ambari_user_mock.called)3166 pass3167 @not_for_platform(PLATFORM_WINDOWS)3168 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3169 @patch.object(OracleConfig, "_get_remote_script_line")3170 @patch("ambari_server.serverSetup.is_server_runing")3171 @patch("ambari_server.dbConfiguration_linux.get_YN_input")3172 @patch("ambari_server.serverSetup.get_YN_input")3173 @patch.object(PGConfig, "_setup_db")3174 @patch("ambari_server.dbConfiguration_linux.print_warning_msg")3175 @patch("ambari_server.dbConfiguration_linux.print_info_msg")3176 @patch("ambari_server.dbConfiguration_linux.run_os_command")3177 @patch("ambari_server.dbConfiguration.decrypt_password_for_alias")3178 @patch("ambari_server.serverSetup.get_ambari_properties")3179 @patch("ambari_server.serverSetup.is_root")3180 def test_reset(self, is_root_mock, get_ambari_properties_mock, decrypt_password_for_alias_mock,3181 run_os_command_mock, print_info_msg_mock, print_warning_msg_mock,3182 setup_db_mock, get_YN_input_mock, get_YN_input_2_mock, is_server_running_mock,3183 get_remote_script_line_mock):3184 def reset_mocks():3185 args = MagicMock()3186 del args.dbms3187 del args.database_index3188 del args.database_host3189 del args.database_port3190 del args.database_name3191 del args.database_username3192 del args.database_password3193 del args.persistence_type3194 del args.init_script_file3195 del args.drop_script_file3196 del args.sid_or_sname3197 del args.jdbc_url3198 return args3199 properties = Properties()3200 get_ambari_properties_mock.return_value = properties3201 args = reset_mocks()3202 args.persistence_type = "local"3203 get_YN_input_mock.return_value = False3204 decrypt_password_for_alias_mock.return_value = "password"3205 is_server_running_mock.return_value = (False, 0)3206 setup_db_mock.side_effect = [(0,None, None),(0,None, "ERROR: database 'ambari' is being accessed by other users"), (0, None, "ERROR: user 'mapred' already exist")]3207 # Testing call under non-root3208 is_root_mock.return_value = False3209 try:3210 reset(args)3211 self.fail("Should throw exception")3212 except FatalException as fe:3213 # Expected3214 self.assertTrue("root-level" in fe.reason)3215 pass3216 # Testing calls under root3217 is_root_mock.return_value = True3218 try:3219 reset(args)3220 self.fail("Should throw exception")3221 except FatalException as fe:3222 # Expected3223 self.assertFalse("root-level" in fe.reason)3224 pass3225 get_YN_input_mock.return_value = True3226 get_YN_input_2_mock.return_value = True3227 run_os_command_mock.return_value = (1, None, None)3228 try:3229 reset(args)3230 self.fail("Should throw exception")3231 except FatalException:3232 # Expected3233 pass3234 run_os_command_mock.return_value = (0, None, None)3235 reset(args)3236 self.assertTrue(setup_db_mock.called)3237 # Database errors cases3238 is_server_running_mock.side_effect = [(True, 123), (False, 0), (False, 0), (False, 0), (False, 0)]3239 try:3240 reset(args)3241 self.fail("Should throw exception")3242 except FatalException:3243 # Expected3244 pass3245 try:3246 reset(args)3247 self.fail("Should throw exception")3248 except NonFatalException:3249 # Expected3250 pass3251 args = reset_mocks()3252 args.dbms = "postgres"3253 #get_remote_script_line_mock.return_value = None3254 try:3255 #remote db case3256 reset(args)3257 self.fail("Should throw exception")3258 except NonFatalException:3259 # Expected3260 pass3261 args = reset_mocks()3262 args.dbms = "oracle"3263 print_warning_msg_mock.reset_mock()3264 get_remote_script_line_mock.reset_mock()3265 get_remote_script_line_mock.side_effect = ["drop", "create"]3266 try:3267 #remote db case (not Postgres)3268 rcode = reset(args)3269 self.fail("Should throw exception")3270 except NonFatalException:3271 # Expected3272 self.assertTrue(get_remote_script_line_mock.called)3273 self.assertTrue(print_warning_msg_mock.called)3274 pass3275 pass3276 @only_for_platform(PLATFORM_WINDOWS)3277 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3278 @patch("ambari_server.serverSetup.is_server_runing")3279 @patch("ambari_server.serverSetup.get_YN_input")3280 @patch("ambari_server.dbConfiguration_windows.print_warning_msg")3281 @patch("ambari_server.dbConfiguration_windows.print_info_msg")3282 @patch("ambari_server.dbConfiguration_windows.run_os_command")3283 @patch("ambari_server.dbConfiguration.decrypt_password_for_alias")3284 @patch("ambari_server.serverSetup.get_ambari_properties")3285 @patch("ambari_server.serverSetup.is_root")3286 def test_reset(self, is_root_mock, get_ambari_properties_mock, decrypt_password_for_alias_mock,3287 run_os_command_mock, print_info_msg_mock, print_warning_msg_mock,3288 get_YN_input_mock, is_server_running_mock):3289 def reset_mocks():3290 args = MagicMock()3291 del args.dbms3292 del args.database_index3293 del args.database_host3294 del args.database_port3295 del args.database_name3296 del args.database_username3297 del args.database_password3298 del args.default_database_host3299 del args.persistence_type3300 del args.init_db_script_file3301 del args.cleanup_db_script_file3302 del args.sid_or_sname3303 del args.jdbc_url3304 return args3305 properties = Properties()3306 get_ambari_properties_mock.return_value = properties3307 args = reset_mocks()3308 args.persistence_type = "local"3309 get_YN_input_mock.return_value = False3310 decrypt_password_for_alias_mock.return_value = "password"3311 is_server_running_mock.return_value = (False, 0)3312 # Testing call under non-root3313 is_root_mock.return_value = False3314 try:3315 reset(args)3316 self.fail("Should throw exception")3317 except FatalException as fe:3318 # Expected3319 self.assertTrue("administrator-level" in fe.reason)3320 pass3321 # Testing calls under root3322 is_root_mock.return_value = True3323 try:3324 reset(args)3325 self.fail("Should throw exception")3326 except FatalException as fe:3327 # Expected3328 self.assertFalse("administrator-level" in fe.reason)3329 pass3330 get_YN_input_mock.return_value = True3331 run_os_command_mock.return_value = (1, None, None)3332 try:3333 reset(args)3334 self.fail("Should throw exception")3335 except FatalException:3336 # Expected3337 pass3338 run_os_command_mock.reset_mock()3339 run_os_command_mock.return_value = (0, None, None)3340 reset(args)3341 self.assertTrue(run_os_command_mock.called)3342 self.assertEqual(run_os_command_mock.call_count, 2)3343 # Database errors cases3344 is_server_running_mock.side_effect = [(True, 123), (False, 0)]3345 try:3346 reset(args)3347 self.fail("Should throw exception")3348 except FatalException:3349 # Expected3350 pass3351 try:3352 reset(args)3353 except NonFatalException:3354 self.fail("Shouldn't throw exception")3355 pass3356 pass3357 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3358 @patch("ambari_server.serverSetup.get_YN_input")3359 @patch("__builtin__.raw_input")3360 @patch("ambari_server.serverSetup.is_root")3361 def test_reset_default(self, is_root_mock, raw_input_mock, get_YN_inputMock):3362 is_root_mock.return_value=True3363 get_YN_inputMock.return_value = False3364 raw_input_mock.return_value=""3365 args = MagicMock()3366 try:3367 reset(args)3368 self.fail("Should throw exception")3369 except FatalException as fe:3370 # Expected3371 self.assertTrue(fe.code == 1)3372 pass3373 pass3374 @not_for_platform(PLATFORM_WINDOWS)3375 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3376 @patch.object(PGConfig, "_setup_db")3377 @patch("ambari_server.dbConfiguration_linux.print_info_msg")3378 @patch("ambari_server.dbConfiguration_linux.run_os_command")3379 #@patch("ambari_server.serverSetup.parse_properties_file")3380 @patch("ambari_server.serverSetup.is_root")3381 #@patch("ambari_server.serverSetup.check_database_name_property")3382 @patch("ambari_server.serverSetup.is_server_runing")3383 def test_silent_reset(self, is_server_runing_mock, #check_database_name_property_mock,3384 is_root_mock, #parse_properties_file_mock,3385 run_os_command_mock, print_info_msg_mock,3386 setup_db_mock):3387 is_root_mock.return_value = True3388 args = MagicMock()3389 del args.dbms3390 del args.database_index3391 del args.database_host3392 del args.database_port3393 del args.database_name3394 del args.database_username3395 del args.database_password3396 del args.persistence_type3397 set_silent(True)3398 self.assertTrue(get_silent())3399 setup_db_mock.return_value = (0, None, None)3400 run_os_command_mock.return_value = (0, None, None)3401 is_server_runing_mock.return_value = (False, 0)3402 def signal_handler(signum, frame):3403 self.fail("Timed out!")3404 signal.signal(signal.SIGALRM, signal_handler)3405 try:3406 signal.alarm(5)3407 rcode = reset(args)3408 signal.alarm(0)3409 self.assertEqual(None, rcode)3410 self.assertTrue(setup_db_mock.called)3411 finally:3412 signal.signal(signal.SIGALRM, signal.SIG_IGN)3413 pass3414 @only_for_platform(PLATFORM_WINDOWS)3415 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3416 @patch("ambari_server.dbConfiguration_windows.MSSQLConfig._execute_db_script")3417 @patch("ambari_server.serverSetup.get_ambari_properties")3418 @patch("ambari_server.serverSetup.is_root")3419 @patch("ambari_server.serverSetup.is_server_runing")3420 def test_silent_reset(self, is_server_runing_mock,3421 is_root_mock, get_ambari_properties_mock,3422 execute_db_script_mock):3423 is_root_mock.return_value = True3424 args = MagicMock()3425 del args.dbms3426 del args.database_index3427 del args.database_host3428 del args.database_port3429 del args.database_name3430 del args.database_username3431 del args.database_password3432 del args.default_database_host3433 del args.persistence_type3434 del args.init_db_script_file3435 del args.cleanup_db_script_file3436 set_silent(True)3437 self.assertTrue(get_silent())3438 properties = Properties()3439 get_ambari_properties_mock.return_value = properties3440 is_server_runing_mock.return_value = (False, 0)3441 rcode = reset(args)3442 self.assertEqual(None, rcode)3443 self.assertEqual(execute_db_script_mock.call_count, 2)3444 @not_for_platform(PLATFORM_WINDOWS)3445 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3446 @patch("sys.stdout.flush")3447 @patch("sys.stdout.write")3448 @patch("ambari_server_main.looking_for_pid")3449 @patch("ambari_server_main.wait_for_pid")3450 @patch("ambari_server_main.save_main_pid_ex")3451 @patch("ambari_server_main.check_exitcode")3452 @patch("os.makedirs")3453 @patch("ambari_server_main.locate_file")3454 @patch.object(_ambari_server_, "is_server_runing")3455 @patch("os.chown")3456 @patch("ambari_server.setupSecurity.get_master_key_location")3457 @patch("ambari_server.setupSecurity.save_master_key")3458 @patch("ambari_server_main.get_is_persisted")3459 @patch("ambari_server_main.get_is_secure")3460 @patch('os.chmod', autospec=True)3461 @patch("ambari_server.serverConfiguration.write_property")3462 @patch("ambari_server.serverConfiguration.get_validated_string_input")3463 @patch("os.environ")3464 @patch("ambari_server.setupSecurity.get_ambari_properties")3465 @patch("ambari_server.serverSetup.get_ambari_properties")3466 @patch("ambari_server.serverConfiguration.get_ambari_properties")3467 @patch("ambari_server_main.get_ambari_properties")3468 @patch("os.path.exists")3469 @patch("__builtin__.open")3470 @patch("subprocess.Popen")3471 @patch("ambari_server.serverConfiguration.search_file")3472 @patch("ambari_server_main.check_database_name_property")3473 @patch("ambari_server_main.find_jdk")3474 @patch("ambari_server_main.print_warning_msg")3475 @patch("ambari_server_main.print_info_msg")3476 @patch.object(PGConfig, "_check_postgre_up")3477 @patch("ambari_server_main.read_ambari_user")3478 @patch("ambari_server.setupSecurity.is_root")3479 @patch("ambari_server.dbConfiguration_linux.is_root")3480 @patch("ambari_server_main.is_root")3481 @patch.object(LinuxDBMSConfig, "_find_jdbc_driver")3482 @patch("getpass.getuser")3483 @patch("os.chdir")3484 @patch.object(ResourceFilesKeeper, "perform_housekeeping")3485 def test_start(self, perform_housekeeping_mock, chdir_mock, getuser_mock, find_jdbc_driver_mock,3486 is_root_mock, is_root_2_mock, is_root_3_mock, read_ambari_user_mock,3487 check_postgre_up_mock, print_info_msg_mock, print_warning_msg_mock,3488 find_jdk_mock, check_database_name_property_mock, search_file_mock,3489 popenMock, openMock, pexistsMock,3490 get_ambari_properties_mock, get_ambari_properties_2_mock, get_ambari_properties_3_mock,3491 get_ambari_properties_4_mock, os_environ_mock,3492 get_validated_string_input_method, write_property_method,3493 os_chmod_method, get_is_secure_mock, get_is_persisted_mock,3494 save_master_key_method, get_master_key_location_method,3495 os_chown_mock, is_server_running_mock, locate_file_mock,3496 os_makedirs_mock, check_exitcode_mock, save_main_pid_ex_mock,3497 wait_for_pid_mock, looking_for_pid_mock, stdout_write_mock, stdout_flush_mock):3498 def reset_mocks():3499 pexistsMock.reset_mock()3500 args = MagicMock()3501 del args.dbms3502 del args.database_index3503 del args.database_host3504 del args.database_port3505 del args.database_name3506 del args.database_username3507 del args.database_password3508 del args.persistence_type3509 del args.sid_or_sname3510 del args.jdbc_url3511 del args.debug3512 del args.suspend_start3513 return args3514 args = reset_mocks()3515 locate_file_mock.side_effect = lambda *args: '/bin/su' if args[0] == 'su' else '/bin/sh'3516 f = MagicMock()3517 f.readline.return_value = '42'3518 openMock.return_value = f3519 looking_for_pid_mock.return_value = [{3520 "pid": "777",3521 "exe": "/test",3522 "cmd": "test arg"3523 }]3524 wait_for_pid_mock.return_value = 13525 check_exitcode_mock.return_value = 03526 p = Properties()3527 p.process_pair(SECURITY_IS_ENCRYPTION_ENABLED, 'False')3528 get_ambari_properties_4_mock.return_value = \3529 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \3530 get_ambari_properties_mock.return_value = p3531 get_is_secure_mock.return_value = False3532 get_is_persisted_mock.return_value = (False, None)3533 search_file_mock.return_value = None3534 is_server_running_mock.return_value = (True, 123)3535 os_chown_mock.return_value = None3536 # Checking "server is running"3537 pexistsMock.return_value = True3538 if get_platform() != PLATFORM_WINDOWS:3539 with patch("pwd.getpwnam") as getpwnam_mock:3540 pw = MagicMock()3541 pw.setattr('pw_uid', 0)3542 pw.setattr('pw_gid', 0)3543 getpwnam_mock.return_value = pw3544 try:3545 _ambari_server_.start(args)3546 self.fail("Should fail with 'Server is running'")3547 except FatalException as e:3548 # Expected3549 self.assertTrue('Ambari Server is already running.' in e.reason)3550 args = reset_mocks()3551 is_server_running_mock.return_value = (False, 0)3552 pexistsMock.return_value = False3553 # Checking situation when ambari user is not set up3554 read_ambari_user_mock.return_value = None3555 try:3556 _ambari_server_.start(args)3557 self.fail("Should fail with 'Can not detect a system user for Ambari'")3558 except FatalException as e:3559 # Expected3560 self.assertTrue('Unable to detect a system user for Ambari Server.' in e.reason)3561 # Checking start from non-root when current user is not the same as a3562 # custom user3563 args = reset_mocks()3564 read_ambari_user_mock.return_value = "dummy-user"3565 getuser_mock.return_value = "non_custom_user"3566 is_root_3_mock.return_value = \3567 is_root_2_mock.return_value = \3568 is_root_mock.return_value = False3569 try:3570 _ambari_server_.start(args)3571 self.fail("Should fail with 'Can not start ambari-server as user...'")3572 except FatalException as e:3573 # Expected3574 self.assertTrue('Unable to start Ambari Server as user' in e.reason)3575 #self.assertFalse(parse_properties_file_mock.called)3576 # Checking "jdk not found"3577 args = reset_mocks()3578 is_root_3_mock.return_value = \3579 is_root_2_mock.return_value = \3580 is_root_mock.return_value = True3581 find_jdk_mock.return_value = None3582 try:3583 _ambari_server_.start(args)3584 self.fail("Should fail with 'No JDK found'")3585 except FatalException as e:3586 # Expected3587 self.assertTrue('No JDK found' in e.reason)3588 args = reset_mocks()3589 find_jdk_mock.return_value = "somewhere"3590 ## Testing workflow under root3591 is_root_3_mock.return_value = \3592 is_root_2_mock.return_value = \3593 is_root_mock.return_value = True3594 # Remote DB3595 p.process_pair(JDBC_DATABASE_PROPERTY, 'oracle')3596 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'remote')3597 # Case when jdbc driver is not used3598 find_jdbc_driver_mock.return_value = -13599 try:3600 _ambari_server_.start(args)3601 self.fail("Should fail with exception")3602 except FatalException as e:3603 self.assertTrue('Before starting Ambari Server' in e.reason)3604 args = reset_mocks()3605 # Remote DB3606 p.process_pair(JDBC_DATABASE_PROPERTY, 'oracle')3607 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'remote')3608 find_jdbc_driver_mock.reset_mock()3609 find_jdbc_driver_mock.return_value = -13610 try:3611 _ambari_server_.start(args)3612 except FatalException as e:3613 # Ignored3614 pass3615 args = reset_mocks()3616 # Remote DB3617 p.process_pair(JDBC_DATABASE_PROPERTY, 'oracle')3618 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'remote')3619 find_jdbc_driver_mock.reset_mock()3620 find_jdbc_driver_mock.return_value = 03621 # Test exception handling on resource files housekeeping3622 perform_housekeeping_mock.reset_mock()3623 perform_housekeeping_mock.side_effect = KeeperException("some_reason")3624 pexistsMock.return_value = True3625 try:3626 _ambari_server_.start(args)3627 self.fail("Should fail with exception")3628 except FatalException as e:3629 self.assertTrue('some_reason' in e.reason)3630 self.assertTrue(perform_housekeeping_mock.called)3631 perform_housekeeping_mock.side_effect = lambda *v, **kv : None3632 perform_housekeeping_mock.reset_mock()3633 self.assertFalse('Unable to start PostgreSQL server' in e.reason)3634 self.assertFalse(check_postgre_up_mock.called)3635 args = reset_mocks()3636 # Local DB3637 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3638 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'local')3639 check_postgre_up_mock.reset_mock()3640 # case: postgres failed to start3641 check_postgre_up_mock.return_value = None, 1, "Unable to start PostgreSQL serv", "error"3642 try:3643 _ambari_server_.start(args)3644 self.fail("Should fail with 'Unable to start PostgreSQL server'")3645 except FatalException as e:3646 # Expected3647 self.assertTrue('Unable to start PostgreSQL server' in e.reason)3648 self.assertTrue(check_postgre_up_mock.called)3649 args = reset_mocks()3650 # Local DB3651 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3652 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'local')3653 check_postgre_up_mock.return_value = "running", 0, "success", ""3654 # Case: custom user is "root"3655 read_ambari_user_mock.return_value = "root"3656 # Java failed to start3657 proc = MagicMock()3658 proc.pid = -1863659 popenMock.return_value = proc3660 try:3661 _ambari_server_.start(args)3662 except FatalException as e:3663 # Expected3664 self.assertTrue(popenMock.called)3665 self.assertTrue('Ambari Server java process died' in e.reason)3666 self.assertTrue(perform_housekeeping_mock.called)3667 args = reset_mocks()3668 # Java OK3669 proc.pid = 1863670 popenMock.reset_mock()3671 _ambari_server_.start(args)3672 self.assertTrue(popenMock.called)3673 popen_arg = popenMock.call_args[0][0]3674 self.assertTrue(popen_arg[0] == "/bin/sh")3675 self.assertTrue(perform_housekeeping_mock.called)3676 args = reset_mocks()3677 # Local DB3678 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3679 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'local')3680 perform_housekeeping_mock.reset_mock()3681 popenMock.reset_mock()3682 # Case: custom user is not "root"3683 read_ambari_user_mock.return_value = "not-root-user"3684 _ambari_server_.start(args)3685 self.assertTrue(chdir_mock.called)3686 self.assertTrue(popenMock.called)3687 popen_arg = popenMock.call_args_list[0][0][0]3688 self.assertTrue("; /bin/su" in popen_arg[2])3689 self.assertTrue(perform_housekeeping_mock.called)3690 args = reset_mocks()3691 # Local DB3692 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3693 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'local')3694 check_postgre_up_mock.reset_mock()3695 popenMock.reset_mock()3696 ## Testing workflow under non-root3697 is_root_3_mock.return_value = \3698 is_root_2_mock.return_value = \3699 is_root_mock.return_value = False3700 read_ambari_user_mock.return_value = "not-root-user"3701 getuser_mock.return_value = read_ambari_user_mock.return_value3702 _ambari_server_.start(args)3703 self.assertFalse(check_postgre_up_mock.called)3704 args = reset_mocks()3705 # Remote DB3706 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3707 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'remote')3708 _ambari_server_.start(args)3709 self.assertFalse(check_postgre_up_mock.called)3710 args = reset_mocks()3711 # Remote DB3712 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3713 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'remote')3714 # Checking call3715 _ambari_server_.start(args)3716 self.assertTrue(popenMock.called)3717 popen_arg = popenMock.call_args[0][0]3718 self.assertTrue(popen_arg[0] == "/bin/sh")3719 args = reset_mocks()3720 # Remote DB3721 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3722 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'remote')3723 # Test start under wrong user3724 read_ambari_user_mock.return_value = "not-root-user"3725 getuser_mock.return_value = "non_custom_user"3726 try:3727 _ambari_server_.start(args)3728 self.fail("Can not start ambari-server as user non_custom_user.")3729 except FatalException as e:3730 # Expected3731 self.assertTrue('Unable to start Ambari Server as user' in e.reason)3732 args = reset_mocks()3733 # Check environ master key is set3734 popenMock.reset_mock()3735 os_environ_mock.copy.return_value = {"a": "b",3736 SECURITY_KEY_ENV_VAR_NAME: "masterkey"}3737 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3738 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'local')3739 read_ambari_user_mock.return_value = "root"3740 is_root_3_mock.return_value = \3741 is_root_2_mock.return_value = \3742 is_root_mock.return_value = True3743 _ambari_server_.start(args)3744 self.assertFalse(get_validated_string_input_method.called)3745 self.assertFalse(save_master_key_method.called)3746 popen_arg = popenMock.call_args[1]['env']3747 self.assertEquals(os_environ_mock.copy.return_value, popen_arg)3748 args = reset_mocks()3749 # Check environ master key is not set3750 popenMock.reset_mock()3751 os_environ_mock.reset_mock()3752 p.process_pair(SECURITY_IS_ENCRYPTION_ENABLED, 'True')3753 os_environ_mock.copy.return_value = {"a": "b"}3754 p.process_pair(JDBC_DATABASE_PROPERTY, 'postgres')3755 p.process_pair(PERSISTENCE_TYPE_PROPERTY, 'local')3756 read_ambari_user_mock.return_value = "root"3757 is_root_3_mock.return_value = \3758 is_root_2_mock.return_value = \3759 is_root_mock.return_value = True3760 get_validated_string_input_method.return_value = "masterkey"3761 os_chmod_method.return_value = None3762 get_is_secure_mock.return_value = True3763 _ambari_server_.start(args)3764 self.assertTrue(get_validated_string_input_method.called)3765 self.assertTrue(save_master_key_method.called)3766 popen_arg = popenMock.call_args[1]['env']3767 self.assertEquals(os_environ_mock.copy.return_value, popen_arg)3768 pass3769 @not_for_platform(PLATFORM_WINDOWS)3770 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3771 @patch.object(_ambari_server_, "is_server_runing")3772 @patch("os.remove")3773 @patch("os.killpg")3774 @patch("os.getpgid")3775 @patch.object(_ambari_server_, "print_info_msg")3776 def test_stop(self, print_info_msg_mock, gpidMock, removeMock,3777 killMock, isServerRuningMock):3778 isServerRuningMock.return_value = (True, 123)3779 _ambari_server_.stop(None)3780 self.assertTrue(killMock.called)3781 self.assertTrue(removeMock.called)3782 pass3783 @only_for_platform(PLATFORM_WINDOWS)3784 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3785 @patch("win32serviceutil.StopServiceWithDeps")3786 @patch("win32serviceutil.StopService")3787 @patch("win32serviceutil.WaitForServiceStatus")3788 def test_stop(self, WaitForServiceStatusMock, StopServiceMock, StopServiceWithDepsMock):3789 _ambari_server_.stop()3790 self.assertTrue(StopServiceWithDepsMock.called)3791 self.assertFalse(StopServiceMock.called)3792 self.assertTrue(WaitForServiceStatusMock.called)3793 pass3794 @patch.object(_ambari_server_, "BackupRestore_main")3795 def test_backup(self, bkrestore_mock):3796 args = ["", "/some/path/file.zip"]3797 _ambari_server_.backup(args)3798 self.assertTrue(bkrestore_mock.called)3799 pass3800 @patch.object(_ambari_server_, "BackupRestore_main")3801 def test_backup_no_path(self, bkrestore_mock):3802 args = [""]3803 _ambari_server_.backup(args)3804 self.assertTrue(bkrestore_mock.called)3805 pass3806 @patch.object(_ambari_server_, "BackupRestore_main")3807 def test_restore(self, bkrestore_mock):3808 args = ["", "/some/path/file.zip"]3809 _ambari_server_.restore(args)3810 self.assertTrue(bkrestore_mock.called)3811 pass3812 @patch.object(_ambari_server_, "BackupRestore_main")3813 def test_restore_no_path(self, bkrestore_mock):3814 args = [""]3815 _ambari_server_.restore(args)3816 self.assertTrue(bkrestore_mock.called)3817 pass3818 @patch("ambari_server.serverUpgrade.is_root")3819 @patch("ambari_server.serverUpgrade.check_database_name_property")3820 @patch("ambari_server.serverUpgrade.run_stack_upgrade")3821 def test_upgrade_stack(self, run_stack_upgrade_mock,3822 check_database_name_property_mock, is_root_mock):3823 # Testing call under non-root3824 is_root_mock.return_value = False3825 args = ['', 'HDP-2.0']3826 try:3827 upgrade_stack(args)3828 self.fail("Should throw exception")3829 except FatalException as fe:3830 # Expected3831 self.assertTrue("root-level" in fe.reason)3832 pass3833 # Testing calls under root3834 is_root_mock.return_value = True3835 run_stack_upgrade_mock.return_value = 03836 upgrade_stack(args)3837 self.assertTrue(run_stack_upgrade_mock.called)3838 run_stack_upgrade_mock.assert_called_with("HDP", "2.0", None, None)3839 pass3840 @patch("ambari_server.serverUpgrade.get_ambari_properties")3841 @patch("os.listdir")3842 @patch("os.path.isfile")3843 @patch("shutil.move")3844 def test_move_user_custom_actions(self, shutil_move_mock, os_path_isfile_mock, os_listdir_mock, get_ambari_properties_mock):3845 properties = Properties()3846 properties.process_pair(RESOURCES_DIR_PROPERTY, 'some/test/fake/resources/dir/path')3847 get_ambari_properties_mock.return_value = properties3848 os_listdir_mock.return_value = ['sometestdir', 'sometestfile.md', 'sometestfile.py', 'sometestfile2.java', 'sometestfile2.py', 'sometestdir2.py']3849 os_path_isfile_mock.side_effect = [False, True, True, True, True, False]3850 move_user_custom_actions()3851 custom_actions_scripts_dir = os.path.join('some/test/fake/resources/dir/path', 'custom_actions', 'scripts')3852 shutil_move_mock.assert_has_calls([call(os.path.join('some/test/fake/resources/dir/path', 'custom_actions', 'sometestfile.py'), custom_actions_scripts_dir),3853 call(os.path.join('some/test/fake/resources/dir/path', 'custom_actions', 'sometestfile2.py'), custom_actions_scripts_dir)])3854 self.assertEqual(shutil_move_mock.call_count, 2)3855 pass3856 @patch("ambari_server.serverConfiguration.get_conf_dir")3857 @patch("ambari_server.serverConfiguration.get_ambari_classpath")3858 @patch("ambari_server.serverUpgrade.run_os_command")3859 @patch("ambari_server.serverUpgrade.get_java_exe_path")3860 def test_run_stack_upgrade(self, java_exe_path_mock, run_os_command_mock,3861 get_ambari_classpath_mock, get_conf_dir_mock):3862 java_exe_path_mock.return_value = "/usr/lib/java/bin/java"3863 run_os_command_mock.return_value = (0, None, None)3864 get_ambari_classpath_mock.return_value = 'test:path12'3865 get_conf_dir_mock.return_value = '/etc/conf'3866 stackIdMap = {'HDP' : '2.0', 'repo_url' : 'http://test.com'}3867 run_stack_upgrade('HDP', '2.0', 'http://test.com', None)3868 self.assertTrue(java_exe_path_mock.called)3869 self.assertTrue(get_ambari_classpath_mock.called)3870 self.assertTrue(get_conf_dir_mock.called)3871 self.assertTrue(run_os_command_mock.called)3872 run_os_command_mock.assert_called_with('/usr/lib/java/bin/java -cp /etc/conf' + os.pathsep + 'test:path12 '3873 'org.apache.ambari.server.upgrade.StackUpgradeHelper '3874 'updateStackId ' + "'" + json.dumps(stackIdMap) + "'" +3875 ' > ' + os.sep + 'var' + os.sep + 'log' + os.sep + 'ambari-server' + os.sep +3876 'ambari-server.out 2>&1')3877 pass3878 @patch("ambari_server.serverConfiguration.get_conf_dir")3879 @patch("ambari_server.serverConfiguration.get_ambari_classpath")3880 @patch("ambari_server.serverUpgrade.run_os_command")3881 @patch("ambari_server.serverUpgrade.get_java_exe_path")3882 def test_run_stack_upgrade_with_url_os(self, java_exe_path_mock, run_os_command_mock,3883 get_ambari_classpath_mock, get_conf_dir_mock):3884 java_exe_path_mock.return_value = "/usr/lib/java/bin/java"3885 run_os_command_mock.return_value = (0, None, None)3886 get_ambari_classpath_mock.return_value = 'test:path12'3887 get_conf_dir_mock.return_value = '/etc/conf'3888 stackIdMap = {'HDP' : '2.0', 'repo_url': 'http://test.com', 'repo_url_os': 'centos5,centos6'}3889 run_stack_upgrade('HDP', '2.0', 'http://test.com', 'centos5,centos6')3890 self.assertTrue(java_exe_path_mock.called)3891 self.assertTrue(get_ambari_classpath_mock.called)3892 self.assertTrue(get_conf_dir_mock.called)3893 self.assertTrue(run_os_command_mock.called)3894 run_os_command_mock.assert_called_with('/usr/lib/java/bin/java -cp /etc/conf' + os.pathsep + 'test:path12 '3895 'org.apache.ambari.server.upgrade.StackUpgradeHelper '3896 'updateStackId ' + "'" + json.dumps(stackIdMap) + "'" +3897 ' > ' + os.sep + 'var' + os.sep + 'log' + os.sep + 'ambari-server' + os.sep +3898 'ambari-server.out 2>&1')3899 pass3900 @patch("ambari_server.serverUpgrade.ensure_can_start_under_current_user")3901 @patch("ambari_server.serverUpgrade.generate_env")3902 @patch("ambari_server.serverUpgrade.read_ambari_user")3903 @patch("ambari_server.serverConfiguration.get_conf_dir")3904 @patch("ambari_server.serverConfiguration.get_ambari_classpath")3905 @patch("ambari_server.serverUpgrade.run_os_command")3906 @patch("ambari_server.serverUpgrade.get_java_exe_path")3907 def test_run_schema_upgrade(self, java_exe_path_mock, run_os_command_mock,3908 get_ambari_classpath_mock, get_conf_dir_mock,3909 read_ambari_user_mock, generate_env_mock,3910 ensure_can_start_under_current_user_mock):3911 java_exe_path_mock.return_value = "/usr/lib/java/bin/java"3912 run_os_command_mock.return_value = (0, None, None)3913 get_ambari_classpath_mock.return_value = 'test' + os.pathsep + 'path12'3914 get_conf_dir_mock.return_value = '/etc/conf'3915 command = '/usr/lib/java/bin/java -cp /etc/conf' + os.pathsep + 'test' + os.pathsep + 'path12 ' \3916 'org.apache.ambari.server.upgrade.SchemaUpgradeHelper ' \3917 '> ' + os.sep + 'var' + os.sep + 'log' + os.sep + 'ambari-server' + os.sep + 'ambari-server.out 2>&1'3918 environ = {}3919 generate_env_mock.return_value = environ3920 ensure_can_start_under_current_user_mock.return_value = "root"3921 read_ambari_user_mock.return_value = "ambari"3922 run_schema_upgrade()3923 self.assertTrue(java_exe_path_mock.called)3924 self.assertTrue(ensure_can_start_under_current_user_mock.called)3925 self.assertTrue(generate_env_mock.called)3926 self.assertTrue(read_ambari_user_mock.called)3927 self.assertTrue(get_ambari_classpath_mock.called)3928 self.assertTrue(get_conf_dir_mock.called)3929 self.assertTrue(run_os_command_mock.called)3930 run_os_command_mock.assert_called_with(command, env=environ)3931 pass3932 @patch("ambari_server.serverConfiguration.get_conf_dir")3933 @patch("ambari_server.serverConfiguration.get_ambari_classpath")3934 @patch("ambari_server.serverUpgrade.run_os_command")3935 @patch("ambari_server.serverUpgrade.get_java_exe_path")3936 def test_run_metainfo_upgrade(self, java_exe_path_mock, run_os_command_mock,3937 get_ambari_classpath_mock, get_conf_dir_mock):3938 java_exe_path_mock.return_value = "/usr/lib/java/bin/java"3939 run_os_command_mock.return_value = (0, None, None)3940 get_ambari_classpath_mock.return_value = 'test' + os.pathsep + 'path12'3941 get_conf_dir_mock.return_value = '/etc/conf'3942 json_map = {'a': 'http://newurl'}3943 run_metainfo_upgrade(json_map)3944 self.assertTrue(java_exe_path_mock.called)3945 self.assertTrue(get_ambari_classpath_mock.called)3946 self.assertTrue(get_conf_dir_mock.called)3947 self.assertTrue(run_os_command_mock.called)3948 run_os_command_mock.assert_called_with('/usr/lib/java/bin/java '3949 '-cp /etc/conf' + os.pathsep + 'test' + os.pathsep + 'path12 '3950 'org.apache.ambari.server.upgrade.StackUpgradeHelper updateMetaInfo ' +3951 "'" + json.dumps(json_map) + "'" +3952 ' > ' + os.sep + 'var' + os.sep + 'log' + os.sep + 'ambari-server' +3953 os.sep + 'ambari-server.out 2>&1')3954 pass3955 @not_for_platform(PLATFORM_WINDOWS)3956 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))3957 @patch("os.path.isfile")3958 @patch("ambari_server.serverSetup.get_ambari_properties")3959 @patch("os.path.exists")3960 @patch("os.path.lexists")3961 @patch("os.remove")3962 @patch("os.symlink")3963 @patch("shutil.copy")3964 def test_proceedJDBCProperties(self, copy_mock, os_symlink_mock, os_remove_mock, lexists_mock, exists_mock,3965 get_ambari_properties_mock, isfile_mock):3966 args = MagicMock()3967 # test incorrect path to jdbc-driver3968 isfile_mock.return_value = False3969 args.jdbc_driver = "test jdbc"3970 fail = False3971 try:3972 proceedJDBCProperties(args)3973 except FatalException as e:3974 self.assertEquals("File test jdbc does not exist!", e.reason)3975 fail = True3976 self.assertTrue(fail)3977 # test incorrect jdbc-db3978 isfile_mock.return_value = True3979 args.jdbc_db = "incorrect db"3980 fail = False3981 try:3982 proceedJDBCProperties(args)3983 except FatalException as e:3984 self.assertEquals("Unsupported database name incorrect db. Please see help for more information.", e.reason)3985 fail = True3986 self.assertTrue(fail)3987 # test getAmbariProperties failed3988 args.jdbc_db = "mysql"3989 get_ambari_properties_mock.return_value = -13990 fail = False3991 try:3992 proceedJDBCProperties(args)3993 except FatalException as e:3994 self.assertEquals("Error getting ambari properties", e.reason)3995 fail = True3996 self.assertTrue(fail)3997 # test getAmbariProperties failed3998 args.jdbc_db = "mssql"3999 get_ambari_properties_mock.return_value = -14000 fail = False4001 try:4002 proceedJDBCProperties(args)4003 except FatalException as e:4004 self.assertEquals("Error getting ambari properties", e.reason)4005 fail = True4006 self.assertTrue(fail)4007 # test get resource dir param failed4008 args.jdbc_db = "oracle"4009 p = MagicMock()4010 get_ambari_properties_mock.return_value = p4011 p.__getitem__.side_effect = KeyError("test exception")4012 exists_mock.return_value = False4013 fail = False4014 try:4015 proceedJDBCProperties(args)4016 except FatalException as e:4017 fail = True4018 self.assertTrue(fail)4019 # test copy jdbc failed and symlink exists4020 lexists_mock.return_value = True4021 args.jdbc_db = "postgres"4022 get_ambari_properties_mock.return_value = MagicMock()4023 isfile_mock.side_effect = [True, False]4024 exists_mock.return_value = True4025 fail = False4026 def side_effect():4027 raise Exception(-1, "Failed to copy!")4028 copy_mock.side_effect = side_effect4029 try:4030 proceedJDBCProperties(args)4031 except FatalException as e:4032 fail = True4033 self.assertTrue(fail)4034 self.assertTrue(os_remove_mock.called)4035 # test success symlink creation4036 get_ambari_properties_mock.reset_mock()4037 os_remove_mock.reset_mock()4038 p = MagicMock()4039 get_ambari_properties_mock.return_value = p4040 p.__getitem__.side_effect = None4041 p.__getitem__.return_value = "somewhere"4042 copy_mock.reset_mock()4043 copy_mock.side_effect = None4044 isfile_mock.side_effect = [True, False]4045 proceedJDBCProperties(args)4046 self.assertTrue(os_remove_mock.called)4047 self.assertTrue(os_symlink_mock.called)4048 self.assertTrue(copy_mock.called)4049 self.assertEquals(os_symlink_mock.call_args_list[0][0][0], os.path.join("somewhere","test jdbc"))4050 self.assertEquals(os_symlink_mock.call_args_list[0][0][1], os.path.join("somewhere","postgres-jdbc-driver.jar"))4051 pass4052 @only_for_platform(PLATFORM_WINDOWS)4053 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4054 @patch("os.path.isfile")4055 @patch("ambari_server.serverSetup.get_ambari_properties")4056 @patch("os.path.exists")4057 @patch("os.path.lexists")4058 @patch("os.remove")4059 @patch("os.symlink")4060 @patch("shutil.copy")4061 def test_proceedJDBCProperties(self, copy_mock, os_symlink_mock, os_remove_mock, lexists_mock, exists_mock,4062 get_ambari_properties_mock, isfile_mock):4063 args = MagicMock()4064 # test incorrect path to jdbc-driver4065 isfile_mock.return_value = False4066 args.jdbc_driver = "test jdbc"4067 fail = False4068 try:4069 proceedJDBCProperties(args)4070 except FatalException as e:4071 self.assertEquals("File test jdbc does not exist!", e.reason)4072 fail = True4073 self.assertTrue(fail)4074 # test incorrect jdbc-db4075 isfile_mock.return_value = True4076 args.jdbc_db = "incorrect db"4077 fail = False4078 try:4079 proceedJDBCProperties(args)4080 except FatalException as e:4081 self.assertEquals("Unsupported database name incorrect db. Please see help for more information.", e.reason)4082 fail = True4083 self.assertTrue(fail)4084 # test getAmbariProperties succeeded4085 args.jdbc_db = "mssql"4086 get_ambari_properties_mock.return_value = -14087 fail = False4088 try:4089 proceedJDBCProperties(args)4090 except FatalException as e:4091 self.assertEquals("Error getting ambari properties", e.reason)4092 fail = True4093 self.assertFalse(fail)4094 pass4095 @not_for_platform(PLATFORM_WINDOWS)4096 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4097 @patch("__builtin__.open")4098 @patch("os.path.isfile")4099 @patch("os.path.lexists")4100 @patch("os.path.exists")4101 @patch("os.remove")4102 @patch("os.symlink")4103 @patch.object(Properties, "store")4104 @patch("ambari_server.serverUpgrade.adjust_directory_permissions")4105 @patch("ambari_server.serverUpgrade.print_warning_msg")4106 @patch("ambari_server.serverUpgrade.read_ambari_user")4107 @patch("ambari_server.serverUpgrade.run_schema_upgrade")4108 @patch("ambari_server.dbConfiguration_linux.run_os_command")4109 @patch("ambari_server.serverConfiguration.find_properties_file")4110 @patch("ambari_server.serverUpgrade.update_ambari_properties")4111 @patch("ambari_server.serverUpgrade.is_root")4112 @patch("ambari_server.serverConfiguration.write_property")4113 @patch("ambari_server.serverConfiguration.get_ambari_version")4114 @patch("ambari_server.dbConfiguration.get_ambari_properties")4115 @patch("ambari_server.serverConfiguration.get_ambari_properties")4116 @patch("ambari_server.serverUpgrade.get_ambari_properties")4117 @patch("ambari_server.serverUpgrade.upgrade_local_repo")4118 @patch("ambari_server.serverUpgrade.move_user_custom_actions")4119 @patch("ambari_server.serverUpgrade.update_krb_jaas_login_properties")4120 def test_upgrade_from_161(self, update_krb_jaas_login_properties_mock, move_user_custom_actions_mock, upgrade_local_repo_mock, get_ambari_properties_mock,4121 get_ambari_properties_2_mock, get_ambari_properties_3_mock, get_ambari_version_mock, write_property_mock,4122 is_root_mock, update_ambari_properties_mock, find_properties_file_mock, run_os_command_mock,4123 run_schema_upgrade_mock, read_ambari_user_mock, print_warning_msg_mock,4124 adjust_directory_permissions_mock, properties_store_mock,4125 os_symlink_mock, os_remove_mock, exists_mock, lexists_mock, isfile_mock, open_mock):4126 def reset_mocks():4127 run_os_command_mock.reset_mock()4128 write_property_mock.reset_mock()4129 isfile_mock.reset_mock()4130 lexists_mock.reeset_mock()4131 os_symlink_mock.reset_mock()4132 lexists_mock.return_value = False4133 args = MagicMock()4134 del args.dbms4135 del args.database_index4136 del args.database_host4137 del args.database_port4138 del args.database_name4139 del args.database_username4140 del args.database_password4141 del args.database_windows_auth4142 del args.default_database_host4143 del args.init_db_script_file4144 del args.cleanup_db_script_file4145 del args.must_set_database_options4146 del args.sid_or_sname4147 del args.jdbc_url4148 args.jdbc_driver= None4149 args.jdbc_db = None4150 args.silent = False4151 return args4152 args = reset_mocks()4153 args.dbms = "postgres"4154 is_root_mock.return_value = True4155 update_ambari_properties_mock.return_value = 04156 get_ambari_version_mock.return_value = "1.7.0"4157 move_user_custom_actions_mock.return_value = None4158 update_krb_jaas_login_properties_mock.return_value = -24159 # Local Postgres4160 # In Ambari 1.6.1 for an embedded postgres database, the "server.jdbc.database" property stored the DB name,4161 # and the DB type was assumed to be "postgres" if the "server.persistence.type" property was "local"4162 properties = Properties()4163 properties.process_pair(PERSISTENCE_TYPE_PROPERTY, "local")4164 properties.process_pair(JDBC_DATABASE_PROPERTY, "ambari")4165 properties.process_pair(RESOURCES_DIR_PROPERTY, "/tmp")4166 get_ambari_properties_mock.return_value = properties4167 properties2 = Properties()4168 properties2.process_pair(PERSISTENCE_TYPE_PROPERTY, "local")4169 properties2.process_pair(JDBC_DATABASE_NAME_PROPERTY, "ambari")4170 properties2.process_pair(JDBC_DATABASE_PROPERTY, "postgres")4171 get_ambari_properties_3_mock.side_effect = get_ambari_properties_2_mock.side_effect = [properties, properties2, properties2]4172 run_schema_upgrade_mock.return_value = 04173 read_ambari_user_mock.return_value = "custom_user"4174 run_os_command_mock.return_value = (0, "", "")4175 isfile_mock.return_value = False4176 try:4177 upgrade(args)4178 except FatalException as fe:4179 self.fail("Did not expect failure: " + str(fe))4180 else:4181 self.assertTrue(write_property_mock.called)4182 self.assertEquals(write_property_mock.call_args_list[0][0][0], JDBC_DATABASE_NAME_PROPERTY)4183 self.assertEquals(write_property_mock.call_args_list[0][0][1], "ambari")4184 self.assertEquals(write_property_mock.call_args_list[1][0][0], JDBC_DATABASE_PROPERTY)4185 self.assertEquals(write_property_mock.call_args_list[1][0][1], "postgres")4186 self.assertTrue(run_os_command_mock.called)4187 self.assertFalse(move_user_custom_actions_mock.called)4188 args = reset_mocks()4189 # External Postgres4190 # In Ambari 1.6.1 for an external postgres database, the "server.jdbc.database" property stored the4191 # DB type ("postgres"), and the "server.jdbc.schema" property stored the DB name.4192 properties = Properties()4193 properties.process_pair(PERSISTENCE_TYPE_PROPERTY, "remote")4194 properties.process_pair(JDBC_DATABASE_PROPERTY, "postgres")4195 properties.process_pair(JDBC_RCA_SCHEMA_PROPERTY, "ambari")4196 properties.process_pair(JDBC_URL_PROPERTY, "jdbc:postgresql://c6410.ambari.apache.org:5432/ambari")4197 properties2 = Properties()4198 properties2.process_pair(PERSISTENCE_TYPE_PROPERTY, "remote")4199 properties2.process_pair(JDBC_DATABASE_NAME_PROPERTY, "ambari")4200 properties2.process_pair(JDBC_DATABASE_PROPERTY, "postgres")4201 properties2.process_pair(JDBC_RCA_SCHEMA_PROPERTY, "ambari")4202 properties2.process_pair(JDBC_URL_PROPERTY, "jdbc:postgresql://c6410.ambari.apache.org:5432/ambari")4203 get_ambari_properties_mock.return_value = properties4204 get_ambari_properties_3_mock.side_effect = get_ambari_properties_2_mock.side_effect = [properties, properties2, properties2]4205 exists_mock.return_value = True4206 try:4207 upgrade(args)4208 except FatalException as fe:4209 self.fail("Did not expect failure: " + str(fe))4210 else:4211 self.assertTrue(write_property_mock.called)4212 self.assertFalse(run_os_command_mock.called)4213 self.assertFalse(move_user_custom_actions_mock.called)4214 args = reset_mocks()4215 # External Postgres missing DB type, so it should be set based on the JDBC URL.4216 properties = Properties()4217 properties.process_pair(PERSISTENCE_TYPE_PROPERTY, "remote")4218 properties.process_pair(JDBC_RCA_SCHEMA_PROPERTY, "ambari")4219 properties.process_pair(JDBC_URL_PROPERTY, "jdbc:postgresql://c6410.ambari.apache.org:5432/ambari")4220 get_ambari_properties_mock.return_value = properties4221 get_ambari_properties_3_mock.side_effect = get_ambari_properties_2_mock.side_effect = [properties, properties2, properties2]4222 try:4223 upgrade(args)4224 except FatalException as fe:4225 self.fail("Did not expect failure: " + str(fe))4226 else:4227 self.assertTrue(write_property_mock.call_count == 2)4228 self.assertFalse(move_user_custom_actions_mock.called)4229 args = reset_mocks()4230 # External MySQL4231 # In Ambari 1.6.1 for an external MySQL database, the "server.jdbc.database" property stored the DB type ("mysql"),4232 # And the "server.jdbc.schema" property stored the DB name.4233 properties = Properties()4234 properties.process_pair(PERSISTENCE_TYPE_PROPERTY, "remote")4235 properties.process_pair(JDBC_DATABASE_PROPERTY, "mysql")4236 properties.process_pair(JDBC_RCA_SCHEMA_PROPERTY, "ambari")4237 properties.process_pair(JDBC_URL_PROPERTY, "jdbc:mysql://c6409.ambari.apache.org:3306/ambari")4238 properties2 = Properties()4239 properties2.process_pair(PERSISTENCE_TYPE_PROPERTY, "remote")4240 properties2.process_pair(JDBC_DATABASE_PROPERTY, "mysql")4241 properties2.process_pair(JDBC_DATABASE_NAME_PROPERTY, "ambari")4242 properties2.process_pair(JDBC_RCA_SCHEMA_PROPERTY, "ambari")4243 properties2.process_pair(JDBC_URL_PROPERTY, "jdbc:mysql://c6409.ambari.apache.org:3306/ambari")4244 get_ambari_properties_mock.return_value = properties4245 get_ambari_properties_3_mock.side_effect = get_ambari_properties_2_mock.side_effect = [properties, properties2, properties2]4246 isfile_mock.side_effect = [False, True, False]4247 try:4248 upgrade(args)4249 except FatalException as fe:4250 self.fail("Did not expect failure: " + str(fe))4251 else:4252 self.assertTrue(write_property_mock.called)4253 self.assertFalse(move_user_custom_actions_mock.called)4254 self.assertTrue(os_symlink_mock.called)4255 self.assertTrue(os_symlink_mock.call_args_list[0][0][0] == "/var/lib/ambari-server/resources/mysql-connector-java.jar")4256 self.assertTrue(os_symlink_mock.call_args_list[0][0][1] == "/var/lib/ambari-server/resources/mysql-jdbc-driver.jar")4257 args = reset_mocks()4258 # External MySQL missing DB type, so it should be set based on the JDBC URL.4259 properties = Properties()4260 properties.process_pair(PERSISTENCE_TYPE_PROPERTY, "remote")4261 properties.process_pair(JDBC_RCA_SCHEMA_PROPERTY, "ambari")4262 properties.process_pair(JDBC_URL_PROPERTY, "jdbc:mysql://c6409.ambari.apache.org:3306/ambari")4263 get_ambari_properties_mock.return_value = properties4264 get_ambari_properties_3_mock.side_effect = get_ambari_properties_2_mock.side_effect = [properties, properties2, properties2]4265 isfile_mock.side_effect = None4266 try:4267 upgrade(args)4268 except FatalException as fe:4269 self.fail("Did not expect failure: " + str(fe))4270 else:4271 self.assertTrue(write_property_mock.call_count == 2)4272 self.assertFalse(move_user_custom_actions_mock.called)4273 pass4274 @not_for_platform(PLATFORM_WINDOWS)4275 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4276 @patch("__builtin__.open")4277 @patch("os.path.isfile")4278 @patch("os.path.exists")4279 @patch("os.path.lexists")4280 @patch("os.remove")4281 @patch("os.symlink")4282 @patch.object(Properties, "store")4283 @patch.object(PGConfig, "_change_db_files_owner")4284 @patch("ambari_server.serverConfiguration.find_properties_file")4285 @patch("ambari_server.serverUpgrade.adjust_directory_permissions")4286 @patch("ambari_server.serverUpgrade.print_warning_msg")4287 @patch("ambari_server.serverUpgrade.read_ambari_user")4288 @patch("ambari_server.serverUpgrade.run_schema_upgrade")4289 @patch("ambari_server.serverUpgrade.update_ambari_properties")4290 @patch("ambari_server.serverUpgrade.parse_properties_file")4291 @patch("ambari_server.serverUpgrade.get_ambari_version")4292 @patch("ambari_server.serverConfiguration.get_ambari_version")4293 @patch("ambari_server.serverUpgrade.is_root")4294 @patch("ambari_server.dbConfiguration.get_ambari_properties")4295 @patch("ambari_server.serverConfiguration.get_ambari_properties")4296 @patch("ambari_server.serverUpgrade.get_ambari_properties")4297 @patch("ambari_server.serverUpgrade.upgrade_local_repo")4298 @patch("ambari_server.serverUpgrade.move_user_custom_actions")4299 @patch("ambari_server.serverUpgrade.update_krb_jaas_login_properties")4300 def test_upgrade(self, update_krb_jaas_login_properties_mock, move_user_custom_actions, upgrade_local_repo_mock,4301 get_ambari_properties_mock, get_ambari_properties_2_mock, get_ambari_properties_3_mock,4302 is_root_mock, get_ambari_version_mock, get_ambari_version_2_mock,4303 parse_properties_file_mock,4304 update_ambari_properties_mock, run_schema_upgrade_mock,4305 read_ambari_user_mock, print_warning_msg_mock,4306 adjust_directory_permissions_mock,4307 find_properties_file_mock, change_db_files_owner_mock, properties_store_mock,4308 os_symlink_mock, os_remove_mock, lexists_mock, exists_mock, isfile_mock, open_mock):4309 def reset_mocks():4310 isfile_mock.reset_mock()4311 args = MagicMock()4312 del args.database_index4313 del args.dbms4314 del args.database_host4315 del args.database_port4316 del args.database_name4317 del args.database_username4318 del args.database_password4319 del args.persistence_type4320 del args.sid_or_sname4321 del args.jdbc_url4322 args.must_set_database_options = True4323 return args4324 args = reset_mocks()4325 properties = Properties()4326 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4327 get_ambari_properties_mock.return_value = properties4328 update_ambari_properties_mock.return_value = 04329 run_schema_upgrade_mock.return_value = 04330 isfile_mock.return_value = False4331 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = CURR_AMBARI_VERSION4332 move_user_custom_actions.return_value = None4333 update_krb_jaas_login_properties_mock.return_value = -24334 # Testing call under non-root4335 is_root_mock.return_value = False4336 try:4337 upgrade(args)4338 self.fail("Should throw exception")4339 except FatalException as fe:4340 # Expected4341 self.assertTrue("root-level" in fe.reason)4342 pass4343 args = reset_mocks()4344 # Testing calls under root4345 is_root_mock.return_value = True4346 # Testing with undefined custom user4347 read_ambari_user_mock.return_value = None4348 run_schema_upgrade_mock.return_value = 04349 change_db_files_owner_mock.return_value = 04350 exists_mock.return_value = True4351 upgrade(args)4352 self.assertTrue(print_warning_msg_mock.called)4353 warning_args = print_warning_msg_mock.call_args[0][0]4354 self.assertTrue("custom ambari user" in warning_args)4355 self.assertTrue(upgrade_local_repo_mock.called)4356 self.assertTrue(move_user_custom_actions.called)4357 args = reset_mocks()4358 # Testing with defined custom user4359 read_ambari_user_mock.return_value = "ambari-custom-user"4360 upgrade(args)4361 self.assertTrue(adjust_directory_permissions_mock.called)4362 args = reset_mocks()4363 run_schema_upgrade_mock.return_value = 04364 parse_properties_file_mock.called = False4365 move_user_custom_actions.called = False4366 retcode = upgrade(args)4367 self.assertTrue(get_ambari_properties_mock.called)4368 self.assertTrue(get_ambari_properties_2_mock.called)4369 self.assertNotEqual(-1, retcode)4370 self.assertTrue(parse_properties_file_mock.called)4371 self.assertTrue(run_schema_upgrade_mock.called)4372 self.assertTrue(move_user_custom_actions.called)4373 # Assert that move_user_custom_actions is called on upgrade to Ambari == 2.0.04374 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = '2.0.0'4375 move_user_custom_actions.called = False4376 upgrade(args)4377 self.assertTrue(move_user_custom_actions.called)4378 # Assert that move_user_custom_actions is not called on upgrade to Ambari < 2.0.04379 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = '1.6.0'4380 move_user_custom_actions.called = False4381 upgrade(args)4382 self.assertFalse(move_user_custom_actions.called)4383 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = CURR_AMBARI_VERSION4384 # test getAmbariProperties failed4385 args = reset_mocks()4386 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4387 get_ambari_properties_mock.return_value = -14388 fail = False4389 try:4390 upgrade(args)4391 except FatalException as e:4392 self.assertEquals("Error getting ambari properties", e.reason)4393 fail = True4394 self.assertTrue(fail)4395 # test get resource dir param failed4396 args = reset_mocks()4397 p = MagicMock()4398 get_ambari_properties_mock.reset_mock()4399 get_ambari_properties_2_mock.reset_mock()4400 get_ambari_properties_3_mock.reset_mock()4401 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4402 get_ambari_properties_mock.return_value = p4403 p.__getitem__.side_effect = ["something", "something", "something", "something", KeyError("test exception")]4404 exists_mock.return_value = False4405 fail = False4406 try:4407 upgrade(args)4408 except FatalException as e:4409 fail = True4410 self.assertTrue(fail)4411 # test if some drivers are available in resources, and symlink available too4412 args = reset_mocks()4413 props = Properties()4414 props.process_pair(JDBC_DATABASE_NAME_PROPERTY, "something")4415 props.process_pair(RESOURCES_DIR_PROPERTY, "resources")4416 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4417 get_ambari_properties_mock.return_value = props4418 exists_mock.return_value = True4419 lexists_mock.return_value = True4420 isfile_mock.side_effect = [True, False, False]4421 upgrade(args)4422 self.assertTrue(os_remove_mock.called)4423 self.assertEquals(os_remove_mock.call_count, 1)4424 self.assertEquals(os_remove_mock.call_args[0][0], os.path.join("resources", "oracle-jdbc-driver.jar"))4425 self.assertEquals(os_symlink_mock.call_count, 1)4426 self.assertEquals(os_symlink_mock.call_args[0][0], os.path.join("resources", "ojdbc6.jar"))4427 self.assertEquals(os_symlink_mock.call_args[0][1], os.path.join("resources", "oracle-jdbc-driver.jar"))4428 pass4429 @only_for_platform(PLATFORM_WINDOWS)4430 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4431 @patch("__builtin__.open")4432 @patch("os.path.isfile")4433 @patch("os.path.exists")4434 @patch("os.path.lexists")4435 @patch("os.remove")4436 @patch("os.symlink")4437 @patch.object(Properties, "store")4438 @patch("ambari_server.serverConfiguration.find_properties_file")4439 @patch("ambari_server.serverUpgrade.adjust_directory_permissions")4440 @patch("ambari_server.serverUpgrade.print_warning_msg")4441 @patch("ambari_server.serverUpgrade.read_ambari_user")4442 @patch("ambari_server.serverUpgrade.run_schema_upgrade")4443 @patch("ambari_server.serverUpgrade.update_ambari_properties")4444 @patch("ambari_server.serverUpgrade.parse_properties_file")4445 @patch("ambari_server.serverUpgrade.get_ambari_version")4446 @patch("ambari_server.serverConfiguration.get_ambari_version")4447 @patch("ambari_server.serverUpgrade.is_root")4448 @patch("ambari_server.dbConfiguration.get_ambari_properties")4449 @patch("ambari_server.serverConfiguration.get_ambari_properties")4450 @patch("ambari_server.serverUpgrade.get_ambari_properties")4451 @patch("ambari_server.serverUpgrade.upgrade_local_repo")4452 @patch("ambari_server.serverUpgrade.move_user_custom_actions")4453 @patch("ambari_server.serverUpgrade.update_krb_jaas_login_properties")4454 def test_upgrade(self, update_krb_jaas_login_properties_mock, move_user_custom_actions, upgrade_local_repo_mock,4455 get_ambari_properties_mock, get_ambari_properties_2_mock, get_ambari_properties_3_mock,4456 is_root_mock, get_ambari_version_mock, get_ambari_version_2_mock,4457 parse_properties_file_mock,4458 update_ambari_properties_mock, run_schema_upgrade_mock,4459 read_ambari_user_mock, print_warning_msg_mock,4460 adjust_directory_permissions_mock,4461 find_properties_file_mock, properties_store_mock,4462 os_symlink_mock, os_remove_mock, lexists_mock, exists_mock, isfile_mock, open_mock):4463 def reset_mocks():4464 isfile_mock.reset_mock()4465 args = MagicMock()4466 del args.dbms4467 del args.database_index4468 del args.database_host4469 del args.database_port4470 del args.database_name4471 del args.database_username4472 del args.database_password4473 del args.default_database_host4474 del args.persistence_type4475 del args.init_db_script_file4476 del args.cleanup_db_script_file4477 del args.sid_or_sname4478 del args.jdbc_url4479 args.must_set_database_options = True4480 return args4481 args = reset_mocks()4482 properties = Properties()4483 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4484 get_ambari_properties_mock.return_value = properties4485 update_ambari_properties_mock.return_value = 04486 run_schema_upgrade_mock.return_value = 04487 isfile_mock.return_value = False4488 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = CURR_AMBARI_VERSION4489 move_user_custom_actions.return_value = None4490 update_krb_jaas_login_properties_mock.return_value = -24491 # Testing call under non-root4492 is_root_mock.return_value = False4493 try:4494 upgrade(args)4495 self.fail("Should throw exception")4496 except FatalException as fe:4497 # Expected4498 self.assertTrue("administrator-level" in fe.reason)4499 pass4500 args = reset_mocks()4501 # Testing calls under root4502 is_root_mock.return_value = True4503 # Testing with undefined custom user4504 read_ambari_user_mock.return_value = None4505 run_schema_upgrade_mock.return_value = 04506 exists_mock.return_value = True4507 upgrade(args)4508 self.assertTrue(print_warning_msg_mock.called)4509 warning_args = print_warning_msg_mock.call_args[0][0]4510 self.assertTrue("custom ambari user" in warning_args)4511 self.assertTrue(upgrade_local_repo_mock.called)4512 self.assertTrue(move_user_custom_actions.called)4513 args = reset_mocks()4514 # Testing with defined custom user4515 read_ambari_user_mock.return_value = "ambari-custom-user"4516 upgrade(args)4517 self.assertTrue(adjust_directory_permissions_mock.called)4518 args = reset_mocks()4519 run_schema_upgrade_mock.return_value = 04520 parse_properties_file_mock.called = False4521 move_user_custom_actions.called = False4522 retcode = upgrade(args)4523 self.assertTrue(get_ambari_properties_mock.called)4524 self.assertTrue(get_ambari_properties_2_mock.called)4525 self.assertNotEqual(-1, retcode)4526 self.assertTrue(parse_properties_file_mock.called)4527 self.assertTrue(run_schema_upgrade_mock.called)4528 self.assertTrue(move_user_custom_actions.called)4529 # Assert that move_user_custom_actions is called on upgrade to Ambari == 2.0.04530 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = '2.0.0'4531 move_user_custom_actions.called = False4532 upgrade(args)4533 self.assertTrue(move_user_custom_actions.called)4534 # Assert that move_user_custom_actions is not called on upgrade to Ambari < 2.0.04535 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = '1.6.0'4536 move_user_custom_actions.called = False4537 upgrade(args)4538 self.assertFalse(move_user_custom_actions.called)4539 get_ambari_version_2_mock.return_value = get_ambari_version_mock.return_value = CURR_AMBARI_VERSION4540 # test getAmbariProperties failed4541 args = reset_mocks()4542 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4543 get_ambari_properties_mock.return_value = -14544 fail = False4545 try:4546 upgrade(args)4547 except FatalException as e:4548 self.assertEquals("Error getting ambari properties", e.reason)4549 fail = True4550 self.assertTrue(fail)4551 # test get resource dir param failed4552 args = reset_mocks()4553 p = MagicMock()4554 get_ambari_properties_mock.reset_mock()4555 get_ambari_properties_2_mock.reset_mock()4556 get_ambari_properties_3_mock.reset_mock()4557 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4558 get_ambari_properties_mock.return_value = p4559 p.__getitem__.side_effect = ["something", "something", "something", "something", KeyError("test exception")]4560 exists_mock.return_value = False4561 fail = False4562 try:4563 upgrade(args)4564 except FatalException as e:4565 fail = True4566 self.assertTrue(fail)4567 # test if some drivers are available in resources, and symlink available too4568 args = reset_mocks()4569 props = Properties()4570 props.process_pair(JDBC_DATABASE_NAME_PROPERTY, "something")4571 props.process_pair(RESOURCES_DIR_PROPERTY, "resources")4572 get_ambari_properties_3_mock.return_value = get_ambari_properties_2_mock.return_value = \4573 get_ambari_properties_mock.return_value = props4574 exists_mock.return_value = True4575 lexists_mock.return_value = True4576 isfile_mock.side_effect = [True, False, False]4577 pass4578 def test_print_info_msg(self):4579 out = StringIO.StringIO()4580 sys.stdout = out4581 set_verbose(True)4582 print_info_msg("msg")4583 self.assertNotEqual("", out.getvalue())4584 sys.stdout = sys.__stdout__4585 pass4586 def test_print_error_msg(self):4587 out = StringIO.StringIO()4588 sys.stdout = out4589 set_verbose(True)4590 print_error_msg("msg")4591 self.assertNotEqual("", out.getvalue())4592 sys.stdout = sys.__stdout__4593 pass4594 def test_print_warning_msg(self):4595 out = StringIO.StringIO()4596 sys.stdout = out4597 set_verbose(True)4598 print_warning_msg("msg")4599 self.assertNotEqual("", out.getvalue())4600 sys.stdout = sys.__stdout__4601 pass4602 @patch("ambari_server.userInput.get_choice_string_input")4603 def test_get_YN_input(self, get_choice_string_input_mock):4604 get_YN_input("prompt", "default")4605 self.assertTrue(get_choice_string_input_mock.called)4606 self.assertEqual(4, len(get_choice_string_input_mock.call_args_list[0][0]))4607 pass4608 @not_for_platform(PLATFORM_WINDOWS)4609 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4610 @patch.object(_ambari_server_, "setup")4611 def test_main_db_options(self, setup_mock):4612 base_args = ["ambari-server.py", "setup"]4613 db_args = ["--database", "postgres", "--databasehost", "somehost.net", "--databaseport", "12345",4614 "--databasename", "ambari", "--databaseusername", "ambari", "--databasepassword", "bigdata"]4615 #test no args4616 failed = False4617 sys.argv = list(base_args)4618 try:4619 _ambari_server_.mainBody()4620 except SystemExit:4621 failed = True4622 pass4623 self.assertFalse(failed)4624 self.assertTrue(setup_mock.called)4625 self.assertTrue(setup_mock.call_args_list[0][0][0].must_set_database_options)4626 setup_mock.reset_mock()4627 # test embedded option4628 failed = False4629 sys.argv = list(base_args)4630 sys.argv.extend(db_args[-10:])4631 sys.argv.extend(["--database", "embedded"])4632 try:4633 _ambari_server_.mainBody()4634 except SystemExit:4635 failed = True4636 pass4637 self.assertFalse(failed)4638 self.assertTrue(setup_mock.called)4639 setup_mock.reset_mock()4640 #test full args4641 sys.argv = list(base_args)4642 sys.argv.extend(db_args)4643 try:4644 _ambari_server_.mainBody()4645 except SystemExit:4646 failed = True4647 pass4648 self.assertFalse(failed)4649 self.assertTrue(setup_mock.called)4650 self.assertFalse(setup_mock.call_args_list[0][0][0].must_set_database_options)4651 setup_mock.reset_mock()4652 #test not full args4653 sys.argv = list(base_args)4654 sys.argv.extend(["--database", "postgres"])4655 try:4656 _ambari_server_.mainBody()4657 except SystemExit:4658 failed = True4659 pass4660 self.assertFalse(setup_mock.called)4661 self.assertTrue(failed)4662 setup_mock.reset_mock()4663 #test wrong database4664 failed = False4665 sys.argv = list(base_args)4666 sys.argv.extend(["--database", "unknown"])4667 sys.argv.extend(db_args[2:])4668 try:4669 _ambari_server_.mainBody()4670 except SystemExit:4671 failed = True4672 pass4673 self.assertTrue(failed)4674 self.assertFalse(setup_mock.called)4675 setup_mock.reset_mock()4676 #test wrong port check4677 failed = False4678 sys.argv = list(base_args)4679 sys.argv.extend(["--databaseport", "unknown"])4680 sys.argv.extend(db_args[:4])4681 sys.argv.extend(db_args[6:])4682 try:4683 _ambari_server_.mainBody()4684 except SystemExit:4685 failed = True4686 pass4687 self.assertTrue(failed)4688 self.assertFalse(setup_mock.called)4689 pass4690 @only_for_platform(PLATFORM_WINDOWS)4691 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4692 @patch.object(_ambari_server_, "setup")4693 def test_main_db_options(self, setup_mock):4694 base_args = ["ambari-server.py", "setup"]4695 db_args = ["--databasehost", "somehost.net", "--databaseport", "12345",4696 "--databasename", "ambari", "--databaseusername", "ambari", "--databasepassword", "bigdata"]4697 #test no args4698 failed = False4699 sys.argv = list(base_args)4700 try:4701 _ambari_server_.mainBody()4702 except SystemExit:4703 failed = True4704 pass4705 self.assertFalse(failed)4706 self.assertTrue(setup_mock.called)4707 self.assertTrue(setup_mock.call_args_list[0][0][0].must_set_database_options)4708 setup_mock.reset_mock()4709 #test full args4710 sys.argv = list(base_args)4711 sys.argv.extend(db_args)4712 try:4713 _ambari_server_.mainBody()4714 except SystemExit:4715 failed = True4716 pass4717 self.assertFalse(failed)4718 self.assertTrue(setup_mock.called)4719 self.assertFalse(setup_mock.call_args_list[0][0][0].must_set_database_options)4720 setup_mock.reset_mock()4721 #test not full args4722 sys.argv = list(base_args)4723 sys.argv.extend(["--databasehost", "somehost.net"])4724 try:4725 _ambari_server_.mainBody()4726 except SystemExit:4727 failed = True4728 pass4729 self.assertFalse(setup_mock.called)4730 self.assertTrue(failed)4731 setup_mock.reset_mock()4732 #test wrong port check4733 failed = False4734 sys.argv = list(base_args)4735 sys.argv.extend(["--databaseport", "unknown"])4736 sys.argv.extend(db_args[:2])4737 sys.argv.extend(db_args[6:])4738 try:4739 _ambari_server_.mainBody()4740 except SystemExit:4741 failed = True4742 pass4743 self.assertTrue(failed)4744 self.assertFalse(setup_mock.called)4745 pass4746 @not_for_platform(PLATFORM_WINDOWS)4747 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4748 @patch("ambari_server.serverSetup.get_YN_input")4749 @patch("ambari_server.dbConfiguration.get_validated_string_input")4750 @patch("ambari_server.dbConfiguration_linux.print_info_msg")4751 def test_prompt_db_properties(self, print_info_msg_mock,4752 get_validated_string_input_mock, get_YN_input_mock):4753 def reset_mocks():4754 get_validated_string_input_mock.reset_mock()4755 get_YN_input_mock.reset_mock()4756 args = MagicMock()4757 del args.database_index4758 del args.dbms4759 del args.database_host4760 del args.database_port4761 del args.database_name4762 del args.database_username4763 del args.database_password4764 del args.persistence_type4765 return args4766 args = reset_mocks()4767 set_silent(False)4768 #test not prompt4769 args.must_set_database_options = False4770 prompt_db_properties(args)4771 self.assertFalse(get_validated_string_input_mock.called)4772 self.assertFalse(get_YN_input_mock.called)4773 args = reset_mocks()4774 #test prompt4775 args.must_set_database_options = True4776 get_YN_input_mock.return_value = False4777 prompt_db_properties(args)4778 self.assertTrue(get_YN_input_mock.called)4779 self.assertFalse(get_validated_string_input_mock.called)4780 args = reset_mocks()4781 #test prompt advanced4782 args.must_set_database_options = True4783 get_YN_input_mock.return_value = True4784 get_validated_string_input_mock.return_value = "4"4785 prompt_db_properties(args)4786 self.assertTrue(get_YN_input_mock.called)4787 self.assertTrue(get_validated_string_input_mock.called)4788 self.assertEquals(args.database_index, 3)4789 pass4790 @patch("ambari_server.serverConfiguration.get_conf_dir")4791 def test_update_ambari_properties(self, get_conf_dir_mock):4792 from ambari_server import serverConfiguration # need to modify constants inside the module4793 properties = ["server.jdbc.user.name=ambari-server\n",4794 "server.jdbc.user.passwd=/etc/ambari-server/conf/password.dat\n",4795 "java.home=/usr/jdk64/jdk1.6.0_31\n",4796 "server.jdbc.database_name=ambari\n",4797 "ambari-server.user=ambari\n",4798 "agent.fqdn.service.url=URL\n"]4799 NEW_PROPERTY = 'some_new_property=some_value\n'4800 CHANGED_VALUE_PROPERTY = 'server.jdbc.database_name=should_not_overwrite_value\n'4801 get_conf_dir_mock.return_value = '/etc/ambari-server/conf'4802 (tf1, fn1) = tempfile.mkstemp()4803 (tf2, fn2) = tempfile.mkstemp()4804 configDefaults.AMBARI_PROPERTIES_BACKUP_FILE = fn14805 os.close(tf1)4806 serverConfiguration.AMBARI_PROPERTIES_FILE = fn24807 os.close(tf2)4808 with open(serverConfiguration.AMBARI_PROPERTIES_FILE, "w") as f:4809 f.write(NEW_PROPERTY)4810 f.write(CHANGED_VALUE_PROPERTY)4811 f.close()4812 with open(configDefaults.AMBARI_PROPERTIES_BACKUP_FILE, 'w') as f:4813 for line in properties:4814 f.write(line)4815 f.close()4816 #Call tested method4817 update_ambari_properties()4818 timestamp = datetime.datetime.now()4819 #RPMSAVE_FILE wasn't found4820 self.assertFalse(os.path.exists(configDefaults.AMBARI_PROPERTIES_BACKUP_FILE))4821 #Renamed RPMSAVE_FILE exists4822 self.assertTrue(os.path.exists(configDefaults.AMBARI_PROPERTIES_BACKUP_FILE4823 + '.' + timestamp.strftime('%Y%m%d%H%M%S')))4824 with open(serverConfiguration.AMBARI_PROPERTIES_FILE, 'r') as f:4825 ambari_properties_content = f.readlines()4826 for line in properties:4827 if (line == "agent.fqdn.service.url=URL\n"):4828 if (not GET_FQDN_SERVICE_URL + "=URL\n" in ambari_properties_content) and (4829 line in ambari_properties_content):4830 self.fail()4831 else:4832 if not line in ambari_properties_content:4833 self.fail()4834 if not NEW_PROPERTY in ambari_properties_content:4835 self.fail()4836 if CHANGED_VALUE_PROPERTY in ambari_properties_content:4837 self.fail()4838 # Command should not fail if *.rpmsave file is missing4839 result = update_ambari_properties()4840 self.assertEquals(result, 0)4841 os.unlink(fn2)4842 #if ambari.properties file is absent then "ambari-server upgrade" should4843 # fail4844 (tf, fn) = tempfile.mkstemp()4845 configDefaults.AMBARI_PROPERTIES_BACKUP_FILE = fn4846 result = update_ambari_properties()4847 self.assertNotEquals(result, 0)4848 pass4849 @patch("ambari_server.properties.Properties.__init__")4850 @patch("ambari_server.serverConfiguration.search_file")4851 def test_update_ambari_properties_negative_case(self, search_file_mock, properties_mock):4852 search_file_mock.return_value = None4853 #Call tested method4854 self.assertEquals(0, update_ambari_properties())4855 self.assertFalse(properties_mock.called)4856 search_file_mock.return_value = False4857 #Call tested method4858 self.assertEquals(0, update_ambari_properties())4859 self.assertFalse(properties_mock.called)4860 search_file_mock.return_value = ''4861 #Call tested method4862 self.assertEquals(0, update_ambari_properties())4863 self.assertFalse(properties_mock.called)4864 pass4865 @patch("ambari_server.serverConfiguration.get_conf_dir")4866 def test_update_ambari_properties_without_some_properties(self, get_conf_dir_mock):4867 '''4868 Checks: update_ambari_properties call should add ambari-server.user property if4869 it's absent4870 '''4871 from ambari_server import serverConfiguration # need to modify constants inside the module4872 properties = ["server.jdbc.user.name=ambari-server\n",4873 "server.jdbc.user.passwd=/etc/ambari-server/conf/password.dat\n",4874 "java.home=/usr/jdk64/jdk1.6.0_31\n",4875 "server.os_type=redhat6\n"]4876 get_conf_dir_mock.return_value = '/etc/ambari-server/conf'4877 (tf1, fn1) = tempfile.mkstemp()4878 os.close(tf1)4879 (tf2, fn2) = tempfile.mkstemp()4880 os.close(tf2)4881 serverConfiguration.AMBARI_PROPERTIES_RPMSAVE_FILE = fn14882 serverConfiguration.AMBARI_PROPERTIES_FILE = fn24883 with open(serverConfiguration.AMBARI_PROPERTIES_RPMSAVE_FILE, 'w') as f:4884 for line in properties:4885 f.write(line)4886 #Call tested method4887 update_ambari_properties()4888 ambari_properties = Properties()4889 ambari_properties.load(open(fn2))4890 self.assertTrue(NR_USER_PROPERTY in ambari_properties.keys())4891 value = ambari_properties[NR_USER_PROPERTY]4892 self.assertEqual(value, "root")4893 self.assertTrue(OS_FAMILY_PROPERTY in ambari_properties.keys())4894 os.unlink(fn2)4895 pass4896 @not_for_platform(PLATFORM_WINDOWS)4897 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4898 @patch("ambari_commons.firewall.run_os_command")4899 @patch("ambari_server.serverSetup.verify_setup_allowed")4900 @patch("sys.exit")4901 @patch("ambari_server.serverSetup.get_YN_input")4902 @patch("ambari_server.dbConfiguration.get_validated_string_input")4903 @patch("ambari_server.dbConfiguration_linux.get_YN_input")4904 @patch("ambari_server.dbConfiguration_linux.get_validated_string_input")4905 @patch("ambari_server.dbConfiguration_linux.PGConfig._store_remote_properties")4906 @patch("ambari_server.dbConfiguration_linux.LinuxDBMSConfig.ensure_jdbc_driver_installed")4907 @patch("ambari_server.dbConfiguration_linux.read_password")4908 @patch("ambari_server.serverSetup.check_jdbc_drivers")4909 @patch("ambari_server.serverSetup.is_root")4910 @patch("ambari_server.serverSetup.check_ambari_user")4911 @patch("ambari_server.serverSetup.download_and_install_jdk")4912 @patch("ambari_server.serverSetup.configure_os_settings")4913 @patch('__builtin__.raw_input')4914 @patch("ambari_server.serverSetup.disable_security_enhancements")4915 @patch("ambari_server.serverSetup.expand_jce_zip_file")4916 def test_setup_remote_db_wo_client(self, expand_jce_zip_file_mock, check_selinux_mock, raw_input, configure_os_settings_mock,4917 download_jdk_mock, check_ambari_user_mock, is_root_mock, check_jdbc_drivers_mock,4918 read_password_mock, ensure_jdbc_driver_installed_mock, store_remote_properties_mock,4919 get_validated_string_input_0_mock, get_YN_input_0_mock,4920 get_validated_string_input_mock, get_YN_input,4921 exit_mock, verify_setup_allowed_method,4922 run_os_command_mock):4923 args = MagicMock()4924 args.jdbc_driver = None4925 args.jdbc_db = None4926 args.silent = False4927 del args.dbms4928 del args.database_index4929 del args.database_host4930 del args.database_port4931 del args.database_name4932 del args.database_username4933 del args.database_password4934 del args.persistence_type4935 raw_input.return_value = ""4936 is_root_mock.return_value = True4937 check_selinux_mock.return_value = (0, "")4938 run_os_command_mock.return_value = 3,"",""4939 store_remote_properties_mock.return_value = 04940 get_YN_input.return_value = True4941 get_validated_string_input_mock.side_effect = ["4"]4942 get_validated_string_input_0_mock.side_effect = ["localhost", "5432", "ambari", "ambari", "admin"]4943 get_YN_input_0_mock.return_value = False4944 read_password_mock.return_value = "encrypted_bigdata"4945 ensure_jdbc_driver_installed_mock.return_value = True4946 check_jdbc_drivers_mock.return_value = 04947 check_ambari_user_mock.return_value = (0, False, 'user', None)4948 download_jdk_mock.return_value = 04949 configure_os_settings_mock.return_value = 04950 verify_setup_allowed_method.return_value = 04951 expand_jce_zip_file_mock.return_value = 04952 try:4953 setup(args)4954 self.fail("Should throw exception")4955 except NonFatalException as fe:4956 # Expected4957 self.assertTrue("Remote database setup aborted." in fe.reason)4958 pass4959 @not_for_platform(PLATFORM_WINDOWS)4960 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))4961 @patch("ambari_commons.firewall.run_os_command")4962 @patch("sys.exit")4963 @patch("ambari_server.userInput.get_YN_input")4964 @patch("ambari_commons.os_utils.is_root")4965 @patch("ambari_server.dbConfiguration_linux.store_password_file")4966 @patch("__builtin__.raw_input")4967 def test_store_remote_properties(self, raw_input_mock, store_password_file_mock,4968 is_root_mock, get_YN_input, exit_mock,4969 run_os_command_mock4970 ):4971 raw_input_mock.return_value = ""4972 is_root_mock.return_value = True4973 get_YN_input.return_value = False4974 run_os_command_mock.return_value = 3,"",""4975 store_password_file_mock.return_value = "encrypted_bigdata"4976 import optparse4977 args = optparse.Values()4978 args.dbms = "oracle"4979 args.database_host = "localhost"4980 args.database_port = "1234"4981 args.database_name = "ambari"4982 args.postgres_schema = "ambari"4983 args.sid_or_sname = "foo"4984 args.database_username = "foo"4985 args.database_password = "foo"4986 properties0 = Properties()4987 properties = Properties()4988 factory = DBMSConfigFactory()4989 dbConfig = factory.create(args, properties0)4990 dbConfig._store_remote_properties(properties)4991 found = False4992 for n in properties.propertyNames():4993 if not found and n.startswith("server.jdbc.properties"):4994 found = True4995 self.assertTrue(found)4996 # verify that some properties exist4997 self.assertEquals("internal", properties.get_property(JDBC_CONNECTION_POOL_TYPE))4998 # now try with MySQL instead of Oracle to verify that the properties are different4999 args.dbms = "mysql"5000 args.database_index = 25001 properties0 = Properties()5002 properties = Properties()5003 factory = DBMSConfigFactory()5004 dbConfig = factory.create(args, properties0)5005 dbConfig._store_remote_properties(properties)5006 # verify MySQL properties5007 self.assertEquals("c3p0", properties.get_property(JDBC_CONNECTION_POOL_TYPE))5008 @not_for_platform(PLATFORM_WINDOWS)5009 @patch("ambari_server.serverConfiguration.find_properties_file")5010 def test_get_ambari_properties(self, find_properties_file_mock):5011 find_properties_file_mock.return_value = None5012 rcode = get_ambari_properties()5013 self.assertEqual(rcode, -1)5014 tf1 = tempfile.NamedTemporaryFile()5015 find_properties_file_mock.return_value = tf1.name5016 prop_name = 'name'5017 prop_value = 'val'5018 with open(tf1.name, 'w') as fout:5019 fout.write(prop_name + '=' + prop_value)5020 fout.close()5021 properties = get_ambari_properties()5022 self.assertEqual(properties[prop_name], prop_value)5023 pass5024 @only_for_platform(PLATFORM_WINDOWS)5025 @patch("ambari_server.serverConfiguration.find_properties_file")5026 def test_get_ambari_properties(self, find_properties_file):5027 find_properties_file.return_value = None5028 rcode = get_ambari_properties()5029 self.assertEqual(rcode, -1)5030 tf1 = tempfile.NamedTemporaryFile(delete=False)5031 find_properties_file.return_value = tf1.name5032 tf1.close()5033 prop_name = 'name'5034 prop_value = 'val'5035 with open(tf1.name, 'w') as fout:5036 fout.write(prop_name + '=' + prop_value)5037 properties = get_ambari_properties()5038 self.assertEqual(properties[prop_name], prop_value)5039 self.assertEqual(properties.fileName, os.path.abspath(tf1.name))5040 sys.stdout = sys.__stdout__5041 pass5042 @patch("os.path.exists")5043 @patch("os.remove")5044 @patch("ambari_commons.os_utils.print_warning_msg")5045 def test_remove_file(self, printWarningMsgMock, removeMock, pathExistsMock):5046 def side_effect():5047 raise Exception(-1, "Failed to delete!")5048 removeMock.side_effect = side_effect5049 pathExistsMock.return_value = 15050 res = remove_file("/someNonExsistantDir/filename")5051 self.assertEquals(res, 1)5052 removeMock.side_effect = None5053 res = remove_file("/someExsistantDir/filename")5054 self.assertEquals(res, 0)5055 @patch("shutil.copyfile")5056 def test_copy_file(self, shutilCopyfileMock):5057 def side_effect():5058 raise Exception(-1, "Failed to copy!")5059 shutilCopyfileMock.side_effect = side_effect5060 try:5061 copy_file("/tmp/psswd", "/someNonExsistantDir/filename")5062 self.fail("Exception on file not copied has not been thrown!")5063 except FatalException:5064 # Expected5065 pass5066 self.assertTrue(shutilCopyfileMock.called)5067 shutilCopyfileMock.side_effect = None5068 try:5069 copy_file("/tmp/psswd", "/root/psswd")5070 except FatalException:5071 self.fail("Exception on file copied should not be thrown!")5072 self.assertTrue(shutilCopyfileMock.called)5073 pass5074 @not_for_platform(PLATFORM_WINDOWS)5075 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))5076 @patch("ambari_server.dbConfiguration_linux.get_ambari_properties")5077 @patch("ambari_server.dbConfiguration_linux.print_error_msg")5078 @patch("ambari_server.dbConfiguration.print_error_msg")5079 @patch("ambari_server.dbConfiguration_linux.print_warning_msg")5080 @patch("__builtin__.raw_input")5081 @patch("glob.glob")5082 @patch("os.path.isdir")5083 @patch("os.path.lexists")5084 @patch("os.remove")5085 @patch("os.symlink")5086 @patch("shutil.copy")5087 def test_ensure_jdbc_drivers_installed(self, shutil_copy_mock, os_symlink_mock, os_remove_mock, lexists_mock, isdir_mock, glob_mock,5088 raw_input_mock, print_warning_msg, print_error_msg_mock, print_error_msg_2_mock,5089 get_ambari_properties_mock):5090 out = StringIO.StringIO()5091 sys.stdout = out5092 def reset_mocks():5093 get_ambari_properties_mock.reset_mock()5094 shutil_copy_mock.reset_mock()5095 print_error_msg_mock.reset_mock()5096 print_warning_msg.reset_mock()5097 raw_input_mock.reset_mock()5098 args = MagicMock()5099 del args.database_index5100 del args.persistence_type5101 del args.silent5102 del args.sid_or_sname5103 del args.jdbc_url5104 args.dbms = "oracle"5105 return args5106 # Check positive scenario5107 drivers_list = [os.path.join(os.sep,'usr','share','java','ojdbc6.jar')]5108 resources_dir = os.sep + 'tmp'5109 props = Properties()5110 props.process_pair(RESOURCES_DIR_PROPERTY, resources_dir)5111 get_ambari_properties_mock.return_value = props5112 factory = DBMSConfigFactory()5113 args = reset_mocks()5114 glob_mock.return_value = drivers_list5115 isdir_mock.return_value = True5116 lexists_mock.return_value = True5117 dbms = factory.create(args, props)5118 rcode = dbms.ensure_jdbc_driver_installed(props)5119 self.assertEquals(os_symlink_mock.call_count, 1)5120 self.assertEquals(os_symlink_mock.call_args_list[0][0][0], os.path.join(os.sep,'tmp','ojdbc6.jar'))5121 self.assertEquals(os_symlink_mock.call_args_list[0][0][1], os.path.join(os.sep,'tmp','oracle-jdbc-driver.jar'))5122 self.assertTrue(rcode)5123 self.assertEquals(shutil_copy_mock.call_count, 1)5124 self.assertEquals(shutil_copy_mock.call_args_list[0][0][0], drivers_list[0])5125 self.assertEquals(shutil_copy_mock.call_args_list[0][0][1], resources_dir)5126 # Check negative scenarios5127 # Silent option, no drivers5128 set_silent(True)5129 args = reset_mocks()5130 glob_mock.return_value = []5131 failed = False5132 try:5133 dbms = factory.create(args, props)5134 rcode = dbms.ensure_jdbc_driver_installed(props)5135 except FatalException:5136 failed = True5137 self.assertTrue(print_error_msg_mock.called)5138 self.assertTrue(failed)5139 # Non-Silent option, no drivers5140 set_silent(False)5141 args = reset_mocks()5142 glob_mock.return_value = []5143 failed = False5144 try:5145 dbms = factory.create(args, props)5146 rcode = dbms.ensure_jdbc_driver_installed(props)5147 except FatalException:5148 failed = True5149 self.assertTrue(failed)5150 self.assertTrue(print_error_msg_mock.called)5151 # Non-Silent option, no drivers at first ask, present drivers after that5152 args = reset_mocks()5153 glob_mock.side_effect = [[], drivers_list]5154 dbms = factory.create(args, props)5155 rcode = dbms.ensure_jdbc_driver_installed(props)5156 self.assertTrue(rcode)5157 self.assertEquals(shutil_copy_mock.call_count, 1)5158 self.assertEquals(shutil_copy_mock.call_args_list[0][0][0], drivers_list[0])5159 self.assertEquals(shutil_copy_mock.call_args_list[0][0][1], resources_dir)5160 # Non-Silent option, no drivers at first ask, no drivers after that5161 args = reset_mocks()5162 glob_mock.side_effect = [[], []]5163 failed = False5164 try:5165 dbms = factory.create(args, props)5166 rcode = dbms.ensure_jdbc_driver_installed(props)5167 except FatalException:5168 failed = True5169 self.assertTrue(failed)5170 self.assertTrue(print_error_msg_mock.called)5171 # Failed to copy_files5172 args = reset_mocks()5173 glob_mock.side_effect = [drivers_list]5174 try:5175 dbms = factory.create(args, props)5176 rcode = dbms.ensure_jdbc_driver_installed(props)5177 except FatalException:5178 failed = True5179 self.assertTrue(failed)5180 sys.stdout = sys.__stdout__5181 pass5182 @not_for_platform(PLATFORM_WINDOWS)5183 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))5184 @patch("ambari_server.dbConfiguration.get_ambari_properties")5185 @patch("os.path.isdir")5186 @patch("os.path.isfile")5187 @patch("os.path.lexists")5188 @patch("os.remove")5189 @patch("os.symlink")5190 def test_check_jdbc_drivers(self, os_symlink_mock, os_remove_mock, lexists_mock, isfile_mock, isdir_mock,5191 get_ambari_properties_mock):5192 args = MagicMock()5193 # Check positive scenario5194 drivers_list = [os.path.join(os.sep,'usr','share','java','ojdbc6.jar')]5195 resources_dir = os.sep + 'tmp'5196 props = Properties()5197 props.process_pair(RESOURCES_DIR_PROPERTY, resources_dir)5198 get_ambari_properties_mock.return_value = props5199 isdir_mock.return_value = True5200 isfile_mock.side_effect = [True, False, False]5201 del args.database_index5202 del args.persistence_type5203 del args.silent5204 del args.sid_or_sname5205 del args.jdbc_url5206 lexists_mock.return_value = True5207 check_jdbc_drivers(args)5208 self.assertEquals(os_symlink_mock.call_count, 1)5209 self.assertEquals(os_symlink_mock.call_args_list[0][0][0], os.path.join(os.sep,'tmp','ojdbc6.jar'))5210 self.assertEquals(os_symlink_mock.call_args_list[0][0][1], os.path.join(os.sep,'tmp','oracle-jdbc-driver.jar'))5211 # Check negative scenarios5212 # No drivers deployed5213 get_ambari_properties_mock.reset_mock()5214 os_symlink_mock.reset_mock()5215 isfile_mock.side_effect = [False, False, False]5216 check_jdbc_drivers(args)5217 self.assertFalse(os_symlink_mock.called)5218 pass5219 @not_for_platform(PLATFORM_WINDOWS)5220 @patch("ambari_server.serverConfiguration.find_properties_file")5221 def test_get_ambari_properties(self, find_properties_file_mock):5222 find_properties_file_mock.return_value = None5223 rcode = get_ambari_properties()5224 self.assertEqual(rcode, -1)5225 tf1 = tempfile.NamedTemporaryFile()5226 find_properties_file_mock.return_value = tf1.name5227 prop_name = 'name'5228 prop_value = 'val'5229 with open(tf1.name, 'w') as fout:5230 fout.write(prop_name + '=' + prop_value)5231 fout.close()5232 properties = get_ambari_properties()5233 self.assertEqual(properties[prop_name], prop_value)5234 self.assertEqual(properties.fileName, os.path.abspath(tf1.name))5235 sys.stdout = sys.__stdout__5236 pass5237 @only_for_platform(PLATFORM_WINDOWS)5238 @patch("ambari_server.serverConfiguration.find_properties_file")5239 def test_get_ambari_properties(self, find_properties_file_mock):5240 find_properties_file_mock.return_value = None5241 rcode = get_ambari_properties()5242 self.assertEqual(rcode, -1)5243 tf1 = tempfile.NamedTemporaryFile(delete=False)5244 find_properties_file_mock.return_value = tf1.name5245 prop_name = 'name'5246 prop_value = 'val'5247 tf1.close()5248 with open(tf1.name, 'w') as fout:5249 fout.write(prop_name + '=' + prop_value)5250 fout.close()5251 properties = get_ambari_properties()5252 self.assertEqual(properties[prop_name], prop_value)5253 self.assertEqual(properties.fileName, os.path.abspath(tf1.name))5254 sys.stdout = sys.__stdout__5255 pass5256 @not_for_platform(PLATFORM_WINDOWS)5257 @patch("ambari_server.serverConfiguration.check_database_name_property")5258 @patch("ambari_server.serverConfiguration.find_properties_file")5259 def test_parse_properties_file(self, find_properties_file_mock, check_database_name_property_mock):5260 check_database_name_property_mock.return_value = 15261 tf1 = tempfile.NamedTemporaryFile(mode='r')5262 find_properties_file_mock.return_value = tf1.name5263 args = MagicMock()5264 parse_properties_file(args)5265 self.assertEquals(args.persistence_type, "local")5266 with open(tf1.name, 'w') as fout:5267 fout.write("\n")5268 fout.write(PERSISTENCE_TYPE_PROPERTY + "=remote")5269 args = MagicMock()5270 parse_properties_file(args)5271 self.assertEquals(args.persistence_type, "remote")5272 pass5273 @not_for_platform(PLATFORM_WINDOWS)5274 @patch("os.path.isabs")5275 @patch("ambari_server.dbConfiguration.decrypt_password_for_alias")5276 @patch("ambari_server.dbConfiguration_linux.get_ambari_properties")5277 def test_configure_database_username_password_masterkey_persisted(self,5278 get_ambari_properties_method,5279 decrypt_password_for_alias_method,5280 path_isabs_method):5281 out = StringIO.StringIO()5282 sys.stdout = out5283 properties = Properties()5284 properties.process_pair(JDBC_USER_NAME_PROPERTY, "fakeuser")5285 properties.process_pair(JDBC_PASSWORD_PROPERTY, "${alias=somealias}")5286 properties.process_pair(JDBC_DATABASE_NAME_PROPERTY, "fakedbname")5287 properties.process_pair(SECURITY_KEY_IS_PERSISTED, "True")5288 get_ambari_properties_method.return_value = properties5289 decrypt_password_for_alias_method.return_value = "falepasswd"5290 args = MagicMock()5291 args.master_key = None5292 del args.database_index5293 del args.dbms5294 del args.database_host5295 del args.database_port5296 del args.database_name5297 del args.database_username5298 del args.database_password5299 del args.sid_or_sname5300 del args.jdbc_url5301 dbms = OracleConfig(args, properties, "local")5302 self.assertTrue(decrypt_password_for_alias_method.called)5303 self.assertFalse(path_isabs_method.called)5304 self.assertEquals("fakeuser", dbms.database_username)5305 self.assertEquals("falepasswd", dbms.database_password)5306 sys.stdout = sys.__stdout__5307 pass5308 @not_for_platform(PLATFORM_WINDOWS)5309 @patch("ambari_server.dbConfiguration_linux.read_password")5310 def test_configure_database_password(self, read_password_method):5311 out = StringIO.StringIO()5312 sys.stdout = out5313 read_password_method.return_value = "fakepasswd"5314 result = LinuxDBMSConfig._configure_database_password(True)5315 self.assertTrue(read_password_method.called)5316 self.assertEquals("fakepasswd", result)5317 result = LinuxDBMSConfig._configure_database_password(True)5318 self.assertEquals("fakepasswd", result)5319 result = LinuxDBMSConfig._configure_database_password(True)5320 self.assertEquals("fakepasswd", result)5321 sys.stdout = sys.__stdout__5322 pass5323 @patch("os.path.exists")5324 @patch("ambari_server.setupSecurity.get_is_secure")5325 @patch("ambari_server.setupSecurity.get_is_persisted")5326 @patch("ambari_server.setupSecurity.remove_password_file")5327 @patch("ambari_server.setupSecurity.save_passwd_for_alias")5328 @patch("ambari_server.setupSecurity.read_master_key")5329 @patch("ambari_server.setupSecurity.read_ambari_user")5330 @patch("ambari_server.setupSecurity.get_master_key_location")5331 @patch("ambari_server.setupSecurity.update_properties_2")5332 @patch("ambari_server.setupSecurity.save_master_key")5333 @patch("ambari_server.setupSecurity.get_YN_input")5334 @patch("ambari_server.setupSecurity.search_file")5335 @patch("ambari_server.setupSecurity.get_ambari_properties")5336 @patch("ambari_server.setupSecurity.is_root")5337 def test_setup_master_key_not_persist(self, is_root_method,5338 get_ambari_properties_method, search_file_message,5339 get_YN_input_method, save_master_key_method,5340 update_properties_method, get_master_key_location_method,5341 read_ambari_user_method, read_master_key_method,5342 save_passwd_for_alias_method, remove_password_file_method,5343 get_is_persisted_method, get_is_secure_method, exists_mock):5344 is_root_method.return_value = True5345 p = Properties()5346 FAKE_PWD_STRING = "fakepasswd"5347 p.process_pair(JDBC_PASSWORD_PROPERTY, FAKE_PWD_STRING)5348 p.process_pair(LDAP_MGR_PASSWORD_PROPERTY, FAKE_PWD_STRING)5349 p.process_pair(SSL_TRUSTSTORE_PASSWORD_PROPERTY, FAKE_PWD_STRING)5350 p.process_pair(JDBC_RCA_PASSWORD_FILE_PROPERTY, FAKE_PWD_STRING)5351 get_ambari_properties_method.return_value = p5352 read_master_key_method.return_value = "aaa"5353 get_YN_input_method.return_value = False5354 read_ambari_user_method.return_value = None5355 save_passwd_for_alias_method.return_value = 05356 get_is_persisted_method.return_value = (True, "filepath")5357 get_is_secure_method.return_value = False5358 exists_mock.return_value = False5359 setup_master_key()5360 self.assertTrue(get_YN_input_method.called)5361 self.assertTrue(read_master_key_method.called)5362 self.assertTrue(read_ambari_user_method.called)5363 self.assertTrue(update_properties_method.called)5364 self.assertFalse(save_master_key_method.called)5365 self.assertTrue(save_passwd_for_alias_method.called)5366 self.assertEquals(3, save_passwd_for_alias_method.call_count)5367 self.assertTrue(remove_password_file_method.called)5368 result_expected = {JDBC_PASSWORD_PROPERTY:5369 get_alias_string(JDBC_RCA_PASSWORD_ALIAS),5370 JDBC_RCA_PASSWORD_FILE_PROPERTY:5371 get_alias_string(JDBC_RCA_PASSWORD_ALIAS),5372 LDAP_MGR_PASSWORD_PROPERTY:5373 get_alias_string(LDAP_MGR_PASSWORD_ALIAS),5374 SSL_TRUSTSTORE_PASSWORD_PROPERTY:5375 get_alias_string(SSL_TRUSTSTORE_PASSWORD_ALIAS),5376 SECURITY_IS_ENCRYPTION_ENABLED: 'true'}5377 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))5378 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5379 key=operator.itemgetter(0))5380 self.assertEquals(sorted_x, sorted_y)5381 pass5382 @patch("ambari_server.setupSecurity.save_passwd_for_alias")5383 @patch("os.path.exists")5384 @patch("ambari_server.setupSecurity.get_is_secure")5385 @patch("ambari_server.setupSecurity.get_is_persisted")5386 @patch("ambari_server.setupSecurity.read_master_key")5387 @patch("ambari_server.setupSecurity.read_ambari_user")5388 @patch("ambari_server.setupSecurity.get_master_key_location")5389 @patch("ambari_server.setupSecurity.update_properties_2")5390 @patch("ambari_server.setupSecurity.save_master_key")5391 @patch("ambari_server.setupSecurity.get_YN_input")5392 @patch("ambari_server.serverConfiguration.search_file")5393 @patch("ambari_server.setupSecurity.get_ambari_properties")5394 @patch("ambari_server.setupSecurity.is_root")5395 def test_setup_master_key_persist(self, is_root_method,5396 get_ambari_properties_method, search_file_message,5397 get_YN_input_method, save_master_key_method,5398 update_properties_method, get_master_key_location_method,5399 read_ambari_user_method, read_master_key_method,5400 get_is_persisted_method, get_is_secure_method, exists_mock,5401 save_passwd_for_alias_method):5402 is_root_method.return_value = True5403 p = Properties()5404 FAKE_PWD_STRING = "fakepasswd"5405 p.process_pair(JDBC_PASSWORD_PROPERTY, FAKE_PWD_STRING)5406 get_ambari_properties_method.return_value = p5407 search_file_message.return_value = "propertiesfile"5408 read_master_key_method.return_value = "aaa"5409 get_YN_input_method.side_effect = [True, False]5410 read_ambari_user_method.return_value = None5411 get_is_persisted_method.return_value = (True, "filepath")5412 get_is_secure_method.return_value = False5413 exists_mock.return_value = False5414 save_passwd_for_alias_method.return_value = 05415 setup_master_key()5416 self.assertTrue(get_YN_input_method.called)5417 self.assertTrue(read_master_key_method.called)5418 self.assertTrue(read_ambari_user_method.called)5419 self.assertTrue(update_properties_method.called)5420 self.assertTrue(save_master_key_method.called)5421 result_expected = {JDBC_PASSWORD_PROPERTY:5422 get_alias_string(JDBC_RCA_PASSWORD_ALIAS),5423 SECURITY_IS_ENCRYPTION_ENABLED: 'true'}5424 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))5425 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5426 key=operator.itemgetter(0))5427 self.assertEquals(sorted_x, sorted_y)5428 pass5429 @patch("ambari_server.setupSecurity.read_master_key")5430 @patch("ambari_server.setupSecurity.remove_password_file")5431 @patch("os.path.exists")5432 @patch("ambari_server.setupSecurity.read_ambari_user")5433 @patch("ambari_server.setupSecurity.get_master_key_location")5434 @patch("ambari_server.setupSecurity.save_passwd_for_alias")5435 @patch("ambari_server.setupSecurity.read_passwd_for_alias")5436 @patch("ambari_server.setupSecurity.update_properties_2")5437 @patch("ambari_server.setupSecurity.save_master_key")5438 @patch("ambari_server.setupSecurity.get_validated_string_input")5439 @patch("ambari_server.setupSecurity.get_YN_input")5440 @patch("ambari_server.setupSecurity.search_file")5441 @patch("ambari_server.setupSecurity.get_ambari_properties")5442 @patch("ambari_server.setupSecurity.is_root")5443 def test_reset_master_key_persisted(self, is_root_method,5444 get_ambari_properties_method, search_file_message,5445 get_YN_input_method, get_validated_string_input_method,5446 save_master_key_method, update_properties_method,5447 read_passwd_for_alias_method, save_passwd_for_alias_method,5448 get_master_key_location_method,5449 read_ambari_user_method, exists_mock,5450 remove_password_file_method, read_master_key_method):5451 # Testing call under non-root5452 is_root_method.return_value = False5453 try:5454 setup_master_key()5455 self.fail("Should throw exception")5456 except FatalException as fe:5457 # Expected5458 self.assertTrue("root-level" in fe.reason)5459 pass5460 # Testing call under root5461 is_root_method.return_value = True5462 search_file_message.return_value = "filepath"5463 read_ambari_user_method.return_value = None5464 p = Properties()5465 FAKE_PWD_STRING = '${alias=fakealias}'5466 p.process_pair(JDBC_PASSWORD_PROPERTY, FAKE_PWD_STRING)5467 p.process_pair(LDAP_MGR_PASSWORD_PROPERTY, FAKE_PWD_STRING)5468 p.process_pair(SSL_TRUSTSTORE_PASSWORD_PROPERTY, FAKE_PWD_STRING)5469 p.process_pair(JDBC_RCA_PASSWORD_FILE_PROPERTY, FAKE_PWD_STRING)5470 get_ambari_properties_method.return_value = p5471 get_YN_input_method.side_effect = [True, True]5472 read_master_key_method.return_value = "aaa"5473 read_passwd_for_alias_method.return_value = "fakepassword"5474 save_passwd_for_alias_method.return_value = 05475 exists_mock.return_value = False5476 setup_master_key()5477 self.assertTrue(save_master_key_method.called)5478 self.assertTrue(get_YN_input_method.called)5479 self.assertTrue(read_master_key_method.called)5480 self.assertTrue(update_properties_method.called)5481 self.assertTrue(read_passwd_for_alias_method.called)5482 self.assertTrue(3, read_passwd_for_alias_method.call_count)5483 self.assertTrue(3, save_passwd_for_alias_method.call_count)5484 result_expected = {JDBC_PASSWORD_PROPERTY:5485 get_alias_string(JDBC_RCA_PASSWORD_ALIAS),5486 JDBC_RCA_PASSWORD_FILE_PROPERTY:5487 get_alias_string(JDBC_RCA_PASSWORD_ALIAS),5488 LDAP_MGR_PASSWORD_PROPERTY:5489 get_alias_string(LDAP_MGR_PASSWORD_ALIAS),5490 SSL_TRUSTSTORE_PASSWORD_PROPERTY:5491 get_alias_string(SSL_TRUSTSTORE_PASSWORD_ALIAS),5492 SECURITY_IS_ENCRYPTION_ENABLED: 'true'}5493 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))5494 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5495 key=operator.itemgetter(0))5496 self.assertEquals(sorted_x, sorted_y)5497 pass5498 @patch("ambari_server.setupSecurity.get_is_persisted")5499 @patch("ambari_server.setupSecurity.get_is_secure")5500 @patch("ambari_server.setupSecurity.remove_password_file")5501 @patch("os.path.exists")5502 @patch("ambari_server.setupSecurity.read_ambari_user")5503 @patch("ambari_server.setupSecurity.get_master_key_location")5504 @patch("ambari_server.setupSecurity.save_passwd_for_alias")5505 @patch("ambari_server.setupSecurity.read_passwd_for_alias")5506 @patch("ambari_server.setupSecurity.update_properties_2")5507 @patch("ambari_server.setupSecurity.save_master_key")5508 @patch("ambari_server.setupSecurity.get_validated_string_input")5509 @patch("ambari_server.setupSecurity.get_YN_input")5510 @patch("ambari_server.setupSecurity.search_file")5511 @patch("ambari_server.setupSecurity.get_ambari_properties")5512 @patch("ambari_server.setupSecurity.is_root")5513 def test_reset_master_key_not_persisted(self, is_root_method,5514 get_ambari_properties_method,5515 search_file_message, get_YN_input_method,5516 get_validated_string_input_method, save_master_key_method,5517 update_properties_method, read_passwd_for_alias_method,5518 save_passwd_for_alias_method,5519 get_master_key_location_method, read_ambari_user_method,5520 exists_mock, remove_password_file_method, get_is_secure_method,5521 get_is_persisted_method):5522 is_root_method.return_value = True5523 search_file_message.return_value = False5524 read_ambari_user_method.return_value = None5525 p = Properties()5526 FAKE_PWD_STRING = '${alias=fakealias}'5527 p.process_pair(JDBC_PASSWORD_PROPERTY, FAKE_PWD_STRING)5528 p.process_pair(LDAP_MGR_PASSWORD_PROPERTY, FAKE_PWD_STRING)5529 p.process_pair(SSL_TRUSTSTORE_PASSWORD_PROPERTY, FAKE_PWD_STRING)5530 p.process_pair(JDBC_RCA_PASSWORD_FILE_PROPERTY, FAKE_PWD_STRING)5531 get_ambari_properties_method.return_value = p5532 get_YN_input_method.side_effect = [True, False]5533 get_validated_string_input_method.return_value = "aaa"5534 read_passwd_for_alias_method.return_value = "fakepassword"5535 save_passwd_for_alias_method.return_value = 05536 exists_mock.return_value = False5537 get_is_secure_method.return_value = True5538 get_is_persisted_method.return_value = (True, "filePath")5539 setup_master_key()5540 self.assertFalse(save_master_key_method.called)5541 self.assertTrue(get_YN_input_method.called)5542 self.assertTrue(get_validated_string_input_method.called)5543 self.assertTrue(update_properties_method.called)5544 self.assertTrue(read_passwd_for_alias_method.called)5545 self.assertTrue(3, read_passwd_for_alias_method.call_count)5546 self.assertTrue(3, save_passwd_for_alias_method.call_count)5547 self.assertFalse(save_master_key_method.called)5548 result_expected = {JDBC_PASSWORD_PROPERTY:5549 get_alias_string(JDBC_RCA_PASSWORD_ALIAS),5550 JDBC_RCA_PASSWORD_FILE_PROPERTY:5551 get_alias_string(JDBC_RCA_PASSWORD_ALIAS),5552 LDAP_MGR_PASSWORD_PROPERTY:5553 get_alias_string(LDAP_MGR_PASSWORD_ALIAS),5554 SSL_TRUSTSTORE_PASSWORD_PROPERTY:5555 get_alias_string(SSL_TRUSTSTORE_PASSWORD_ALIAS),5556 SECURITY_IS_ENCRYPTION_ENABLED: 'true'}5557 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))5558 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5559 key=operator.itemgetter(0))5560 self.assertEquals(sorted_x, sorted_y)5561 pass5562 @staticmethod5563 @OsFamilyFuncImpl(OSConst.WINSRV_FAMILY)5564 def _init_test_ldap_properties_map_invalid_input_1():5565 ldap_properties_map = \5566 {5567 LDAP_PRIMARY_URL_PROPERTY: "a:3",5568 "authentication.ldap.secondaryUrl": "b:2",5569 "authentication.ldap.useSSL": "false",5570 "authentication.ldap.usernameAttribute": "user",5571 "authentication.ldap.baseDn": "uid",5572 "authentication.ldap.bindAnonymously": "true",5573 "authentication.ldap.referral": "follow",5574 "client.security": "ldap",5575 "ambari.ldap.isConfigured": "true"5576 }5577 return ldap_properties_map5578 @staticmethod5579 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)5580 def _init_test_ldap_properties_map_invalid_input_1():5581 ldap_properties_map = \5582 {5583 LDAP_PRIMARY_URL_PROPERTY: "a:3",5584 "authentication.ldap.secondaryUrl": "b:2",5585 "authentication.ldap.useSSL": "false",5586 "authentication.ldap.userObjectClass": "user",5587 "authentication.ldap.usernameAttribute": "uid",5588 "authentication.ldap.groupObjectClass": "group",5589 "authentication.ldap.groupNamingAttr": "cn",5590 "authentication.ldap.groupMembershipAttr": "member",5591 "authentication.ldap.dnAttribute": "dn",5592 "authentication.ldap.baseDn": "base",5593 "authentication.ldap.referral": "follow",5594 "authentication.ldap.bindAnonymously": "true",5595 "client.security": "ldap",5596 "ambari.ldap.isConfigured": "true"5597 }5598 return ldap_properties_map5599 @staticmethod5600 @OsFamilyFuncImpl(OSConst.WINSRV_FAMILY)5601 def _init_test_ldap_properties_map_invalid_input_2():5602 ldap_properties_map = \5603 {5604 LDAP_PRIMARY_URL_PROPERTY: "a:3",5605 "authentication.ldap.useSSL": "false",5606 "authentication.ldap.usernameAttribute": "user",5607 "authentication.ldap.baseDn": "uid",5608 "authentication.ldap.bindAnonymously": "true",5609 "authentication.ldap.referral": "follow",5610 "client.security": "ldap",5611 "ambari.ldap.isConfigured": "true"5612 }5613 return ldap_properties_map5614 @staticmethod5615 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)5616 def _init_test_ldap_properties_map_invalid_input_2():5617 ldap_properties_map = \5618 {5619 LDAP_PRIMARY_URL_PROPERTY: "a:3",5620 "authentication.ldap.useSSL": "false",5621 "authentication.ldap.userObjectClass": "user",5622 "authentication.ldap.usernameAttribute": "uid",5623 "authentication.ldap.groupObjectClass": "group",5624 "authentication.ldap.groupNamingAttr": "cn",5625 "authentication.ldap.groupMembershipAttr": "member",5626 "authentication.ldap.dnAttribute": "dn",5627 "authentication.ldap.baseDn": "base",5628 "authentication.ldap.referral": "follow",5629 "authentication.ldap.bindAnonymously": "true",5630 "client.security": "ldap",5631 "ambari.ldap.isConfigured": "true"5632 }5633 return ldap_properties_map5634 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))5635 @patch("__builtin__.raw_input")5636 @patch("ambari_server.setupSecurity.get_is_secure")5637 @patch("ambari_server.setupSecurity.get_YN_input")5638 @patch("ambari_server.setupSecurity.update_properties_2")5639 @patch("ambari_server.setupSecurity.search_file")5640 @patch("ambari_server.setupSecurity.get_ambari_properties")5641 @patch("ambari_server.setupSecurity.is_root")5642 def test_setup_ldap_invalid_input(self, is_root_method, get_ambari_properties_method,5643 search_file_message,5644 update_properties_method,5645 get_YN_input_method,5646 get_is_secure_method,5647 raw_input_mock):5648 out = StringIO.StringIO()5649 sys.stdout = out5650 is_root_method.return_value = True5651 search_file_message.return_value = "filepath"5652 configs = {SECURITY_MASTER_KEY_LOCATION: "filepath",5653 SECURITY_KEYS_DIR: tempfile.gettempdir(),5654 SECURITY_IS_ENCRYPTION_ENABLED: "true"5655 }5656 get_ambari_properties_method.return_value = configs5657 raw_input_mock.side_effect = ['a:3', 'b:b', 'hody', 'b:2', 'false', 'user', 'uid', 'group', 'cn', 'member', 'dn', 'base', 'follow', 'true']5658 set_silent(False)5659 get_YN_input_method.return_value = True5660 setup_ldap()5661 ldap_properties_map = TestAmbariServer._init_test_ldap_properties_map_invalid_input_1()5662 sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))5663 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5664 key=operator.itemgetter(0))5665 self.assertEquals(sorted_x, sorted_y)5666 self.assertTrue(get_YN_input_method.called)5667 self.assertEquals(14, raw_input_mock.call_count)5668 raw_input_mock.reset_mock()5669 raw_input_mock.side_effect = ['a:3', '', 'b:2', 'false', 'user', 'uid', 'group', 'cn', 'member', 'dn', 'base', 'follow', 'true']5670 setup_ldap()5671 ldap_properties_map = TestAmbariServer._init_test_ldap_properties_map_invalid_input_2()5672 sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))5673 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5674 key=operator.itemgetter(0))5675 self.assertEquals(sorted_x, sorted_y)5676 self.assertEquals(13, raw_input_mock.call_count)5677 sys.stdout = sys.__stdout__5678 pass5679 @staticmethod5680 @OsFamilyFuncImpl(OSConst.WINSRV_FAMILY)5681 def _init_test_ldap_properties_map():5682 ldap_properties_map = \5683 {5684 "authentication.ldap.primaryUrl": "test",5685 "authentication.ldap.secondaryUrl": "test",5686 "authentication.ldap.useSSL": "false",5687 "authentication.ldap.usernameAttribute": "test",5688 "authentication.ldap.baseDn": "test",5689 "authentication.ldap.bindAnonymously": "false",5690 "authentication.ldap.managerDn": "test",5691 "authentication.ldap.referral": "test",5692 "client.security": "ldap",5693 LDAP_MGR_PASSWORD_PROPERTY: "ldap-password.dat",5694 "ambari.ldap.isConfigured": "true"5695 }5696 return ldap_properties_map5697 @staticmethod5698 @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)5699 def _init_test_ldap_properties_map():5700 ldap_properties_map = \5701 {5702 "authentication.ldap.primaryUrl": "test",5703 "authentication.ldap.secondaryUrl": "test",5704 "authentication.ldap.useSSL": "false",5705 "authentication.ldap.userObjectClass": "test",5706 "authentication.ldap.usernameAttribute": "test",5707 "authentication.ldap.baseDn": "test",5708 "authentication.ldap.bindAnonymously": "false",5709 "authentication.ldap.managerDn": "test",5710 "authentication.ldap.groupObjectClass": "test",5711 "authentication.ldap.groupMembershipAttr": "test",5712 "authentication.ldap.groupNamingAttr": "test",5713 "authentication.ldap.dnAttribute": "test",5714 "authentication.ldap.referral": "test",5715 "client.security": "ldap",5716 LDAP_MGR_PASSWORD_PROPERTY: "ldap-password.dat",5717 "ambari.ldap.isConfigured": "true"5718 }5719 return ldap_properties_map5720 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))5721 @patch("ambari_server.setupSecurity.get_is_secure")5722 @patch("ambari_server.setupSecurity.encrypt_password")5723 @patch("ambari_server.setupSecurity.save_passwd_for_alias")5724 @patch("ambari_server.setupSecurity.get_YN_input")5725 @patch("ambari_server.setupSecurity.update_properties_2")5726 @patch("ambari_server.setupSecurity.configure_ldap_password")5727 @patch("ambari_server.setupSecurity.get_validated_string_input")5728 @patch("ambari_server.serverConfiguration.search_file")5729 @patch("ambari_server.setupSecurity.get_ambari_properties")5730 @patch("ambari_server.setupSecurity.is_root")5731 @patch("ambari_server.setupSecurity.read_password")5732 @patch("os.path.exists")5733 def test_setup_ldap(self, exists_method, read_password_method, is_root_method, get_ambari_properties_method,5734 search_file_message,5735 get_validated_string_input_method,5736 configure_ldap_password_method, update_properties_method,5737 get_YN_input_method, save_passwd_for_alias_method,5738 encrypt_password_method, get_is_secure_method):5739 out = StringIO.StringIO()5740 sys.stdout = out5741 # Testing call under non-root5742 is_root_method.return_value = False5743 try:5744 setup_ldap()5745 self.fail("Should throw exception")5746 except FatalException as fe:5747 # Expected5748 self.assertTrue("root-level" in fe.reason)5749 pass5750 # Testing call under root5751 is_root_method.return_value = True5752 search_file_message.return_value = "filepath"5753 configs = {SECURITY_MASTER_KEY_LOCATION: "filepath",5754 SECURITY_KEYS_DIR: tempfile.gettempdir(),5755 SECURITY_IS_ENCRYPTION_ENABLED: "true"5756 }5757 get_ambari_properties_method.return_value = configs5758 configure_ldap_password_method.return_value = "password"5759 save_passwd_for_alias_method.return_value = 05760 encrypt_password_method.return_value = get_alias_string(LDAP_MGR_PASSWORD_ALIAS)5761 def yn_input_side_effect(*args, **kwargs):5762 if 'TrustStore' in args[0]:5763 return False5764 else:5765 return True5766 #get_YN_input_method.side_effect = yn_input_side_effect()5767 get_YN_input_method.side_effect = [True, ]5768 def valid_input_side_effect(*args, **kwargs):5769 if 'Bind anonymously' in args[0]:5770 return 'false'5771 if args[1] == "true" or args[1] == "false":5772 return args[1]5773 else:5774 return "test"5775 get_validated_string_input_method.side_effect = valid_input_side_effect5776 setup_ldap()5777 ldap_properties_map = TestAmbariServer._init_test_ldap_properties_map()5778 sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))5779 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5780 key=operator.itemgetter(0))5781 self.assertEquals(sorted_x, sorted_y)5782 self.assertTrue(update_properties_method.called)5783 self.assertTrue(configure_ldap_password_method.called)5784 self.assertTrue(get_validated_string_input_method.called)5785 self.assertTrue(get_YN_input_method.called)5786 # truststore not found case5787 def os_path_exists(*args, **kwargs):5788 if "bogus" in args[0]:5789 return False5790 else:5791 return True5792 pass5793 def input_enable_ssl(*args, **kwargs):5794 if 'Bind anonymously' in args[0]:5795 return 'false'5796 if "SSL" in args[0]:5797 return "true"5798 if "Path to TrustStore file" in args[0]:5799 if input_enable_ssl.path_counter < 2:5800 input_enable_ssl.path_counter += 15801 return "bogus"5802 else:5803 return "valid"5804 if args[1] == "true" or args[1] == "false":5805 return args[1]5806 else:5807 return "test"5808 pass5809 input_enable_ssl.path_counter = 05810 exists_method.side_effect = os_path_exists5811 get_validated_string_input_method.side_effect = input_enable_ssl5812 read_password_method.return_value = "password"5813 get_YN_input_method.reset_mock()5814 get_YN_input_method.side_effect = [True, True]5815 update_properties_method.reset_mock()5816 setup_ldap()5817 self.assertTrue(read_password_method.called)5818 ldap_properties_map = \5819 {5820 "authentication.ldap.primaryUrl": "test",5821 "authentication.ldap.secondaryUrl": "test",5822 "authentication.ldap.useSSL": "true",5823 "authentication.ldap.usernameAttribute": "test",5824 "authentication.ldap.baseDn": "test",5825 "authentication.ldap.dnAttribute": "test",5826 "authentication.ldap.bindAnonymously": "false",5827 "authentication.ldap.managerDn": "test",5828 "client.security": "ldap",5829 "ssl.trustStore.type": "test",5830 "ssl.trustStore.path": "valid",5831 "ssl.trustStore.password": "password",5832 LDAP_MGR_PASSWORD_PROPERTY: get_alias_string(LDAP_MGR_PASSWORD_ALIAS)5833 }5834 sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))5835 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),5836 key=operator.itemgetter(0))5837 sys.stdout = sys.__stdout__5838 pass5839 @patch("urllib2.urlopen")5840 @patch("ambari_server.setupSecurity.get_validated_string_input")5841 @patch("ambari_server.setupSecurity.get_ambari_properties")5842 @patch("ambari_server.setupSecurity.is_server_runing")5843 @patch("ambari_server.setupSecurity.is_root")5844 def test_ldap_sync_all(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,5845 get_validated_string_input_mock, urlopen_mock):5846 is_root_method.return_value = True5847 is_server_runing_mock.return_value = (True, 0)5848 properties = Properties()5849 properties.process_pair(IS_LDAP_CONFIGURED, 'true')5850 properties.process_pair(CLIENT_API_PORT_PROPERTY, '8080')5851 get_ambari_properties_mock.return_value = properties5852 get_validated_string_input_mock.side_effect = ['admin', 'admin']5853 response = MagicMock()5854 response.getcode.side_effect = [201, 200, 200]5855 response.read.side_effect = ['{"resources" : [{"href" : "http://c6401.ambari.apache.org:8080/api/v1/ldap_sync_events/16","Event" : {"id" : 16}}]}',5856 '{"Event":{"status" : "RUNNING","summary" : {"groups" : {"created" : 0,"removed" : 0,"updated" : 0},"memberships" : {"created" : 0,"removed" : 0},"users" : {"created" : 0,"removed" : 0,"updated" : 0}}}}',5857 '{"Event":{"status" : "COMPLETE","summary" : {"groups" : {"created" : 1,"removed" : 0,"updated" : 0},"memberships" : {"created" : 5,"removed" : 0},"users" : {"created" : 5,"removed" : 0,"updated" : 0}}}}']5858 urlopen_mock.return_value = response5859 options = MagicMock()5860 options.ldap_sync_all = True5861 options.ldap_sync_existing = False5862 options.ldap_sync_users = None5863 options.ldap_sync_groups = None5864 sync_ldap(options)5865 url = '{0}://{1}:{2!s}{3}'.format('http', '127.0.0.1', '8080', '/api/v1/ldap_sync_events')5866 request = urlopen_mock.call_args_list[0][0][0]5867 self.assertEquals(url, str(request.get_full_url()))5868 self.assertEquals('[{"Event": {"specs": [{"principal_type": "users", "sync_type": "all"}, {"principal_type": "groups", "sync_type": "all"}]}}]', request.data)5869 self.assertTrue(response.getcode.called)5870 self.assertTrue(response.read.called)5871 pass5872 @patch("__builtin__.open")5873 @patch("os.path.exists")5874 @patch("urllib2.urlopen")5875 @patch("ambari_server.setupSecurity.get_validated_string_input")5876 @patch("ambari_server.setupSecurity.get_ambari_properties")5877 @patch("ambari_server.setupSecurity.is_server_runing")5878 @patch("ambari_server.setupSecurity.is_root")5879 def test_ldap_sync_users(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,5880 get_validated_string_input_mock, urlopen_mock, os_path_exists_mock, open_mock):5881 os_path_exists_mock.return_value = 15882 f = MagicMock()5883 f.__enter__().read.return_value = "bob, tom"5884 open_mock.return_value = f5885 is_root_method.return_value = True5886 is_server_runing_mock.return_value = (True, 0)5887 properties = Properties()5888 properties.process_pair(IS_LDAP_CONFIGURED, 'true')5889 get_ambari_properties_mock.return_value = properties5890 get_validated_string_input_mock.side_effect = ['admin', 'admin']5891 response = MagicMock()5892 response.getcode.side_effect = [201, 200, 200]5893 response.read.side_effect = ['{"resources" : [{"href" : "http://c6401.ambari.apache.org:8080/api/v1/ldap_sync_events/16","Event" : {"id" : 16}}]}',5894 '{"Event":{"status" : "RUNNING","summary" : {"groups" : {"created" : 0,"removed" : 0,"updated" : 0},"memberships" : {"created" : 0,"removed" : 0},"users" : {"created" : 0,"removed" : 0,"updated" : 0}}}}',5895 '{"Event":{"status" : "COMPLETE","summary" : {"groups" : {"created" : 1,"removed" : 0,"updated" : 0},"memberships" : {"created" : 5,"removed" : 0},"users" : {"created" : 5,"removed" : 0,"updated" : 0}}}}']5896 urlopen_mock.return_value = response5897 options = MagicMock()5898 options.ldap_sync_all = False5899 options.ldap_sync_existing = False5900 options.ldap_sync_users = 'users.txt'5901 options.ldap_sync_groups = None5902 sync_ldap(options)5903 request = urlopen_mock.call_args_list[0][0][0]5904 self.assertEquals('[{"Event": {"specs": [{"principal_type": "users", "sync_type": "specific", "names": "bob, tom"}]}}]', request.data)5905 self.assertTrue(response.getcode.called)5906 self.assertTrue(response.read.called)5907 pass5908 @patch("__builtin__.open")5909 @patch("os.path.exists")5910 @patch("urllib2.urlopen")5911 @patch("ambari_server.setupSecurity.get_validated_string_input")5912 @patch("ambari_server.setupSecurity.get_ambari_properties")5913 @patch("ambari_server.setupSecurity.is_server_runing")5914 @patch("ambari_server.setupSecurity.is_root")5915 def test_ldap_sync_groups(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,5916 get_validated_string_input_mock, urlopen_mock, os_path_exists_mock, open_mock):5917 os_path_exists_mock.return_value = 15918 f = MagicMock()5919 f.__enter__().read.return_value = "group1, group2"5920 open_mock.return_value = f5921 is_root_method.return_value = True5922 is_server_runing_mock.return_value = (True, 0)5923 properties = Properties()5924 properties.process_pair(IS_LDAP_CONFIGURED, 'true')5925 get_ambari_properties_mock.return_value = properties5926 get_validated_string_input_mock.side_effect = ['admin', 'admin']5927 response = MagicMock()5928 response.getcode.side_effect = [201, 200, 200]5929 response.read.side_effect = ['{"resources" : [{"href" : "http://c6401.ambari.apache.org:8080/api/v1/ldap_sync_events/16","Event" : {"id" : 16}}]}',5930 '{"Event":{"status" : "RUNNING","summary" : {"groups" : {"created" : 0,"removed" : 0,"updated" : 0},"memberships" : {"created" : 0,"removed" : 0},"users" : {"created" : 0,"removed" : 0,"updated" : 0}}}}',5931 '{"Event":{"status" : "COMPLETE","summary" : {"groups" : {"created" : 1,"removed" : 0,"updated" : 0},"memberships" : {"created" : 5,"removed" : 0},"users" : {"created" : 5,"removed" : 0,"updated" : 0}}}}']5932 urlopen_mock.return_value = response5933 options = MagicMock()5934 options.ldap_sync_all = False5935 options.ldap_sync_existing = False5936 options.ldap_sync_users = None5937 options.ldap_sync_groups = 'groups.txt'5938 sync_ldap(options)5939 request = urlopen_mock.call_args_list[0][0][0]5940 self.assertEquals('[{"Event": {"specs": [{"principal_type": "groups", "sync_type": "specific", "names": "group1, group2"}]}}]', request.data)5941 self.assertTrue(response.getcode.called)5942 self.assertTrue(response.read.called)5943 pass5944 @patch("urllib2.urlopen")5945 @patch("ambari_server.setupSecurity.get_validated_string_input")5946 @patch("ambari_server.setupSecurity.get_ambari_properties")5947 @patch("ambari_server.setupSecurity.is_server_runing")5948 @patch("ambari_server.setupSecurity.is_root")5949 def test_ldap_sync_ssl(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,5950 get_validated_string_input_mock, urlopen_mock):5951 is_root_method.return_value = True5952 is_server_runing_mock.return_value = (True, 0)5953 properties = Properties()5954 properties.process_pair(IS_LDAP_CONFIGURED, 'true')5955 properties.process_pair(SSL_API, 'true')5956 properties.process_pair(SSL_API_PORT, '8443')5957 get_ambari_properties_mock.return_value = properties5958 get_validated_string_input_mock.side_effect = ['admin', 'admin']5959 response = MagicMock()5960 response.getcode.side_effect = [201, 200, 200]5961 response.read.side_effect = ['{"resources" : [{"href" : "https://c6401.ambari.apache.org:8443/api/v1/ldap_sync_events/16","Event" : {"id" : 16}}]}',5962 '{"Event":{"status" : "RUNNING","summary" : {"groups" : {"created" : 0,"removed" : 0,"updated" : 0},"memberships" : {"created" : 0,"removed" : 0},"users" : {"created" : 0,"removed" : 0,"updated" : 0}}}}',5963 '{"Event":{"status" : "COMPLETE","summary" : {"groups" : {"created" : 1,"removed" : 0,"updated" : 0},"memberships" : {"created" : 5,"removed" : 0},"users" : {"created" : 5,"removed" : 0,"updated" : 0}}}}']5964 urlopen_mock.return_value = response5965 options = MagicMock()5966 options.ldap_sync_all = True5967 options.ldap_sync_existing = False5968 options.ldap_sync_users = None5969 options.ldap_sync_groups = None5970 sync_ldap(options)5971 url = '{0}://{1}:{2!s}{3}'.format('https', '127.0.0.1', '8443', '/api/v1/ldap_sync_events')5972 request = urlopen_mock.call_args_list[0][0][0]5973 self.assertEquals(url, str(request.get_full_url()))5974 self.assertTrue(response.getcode.called)5975 self.assertTrue(response.read.called)5976 pass5977 @patch("urllib2.urlopen")5978 @patch("ambari_server.setupSecurity.get_validated_string_input")5979 @patch("ambari_server.setupSecurity.get_ambari_properties")5980 @patch("ambari_server.setupSecurity.is_server_runing")5981 @patch("ambari_server.setupSecurity.is_root")5982 def test_ldap_sync_existing(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,5983 get_validated_string_input_mock, urlopen_mock):5984 is_root_method.return_value = True5985 is_server_runing_mock.return_value = (True, 0)5986 properties = Properties()5987 properties.process_pair(IS_LDAP_CONFIGURED, 'true')5988 get_ambari_properties_mock.return_value = properties5989 get_validated_string_input_mock.side_effect = ['admin', 'admin']5990 response = MagicMock()5991 response.getcode.side_effect = [201, 200, 200]5992 response.read.side_effect = ['{"resources" : [{"href" : "http://c6401.ambari.apache.org:8080/api/v1/ldap_sync_events/16","Event" : {"id" : 16}}]}',5993 '{"Event":{"status" : "RUNNING","summary" : {"groups" : {"created" : 0,"removed" : 0,"updated" : 0},"memberships" : {"created" : 0,"removed" : 0},"users" : {"created" : 0,"removed" : 0,"updated" : 0}}}}',5994 '{"Event":{"status" : "COMPLETE","summary" : {"groups" : {"created" : 1,"removed" : 0,"updated" : 0},"memberships" : {"created" : 5,"removed" : 0},"users" : {"created" : 5,"removed" : 0,"updated" : 0}}}}']5995 urlopen_mock.return_value = response5996 options = MagicMock()5997 options.ldap_sync_all = False5998 options.ldap_sync_existing = True5999 options.ldap_sync_users = None6000 options.ldap_sync_groups = None6001 sync_ldap(options)6002 self.assertTrue(response.getcode.called)6003 self.assertTrue(response.read.called)6004 pass6005 @patch("urllib2.urlopen")6006 @patch("ambari_server.setupSecurity.get_validated_string_input")6007 @patch("ambari_server.setupSecurity.get_ambari_properties")6008 @patch("ambari_server.setupSecurity.is_server_runing")6009 @patch("ambari_server.setupSecurity.is_root")6010 def test_ldap_sync_no_sync_mode(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,6011 get_validated_string_input_mock, urlopen_mock):6012 is_root_method.return_value = True6013 is_server_runing_mock.return_value = (True, 0)6014 properties = Properties()6015 properties.process_pair(IS_LDAP_CONFIGURED, 'true')6016 get_ambari_properties_mock.return_value = properties6017 get_validated_string_input_mock.side_effect = ['admin', 'admin']6018 response = MagicMock()6019 response.getcode.side_effect = [201, 200, 200]6020 response.read.side_effect = ['{"resources" : [{"href" : "http://c6401.ambari.apache.org:8080/api/v1/ldap_sync_events/16","Event" : {"id" : 16}}]}',6021 '{"Event":{"status" : "RUNNING","summary" : {"groups" : {"created" : 0,"removed" : 0,"updated" : 0},"memberships" : {"created" : 0,"removed" : 0},"users" : {"created" : 0,"removed" : 0,"updated" : 0}}}}',6022 '{"Event":{"status" : "COMPLETE","summary" : {"groups" : {"created" : 1,"removed" : 0,"updated" : 0},"memberships" : {"created" : 5,"removed" : 0},"users" : {"created" : 5,"removed" : 0,"updated" : 0}}}}']6023 urlopen_mock.return_value = response6024 options = MagicMock()6025 del options.ldap_sync_all6026 del options.ldap_sync_existing6027 del options.ldap_sync_users6028 del options.ldap_sync_groups6029 try:6030 sync_ldap(options)6031 self.fail("Should fail with exception")6032 except FatalException as e:6033 pass6034 pass6035 @patch("urllib2.urlopen")6036 @patch("ambari_server.setupSecurity.get_validated_string_input")6037 @patch("ambari_server.setupSecurity.get_ambari_properties")6038 @patch("ambari_server.setupSecurity.is_server_runing")6039 @patch("ambari_server.setupSecurity.is_root")6040 def test_ldap_sync_error_status(self, is_root_method, is_server_runing_mock, get_ambari_properties_mock,6041 get_validated_string_input_mock, urlopen_mock):6042 is_root_method.return_value = True6043 is_server_runing_mock.return_value = (True, 0)6044 properties = Properties()6045 properties.process_pair(IS_LDAP_CONFIGURED, 'true')6046 get_ambari_properties_mock.return_value = properties6047 get_validated_string_input_mock.side_effect = ['admin', 'admin']6048 response = MagicMock()6049 response.getcode.side_effect = [201, 200]6050 response.read.side_effect = ['{"resources" : [{"href" : "http://c6401.ambari.apache.org:8080/api/v1/ldap_sync_events/16","Event" : {"id" : 16}}]}',6051 '{"Event":{"status" : "ERROR","status_detail" : "Error!!","summary" : {"groups" : {"created" : 0,"removed" : 0,"updated" : 0},"memberships" : {"created" : 0,"removed" : 0},"users" : {"created" : 0,"removed" : 0,"updated" : 0}}}}']6052 urlopen_mock.return_value = response6053 options = MagicMock()6054 options.ldap_sync_all = False6055 options.ldap_sync_existing = False6056 options.ldap_sync_users = None6057 options.ldap_sync_groups = None6058 try:6059 sync_ldap(options)6060 self.fail("Should fail with exception")6061 except FatalException as e:6062 pass6063 pass6064 @patch("urllib2.urlopen")6065 @patch("urllib2.Request")6066 @patch("base64.encodestring")6067 @patch("ambari_server.setupSecurity.is_root")6068 @patch("ambari_server.setupSecurity.is_server_runing")6069 @patch("ambari_server.setupSecurity.get_ambari_properties")6070 @patch("ambari_server.setupSecurity.get_validated_string_input")6071 def test_sync_ldap_forbidden(self, get_validated_string_input_method, get_ambari_properties_method,6072 is_server_runing_method, is_root_method,6073 encodestring_method, request_constructor, urlopen_method):6074 options = MagicMock()6075 options.ldap_sync_all = True6076 options.ldap_sync_existing = False6077 options.ldap_sync_users = None6078 options.ldap_sync_groups = None6079 is_root_method.return_value = False6080 try:6081 sync_ldap(options)6082 self.fail("Should throw exception if not root")6083 except FatalException as fe:6084 # Expected6085 self.assertTrue("root-level" in fe.reason)6086 pass6087 is_root_method.return_value = True6088 is_server_runing_method.return_value = (None, None)6089 try:6090 sync_ldap(options)6091 self.fail("Should throw exception if ambari is stopped")6092 except FatalException as fe:6093 # Expected6094 self.assertTrue("not running" in fe.reason)6095 pass6096 is_server_runing_method.return_value = (True, None)6097 configs = MagicMock()6098 configs.get_property.return_value = None6099 get_ambari_properties_method.return_value = configs6100 try:6101 sync_ldap(options)6102 self.fail("Should throw exception if ldap is not configured")6103 except FatalException as fe:6104 # Expected6105 self.assertTrue("not configured" in fe.reason)6106 pass6107 configs.get_property.return_value = 'true'6108 get_validated_string_input_method.return_value = 'admin'6109 encodestring_method.return_value = 'qwe123'6110 requestMocks = [MagicMock()]6111 request_constructor.side_effect = requestMocks6112 response = MagicMock()6113 response.getcode.return_value = 4036114 urlopen_method.return_value = response6115 try:6116 sync_ldap(options)6117 self.fail("Should throw exception if return code != 200")6118 except FatalException as fe:6119 # Expected6120 self.assertTrue("status code" in fe.reason)6121 pass6122 pass6123 @patch("ambari_server.setupSecurity.is_root")6124 def test_sync_ldap_ambari_stopped(self, is_root_method):6125 is_root_method.return_value = False6126 options = MagicMock()6127 options.ldap_sync_all = True6128 options.ldap_sync_existing = False6129 options.ldap_sync_users = None6130 options.ldap_sync_groups = None6131 try:6132 sync_ldap(options)6133 self.fail("Should throw exception if not root")6134 except FatalException as fe:6135 # Expected6136 self.assertTrue("root-level" in fe.reason)6137 pass6138 pass6139 @patch("ambari_server.setupSecurity.is_root")6140 @patch("ambari_server.setupSecurity.is_server_runing")6141 def test_sync_ldap_ambari_stopped(self, is_server_runing_method, is_root_method):6142 is_root_method.return_value = True6143 is_server_runing_method.return_value = (None, None)6144 options = MagicMock()6145 options.ldap_sync_all = True6146 options.ldap_sync_existing = False6147 options.ldap_sync_users = None6148 options.ldap_sync_groups = None6149 try:6150 sync_ldap(options)6151 self.fail("Should throw exception if ambari is stopped")6152 except FatalException as fe:6153 # Expected6154 self.assertTrue("not running" in fe.reason)6155 pass6156 pass6157 @patch("ambari_server.setupSecurity.is_root")6158 @patch("ambari_server.setupSecurity.is_server_runing")6159 @patch("ambari_server.setupSecurity.get_ambari_properties")6160 def test_sync_ldap_not_configured(self, get_ambari_properties_method,6161 is_server_runing_method, is_root_method):6162 is_root_method.return_value = True6163 is_server_runing_method.return_value = (True, None)6164 configs = MagicMock()6165 configs.get_property.return_value = None6166 get_ambari_properties_method.return_value = configs6167 options = MagicMock()6168 options.ldap_sync_all = True6169 del options.ldap_sync_existing6170 del options.ldap_sync_users6171 del options.ldap_sync_groups6172 try:6173 sync_ldap(options)6174 self.fail("Should throw exception if ldap is not configured")6175 except FatalException as fe:6176 # Expected6177 self.assertTrue("not configured" in fe.reason)6178 pass6179 pass6180 @patch("__builtin__.open")6181 @patch("os.path.exists")6182 def test_get_ldap_event_spec_names(self, os_path_exists_mock, open_mock):6183 os_path_exists_mock.return_value = 16184 f = MagicMock()6185 f.__enter__().read.return_value = "\n\n\t some group, \tanother group, \n\t\tgrp, \ngroup*\n\n\n\n"6186 open_mock.return_value = f6187 bodies = [{"Event":{"specs":[]}}]6188 body = bodies[0]6189 events = body['Event']6190 specs = events['specs']6191 new_specs = [{"principal_type":"groups","sync_type":"specific","names":""}]6192 get_ldap_event_spec_names("groups.txt", specs, new_specs)6193 self.assertEquals("[{'Event': {'specs': [{'principal_type': 'groups', 'sync_type': 'specific', 'names': ' some group, another group, grp, group*'}]}}]", str(bodies))6194 pass6195 @patch("ambari_server.setupSecurity.read_password")6196 def test_configure_ldap_password(self, read_password_method):6197 out = StringIO.StringIO()6198 sys.stdout = out6199 read_password_method.return_value = "blah"6200 configure_ldap_password()6201 self.assertTrue(read_password_method.called)6202 sys.stdout = sys.__stdout__6203 pass6204 @patch("ambari_server.userInput.get_validated_string_input")6205 def test_read_password(self, get_validated_string_input_method):6206 out = StringIO.StringIO()6207 sys.stdout = out6208 passwordDefault = ""6209 passwordPrompt = 'Enter Manager Password* : '6210 passwordPattern = ".*"6211 passwordDescr = "Invalid characters in password."6212 get_validated_string_input_method.side_effect = ['', 'aaa', 'aaa']6213 password = read_password(passwordDefault, passwordPattern,6214 passwordPrompt, passwordDescr)6215 self.assertTrue(3, get_validated_string_input_method.call_count)6216 self.assertEquals('aaa', password)6217 get_validated_string_input_method.reset_mock()6218 get_validated_string_input_method.side_effect = ['aaa', 'aaa']6219 password = read_password(passwordDefault, passwordPattern,6220 passwordPrompt, passwordDescr)6221 self.assertTrue(2, get_validated_string_input_method.call_count)6222 self.assertEquals('aaa', password)6223 get_validated_string_input_method.reset_mock()6224 get_validated_string_input_method.side_effect = ['aaa']6225 password = read_password('aaa', passwordPattern,6226 passwordPrompt, passwordDescr)6227 self.assertTrue(1, get_validated_string_input_method.call_count)6228 self.assertEquals('aaa', password)6229 sys.stdout = sys.__stdout__6230 pass6231 def test_generate_random_string(self):6232 random_str_len = 1006233 str1 = generate_random_string(random_str_len)6234 self.assertTrue(len(str1) == random_str_len)6235 str2 = generate_random_string(random_str_len)6236 self.assertTrue(str1 != str2)6237 pass6238 @patch("__builtin__.open")6239 @patch("ambari_server.serverConfiguration.search_file")6240 @patch("ambari_server.serverConfiguration.backup_file_in_temp")6241 def test_update_properties_2(self, backup_file_in_temp_mock, search_file_mock, open_mock):6242 conf_file = "ambari.properties"6243 propertyMap = {"1": "1", "2": "2"}6244 properties = MagicMock()6245 f = MagicMock(name="file")6246 # f.__enter__.return_value = f #mimic file behavior6247 search_file_mock.return_value = conf_file6248 open_mock.return_value = f6249 update_properties_2(properties, propertyMap)6250 properties.store_ordered.assert_called_with(f.__enter__.return_value)6251 backup_file_in_temp_mock.assert_called_with(conf_file)6252 self.assertEquals(2, properties.removeOldProp.call_count)6253 self.assertEquals(2, properties.process_pair.call_count)6254 properties = MagicMock()6255 backup_file_in_temp_mock.reset_mock()6256 open_mock.reset_mock()6257 update_properties_2(properties, None)6258 properties.store_ordered.assert_called_with(f.__enter__.return_value)6259 backup_file_in_temp_mock.assert_called_with(conf_file)6260 self.assertFalse(properties.removeOldProp.called)6261 self.assertFalse(properties.process_pair.called)6262 pass6263 def test_regexps(self):6264 res = re.search(REGEX_HOSTNAME_PORT, "")6265 self.assertTrue(res is None)6266 res = re.search(REGEX_HOSTNAME_PORT, "ddd")6267 self.assertTrue(res is None)6268 res = re.search(REGEX_HOSTNAME_PORT, "gg:ff")6269 self.assertTrue(res is None)6270 res = re.search(REGEX_HOSTNAME_PORT, "gg:55444325")6271 self.assertTrue(res is None)6272 res = re.search(REGEX_HOSTNAME_PORT, "gg:555")6273 self.assertTrue(res is not None)6274 res = re.search(REGEX_TRUE_FALSE, "")6275 self.assertTrue(res is not None)6276 res = re.search(REGEX_TRUE_FALSE, "t")6277 self.assertTrue(res is None)6278 res = re.search(REGEX_TRUE_FALSE, "trrrr")6279 self.assertTrue(res is None)6280 res = re.search(REGEX_TRUE_FALSE, "true|false")6281 self.assertTrue(res is None)6282 res = re.search(REGEX_TRUE_FALSE, "true")6283 self.assertTrue(res is not None)6284 res = re.search(REGEX_TRUE_FALSE, "false")6285 self.assertTrue(res is not None)6286 res = re.search(REGEX_ANYTHING, "")6287 self.assertTrue(res is not None)6288 res = re.search(REGEX_ANYTHING, "t")6289 self.assertTrue(res is not None)6290 res = re.search(REGEX_ANYTHING, "trrrr")6291 self.assertTrue(res is not None)6292 pass6293 def get_sample(self, sample):6294 """6295 Returns sample file content as string with normalized line endings6296 """6297 path = self.get_samples_dir(sample)6298 return self.get_file_string(path)6299 def get_file_string(self, file):6300 """6301 Returns file content as string with normalized line endings6302 """6303 string = open(file, 'r').read()6304 return self.normalize(string)6305 def normalize(self, string):6306 """6307 Normalizes line ending in string according to platform-default encoding6308 """6309 return string.replace("\n", os.linesep)6310 def get_samples_dir(self, sample):6311 """6312 Returns full file path by sample name6313 """6314 testdir = os.path.dirname(__file__)6315 return os.path.dirname(testdir) + os.sep + "resources" + os.sep \6316 + 'TestAmbaryServer.samples/' + sample6317 @not_for_platform(PLATFORM_WINDOWS)6318 @patch("ambari_server.dbConfiguration_linux.get_ambari_properties")6319 def test_is_jdbc_user_changed(self, get_ambari_properties_mock):6320 previous_user = "previous_user"6321 new_user = "new_user"6322 props = Properties()6323 props.process_pair(JDBC_USER_NAME_PROPERTY, previous_user)6324 get_ambari_properties_mock.return_value = props6325 #check if users are different6326 result = PGConfig._is_jdbc_user_changed(new_user)6327 self.assertTrue(result)6328 #check if users are equal6329 result = PGConfig._is_jdbc_user_changed(previous_user)6330 self.assertFalse(result)6331 #check if one of users is None6332 result = PGConfig._is_jdbc_user_changed(None)6333 self.assertEqual(None, result)6334 pass6335 @not_for_platform(PLATFORM_WINDOWS)6336 @patch("ambari_server.serverConfiguration.write_property")6337 @patch("ambari_server.serverConfiguration.get_ambari_properties")6338 @patch("ambari_server.serverConfiguration.get_ambari_version")6339 def test_check_database_name_property(self, get_ambari_version_mock, get_ambari_properties_mock, write_property_mock):6340 parser = OptionParser()6341 parser.add_option('--database', default=None, help="Database to use embedded|oracle|mysql|mssql|postgres", dest="dbms")6342 args = parser.parse_args()6343 # negative case6344 get_ambari_properties_mock.return_value = {JDBC_DATABASE_NAME_PROPERTY: ""}6345 try:6346 result = check_database_name_property()6347 self.fail("Should fail with exception")6348 except FatalException as e:6349 self.assertTrue('DB Name property not set in config file.' in e.reason)6350 # positive case6351 dbname = "ambari"6352 get_ambari_properties_mock.reset_mock()6353 get_ambari_properties_mock.return_value = {JDBC_DATABASE_NAME_PROPERTY: dbname}6354 try:6355 result = check_database_name_property()6356 except FatalException:6357 self.fail("Setup should be successful")6358 # Check upgrade. In Ambari < 1.7.1 "database" property contained db name for local db6359 dbname = "ambari"6360 database = "ambari"6361 persistence = "local"6362 get_ambari_properties_mock.reset_mock()6363 get_ambari_properties_mock.return_value = {JDBC_DATABASE_NAME_PROPERTY: dbname,6364 JDBC_DATABASE_PROPERTY: database,6365 PERSISTENCE_TYPE_PROPERTY: persistence}6366 try:6367 result = check_database_name_property(upgrade=True)6368 except FatalException:6369 self.fail("Setup should be successful")6370 self.assertTrue(write_property_mock.called)6371 @not_for_platform(PLATFORM_WINDOWS)6372 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))6373 @patch("ambari_commons.firewall.run_os_command")6374 @patch("ambari_server.dbConfiguration_linux.PGConfig._is_jdbc_user_changed")6375 @patch("ambari_server.serverSetup.verify_setup_allowed")6376 @patch("ambari_server.serverSetup.get_YN_input")6377 @patch("ambari_server.serverSetup.configure_os_settings")6378 @patch("ambari_server.serverSetup.download_and_install_jdk")6379 @patch.object(PGConfig, "_configure_postgres")6380 @patch.object(PGConfig, "_check_postgre_up")6381 @patch("ambari_server.serverSetup.check_ambari_user")6382 @patch("ambari_server.serverSetup.check_jdbc_drivers")6383 @patch("ambari_server.serverSetup.check_selinux")6384 @patch("ambari_server.serverSetup.is_root")6385 @patch.object(PGConfig, "_setup_db")6386 @patch("ambari_server.serverSetup.get_is_secure")6387 @patch("ambari_server.dbConfiguration_linux.store_password_file")6388 @patch("ambari_server.serverSetup.extract_views")6389 @patch("ambari_server.serverSetup.adjust_directory_permissions")6390 @patch("sys.exit")6391 @patch("__builtin__.raw_input")6392 @patch("ambari_server.serverSetup.expand_jce_zip_file")6393 def test_ambariServerSetupWithCustomDbName(self, expand_jce_zip_file_mock, raw_input, exit_mock, adjust_dirs_mock,6394 extract_views_mock, store_password_file_mock,6395 get_is_secure_mock, setup_db_mock, is_root_mock, #is_local_database_mock,6396 check_selinux_mock, check_jdbc_drivers_mock, check_ambari_user_mock,6397 check_postgre_up_mock, configure_postgres_mock,6398 download_jdk_mock, configure_os_settings_mock, get_YN_input,6399 verify_setup_allowed_method, is_jdbc_user_changed_mock,6400 run_os_command_mock):6401 args = MagicMock()6402 raw_input.return_value = ""6403 get_YN_input.return_value = False6404 verify_setup_allowed_method.return_value = 06405 is_root_mock.return_value = True6406 check_selinux_mock.return_value = 06407 check_ambari_user_mock.return_value = (0, False, 'user', None)6408 check_jdbc_drivers_mock.return_value = 06409 check_postgre_up_mock.return_value = "running", 0, "", ""6410 #is_local_database_mock.return_value = True6411 configure_postgres_mock.return_value = 0, "", ""6412 download_jdk_mock.return_value = 06413 configure_os_settings_mock.return_value = 06414 is_jdbc_user_changed_mock.return_value = False6415 setup_db_mock.return_value = (0, None, None)6416 get_is_secure_mock.return_value = False6417 store_password_file_mock.return_value = "password"6418 extract_views_mock.return_value = 06419 run_os_command_mock.return_value = 3,"",""6420 new_db = "newDBName"6421 args.dbms = "postgres"6422 args.database_name = new_db6423 args.postgres_schema = new_db6424 args.database_username = "user"6425 args.database_password = "password"6426 args.jdbc_driver= None6427 args.jdbc_db = None6428 args.must_set_database_options = True6429 del args.database_index6430 del args.persistence_type6431 tempdir = tempfile.gettempdir()6432 prop_file = os.path.join(tempdir, "ambari.properties")6433 with open(prop_file, "w") as f:6434 f.write("server.jdbc.database_name=oldDBName")6435 f.close()6436 os.environ[AMBARI_CONF_VAR] = tempdir6437 try:6438 result = setup(args)6439 except FatalException as ex:6440 self.fail("Setup should be successful")6441 properties = get_ambari_properties()6442 self.assertTrue(JDBC_DATABASE_NAME_PROPERTY in properties.keys())6443 value = properties[JDBC_DATABASE_NAME_PROPERTY]6444 self.assertEqual(value, new_db)6445 del os.environ[AMBARI_CONF_VAR]6446 os.remove(prop_file)6447 pass6448 @only_for_platform(PLATFORM_WINDOWS)6449 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))6450 @patch("ambari_server.serverSetup.service_setup")6451 @patch("ambari_server.dbConfiguration_windows.MSSQLConfig._execute_db_script")6452 @patch("ambari_server.dbConfiguration_windows.store_password_file")6453 @patch("ambari_server.dbConfiguration_windows.MSSQLConfig._is_jdbc_driver_installed")6454 @patch("ambari_server.serverSetup.verify_setup_allowed")6455 @patch("ambari_server.serverSetup.get_YN_input")6456 @patch("ambari_server.serverSetup.configure_os_settings")6457 @patch("ambari_server.serverSetup.download_and_install_jdk")6458 @patch("ambari_server.serverSetup.check_firewall")6459 @patch("ambari_server.serverSetup.check_ambari_user")6460 @patch("ambari_server.serverSetup.check_jdbc_drivers")6461 @patch("ambari_server.serverSetup.is_root")6462 @patch("ambari_server.serverSetup.extract_views")6463 @patch("ambari_server.serverSetup.adjust_directory_permissions")6464 def test_ambariServerSetupWithCustomDbName(self,6465 adjust_dirs_mock,6466 extract_views_mock,6467 is_root_mock,6468 check_jdbc_drivers_mock,6469 check_ambari_user_mock,6470 check_firewall_mock,6471 download_jdk_mock,6472 configure_os_settings_mock,6473 get_YN_input,6474 verify_setup_allowed_method,6475 is_jdbc_driver_installed_mock,6476 store_password_file_mock,6477 execute_db_script_mock,6478 service_setup_mock):6479 args = MagicMock()6480 get_YN_input.return_value = False6481 verify_setup_allowed_method.return_value = 06482 is_root_mock.return_value = True6483 check_ambari_user_mock.return_value = (0, False, 'user', None)6484 check_jdbc_drivers_mock.return_value = 06485 download_jdk_mock.return_value = 06486 configure_os_settings_mock.return_value = 06487 is_jdbc_driver_installed_mock.return_value = True6488 store_password_file_mock.return_value = "password.dat"6489 extract_views_mock.return_value = 06490 new_db = "newDBName"6491 del args.dbms6492 del args.database_index6493 del args.database_host6494 del args.database_port6495 args.database_name = new_db6496 args.database_username = "user"6497 args.database_password = "password"6498 del args.database_windows_auth6499 args.jdbc_driver= None6500 args.jdbc_db = None6501 args.must_set_database_options = True6502 del args.default_database_host6503 del args.persistence_type6504 del args.init_db_script_file6505 del args.cleanup_db_script_file6506 tempdir = tempfile.gettempdir()6507 prop_file = os.path.join(tempdir, "ambari.properties")6508 with open(prop_file, "w") as f:6509 f.write("server.jdbc.database_name=oldDBName")6510 f.close()6511 os.environ[AMBARI_CONF_VAR] = tempdir6512 try:6513 result = setup(args)6514 except FatalException as ex:6515 self.fail("Setup should be successful")6516 properties = get_ambari_properties()6517 self.assertTrue(JDBC_DATABASE_NAME_PROPERTY in properties.keys())6518 value = properties[JDBC_DATABASE_NAME_PROPERTY]6519 self.assertEqual(value, new_db)6520 self.assertEqual(store_password_file_mock.call_count, 2)6521 self.assertEqual(execute_db_script_mock.call_count, 2)6522 del os.environ[AMBARI_CONF_VAR]6523 os.remove(prop_file)6524 pass6525 def test_is_valid_filepath(self):6526 temp_dir = tempfile.gettempdir()6527 temp_file = tempfile.NamedTemporaryFile(mode='r')6528 # Correct path to an existing file6529 self.assertTrue(temp_file)6530 # Correct path to an existing directory6531 self.assertFalse(is_valid_filepath(temp_dir), \6532 'is_valid_filepath(path) should return False is path is a directory')6533 # Incorrect path6534 self.assertFalse(is_valid_filepath(''))6535 pass6536 @patch("os.listdir")6537 @patch("os.path.exists")6538 @patch("ambari_server.serverUpgrade.load_stack_values")6539 @patch("ambari_server.serverUpgrade.get_ambari_properties")6540 @patch("ambari_server.serverUpgrade.run_metainfo_upgrade")6541 def test_upgrade_local_repo(self,6542 run_metainfo_upgrade_mock,6543 get_ambari_properties_mock,6544 load_stack_values_mock,6545 os_path_exists_mock,6546 os_listdir_mock):6547 from mock.mock import call6548 args = MagicMock()6549 args.persistence_type = "local"6550 def load_values_side_effect(*args, **kwargs):6551 res = {}6552 res['a'] = 'http://oldurl'6553 if -1 != args[1].find("HDPLocal"):6554 res['a'] = 'http://newurl'6555 return res6556 load_stack_values_mock.side_effect = load_values_side_effect6557 properties = Properties()6558 get_ambari_properties_mock.return_value = properties6559 os_path_exists_mock.return_value = 16560 os_listdir_mock.return_value = ['1.1']6561 upgrade_local_repo(args)6562 self.assertTrue(get_ambari_properties_mock.called)6563 self.assertTrue(load_stack_values_mock.called)6564 self.assertTrue(run_metainfo_upgrade_mock.called)6565 run_metainfo_upgrade_mock.assert_called_with({'a': 'http://newurl'})6566 pass6567 @patch("os.listdir")6568 @patch("os.path.exists")6569 @patch("ambari_server.serverUpgrade.load_stack_values")6570 @patch("ambari_server.serverUpgrade.get_ambari_properties")6571 @patch("ambari_server.serverUpgrade.run_metainfo_upgrade")6572 def test_upgrade_local_repo_nochange(self,6573 run_metainfo_upgrade_mock,6574 get_ambari_properties_mock,6575 load_stack_values_mock,6576 os_path_exists_mock,6577 os_listdir_mock):6578 from mock.mock import call6579 args = MagicMock()6580 args.persistence_type = "local"6581 def load_values_side_effect(*args, **kwargs):6582 res = {}6583 res['a'] = 'http://oldurl'6584 return res6585 load_stack_values_mock.side_effect = load_values_side_effect6586 properties = Properties()6587 get_ambari_properties_mock.return_value = properties6588 os_path_exists_mock.return_value = 16589 os_listdir_mock.return_value = ['1.1']6590 upgrade_local_repo(args)6591 self.assertTrue(get_ambari_properties_mock.called)6592 self.assertTrue(load_stack_values_mock.called)6593 self.assertTrue(run_metainfo_upgrade_mock.called)6594 run_metainfo_upgrade_mock.assert_called_with({})6595 pass6596 @patch("os.path.exists")6597 @patch.object(ResourceFilesKeeper, "perform_housekeeping")6598 def test_refresh_stack_hash(self,6599 perform_housekeeping_mock, path_exists_mock):6600 path_exists_mock.return_value = True6601 properties = Properties()6602 refresh_stack_hash(properties)6603 self.assertTrue(perform_housekeeping_mock.called)6604 pass6605 @not_for_platform(PLATFORM_WINDOWS)6606 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))6607 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))6608 @patch("ambari_server.dbConfiguration_linux.run_os_command")6609 @patch("ambari_server.dbConfiguration_linux.print_error_msg")6610 def test_change_objects_owner_both(self,6611 print_error_msg_mock,6612 run_os_command_mock):6613 args = MagicMock()6614 args.master_key = None6615 del args.database_index6616 del args.dbms6617 del args.database_host6618 del args.database_port6619 del args.database_name6620 del args.database_username6621 del args.database_password6622 del args.persistence_type6623 stdout = " stdout "6624 stderr = " stderr "6625 run_os_command_mock.return_value = 1, stdout, stderr6626 set_verbose(True)6627 self.assertRaises(FatalException, change_objects_owner, args)6628 print_error_msg_mock.assert_any_call("stderr:\nstderr")6629 print_error_msg_mock.assert_any_call("stdout:\nstdout")6630 pass6631 @not_for_platform(PLATFORM_WINDOWS)6632 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))6633 @patch("ambari_server.dbConfiguration_linux.run_os_command")6634 @patch("ambari_server.dbConfiguration_linux.print_error_msg")6635 def test_change_objects_owner_only_stdout(self,6636 print_error_msg_mock,6637 run_os_command_mock):6638 args = MagicMock()6639 del args.database_index6640 del args.dbms6641 del args.database_host6642 del args.database_port6643 del args.database_name6644 del args.database_username6645 del args.database_password6646 del args.persistence_type6647 stdout = " stdout "6648 stderr = ""6649 run_os_command_mock.return_value = 1, stdout, stderr6650 set_verbose(True)6651 self.assertRaises(FatalException, change_objects_owner, args)6652 print_error_msg_mock.assert_called_once_with("stdout:\nstdout")6653 pass6654 @not_for_platform(PLATFORM_WINDOWS)6655 @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value))6656 @patch("ambari_server.dbConfiguration_linux.run_os_command")6657 @patch("ambari_server.dbConfiguration_linux.print_error_msg")6658 def test_change_objects_owner_only_stderr(self,6659 print_error_msg_mock,6660 run_os_command_mock):6661 args = MagicMock()6662 del args.database_index6663 del args.dbms6664 del args.database_host6665 del args.database_port6666 del args.database_name6667 del args.database_username6668 del args.database_password6669 del args.persistence_type6670 stdout = ""6671 stderr = " stderr "6672 run_os_command_mock.return_value = 1, stdout, stderr6673 set_verbose(True)6674 self.assertRaises(FatalException, change_objects_owner, args)6675 print_error_msg_mock.assert_called_once_with("stderr:\nstderr")...
TestAmbaryServer.py
Source:TestAmbaryServer.py
1'''2Licensed to the Apache Software Foundation (ASF) under one3or more contributor license agreements. See the NOTICE file4distributed with this work for additional information5regarding copyright ownership. The ASF licenses this file6to you under the Apache License, Version 2.0 (the7"License"); you may not use this file except in compliance8with the License. You may obtain a copy of the License at9 http://www.apache.org/licenses/LICENSE-2.010Unless required by applicable law or agreed to in writing, software11distributed under the License is distributed on an "AS IS" BASIS,12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13See the License for the specific language governing permissions and14limitations under the License.15'''16import StringIO17import re18from unittest import TestCase19import sys20from mock.mock import patch21from mock.mock import MagicMock22from mock.mock import create_autospec23import os, errno, tempfile24import signal25import stat26import datetime27import operator28from pwd import getpwnam29# We have to use this import HACK because the filename contains a dash30ambari_server = __import__('ambari-server')31FatalException = ambari_server.FatalException32NonFatalException = ambari_server.NonFatalException33class TestAmbariServer(TestCase):34 def setUp(self):35 out = StringIO.StringIO()36 sys.stdout = out37 def tearDown(self):38 sys.stdout = sys.__stdout__39 @patch.object(ambari_server, 'configure_database_username_password')40 @patch.object(ambari_server, 'run_os_command')41 @patch('optparse.Values')42 def test_configure_pg_hba_ambaridb_users(self, OptParseValuesMock,43 run_os_command_method,44 configure_database_username_password_method):45 # Prepare mocks46 run_os_command_method.return_value = (0, "", "")47 opvm = OptParseValuesMock.return_value48 opvm.database_username = "ffdf"49 tf1 = tempfile.NamedTemporaryFile()50 ambari_server.PG_HBA_CONF_FILE = tf1.name51 # Run test52 ambari_server.configure_pg_hba_ambaridb_users()53 # Check results54 self.assertTrue(run_os_command_method.called)55 self.assertTrue(configure_database_username_password_method.called)56 string_expected = self.get_file_string(self57 .get_samples_dir("configure_pg_hba_ambaridb_users1"))58 string_actual = self.get_file_string(ambari_server.PG_HBA_CONF_FILE)59 self.assertEquals(string_expected, string_actual)60 pass61 @patch('__builtin__.raw_input')62 def test_servicename_regex(self, raw_input_method):63 ''' Test to make sure the service name can contain digits '''64 ambari_server.SILENT = False65 raw_input_method.return_value = "OT100"66 result = ambari_server.get_validated_service_name("ambari", 1)67 self.assertEqual("OT100", result, "Not accepting digits")68 pass69 70 @patch('__builtin__.raw_input')71 def test_dbname_regex(self, raw_input_method):72 ''' Test to make sure the service name can contain digits '''73 ambari_server.SILENT = False74 raw_input_method.return_value = "OT100"75 result = ambari_server.get_validated_db_name("ambari")76 self.assertEqual("OT100", result, "Not accepting digits")77 78 def test_configure_pg_hba_postgres_user(self):79 tf1 = tempfile.NamedTemporaryFile()80 ambari_server.PG_HBA_CONF_FILE = tf1.name81 with open(ambari_server.PG_HBA_CONF_FILE, 'w') as fout:82 fout.write("\n")83 fout.write("local all all md5\n")84 fout.write("host all all 0.0.0.0/0 md5\n")85 fout.write("host all all ::/0 md5\n")86 ambari_server.configure_pg_hba_postgres_user()87 expected = self.get_file_string(self.get_samples_dir(88 "configure_pg_hba_ambaridb_users2"))89 result = self.get_file_string(ambari_server.PG_HBA_CONF_FILE)90 self.assertEqual(expected, result, "pg_hba_conf not processed")91 mode = oct(os.stat(ambari_server.PG_HBA_CONF_FILE)[stat.ST_MODE])92 str_mode = str(mode)[-4:]93 self.assertEqual("0644", str_mode, "Wrong file permissions")94 @patch('__builtin__.raw_input')95 def get_choice_string_input(self, raw_input_method):96 prompt = "blablabla"97 default = "default blablabla"98 firstChoice = set(['yes','ye', 'y'])99 secondChoice = set(['no','n'])100 # test first input101 raw_input_method.return_value = "Y"102 result = ambari_server.get_choice_string_input(prompt, default,103 firstChoice, secondChoice)104 self.assertEquals(result, True)105 raw_input_method.reset_mock()106 # test second input107 raw_input_method.return_value = "N"108 result = ambari_server.get_choice_string_input(prompt, default,109 firstChoice, secondChoice)110 self.assertEquals(result, False)111 raw_input_method.reset_mock()112 # test enter pressed113 raw_input_method.return_value = ""114 result = ambari_server.get_choice_string_input(prompt, default,115 firstChoice, secondChoice)116 self.assertEquals(result, default)117 raw_input_method.reset_mock()118 # test wrong input119 list_of_return_values= ['yes', 'dsad', 'fdsfds']120 def side_effect(list):121 return list_of_return_values.pop()122 raw_input_method.side_effect = side_effect123 result = ambari_server.get_choice_string_input(prompt, default,124 firstChoice, secondChoice)125 self.assertEquals(result, True)126 self.assertEquals(raw_input_method.call_count, 3)127 pass128 @patch('re.search')129 @patch('__builtin__.raw_input')130 @patch('getpass.getpass')131 def get_validated_string_input(self, get_pass_method,132 raw_input_method, re_search_method):133 prompt = "blabla"134 default = "default_pass"135 pattern = "pattern_pp"136 description = "blabla2"137 # check password input138 self.assertFalse(False, ambari_server.SILENT)139 is_pass = True140 get_pass_method.return_value = "dfdsfdsfds"141 result = ambari_server.get_validated_string_input(prompt, default,142 pattern, description, is_pass)143 self.assertEquals(get_pass_method.return_value, result)144 get_pass_method.assure_called_once(prompt)145 self.assertFalse(raw_input_method.called)146 # check raw input147 get_pass_method.reset_mock()148 raw_input_method.reset_mock()149 is_pass = False150 raw_input_method.return_value = "dkf90ewuf0"151 result = ambari_server.get_validated_string_input(prompt, default,152 pattern, description, is_pass)153 self.assertEquals(raw_input_method.return_value, result)154 self.assertFalse(get_pass_method.called)155 raw_input_method.assure_called_once(prompt)156 def test_get_pass_file_path(self):157 result = ambari_server.get_pass_file_path("/etc/ambari/conf_file")158 self.assertEquals("/etc/ambari/password.dat", result)159 pass160 @patch.object(ambari_server, 'setup')161 @patch.object(ambari_server, 'start')162 @patch.object(ambari_server, 'stop')163 @patch.object(ambari_server, 'reset')164 @patch('optparse.OptionParser')165 def test_main_test_setup(self, OptionParserMock, reset_method, stop_method,166 start_method, setup_method):167 opm = OptionParserMock.return_value168 options = MagicMock()169 args = ["setup"]170 opm.parse_args.return_value = (options, args)171 options.database=None172 options.sid_or_sname = "sid"173 ambari_server.main()174 self.assertTrue(setup_method.called)175 self.assertFalse(start_method.called)176 self.assertFalse(stop_method.called)177 self.assertFalse(reset_method.called)178 self.assertFalse(False, ambari_server.VERBOSE)179 self.assertFalse(False, ambari_server.SILENT)180 @patch.object(ambari_server, 'setup')181 @patch.object(ambari_server, 'start')182 @patch.object(ambari_server, 'stop')183 @patch.object(ambari_server, 'reset')184 @patch('optparse.OptionParser')185 def test_main_test_start(self, OptionParserMock, reset_method, stop_method,186 start_method, setup_method):187 opm = OptionParserMock.return_value188 options = MagicMock()189 args = ["setup"]190 opm.parse_args.return_value = (options, args)191 options.database=None192 options.sid_or_sname = "sname"193 ambari_server.main()194 self.assertTrue(setup_method.called)195 self.assertFalse(start_method.called)196 self.assertFalse(stop_method.called)197 self.assertFalse(reset_method.called)198 self.assertFalse(False, ambari_server.VERBOSE)199 self.assertFalse(False, ambari_server.SILENT)200 @patch.object(ambari_server, 'setup')201 @patch.object(ambari_server, 'start')202 @patch.object(ambari_server, 'stop')203 @patch.object(ambari_server, 'reset')204 @patch('optparse.OptionParser')205 def test_main_test_start_debug_short(self, OptionParserMock, reset_method, stop_method,206 start_method, setup_method):207 opm = OptionParserMock.return_value208 options = MagicMock()209 args = ["start", "-g"]210 opm.parse_args.return_value = (options, args)211 options.database=None212 options.sid_or_sname = "sid"213 ambari_server.main()214 self.assertFalse(setup_method.called)215 self.assertTrue(start_method.called)216 self.assertFalse(stop_method.called)217 self.assertFalse(reset_method.called)218 self.assertTrue(ambari_server.SERVER_DEBUG_MODE) 219 @patch.object(ambari_server, 'setup')220 @patch.object(ambari_server, 'start')221 @patch.object(ambari_server, 'stop')222 @patch.object(ambari_server, 'reset')223 @patch('optparse.OptionParser')224 def test_main_test_start_debug_long(self, OptionParserMock, reset_method, stop_method,225 start_method, setup_method):226 opm = OptionParserMock.return_value227 options = MagicMock()228 args = ["start", "--debug"]229 opm.parse_args.return_value = (options, args)230 options.database=None231 options.sid_or_sname = "sid"232 ambari_server.main()233 self.assertFalse(setup_method.called)234 self.assertTrue(start_method.called)235 self.assertFalse(stop_method.called)236 self.assertFalse(reset_method.called)237 self.assertTrue(ambari_server.SERVER_DEBUG_MODE)238 @patch.object(ambari_server, 'setup')239 @patch.object(ambari_server, 'start')240 @patch.object(ambari_server, 'stop')241 @patch.object(ambari_server, 'reset')242 @patch('optparse.OptionParser')243 def test_main_test_stop(self, OptionParserMock, reset_method, stop_method,244 start_method, setup_method):245 opm = OptionParserMock.return_value246 options = MagicMock()247 args = ["stop"]248 opm.parse_args.return_value = (options, args)249 options.database = None250 options.sid_or_sname = "sid"251 ambari_server.main()252 self.assertFalse(setup_method.called)253 self.assertFalse(start_method.called)254 self.assertTrue(stop_method.called)255 self.assertFalse(reset_method.called)256 self.assertFalse(False, ambari_server.VERBOSE)257 self.assertFalse(False, ambari_server.SILENT)258 @patch.object(ambari_server, 'setup')259 @patch.object(ambari_server, 'start')260 @patch.object(ambari_server, 'stop')261 @patch.object(ambari_server, 'reset')262 @patch('optparse.OptionParser')263 def test_main_test_reset(self, OptionParserMock, reset_method, stop_method,264 start_method, setup_method):265 opm = OptionParserMock.return_value266 options = MagicMock()267 args = ["reset"]268 opm.parse_args.return_value = (options, args)269 options.database=None270 options.sid_or_sname = "sid"271 ambari_server.main()272 self.assertFalse(setup_method.called)273 self.assertFalse(start_method.called)274 self.assertFalse(stop_method.called)275 self.assertTrue(reset_method.called)276 self.assertFalse(False, ambari_server.VERBOSE)277 self.assertFalse(False, ambari_server.SILENT)278 def test_configure_postgresql_conf(self):279 tf1 = tempfile.NamedTemporaryFile()280 ambari_server.POSTGRESQL_CONF_FILE = tf1.name281 with open(ambari_server.POSTGRESQL_CONF_FILE, 'w') as f:282 f.write("#listen_addresses = '127.0.0.1' #\n")283 f.write("#listen_addresses = '127.0.0.1'")284 ambari_server.configure_postgresql_conf()285 expected = self.get_file_string(self.get_samples_dir(286 "configure_postgresql_conf1"))287 result = self.get_file_string(ambari_server.POSTGRESQL_CONF_FILE)288 self.assertEqual(expected, result, "postgresql.conf not updated")289 mode = oct(os.stat(ambari_server.POSTGRESQL_CONF_FILE)[stat.ST_MODE])290 str_mode = str(mode)[-4:]291 self.assertEqual("0644", str_mode, "Wrong file permissions")292 @patch.object(ambari_server, "restart_postgres")293 @patch.object(ambari_server, "get_postgre_status")294 @patch.object(ambari_server, "configure_postgresql_conf")295 @patch.object(ambari_server, "configure_pg_hba_ambaridb_users")296 @patch.object(ambari_server, "configure_pg_hba_postgres_user")297 def test_configure_postgres(self, configure_pg_hba_postgres_user_mock,298 configure_pg_hba_ambaridb_users_mock,299 configure_postgresql_conf_mock,300 get_postgre_status_mock,301 restart_postgres_mock):302 tf1 = tempfile.NamedTemporaryFile()303 tf2 = tempfile.NamedTemporaryFile()304 ambari_server.PG_HBA_CONF_FILE = tf1.name305 ambari_server.PG_HBA_CONF_FILE_BACKUP = tf2.name306 args = MagicMock()307 out = StringIO.StringIO()308 sys.stdout = out309 rcode = ambari_server.configure_postgres()310 sys.stdout = sys.__stdout__311 self.assertEqual(0, rcode)312 self.assertEqual("Backup for pg_hba found, reconfiguration not required\n",313 out.getvalue())314 ambari_server.PG_HBA_CONF_FILE_BACKUP = tempfile.mktemp()315 get_postgre_status_mock.return_value = ambari_server.PG_STATUS_RUNNING316 restart_postgres_mock.return_value = 0317 rcode = ambari_server.configure_postgres()318 self.assertTrue(os.path.isfile(ambari_server.PG_HBA_CONF_FILE_BACKUP),319 "postgresql.conf backup not created")320 self.assertTrue(configure_pg_hba_postgres_user_mock.called)321 self.assertTrue(configure_pg_hba_ambaridb_users_mock.called)322 mode = oct(os.stat(ambari_server.PG_HBA_CONF_FILE)[stat.ST_MODE])323 str_mode = str(mode)[-4:]324 self.assertEqual("0644", str_mode, "Wrong file permissions")325 self.assertTrue(configure_postgresql_conf_mock.called)326 self.assertEqual(0, rcode)327 os.unlink(ambari_server.PG_HBA_CONF_FILE_BACKUP)328 get_postgre_status_mock.return_value = "stopped"329 rcode = ambari_server.configure_postgres()330 self.assertEqual(0, rcode)331 os.unlink(ambari_server.PG_HBA_CONF_FILE_BACKUP)332 sys.stdout = sys.__stdout__333 @patch("time.sleep")334 @patch("subprocess.Popen")335 @patch.object(ambari_server, "run_os_command")336 @patch.object(ambari_server, "get_postgre_status")337 @patch.object(ambari_server, "print_info_msg")338 def test_restart_postgres(self, printInfoMsg_mock, get_postgre_status_mock,339 run_os_command_mock, popenMock, sleepMock):340 p = MagicMock()341 p.poll.return_value = 0342 popenMock.return_value = p343 rcode = ambari_server.restart_postgres()344 self.assertEqual(0, rcode)345 p.poll.return_value = None346 get_postgre_status_mock.return_value = "stopped"347 run_os_command_mock.return_value = (1, None, None)348 rcode = ambari_server.restart_postgres()349 self.assertEqual(1, rcode)350 @patch("shlex.split")351 @patch("subprocess.Popen")352 @patch.object(ambari_server, "print_info_msg")353 def test_run_os_command(self, printInfoMsg_mock, popenMock, splitMock):354 p = MagicMock()355 p.communicate.return_value = (None, None)356 p.returncode = 3357 popenMock.return_value = p358 # with list arg359 cmd = ["exec", "arg"]360 ambari_server.run_os_command(cmd)361 self.assertFalse(splitMock.called)362 # with str arg363 resp = ambari_server.run_os_command("runme")364 self.assertEqual(3, resp[0])365 self.assertTrue(splitMock.called)366 @patch.object(ambari_server, "get_conf_dir")367 @patch.object(ambari_server, "search_file")368 def test_write_property(self, search_file_mock, get_conf_dir_mock):369 expected_content = "key1=val1\n"370 tf1 = tempfile.NamedTemporaryFile()371 search_file_mock.return_value = tf1.name372 ambari_server.write_property("key1", "val1")373 result = tf1.read()374 self.assertTrue(expected_content in result)375 @patch.object(ambari_server, "configure_database_username_password")376 @patch.object(ambari_server, "run_os_command")377 def test_setup_db(self, run_os_command_mock,378 configure_database_username_password_mock):379 run_os_command_mock.return_value = (0, None, None)380 result = ambari_server.setup_db(MagicMock())381 self.assertTrue(configure_database_username_password_mock.called)382 self.assertEqual(0, result)383 @patch.object(ambari_server, "get_YN_input")384 @patch.object(ambari_server, "run_os_command")385 def test_check_selinux(self, run_os_command_mock, getYNInput_mock):386 run_os_command_mock.return_value = (0, ambari_server.SE_STATUS_DISABLED,387 None)388 rcode = ambari_server.check_selinux()389 self.assertEqual(0, rcode)390 getYNInput_mock.return_value = True391 run_os_command_mock.return_value = (0,"enabled "392 + ambari_server.SE_MODE_ENFORCING,393 None)394 rcode = ambari_server.check_selinux()395 self.assertEqual(0, rcode)396 self.assertTrue(run_os_command_mock.called)397 self.assertTrue(getYNInput_mock.called)398 @patch.object(ambari_server, "print_info_msg")399 def test_get_ambari_jars(self, printInfoMsg_mock):400 env = "/ambari/jars"401 os.environ[ambari_server.AMBARI_SERVER_LIB] = env402 result = ambari_server.get_ambari_jars()403 self.assertEqual(env, result)404 del os.environ[ambari_server.AMBARI_SERVER_LIB]405 result = ambari_server.get_ambari_jars()406 self.assertEqual("/usr/lib/ambari-server", result)407 self.assertTrue(printInfoMsg_mock.called)408 @patch("glob.glob")409 @patch.object(ambari_server, "print_info_msg")410 def test_get_share_jars(self, printInfoMsg_mock, globMock):411 globMock.return_value = ["one", "two"]412 expected = "one:two:one:two"413 result = ambari_server.get_share_jars()414 self.assertEqual(expected, result)415 globMock.return_value = []416 expected = ""417 result = ambari_server.get_share_jars()418 self.assertEqual(expected, result)419 @patch("glob.glob")420 @patch.object(ambari_server, "print_info_msg")421 def test_get_ambari_classpath(self, printInfoMsg_mock, globMock):422 globMock.return_value = ["one"]423 result = ambari_server.get_ambari_classpath()424 self.assertTrue(ambari_server.get_ambari_jars() in result)425 self.assertTrue(ambari_server.get_share_jars() in result)426 globMock.return_value = []427 result = ambari_server.get_ambari_classpath()428 self.assertTrue(ambari_server.get_ambari_jars() in result)429 self.assertFalse(":" in result)430 @patch.object(ambari_server, "print_info_msg")431 def test_get_conf_dir(self, printInfoMsg_mock):432 env = "/dummy/ambari/conf"433 os.environ[ambari_server.AMBARI_CONF_VAR] = env434 result = ambari_server.get_conf_dir()435 self.assertEqual(env, result)436 del os.environ[ambari_server.AMBARI_CONF_VAR]437 result = ambari_server.get_conf_dir()438 self.assertEqual("/etc/ambari-server/conf", result)439 def test_search_file(self):440 path = os.path.dirname(__file__)441 result = ambari_server.search_file(__file__, path)442 expected = os.path.abspath(__file__)443 self.assertEqual(expected, result)444 result = ambari_server.search_file("non_existent_file", path)445 self.assertEqual(None, result)446 @patch.object(ambari_server, "search_file")447 def test_find_properties_file(self, search_file_mock):448 # Testing case when file is not found449 search_file_mock.return_value = None450 try:451 ambari_server.find_properties_file()452 self.fail("File not found'")453 except FatalException:454 # Expected455 pass456 self.assertTrue(search_file_mock.called)457 # Testing case when file is found458 value = MagicMock()459 search_file_mock.return_value = value460 result = ambari_server.find_properties_file()461 self.assertTrue(result is value)462 @patch.object(ambari_server, "find_properties_file")463 @patch("__builtin__.open")464 @patch("ambari-server.Properties")465 def test_read_ambari_user(self, properties_mock, open_mock, find_properties_file_mock):466 open_mock.return_value = "dummy"467 find_properties_file_mock.return_value = "dummy"468 # Testing with defined user469 properties_mock.return_value.__getitem__.return_value = "dummy_user"470 user = ambari_server.read_ambari_user()471 self.assertEquals(user, "dummy_user")472 # Testing with undefined user473 properties_mock.return_value.__getitem__.return_value = None474 user = ambari_server.read_ambari_user()475 self.assertEquals(user, None)476 @patch("os.path.exists")477 @patch.object(ambari_server, "set_file_permissions")478 @patch.object(ambari_server, "run_os_command")479 @patch.object(ambari_server, "get_ambari_properties")480 @patch.object(ambari_server, "get_value_from_properties")481 @patch("os.mkdir")482 def test_adjust_directory_permissions(self, mkdir_mock,483 get_value_from_properties_mock, get_ambari_properties_mock,484 run_os_command_mock, set_file_permissions_mock, exists_mock):485 # Testing boostrap dir wipe486 properties_mock = MagicMock()487 get_value_from_properties_mock.return_value = "dummy_bootstrap_dir"488 ambari_server.adjust_directory_permissions("user")489 exists_mock.return_value = False490 self.assertEquals(run_os_command_mock.call_args_list[0][0][0], "rm -rf dummy_bootstrap_dir")491 self.assertTrue(mkdir_mock.called)492 set_file_permissions_mock.reset_mock()493 # Test recursive calls494 old_list = ambari_server.NR_ADJUST_OWNERSHIP_LIST495 ambari_server.NR_ADJUST_OWNERSHIP_LIST = [496 ( "/etc/ambari-server/conf", "755", "{0}", True ),497 ( "/etc/ambari-server/conf/ambari.properties", "644", "{0}", False )498 ]499 ambari_server.adjust_directory_permissions("user")500 self.assertTrue(len(set_file_permissions_mock.call_args_list) ==501 len(ambari_server.NR_ADJUST_OWNERSHIP_LIST))502 self.assertEquals(set_file_permissions_mock.call_args_list[0][0][3], True)503 self.assertEquals(set_file_permissions_mock.call_args_list[1][0][3], False)504 ambari_server.NR_ADJUST_OWNERSHIP_LIST = old_list505 @patch("os.path.exists")506 @patch.object(ambari_server, "run_os_command")507 @patch.object(ambari_server, "print_warning_msg")508 @patch.object(ambari_server, "print_info_msg")509 def test_set_file_permissions(self, print_info_msg_mock, print_warning_msg_mock,510 run_os_command_mock, exists_mock):511 # Testing not existent file scenario512 exists_mock.return_value = False513 ambari_server.set_file_permissions("dummy-file", "dummy-mod",514 "dummy-user", False)515 self.assertFalse(run_os_command_mock.called)516 self.assertTrue(print_info_msg_mock.called)517 run_os_command_mock.reset_mock()518 print_warning_msg_mock.reset_mock()519 # Testing OK scenario520 exists_mock.return_value = True521 run_os_command_mock.side_effect = [(0, "", ""), (0, "", "")]522 ambari_server.set_file_permissions("dummy-file", "dummy-mod",523 "dummy-user", False)524 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)525 self.assertFalse(print_warning_msg_mock.called)526 run_os_command_mock.reset_mock()527 print_warning_msg_mock.reset_mock()528 # Testing first command fail529 run_os_command_mock.side_effect = [(1, "", ""), (0, "", "")]530 ambari_server.set_file_permissions("dummy-file", "dummy-mod",531 "dummy-user", False)532 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)533 self.assertTrue(print_warning_msg_mock.called)534 run_os_command_mock.reset_mock()535 print_warning_msg_mock.reset_mock()536 # Testing second command fail537 run_os_command_mock.side_effect = [(0, "", ""), (1, "", "")]538 ambari_server.set_file_permissions("dummy-file", "dummy-mod",539 "dummy-user", False)540 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)541 self.assertTrue(print_warning_msg_mock.called)542 run_os_command_mock.reset_mock()543 print_warning_msg_mock.reset_mock()544 # Testing recursive operation545 exists_mock.return_value = True546 run_os_command_mock.side_effect = [(0, "", ""), (0, "", "")]547 ambari_server.set_file_permissions("dummy-file", "dummy-mod",548 "dummy-user", True)549 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)550 self.assertTrue("-R" in run_os_command_mock.call_args_list[0][0][0])551 self.assertTrue("-R" in run_os_command_mock.call_args_list[1][0][0])552 self.assertFalse(print_warning_msg_mock.called)553 run_os_command_mock.reset_mock()554 print_warning_msg_mock.reset_mock()555 # Testing non-recursive operation556 exists_mock.return_value = True557 run_os_command_mock.side_effect = [(0, "", ""), (0, "", "")]558 ambari_server.set_file_permissions("dummy-file", "dummy-mod",559 "dummy-user", False)560 self.assertTrue(len(run_os_command_mock.call_args_list) == 2)561 self.assertFalse("-R" in run_os_command_mock.call_args_list[0][0][0])562 self.assertFalse("-R" in run_os_command_mock.call_args_list[1][0][0])563 self.assertFalse(print_warning_msg_mock.called)564 run_os_command_mock.reset_mock()565 print_warning_msg_mock.reset_mock()566 @patch.object(ambari_server, "get_validated_string_input")567 @patch.object(ambari_server, "print_info_msg")568 @patch.object(ambari_server, "print_warning_msg")569 @patch.object(ambari_server, "run_os_command")570 def test_create_custom_user(self, run_os_command_mock, print_warning_msg_mock,571 print_info_msg_mock, get_validated_string_input_mock):572 user = "dummy-user"573 get_validated_string_input_mock.return_value = user574 # Testing scenario: absent user575 run_os_command_mock.side_effect = [(0, "", "")]576 result = ambari_server.create_custom_user()577 self.assertFalse(print_warning_msg_mock.called)578 self.assertEquals(result, (0, user))579 print_info_msg_mock.reset_mock()580 print_warning_msg_mock.reset_mock()581 run_os_command_mock.reset_mock()582 # Testing scenario: existing user583 run_os_command_mock.side_effect = [(9, "", "")]584 result = ambari_server.create_custom_user()585 self.assertTrue("User dummy-user already exists" in str(print_info_msg_mock.call_args_list[1][0]))586 self.assertEquals(result, (0, user))587 print_info_msg_mock.reset_mock()588 print_warning_msg_mock.reset_mock()589 run_os_command_mock.reset_mock()590 # Testing scenario: os command fail591 run_os_command_mock.side_effect = [(1, "", "")]592 result = ambari_server.create_custom_user()593 self.assertTrue(print_warning_msg_mock.called)594 self.assertEquals(result, (1, None))595 @patch.object(ambari_server, "read_ambari_user")596 @patch.object(ambari_server, "get_YN_input")597 @patch.object(ambari_server, "create_custom_user")598 @patch.object(ambari_server, "write_property")599 @patch.object(ambari_server, "adjust_directory_permissions")600 @patch.object(ambari_server, "print_error_msg")601 def test_check_ambari_user(self, print_error_msg_mock,602 adjust_directory_permissions_mock, write_property_mock,603 create_custom_user_mock, get_YN_input_mock, read_ambari_user_mock):604 # Scenario: user is already defined, user does not want to reconfigure it605 read_ambari_user_mock.return_value = "dummy-user"606 get_YN_input_mock.return_value = False607 result = ambari_server.check_ambari_user()608 self.assertTrue(get_YN_input_mock.called)609 self.assertFalse(write_property_mock.called)610 self.assertFalse(create_custom_user_mock.called)611 self.assertTrue(adjust_directory_permissions_mock.called)612 self.assertEqual(result, 0)613 get_YN_input_mock.reset_mock()614 write_property_mock.reset_mock()615 adjust_directory_permissions_mock.reset_mock()616 create_custom_user_mock.reset_mock()617 # Scenario: user is already defined, but user wants to reconfigure it618 read_ambari_user_mock.return_value = "dummy-user"619 create_custom_user_mock.return_value = (0, "new-dummy-user")620 get_YN_input_mock.return_value = True621 result = ambari_server.check_ambari_user()622 self.assertTrue(get_YN_input_mock.called)623 self.assertTrue(write_property_mock.called)624 self.assertTrue(write_property_mock.call_args[0][1] == "new-dummy-user")625 self.assertTrue(create_custom_user_mock.called)626 self.assertTrue(adjust_directory_permissions_mock.called)627 self.assertEqual(result, 0)628 get_YN_input_mock.reset_mock()629 write_property_mock.reset_mock()630 adjust_directory_permissions_mock.reset_mock()631 create_custom_user_mock.reset_mock()632 # Negative scenario: user is already defined, but user wants633 # to reconfigure it, user creation failed634 read_ambari_user_mock.return_value = "dummy-user"635 create_custom_user_mock.return_value = (1, None)636 get_YN_input_mock.return_value = True637 result = ambari_server.check_ambari_user()638 self.assertTrue(get_YN_input_mock.called)639 self.assertTrue(create_custom_user_mock.called)640 self.assertFalse(write_property_mock.called)641 self.assertFalse(adjust_directory_permissions_mock.called)642 self.assertEqual(result, 1)643 get_YN_input_mock.reset_mock()644 create_custom_user_mock.reset_mock()645 write_property_mock.reset_mock()646 adjust_directory_permissions_mock.reset_mock()647 # Scenario: user is not defined (setup process)648 read_ambari_user_mock.return_value = None649 get_YN_input_mock.return_value = True650 create_custom_user_mock.return_value = (0, "dummy-user")651 result = ambari_server.check_ambari_user()652 self.assertTrue(get_YN_input_mock.called)653 self.assertTrue(create_custom_user_mock.called)654 self.assertTrue(write_property_mock.called)655 self.assertTrue(write_property_mock.call_args[0][1] == "dummy-user")656 self.assertTrue(adjust_directory_permissions_mock.called)657 self.assertEqual(result, 0)658 get_YN_input_mock.reset_mock()659 create_custom_user_mock.reset_mock()660 write_property_mock.reset_mock()661 adjust_directory_permissions_mock.reset_mock()662 # Scenario: user is not defined (setup process), user creation failed663 read_ambari_user_mock.return_value = None664 get_YN_input_mock.return_value = True665 create_custom_user_mock.return_value = (1, None)666 result = ambari_server.check_ambari_user()667 self.assertTrue(get_YN_input_mock.called)668 self.assertTrue(create_custom_user_mock.called)669 self.assertFalse(write_property_mock.called)670 self.assertFalse(adjust_directory_permissions_mock.called)671 self.assertEqual(result, 1)672 get_YN_input_mock.reset_mock()673 create_custom_user_mock.reset_mock()674 write_property_mock.reset_mock()675 adjust_directory_permissions_mock.reset_mock()676 # negative scenario: user is not defined (setup process), user creation failed677 read_ambari_user_mock.return_value = None678 get_YN_input_mock.return_value = True679 create_custom_user_mock.return_value = (1, None)680 result = ambari_server.check_ambari_user()681 self.assertTrue(get_YN_input_mock.called)682 self.assertTrue(create_custom_user_mock.called)683 self.assertFalse(write_property_mock.called)684 self.assertFalse(adjust_directory_permissions_mock.called)685 self.assertEqual(result, 1)686 get_YN_input_mock.reset_mock()687 create_custom_user_mock.reset_mock()688 write_property_mock.reset_mock()689 adjust_directory_permissions_mock.reset_mock()690 # Scenario: user is not defined and left to be root691 read_ambari_user_mock.return_value = None692 get_YN_input_mock.return_value = False693 result = ambari_server.check_ambari_user()694 self.assertTrue(get_YN_input_mock.called)695 self.assertFalse(create_custom_user_mock.called)696 self.assertTrue(write_property_mock.called)697 self.assertTrue(write_property_mock.call_args[0][1] == "root")698 self.assertTrue(adjust_directory_permissions_mock.called)699 self.assertEqual(result, 0)700 @patch.object(ambari_server, "search_file")701 @patch("__builtin__.open")702 @patch.object(ambari_server, "read_ambari_user")703 @patch.object(ambari_server, "set_file_permissions")704 def test_store_password_file(self, set_file_permissions_mock,705 read_ambari_user_mock, open_mock, search_file_mock):706 search_file_mock.return_value = "/etc/ambari-server/conf/ambari.properties"707 open_mock.return_value = MagicMock()708 ambari_server.store_password_file("password", "passfile")709 self.assertTrue(set_file_permissions_mock.called)710 @patch.object(ambari_server, "run_os_command")711 def test_check_iptables(self, run_os_command_mock):712 run_os_command_mock.return_value = (1, "test", "")713 rcode, info = ambari_server.check_iptables()714 self.assertEqual(1, rcode)715 self.assertEqual("test", info)716 run_os_command_mock.return_value = (2, "",717 ambari_server.IP_TBLS_SRVC_NT_FND)718 rcode = ambari_server.check_iptables()719 self.assertEqual(0, rcode)720 def test_dlprogress(self):721 out = StringIO.StringIO()722 sys.stdout = out723 ambari_server.dlprogress("filename", 10, 2, 100)724 sys.stdout = sys.__stdout__725 self.assertNotEqual("", out.getvalue())726 @patch("urllib2.urlopen")727 @patch("__builtin__.open")728 @patch.object(ambari_server, "dlprogress")729 def test_track_jdk(self, dlprogress_mock, openMock, urlopenMock):730 u = MagicMock()731 u.info.return_value = {"Content-Length":"24576"}732 chunks = [None, "second", "first"]733 def side_effect(*args, **kwargs):734 return chunks.pop()735 u.read.side_effect = side_effect736 urlopenMock.return_value = u737 f = MagicMock()738 openMock.return_value = f739 ambari_server.track_jdk("base", "url", "local")740 self.assertEqual(0, len(chunks))741 self.assertTrue(f.write.called)742 self.assertTrue(f.flush.called)743 self.assertTrue(f.close.called)744 self.assertEqual(2, len(dlprogress_mock.call_args_list))745 @patch("shutil.copy")746 @patch("os.path.join")747 @patch("os.path.exists")748 @patch.object(ambari_server, "get_ambari_properties")749 def test_install_jce_manualy(self, get_ambari_properties_mock,\750 os_path_exists_mock, os_path_join_mock,\751 shutil_copy_mock):752 args = MagicMock()753 args.jce_policy = "somewhere"754 p = MagicMock()755 get_ambari_properties_mock.return_value = p756 p.__getitem__.side_effect = None757 p.__getitem__.return_value = "somewhere"758 os_path_exists_mock.return_value = True759 os_path_join_mock.return_value = \760 "/var/lib/ambari-server/resources/jce_policy-6.zip" 761 ambari_server.install_jce_manualy(args) 762 self.assertTrue(shutil_copy_mock.called)763 shutil_copy_mock.side_effect = Exception("exception")764 try:765 ambari_server.install_jce_manualy(args)766 self.fail("Should throw exception because of not found jce_policy-6.zip")767 except Exception:768 # Expected769 self.assertTrue(shutil_copy_mock.called)770 pass 771 shutil_copy_mock.side_effect = None772 args.jce_policy = None773 ambari_server.install_jce_manualy(args)774 @patch.object(ambari_server, 'read_ambari_user')775 @patch.object(ambari_server, "get_validated_string_input")776 @patch.object(ambari_server, "find_properties_file")777 @patch.object(ambari_server, "get_ambari_properties")778 @patch.object(ambari_server, "is_server_runing")779 @patch.object(ambari_server, "import_cert_and_key_action") 780 @patch.object(ambari_server, "get_YN_input") 781 @patch("__builtin__.open")782 @patch("ambari-server.Properties")783 @patch.object(ambari_server, "is_root")784 @patch.object(ambari_server, "is_valid_cert_host") 785 @patch.object(ambari_server, "is_valid_cert_exp") 786 def test_setup_https(self, is_valid_cert_exp_mock, is_valid_cert_host_mock,\787 is_root_mock, Properties_mock, open_Mock, get_YN_input_mock,\788 import_cert_and_key_action_mock,789 is_server_runing_mock, get_ambari_properties_mock,\790 find_properties_file_mock,\791 get_validated_string_input_mock,792 read_ambari_user_method):793 794 is_valid_cert_exp_mock.return_value=True795 is_valid_cert_host_mock.return_value=True796 args = MagicMock()797 open_Mock.return_value = file798 p = get_ambari_properties_mock.return_value799 # Testing call under non-root800 is_root_mock.return_value = False801 try:802 ambari_server.setup_https(args)803 self.fail("Should throw exception")804 except FatalException as fe:805 # Expected806 self.assertTrue("root-level" in fe.reason)807 pass808 # Testing call under root809 is_root_mock.return_value = True810 read_ambari_user_method.return_value = None811 #Case #1: if client ssl is on and user didnt choose 812 #disable ssl option and choose import certs and keys813 p.get_property.side_effect = ["key_dir","5555","6666", "true"]814 get_YN_input_mock.side_effect = [False,True]815 get_validated_string_input_mock.side_effect = ["4444"]816 get_property_expected = "[call('security.server.keys_dir'),\n"+\817 " call('client.api.ssl.port'),\n"+\818 " call('client.api.ssl.port'),\n call('api.ssl')]"819 process_pair_expected = "[call('client.api.ssl.port', '4444')]"820 ambari_server.SILENT = False821 ambari_server.setup_https(args)822 823 self.assertTrue(p.process_pair.called)824 self.assertTrue(p.get_property.call_count == 4)825 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)826 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)827 self.assertTrue(p.store.called)828 self.assertTrue(import_cert_and_key_action_mock.called)829 p.process_pair.reset_mock()830 p.get_property.reset_mock()831 p.store.reset_mock()832 import_cert_and_key_action_mock.reset_mock()833 #Case #2: if client ssl is on and user choose to disable ssl option834 p.get_property.side_effect = ["key_dir","", "true"]835 get_YN_input_mock.side_effect = [True]836 get_validated_string_input_mock.side_effect = ["4444"]837 get_property_expected = "[call('security.server.keys_dir'),\n"+\838 " call('client.api.ssl.port'),\n call('api.ssl')]"839 process_pair_expected = "[call('api.ssl', 'false')]"840 ambari_server.setup_https(args)841 842 self.assertTrue(p.process_pair.called)843 self.assertTrue(p.get_property.call_count == 3)844 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)845 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)846 self.assertTrue(p.store.called)847 self.assertFalse(import_cert_and_key_action_mock.called)848 p.process_pair.reset_mock()849 p.get_property.reset_mock()850 p.store.reset_mock()851 import_cert_and_key_action_mock.reset_mock()852 #Case #3: if client ssl is off and user choose option 853 #to import cert and keys854 p.get_property.side_effect = ["key_dir","", None]855 get_YN_input_mock.side_effect = [True, True]856 get_validated_string_input_mock.side_effect = ["4444"]857 get_property_expected = "[call('security.server.keys_dir'),\n"+\858 " call('client.api.ssl.port'),\n call('api.ssl')]"859 process_pair_expected = "[call('client.api.ssl.port', '4444')]"860 ambari_server.setup_https(args)861 self.assertTrue(p.process_pair.called)862 self.assertTrue(p.get_property.call_count == 3)863 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)864 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)865 self.assertTrue(p.store.called)866 self.assertTrue(import_cert_and_key_action_mock.called)867 p.process_pair.reset_mock()868 p.get_property.reset_mock()869 p.store.reset_mock()870 import_cert_and_key_action_mock.reset_mock()871 872 #Case #4: if client ssl is off and 873 #user did not choose option to import cert and keys874 p.get_property.side_effect = ["key_dir","", None]875 get_YN_input_mock.side_effect = [False]876 get_validated_string_input_mock.side_effect = ["4444"]877 get_property_expected = "[call('security.server.keys_dir'),\n"+\878 " call('client.api.ssl.port'),\n call('api.ssl')]"879 process_pair_expected = "[]"880 ambari_server.setup_https(args)881 self.assertFalse(p.process_pair.called)882 self.assertTrue(p.get_property.call_count == 3)883 self.assertEqual(str(p.get_property.call_args_list), get_property_expected)884 self.assertEqual(str(p.process_pair.call_args_list), process_pair_expected)885 self.assertFalse(p.store.called)886 self.assertFalse(import_cert_and_key_action_mock.called)887 p.process_pair.reset_mock()888 p.get_property.reset_mock()889 p.store.reset_mock()890 import_cert_and_key_action_mock.reset_mock() 891 ambari_server.SILENT = True892 893 @patch.object(ambari_server, "import_cert_and_key")894 def test_import_cert_and_key_action(self, import_cert_and_key_mock):895 import_cert_and_key_mock.return_value = True896 properties = MagicMock()897 properties.get_property.side_effect = ["key_dir","5555","6666", "true"]898 properties.process_pair = MagicMock()899 expect_process_pair = "[call('client.api.ssl.cert_name', 'https.crt'),\n"+\900 " call('client.api.ssl.key_name', 'https.key'),\n"+\901 " call('api.ssl', 'true')]"902 ambari_server.import_cert_and_key_action("key_dir", properties)903 904 self.assertEqual(str(properties.process_pair.call_args_list),\905 expect_process_pair)906 907 @patch.object(ambari_server, "remove_file")908 @patch.object(ambari_server, "copy_file")909 @patch.object(ambari_server, "read_ambari_user")910 @patch.object(ambari_server, "set_file_permissions")911 @patch.object(ambari_server, "import_file_to_keystore")912 @patch("__builtin__.open")913 @patch.object(ambari_server, "run_os_command")914 @patch("os.path.join")915 @patch.object(ambari_server, "get_validated_filepath_input")916 @patch.object(ambari_server, "get_validated_string_input")917 @patch.object(ambari_server, "is_valid_cert_host") 918 @patch.object(ambari_server, "is_valid_cert_exp") 919 def test_import_cert_and_key(self,is_valid_cert_exp_mock,\920 is_valid_cert_host_mock,\921 get_validated_string_input_mock,\922 get_validated_filepath_input_mock,\923 os_path_join_mock, run_os_command_mock,\924 open_mock, import_file_to_keystore_mock,\925 set_file_permissions_mock, read_ambari_user_mock, copy_file_mock,\926 remove_file_mock):927 is_valid_cert_exp_mock.return_value=True928 is_valid_cert_host_mock.return_value=True929 get_validated_string_input_mock.return_value = "password"930 get_validated_filepath_input_mock.side_effect = \931 ["cert_file_path","key_file_path"]932 os_path_join_mock.side_effect = ["keystore_file_path", "keystore_file_path_tmp",\933 "pass_file_path", "pass_file_path_tmp",\934 "passin_file_path","password_file_path",\935 "keystore_cert_file_path",\936 "keystore_cert_key_file_path",]937 run_os_command_mock.return_value = (0, "", "")938 om = open_mock.return_value939 expect_import_file_to_keystore = "[call('keystore_file_path_tmp',"+\940 " 'keystore_file_path'),\n"+\941 " call('pass_file_path_tmp',"+\942 " 'pass_file_path'),\n"+\943 " call('cert_file_path',"+\944 " 'keystore_cert_file_path'),\n"+\945 " call('key_file_path',"+\946 " 'keystore_cert_key_file_path')]"947 ambari_server.import_cert_and_key("key_dir")948 self.assertTrue(get_validated_filepath_input_mock.call_count == 2)949 self.assertTrue(get_validated_string_input_mock.called)950 self.assertEqual(os_path_join_mock.call_count, 8)951 self.assertTrue(set_file_permissions_mock.call_count == 1)952 self.assertEqual(str(import_file_to_keystore_mock.call_args_list),\953 expect_import_file_to_keystore)954 @patch.object(ambari_server, "remove_file")955 @patch.object(ambari_server, "copy_file")956 @patch.object(ambari_server, "generate_random_string")957 @patch.object(ambari_server, "read_ambari_user")958 @patch.object(ambari_server, "set_file_permissions")959 @patch.object(ambari_server, "import_file_to_keystore")960 @patch("__builtin__.open")961 @patch.object(ambari_server, "run_os_command")962 @patch("os.path.join")963 @patch.object(ambari_server, "get_validated_filepath_input")964 @patch.object(ambari_server, "get_validated_string_input")965 @patch.object(ambari_server, "is_valid_cert_host") 966 @patch.object(ambari_server, "is_valid_cert_exp") 967 def test_import_cert_and_key_with_empty_password(self, \968 is_valid_cert_exp_mock, is_valid_cert_host_mock, 969 get_validated_string_input_mock, get_validated_filepath_input_mock,\970 os_path_join_mock, run_os_command_mock, open_mock, \971 import_file_to_keystore_mock, set_file_permissions_mock,972 read_ambari_user_mock, generate_random_string_mock, copy_file_mock,\973 remove_file_mock):974 975 is_valid_cert_exp_mock.return_value=True976 is_valid_cert_host_mock.return_value=True977 get_validated_string_input_mock.return_value = ""978 get_validated_filepath_input_mock.side_effect =\979 ["cert_file_path","key_file_path"]980 os_path_join_mock.side_effect = ["keystore_file_path", "keystore_file_path_tmp",\981 "pass_file_path", "pass_file_path_tmp",\982 "passin_file_path","password_file_path",\983 "keystore_cert_file_path",\984 "keystore_cert_key_file_path",]985 run_os_command_mock.return_value = (0, "", "")986 expect_import_file_to_keystore = "[call('keystore_file_path_tmp',"+\987 " 'keystore_file_path'),\n"+\988 " call('pass_file_path_tmp',"+\989 " 'pass_file_path'),\n"+\990 " call('cert_file_path',"+\991 " 'keystore_cert_file_path'),\n"+\992 " call('key_file_path.secured',"+\993 " 'keystore_cert_key_file_path')]"994 ambari_server.import_cert_and_key("key_dir")995 self.assertEquals(get_validated_filepath_input_mock.call_count, 2)996 self.assertTrue(get_validated_string_input_mock.called)997 self.assertEquals(os_path_join_mock.call_count, 8)998 self.assertEquals(set_file_permissions_mock.call_count, 1)999 self.assertEqual(str(import_file_to_keystore_mock.call_args_list),\1000 expect_import_file_to_keystore)1001 self.assertTrue(generate_random_string_mock.called)1002 @patch("__builtin__.open")1003 @patch.object(ambari_server, "copy_file")1004 @patch.object(ambari_server, "is_root")1005 @patch.object(ambari_server, "read_ambari_user")1006 @patch.object(ambari_server, "set_file_permissions")1007 @patch.object(ambari_server, "import_file_to_keystore")1008 @patch.object(ambari_server, "run_os_command")1009 @patch("os.path.join")1010 @patch.object(ambari_server, "get_validated_filepath_input")1011 @patch.object(ambari_server, "get_validated_string_input")1012 def test_import_cert_and_key_with_incorrect_password(self,1013 get_validated_string_input_mock,\1014 get_validated_filepath_input_mock,\1015 os_path_join_mock,\1016 run_os_command_mock,\1017 import_file_to_keystore_mock,\1018 set_file_permissions_mock,\1019 read_ambari_user_mock,\1020 is_root_mock,\1021 copy_file_mock,\1022 open_mock):1023 get_validated_string_input_mock.return_value = "incorrect_password"1024 get_validated_filepath_input_mock.return_value = 'filename'1025 open_mock.return_value = MagicMock()1026 os_path_join_mock.return_value = ''1027 is_root_mock.return_value = True1028 #provided password doesn't match, openssl command returns an error1029 run_os_command_mock.return_value = (1, "", "Some error message")1030 self.assertFalse(ambari_server.import_cert_and_key_action(*["key_dir", None]))1031 self.assertFalse(ambari_server.import_cert_and_key("key_dir"))1032 def test_is_valid_cert_exp(self):1033 1034 #No data in certInfo1035 certInfo = {}1036 is_valid = ambari_server.is_valid_cert_exp(certInfo)1037 self.assertFalse(is_valid)1038 1039 #Issued in future1040 issuedOn = (datetime.datetime.now() + datetime.timedelta(hours=1000)).strftime(ambari_server.SSL_DATE_FORMAT)1041 expiresOn = (datetime.datetime.now() + datetime.timedelta(hours=2000)).strftime(ambari_server.SSL_DATE_FORMAT)1042 certInfo = {ambari_server.NOT_BEFORE_ATTR : issuedOn,1043 ambari_server.NOT_AFTER_ATTR : expiresOn}1044 is_valid = ambari_server.is_valid_cert_exp(certInfo)1045 self.assertFalse(is_valid)1046 1047 #Was expired1048 issuedOn = (datetime.datetime.now() - datetime.timedelta(hours=2000)).strftime(ambari_server.SSL_DATE_FORMAT)1049 expiresOn = (datetime.datetime.now() - datetime.timedelta(hours=1000)).strftime(ambari_server.SSL_DATE_FORMAT)1050 certInfo = {ambari_server.NOT_BEFORE_ATTR : issuedOn,1051 ambari_server.NOT_AFTER_ATTR : expiresOn}1052 is_valid = ambari_server.is_valid_cert_exp(certInfo)1053 self.assertFalse(is_valid)1054 1055 #Valid1056 issuedOn = (datetime.datetime.now() - datetime.timedelta(hours=2000)).strftime(ambari_server.SSL_DATE_FORMAT)1057 expiresOn = (datetime.datetime.now() + datetime.timedelta(hours=1000)).strftime(ambari_server.SSL_DATE_FORMAT)1058 certInfo = {ambari_server.NOT_BEFORE_ATTR : issuedOn,1059 ambari_server.NOT_AFTER_ATTR : expiresOn}1060 is_valid = ambari_server.is_valid_cert_exp(certInfo)1061 self.assertTrue(is_valid)1062 1063 @patch.object(ambari_server, "get_fqdn")1064 def is_valid_cert_host(self, get_fqdn_mock):1065 1066 #No data in certInfo1067 certInfo = {}1068 is_valid = ambari_server.is_valid_cert_host(certInfo)1069 self.assertFalse(is_valid)1070 1071 #Failed to get FQDN1072 get_fqdn_mock.return_value = None1073 is_valid = ambari_server.is_valid_cert_host(certInfo)1074 self.assertFalse(is_valid)1075 1076 #FQDN and Common name in certificated don't correspond1077 get_fqdn_mock.return_value = 'host1'1078 certInfo = {ambari_server.COMMON_NAME_ATTR : 'host2'}1079 is_valid = ambari_server.is_valid_cert_host(certInfo)1080 self.assertFalse(is_valid)1081 1082 #FQDN and Common name in certificated correspond1083 get_fqdn_mock.return_value = 'host1'1084 certInfo = {ambari_server.COMMON_NAME_ATTR : 'host1'}1085 is_valid = ambari_server.is_valid_cert_host(certInfo)1086 self.assertFalse(is_valid)1087 @patch("socket.getfqdn")1088 @patch("urllib2.urlopen")1089 @patch.object(ambari_server, "get_ambari_properties")1090 def test_get_fqdn(self, get_ambari_properties_mock, url_open_mock, getfqdn_mock):1091 1092 #No ambari.properties1093 get_ambari_properties_mock.return_value = -11094 fqdn = ambari_server.get_fqdn()1095 self.assertEqual(fqdn, None)1096 1097 #Check mbari_server.GET_FQDN_SERVICE_URL property name (AMBARI-2612)1098 #property name should be server.fqdn.service.url1099 self.assertEqual(ambari_server.GET_FQDN_SERVICE_URL, "server.fqdn.service.url")1100 1101 #Read FQDN from service1102 p = MagicMock()1103 p[ambari_server.GET_FQDN_SERVICE_URL] = 'someurl'1104 get_ambari_properties_mock.return_value = p1105 1106 u = MagicMock()1107 host = 'host1.domain.com'1108 u.read.return_value = host1109 url_open_mock.return_value = u1110 1111 fqdn = ambari_server.get_fqdn()1112 self.assertEqual(fqdn, host)1113 1114 #Failed to read FQDN from service, getting from socket1115 u.reset_mock()1116 u.side_effect = Exception("Failed to read FQDN from service")1117 getfqdn_mock.return_value = host1118 fqdn = ambari_server.get_fqdn()1119 self.assertEqual(fqdn, host)1120 1121 @patch.object(ambari_server, "run_os_command")1122 def test_get_cert_info(self, run_os_command_mock):1123 # Error running openssl command1124 path = 'path/to/certificate'1125 run_os_command_mock.return_value = -1, None, None1126 cert_info = ambari_server.get_cert_info(path)1127 self.assertEqual(cert_info, None)1128 1129 #Empty result of openssl command1130 run_os_command_mock.return_value = 0, None, None1131 cert_info = ambari_server.get_cert_info(path)1132 self.assertEqual(cert_info, None)1133 1134 #Positive scenario1135 notAfter = 'Jul 3 14:12:57 2014 GMT'1136 notBefore = 'Jul 3 14:12:57 2013 GMT'1137 attr1_key = 'A'1138 attr1_value = 'foo'1139 attr2_key = 'B'1140 attr2_value = 'bar'1141 attr3_key = 'CN'1142 attr3_value = 'host.domain.com'1143 subject_pattern = '/{attr1_key}={attr1_value}/{attr2_key}={attr2_value}/{attr3_key}={attr3_value}'1144 subject = subject_pattern.format(attr1_key = attr1_key, attr1_value = attr1_value,1145 attr2_key = attr2_key, attr2_value = attr2_value,1146 attr3_key = attr3_key, attr3_value = attr3_value)1147 out_pattern = """1148notAfter={notAfter}1149notBefore={notBefore}1150subject={subject}1151-----BEGIN CERTIFICATE-----1152MIIFHjCCAwYCCQDpHKOBI+Lt0zANBgkqhkiG9w0BAQUFADBRMQswCQYDVQQGEwJV1153...11545lqd8XxOGSYoMOf+70BLN2sB1155-----END CERTIFICATE-----1156 """1157 out = out_pattern.format(notAfter = notAfter, notBefore = notBefore, subject = subject)1158 run_os_command_mock.return_value = 0, out, None1159 cert_info = ambari_server.get_cert_info(path)1160 self.assertEqual(cert_info['notAfter'], notAfter)1161 self.assertEqual(cert_info['notBefore'], notBefore)1162 self.assertEqual(cert_info['subject'], subject)1163 self.assertEqual(cert_info[attr1_key], attr1_value)1164 self.assertEqual(cert_info[attr2_key], attr2_value)1165 self.assertEqual(cert_info[attr3_key], attr3_value)1166 1167 @patch.object(ambari_server, "run_os_command")1168 @patch("__builtin__.open")1169 @patch("os.path.exists")1170 def test_is_server_runing(self, os_path_exists_mock, open_mock,\1171 run_os_command_mock):1172 os_path_exists_mock.return_value = True1173 f = open_mock.return_value1174 f.readline.return_value = "111"1175 run_os_command_mock.return_value = 0, "", ""1176 status, pid = ambari_server.is_server_runing()1177 self.assertTrue(status)1178 self.assertEqual(111, pid)1179 os_path_exists_mock.return_value = False1180 status, pid = ambari_server.is_server_runing()1181 self.assertFalse(status)1182 1183 @patch.object(ambari_server, "install_jce_manualy")1184 @patch("os.stat")1185 @patch("os.path.isfile")1186 @patch("os.path.exists")1187 @patch.object(ambari_server, "track_jdk")1188 @patch.object(ambari_server, "get_YN_input")1189 @patch.object(ambari_server, "run_os_command")1190 @patch.object(ambari_server, "write_property")1191 @patch.object(ambari_server, "print_info_msg")1192 @patch.object(ambari_server, "get_JAVA_HOME")1193 @patch.object(ambari_server, "get_ambari_properties")1194 @patch("shutil.copyfile")1195 def test_download_jdk(self, copyfile_mock, get_ambari_properties_mock, get_JAVA_HOME_mock,\1196 print_info_msg_mock, write_property_mock,\1197 run_os_command_mock, get_YN_input_mock, track_jdk_mock,1198 path_existsMock, path_isfileMock, statMock,\1199 install_jce_manualy_mock):1200 args = MagicMock()1201 args.java_home = "somewhere"1202 path_existsMock.return_value = False1203 get_JAVA_HOME_mock.return_value = False1204 get_ambari_properties_mock.return_value = -11205 # Test case: ambari.properties not found1206 try:1207 ambari_server.download_jdk(args)1208 self.fail("Should throw exception because of not found ambari.properties")1209 except FatalException:1210 # Expected1211 self.assertTrue(get_ambari_properties_mock.called)1212 pass1213 # Test case: JDK already exists1214 p = MagicMock()1215 get_ambari_properties_mock.return_value = p1216 p.__getitem__.return_value = "somewhere"1217 get_JAVA_HOME_mock.return_value = True1218 path_existsMock.return_value = True1219 rcode = ambari_server.download_jdk(args)1220 self.assertEqual(0, rcode)1221 # Test case: java home setup1222 get_JAVA_HOME_mock.return_value = False1223 rcode = ambari_server.download_jdk(args)1224 self.assertEqual(0, rcode)1225 self.assertTrue(write_property_mock.called)1226 # Test case: JDK file does not exist, property not defined1227 path_existsMock.return_value = False1228 p = MagicMock()1229 get_ambari_properties_mock.return_value = p1230 p.__getitem__.side_effect = KeyError("test exception")1231 try:1232 ambari_server.download_jdk(args)1233 self.fail("Should throw exception")1234 except FatalException:1235 # Expected1236 pass1237 # Test case: JDK file does not exist, os command (curl) result does not1238 # contain Content-Length1239 p.__getitem__.return_value = "somewhere"1240 p.__getitem__.side_effect = None1241 path_existsMock.return_value = False1242 run_os_command_mock.return_value = (0, "Wrong out", None)1243 try:1244 ambari_server.download_jdk(args)1245 self.fail("Should throw exception")1246 except FatalException:1247 # Expected1248 pass1249 # Successful JDK download1250 ambari_server.JDK_INSTALL_DIR = os.getcwd()1251 get_YN_input_mock.return_value = True1252 run_os_command_mock.return_value = (0, "Creating jdk-1.2/jre"1253 "Content-Length: 32000\r\n"1254 , None)1255 statResult = MagicMock()1256 statResult.st_size = 320001257 statMock.return_value = statResult1258 rcode = ambari_server.download_jdk(args)1259 self.assertEqual(0, rcode)1260 # Test case: JDK file does not exist, jdk-location argument passed1261 p.__getitem__.return_value = "somewhere"1262 p.__getitem__.side_effect = None1263 args.jdk_location = "/existing/jdk/file"1264 path_existsMock.side_effect = [False, False, True, False, False, False]1265 ambari_server.download_jdk(args)1266 self.assertTrue(copyfile_mock.called)1267 copyfile_mock.reset_mock()1268 # Negative test case: JDK file does not exist, jdk-location argument1269 # (non-accessible file) passed1270 p.__getitem__.return_value = "somewhere"1271 p.__getitem__.side_effect = None1272 args.jdk_location = "/existing/jdk/file"1273 path_existsMock.side_effect = [False, False, True, False, False, False]1274 def copyfile_side_effect(s, d):1275 raise Exception("TerribleException")1276 copyfile_mock.side_effect = copyfile_side_effect1277 try:1278 ambari_server.download_jdk(args)1279 self.fail("Should throw exception")1280 except FatalException:1281 # Expected1282 self.assertTrue(copyfile_mock.called)1283 copyfile_mock.reset_mock()1284 # Test case: jdk is already installed, ensure that JCE check is not skipped1285 p = MagicMock()1286 get_ambari_properties_mock.return_value = p1287 p.__getitem__.return_value = "somewhere"1288 get_JAVA_HOME_mock.return_value = True1289 path_existsMock.return_value = True1290 install_jce_manualy_mock.return_value = 11291 with patch.object(ambari_server, "download_jce_policy") as download_jce_policy_mock:1292 rcode = ambari_server.download_jdk(args)1293 self.assertTrue(download_jce_policy_mock.called)1294 @patch.object(ambari_server, "run_os_command")1295 def test_get_postgre_status(self, run_os_command_mock):1296 run_os_command_mock.return_value = (1, "running", None)1297 result = ambari_server.get_postgre_status()1298 self.assertEqual("running", result)1299 run_os_command_mock.return_value = (1, "wrong", None)1300 result = ambari_server.get_postgre_status()1301 self.assertEqual(None, result)1302 @patch("time.sleep")1303 @patch("subprocess.Popen")1304 @patch.object(ambari_server, "run_os_command")1305 @patch.object(ambari_server, "get_postgre_status")1306 def test_check_postgre_up(self, get_postgre_status_mock, run_os_command_mock,1307 popen_mock, sleep_mock):1308 p = MagicMock()1309 p.poll.return_value = 01310 popen_mock.return_value = p1311 run_os_command_mock.return_value = (0, None, None)1312 rcode = ambari_server.check_postgre_up()1313 self.assertEqual(0, rcode)1314 p.poll.return_value = 41315 get_postgre_status_mock.return_value = None1316 rcode = ambari_server.check_postgre_up()1317 self.assertEqual(4, rcode)1318 @patch("platform.linux_distribution")1319 @patch("platform.system")1320 @patch.object(ambari_server, "print_info_msg")1321 @patch.object(ambari_server, "print_error_msg")1322 @patch.object(ambari_server, "get_ambari_properties")1323 @patch.object(ambari_server, "write_property")1324 @patch.object(ambari_server, "get_conf_dir")1325 def test_configure_os_settings(self, get_conf_dir_mock, write_property_mock, get_ambari_properties_mock,1326 print_error_msg_mock, print_info_msg_mock,1327 systemMock, distMock):1328 get_ambari_properties_mock.return_value = -11329 rcode = ambari_server.configure_os_settings()1330 self.assertEqual(-1, rcode)1331 p = MagicMock()1332 p[ambari_server.OS_TYPE_PROPERTY] = 'somevalue'1333 get_ambari_properties_mock.return_value = p1334 rcode = ambari_server.configure_os_settings()1335 self.assertEqual(0, rcode)1336 p.__getitem__.return_value = ""1337 systemMock.return_value = "NonLinux"1338 rcode = ambari_server.configure_os_settings()1339 self.assertEqual(-1, rcode)1340 systemMock.return_value = "Linux"1341 distMock.return_value = ("CentOS", "6.3", None)1342 rcode = ambari_server.configure_os_settings()1343 self.assertEqual(0, rcode)1344 self.assertTrue(write_property_mock.called)1345 @patch("__builtin__.open")1346 @patch.object(ambari_server, "Properties")1347 @patch.object(ambari_server, "search_file")1348 @patch.object(ambari_server, "get_conf_dir")1349 def test_get_JAVA_HOME(self, get_conf_dir_mock, search_file_mock,1350 Properties_mock, openMock):1351 openMock.side_effect = Exception("exception")1352 result = ambari_server.get_JAVA_HOME()1353 self.assertEqual(None, result)1354 expected = os.path.dirname(__file__)1355 p = MagicMock()1356 p.__getitem__.return_value = expected1357 openMock.side_effect = None1358 Properties_mock.return_value = p1359 result = ambari_server.get_JAVA_HOME()1360 self.assertEqual(expected, result)1361 def test_prompt_db_properties_default(self):1362 args = MagicMock()1363 ambari_server.load_default_db_properties(args)1364 ambari_server.prompt_db_properties(args)1365 self.assertEqual(args.database, "postgres")1366 self.assertEqual(args.database_host, "localhost")1367 self.assertEqual(args.database_name, "ambari")1368 self.assertEqual(args.database_port, "5432")1369 @patch.object(ambari_server, "setup_master_key")1370 @patch.object(ambari_server, "read_password")1371 @patch.object(ambari_server, "get_validated_string_input")1372 @patch.object(ambari_server, "get_YN_input")1373 def test_prompt_db_properties_oracle_sname(self, gyni_mock, gvsi_mock, rp_mock, smk_mock):1374 ambari_server.PROMPT_DATABASE_OPTIONS = True1375 gyni_mock.return_value = True1376 list_of_return_values= ["ambari-server", "ambari", "1", "1521", "localhost", "2"]1377 def side_effect(*args, **kwargs):1378 return list_of_return_values.pop()1379 gvsi_mock.side_effect = side_effect1380 rp_mock.return_value = "password"1381 smk_mock.return_value = (None, False, True)1382 args = MagicMock()1383 ambari_server.load_default_db_properties(args)1384 ambari_server.prompt_db_properties(args)1385 self.assertEqual(args.database, "oracle")1386 self.assertEqual(args.database_port, "1521")1387 self.assertEqual(args.database_host, "localhost")1388 self.assertEqual(args.database_name, "ambari")1389 self.assertEqual(args.database_username, "ambari-server")1390 self.assertEqual(args.sid_or_sname, "sname")1391 @patch.object(ambari_server, "setup_master_key")1392 @patch.object(ambari_server, "read_password")1393 @patch.object(ambari_server, "get_validated_string_input")1394 @patch.object(ambari_server, "get_YN_input")1395 def test_prompt_db_properties_oracle_sid(self, gyni_mock, gvsi_mock, rp_mock, smk_mock):1396 ambari_server.PROMPT_DATABASE_OPTIONS = True1397 gyni_mock.return_value = True1398 list_of_return_values= ["ambari-server", "ambari", "2", "1521", "localhost", "2"]1399 def side_effect(*args, **kwargs):1400 return list_of_return_values.pop()1401 gvsi_mock.side_effect = side_effect1402 rp_mock.return_value = "password"1403 smk_mock.return_value = (None, False, True)1404 args = MagicMock()1405 ambari_server.load_default_db_properties(args)1406 ambari_server.prompt_db_properties(args)1407 self.assertEqual(args.database, "oracle")1408 self.assertEqual(args.database_port, "1521")1409 self.assertEqual(args.database_host, "localhost")1410 self.assertEqual(args.database_name, "ambari")1411 self.assertEqual(args.database_username, "ambari-server")1412 self.assertEqual(args.sid_or_sname, "sid")1413 @patch.object(ambari_server, "setup_master_key")1414 @patch.object(ambari_server, "read_password")1415 @patch.object(ambari_server, "get_validated_string_input")1416 @patch.object(ambari_server, "get_YN_input")1417 def test_prompt_db_properties_postgre_adv(self, gyni_mock, gvsi_mock, rp_mock, smk_mock):1418 ambari_server.PROMPT_DATABASE_OPTIONS = True1419 gyni_mock.return_value = True1420 list_of_return_values= ["ambari-server", "ambari", "1"]1421 def side_effect(*args, **kwargs):1422 return list_of_return_values.pop()1423 gvsi_mock.side_effect = side_effect1424 rp_mock.return_value = "password"1425 smk_mock.return_value = (None, False, True)1426 args = MagicMock()1427 ambari_server.load_default_db_properties(args)1428 ambari_server.prompt_db_properties(args)1429 self.assertEqual(args.database, "postgres")1430 self.assertEqual(args.database_port, "5432")1431 self.assertEqual(args.database_host, "localhost")1432 self.assertEqual(args.database_name, "ambari")1433 self.assertEqual(args.database_username, "ambari-server")1434 self.assertEqual(args.sid_or_sname, "sname")1435 @patch("glob.glob")1436 @patch.object(ambari_server, "get_JAVA_HOME")1437 def test_find_jdk(self, get_JAVA_HOME_mock, globMock):1438 get_JAVA_HOME_mock.return_value = "somewhere"1439 result = ambari_server.find_jdk()1440 self.assertEqual("somewhere", result)1441 get_JAVA_HOME_mock.return_value = None1442 globMock.return_value = []1443 result = ambari_server.find_jdk()1444 self.assertEqual(None, result)1445 globMock.return_value = ["one", "two"]1446 result = ambari_server.find_jdk()1447 self.assertNotEqual(None, result)1448 @patch("os.path.exists")1449 @patch.object(ambari_server, 'verify_setup_allowed')1450 @patch.object(ambari_server, "get_YN_input")1451 @patch.object(ambari_server, "configure_os_settings")1452 @patch.object(ambari_server, "download_jdk")1453 @patch.object(ambari_server, "configure_postgres")1454 @patch.object(ambari_server, "setup_db")1455 @patch.object(ambari_server, "check_postgre_up")1456 @patch.object(ambari_server, "check_iptables")1457 @patch.object(ambari_server, "check_ambari_user")1458 @patch.object(ambari_server, "check_jdbc_drivers")1459 @patch.object(ambari_server, "check_selinux")1460 @patch.object(ambari_server, "setup_remote_db")1461 @patch.object(ambari_server, "store_remote_properties")1462 @patch.object(ambari_server, "is_local_database")1463 @patch.object(ambari_server, "store_local_properties")1464 @patch.object(ambari_server, "is_root")1465 def test_setup(self, is_root_mock, store_local_properties_mock, is_local_database_mock, store_remote_properties_mock,1466 setup_remote_db_mock, check_selinux_mock, check_jdbc_drivers_mock, check_ambari_user_mock,1467 check_iptables_mock, check_postgre_up_mock, setup_db_mock, configure_postgres_mock,1468 download_jdk_mock, configure_os_settings_mock,get_YN_input,1469 verify_setup_allowed_method, exists_mock):1470 args = MagicMock()1471 failed = False1472 get_YN_input.return_value = False1473 verify_setup_allowed_method.return_value = 01474 exists_mock.return_value = False1475 def reset_mocks():1476 is_root_mock.reset_mock()1477 store_local_properties_mock.reset_mock()1478 store_remote_properties_mock.reset_mock()1479 is_local_database_mock.reset_mock()1480 setup_remote_db_mock.reset_mock()1481 check_selinux_mock.reset_mock()1482 check_jdbc_drivers_mock.reset_mock()1483 check_ambari_user_mock.reset_mock()1484 check_iptables_mock.reset_mock()1485 check_postgre_up_mock.reset_mock()1486 setup_db_mock.reset_mock()1487 configure_postgres_mock.reset_mock()1488 download_jdk_mock.reset_mock()1489 configure_os_settings_mock.reset_mock()1490 pass1491 # Testing call under non-root1492 is_root_mock.return_value = False1493 try:1494 ambari_server.setup(args)1495 self.fail("Should throw exception")1496 except FatalException as fe:1497 # Expected1498 self.assertTrue("root-level" in fe.reason)1499 pass1500 # Testing calls under root1501 # remote case1502 is_root_mock.return_value = True1503 check_selinux_mock.return_value = 01504 check_ambari_user_mock.return_value = 01505 check_jdbc_drivers_mock.return_value = 01506 check_iptables_mock.return_value = (0, "other")1507 check_postgre_up_mock.return_value = 01508 setup_db_mock.return_value = 01509 setup_remote_db_mock.return_value = 01510 is_local_database_mock.return_value = False1511 configure_postgres_mock.return_value = 01512 download_jdk_mock.return_value = 01513 configure_os_settings_mock.return_value = 01514 store_remote_properties_mock.return_value = 01515 store_local_properties_mock.return_value = 01516 result = ambari_server.setup(args)1517 self.assertEqual(None, result)1518 self.assertTrue(check_ambari_user_mock.called)1519 self.assertEquals(True, store_remote_properties_mock.called)1520 self.assertEquals(False, store_local_properties_mock.called)1521 #Local case1522 reset_mocks()1523 is_local_database_mock.return_value = True1524 try:1525 result = ambari_server.setup(args)1526 except FatalException:1527 self.fail("Setup should be successful")1528 self.assertEqual(None, result)1529 self.assertEquals(True, store_local_properties_mock.called)1530 self.assertEquals(False, store_remote_properties_mock.called)1531 #negative case1532 reset_mocks()1533 is_local_database_mock.return_value = False1534 setup_remote_db_mock.return_value = -11535 try:1536 result = ambari_server.setup(args)1537 self.fail("Should throw exception")1538 except NonFatalException as fe:1539 self.assertTrue("cli was not found" in fe.reason)1540 @patch.object(ambari_server, "get_YN_input")1541 @patch.object(ambari_server, "setup_db")1542 @patch.object(ambari_server, "print_info_msg")1543 @patch.object(ambari_server, "run_os_command")1544 @patch.object(ambari_server, "configure_database_username_password")1545 @patch.object(ambari_server, "parse_properties_file")1546 @patch.object(ambari_server, "execute_remote_script")1547 @patch.object(ambari_server, "is_root")1548 def test_reset(self, is_root_mock, execute_remote_script_mock, parse_properties_file_mock, configure_database_username_password_mock,1549 run_os_command_mock, print_info_msg_mock,1550 setup_db_mock, get_YN_inputMock):1551 parse_properties_file_mock.return_value = 01552 args = MagicMock()1553 args.persistence_type = "local"1554 get_YN_inputMock.return_value = False1555 # Testing call under non-root1556 is_root_mock.return_value = False1557 try:1558 ambari_server.reset(args)1559 self.fail("Should throw exception")1560 except FatalException as fe:1561 # Expected1562 self.assertTrue("root-level" in fe.reason)1563 pass1564 # Testing calls under root1565 is_root_mock.return_value = True1566 try:1567 ambari_server.reset(args)1568 self.fail("Should throw exception")1569 except FatalException as fe:1570 # Expected1571 self.assertFalse("root-level" in fe.reason)1572 pass1573 get_YN_inputMock.return_value = True1574 run_os_command_mock.return_value = (1, None, None)1575 try:1576 ambari_server.reset(args)1577 self.fail("Should throw exception")1578 except FatalException:1579 # Expected1580 pass1581 run_os_command_mock.return_value = (0, None, None)1582 ambari_server.reset(args)1583 self.assertTrue(setup_db_mock.called)1584 1585 #remote db case1586 args.persistence_type = "remote"1587 execute_remote_script_mock.return_value=(0, None, None)1588 rcode = ambari_server.reset(args)1589 self.assertEqual(None, rcode)1590 self.assertTrue(execute_remote_script_mock.called)1591 @patch.object(ambari_server, "setup_db")1592 @patch.object(ambari_server, "print_info_msg")1593 @patch.object(ambari_server, "run_os_command")1594 @patch.object(ambari_server, "parse_properties_file")1595 @patch.object(ambari_server, "is_root")1596 def test_silent_reset(self, is_root_mock, parse_properties_file_mock,1597 run_os_command_mock, print_info_msg_mock,1598 setup_db_mock):1599 is_root_mock.return_value = True1600 args = MagicMock()1601 ambari_server.SILENT = True1602 self.assertTrue(ambari_server.SILENT)1603 run_os_command_mock.return_value = (0, None, None)1604 def signal_handler(signum, frame):1605 self.fail("Timed out!")1606 signal.signal(signal.SIGALRM, signal_handler)1607 signal.alarm(5)1608 rcode = ambari_server.reset(args)1609 signal.alarm(0)1610 self.assertEqual(None, rcode)1611 self.assertTrue(setup_db_mock.called)1612 @patch("os.chown")1613 @patch("pwd.getpwnam")1614 @patch.object(ambari_server, 'get_master_key_location')1615 @patch.object(ambari_server, 'save_master_key')1616 @patch('os.chmod', autospec=True)1617 @patch.object(ambari_server, 'get_validated_string_input')1618 @patch("os.environ")1619 @patch.object(ambari_server, "get_ambari_properties")1620 @patch("os.kill")1621 @patch("os.path.exists")1622 @patch("__builtin__.open")1623 @patch("subprocess.Popen")1624 @patch.object(ambari_server, "print_info_msg")1625 @patch.object(ambari_server, "search_file")1626 @patch.object(ambari_server, "find_jdk")1627 @patch.object(ambari_server, "print_error_msg")1628 @patch.object(ambari_server, "check_postgre_up")1629 @patch.object(ambari_server, "check_iptables")1630 @patch.object(ambari_server, "parse_properties_file")1631 @patch.object(ambari_server, "read_ambari_user")1632 @patch.object(ambari_server, "is_root")1633 @patch("getpass.getuser")1634 @patch("os.chdir")1635 def test_start(self, chdir_mock, getuser_mock, is_root_mock, read_ambari_user_mock,1636 parse_properties_file_mock, check_iptables_mock, check_postgre_up_mock,1637 print_error_msg_mock, find_jdk_mock, search_file_mock,1638 print_info_msg_mock, popenMock, openMock, pexistsMock,1639 killMock, get_ambari_properties_mock, os_environ_mock,1640 get_validated_string_input_method, os_chmod_method,1641 save_master_key_method, get_master_key_location_method,1642 getpwnam_mock, os_chown_mock):1643 args = MagicMock()1644 f = MagicMock()1645 f.readline.return_value = 421646 openMock.return_value = f1647 p = get_ambari_properties_mock.return_value1648 p.get_property.return_value = 'False'1649 search_file_mock.return_value = None1650 pw = MagicMock()1651 pw.setattr('pw_uid', 0)1652 pw.setattr('pw_gid', 0)1653 getpwnam_mock.return_value = pw1654 os_chown_mock.return_value = None1655 # Checking "server is running"1656 pexistsMock.return_value = True1657 try:1658 ambari_server.start(args)1659 self.fail("Should fail with 'Server is running'")1660 except FatalException:1661 # Expected1662 pass1663 self.assertTrue(killMock.called)1664 killMock.reset_mock()1665 parse_properties_file_mock.reset_mock()1666 pexistsMock.return_value = False1667 # Checking situation when ambari user is not set up1668 read_ambari_user_mock.return_value = None1669 try:1670 ambari_server.start(args)1671 self.fail("Should fail with 'Can not detect a system user for Ambari'")1672 except FatalException as e:1673 # Expected1674 self.assertTrue('Unable to detect a system user for Ambari Server.' in e.reason)1675 parse_properties_file_mock.reset_mock()1676 # Checking start from non-root when current user is not the same as a1677 # custom user1678 read_ambari_user_mock.return_value = "dummy-user"1679 getuser_mock.return_value = "non_custom_user"1680 is_root_mock.return_value = False1681 try:1682 ambari_server.start(args)1683 self.fail("Should fail with 'Can not start ambari-server as user...'")1684 except FatalException as e:1685 # Expected1686 self.assertTrue('Unable to start Ambari Server as user' in e.reason)1687 self.assertFalse(parse_properties_file_mock.called)1688 parse_properties_file_mock.reset_mock()1689 # Checking "jdk not found"1690 is_root_mock.return_value = True1691 find_jdk_mock.return_value = None1692 try:1693 ambari_server.start(args)1694 self.fail("Should fail with 'No JDK found'")1695 except FatalException as e:1696 # Expected1697 self.assertTrue('No JDK found' in e.reason)1698 find_jdk_mock.return_value = "somewhere"1699 parse_properties_file_mock.reset_mock()1700 ## Testing workflow under root1701 is_root_mock.return_value = True1702 # Remote DB1703 args.persistence_type="remote"1704 check_iptables_mock.return_value = (0, None)1705 try:1706 ambari_server.start(args)1707 except FatalException as e:1708 # Ignored1709 pass1710 self.assertFalse('Unable to start PostgreSQL server' in e.reason)1711 self.assertFalse(check_postgre_up_mock.called)1712 check_postgre_up_mock.reset_mock()1713 parse_properties_file_mock.reset_mock()1714 # Local DB1715 args.persistence_type="local"1716 # case: postgres failed to start1717 check_postgre_up_mock.return_value = 11718 try:1719 ambari_server.start(args)1720 self.fail("Should fail with 'Unable to start PostgreSQL server'")1721 except FatalException as e:1722 # Expected1723 self.assertTrue('Unable to start PostgreSQL server' in e.reason)1724 self.assertTrue(check_postgre_up_mock.called)1725 parse_properties_file_mock.reset_mock()1726 # case: iptables failed to stop1727 check_postgre_up_mock.return_value = 01728 check_iptables_mock.return_value = (1, ambari_server.IP_TBLS_ENABLED)1729 try:1730 ambari_server.start(args)1731 self.fail("Should fail with 'Failed to stop iptables'")1732 except FatalException as e:1733 # Expected1734 self.assertTrue('Failed to stop iptables' in e.reason)1735 parse_properties_file_mock.reset_mock()1736 check_iptables_mock.return_value = (0, None)1737 # Case: custom user is "root"1738 read_ambari_user_mock.return_value = "root"1739 ambari_server.start(args)1740 self.assertTrue(popenMock.called)1741 popen_arg = popenMock.call_args[0][0]1742 self.assertTrue(popen_arg[0] == "/bin/sh")1743 popenMock.reset_mock()1744 parse_properties_file_mock.reset_mock()1745 # Case: custom user is not "root"1746 read_ambari_user_mock.return_value = "not-root-user"1747 ambari_server.start(args)1748 self.assertTrue(chdir_mock.called)1749 self.assertTrue(popenMock.called)1750 popen_arg = popenMock.call_args[0][0]1751 self.assertTrue(popen_arg[0] == "/bin/su")1752 check_postgre_up_mock.reset_mock()1753 popenMock.reset_mock()1754 parse_properties_file_mock.reset_mock()1755 ## Testing workflow under non-root1756 is_root_mock.return_value = False1757 read_ambari_user_mock.return_value = "not-root-user"1758 getuser_mock.return_value = read_ambari_user_mock.return_value1759 parse_properties_file_mock.reset_mock()1760 # Local DB1761 args.persistence_type="local"1762 ambari_server.start(args)1763 self.assertFalse(check_postgre_up_mock.called)1764 parse_properties_file_mock.reset_mock()1765 # Remote DB1766 args.persistence_type="remote"1767 ambari_server.start(args)1768 self.assertFalse(check_postgre_up_mock.called)1769 parse_properties_file_mock.reset_mock()1770 # Checking call1771 check_iptables_mock.reset_mock()1772 check_iptables_mock.return_value = (0, None)1773 ambari_server.start(args)1774 self.assertTrue(popenMock.called)1775 popen_arg = popenMock.call_args[0][0]1776 self.assertTrue(popen_arg[0] == "/bin/sh")1777 self.assertFalse(check_iptables_mock.called)1778 parse_properties_file_mock.reset_mock()1779 # Test start under wrong user1780 read_ambari_user_mock.return_value = "not-root-user"1781 getuser_mock.return_value = "non_custom_user"1782 try:1783 ambari_server.start(args)1784 self.fail("Can not start ambari-server as user non_custom_user.")1785 except FatalException as e:1786 # Expected1787 self.assertTrue('Unable to start Ambari Server as user' in e.reason)1788 parse_properties_file_mock.reset_mock()1789 # Check environ master key is set1790 popenMock.reset_mock()1791 os_environ_mock.copy.return_value = {"a" : "b",1792 ambari_server.SECURITY_KEY_ENV_VAR_NAME : "masterkey"}1793 args.persistence_type="local"1794 read_ambari_user_mock.return_value = "root"1795 is_root_mock.return_value = True1796 ambari_server.start(args)1797 self.assertFalse(get_validated_string_input_method.called)1798 self.assertFalse(save_master_key_method.called)1799 popen_arg = popenMock.call_args[1]['env']1800 self.assertEquals(os_environ_mock.copy.return_value, popen_arg)1801 parse_properties_file_mock.reset_mock()1802 # Check environ master key is not set1803 popenMock.reset_mock()1804 os_environ_mock.reset_mock()1805 p.get_property.return_value = 'True'1806 os_environ_mock.copy.return_value = {"a" : "b"}1807 args.persistence_type="local"1808 read_ambari_user_mock.return_value = "root"1809 is_root_mock.return_value = True1810 get_validated_string_input_method.return_value = "masterkey"1811 os_chmod_method.return_value = None1812 ambari_server.start(args)1813 self.assertTrue(get_validated_string_input_method.called)1814 self.assertTrue(save_master_key_method.called)1815 popen_arg = popenMock.call_args[1]['env']1816 self.assertEquals(os_environ_mock.copy.return_value, popen_arg)1817 @patch("__builtin__.open")1818 @patch("os.path.exists")1819 @patch("os.remove")1820 @patch("os.killpg")1821 @patch("os.getpgid")1822 @patch.object(ambari_server, "print_info_msg")1823 def test_stop(self, print_info_msg_mock, gpidMock, removeMock,1824 killMock, pexistsMock, openMock):1825 pexistsMock.return_value = True1826 f = MagicMock()1827 f.readline.return_value = "42"1828 openMock.return_value = f1829 ambari_server.stop(None)1830 self.assertTrue(f.readline.called)1831 self.assertTrue(killMock.called)1832 self.assertTrue(killMock.called)1833 self.assertTrue(f.close.called)1834 self.assertTrue(removeMock.called)1835 @patch.object(ambari_server, "configure_database_username_password")1836 @patch.object(ambari_server, "run_os_command")1837 @patch.object(ambari_server, "is_root")1838 def test_upgrade_stack(self, is_root_mock, run_os_command_mock,1839 configure_postgres_username_password_mock):1840 args = MagicMock()1841 # Testing call under non-root1842 is_root_mock.return_value = False1843 try:1844 ambari_server.upgrade_stack(args, 'HDP-2.0')1845 self.fail("Should throw exception")1846 except FatalException as fe:1847 # Expected1848 self.assertTrue("root-level" in fe.reason)1849 pass1850 # Testing calls under root1851 is_root_mock.return_value = True1852 run_os_command_mock.return_value = (0, '', '')1853 ambari_server.upgrade_stack(args, 'HDP-2.0')1854 self.assertTrue(configure_postgres_username_password_mock.called)1855 self.assertTrue(run_os_command_mock.called)1856 @patch.object(ambari_server, "adjust_directory_permissions")1857 @patch.object(ambari_server, "print_warning_msg")1858 @patch.object(ambari_server, "read_ambari_user")1859 @patch.object(ambari_server, "check_db_consistency")1860 @patch.object(ambari_server, "execute_db_script")1861 @patch.object(ambari_server, "check_postgre_up")1862 @patch.object(ambari_server, "update_ambari_properties")1863 @patch.object(ambari_server, "parse_properties_file")1864 @patch.object(ambari_server, "is_root")1865 def test_upgrade(self, is_root_mock, parse_properties_file_mock, update_ambari_properties_mock,1866 check_postgre_up_mock, execute_db_script_mock,1867 check_db_consistency_mock, read_ambari_user_mock,1868 print_warning_msg_mock, adjust_directory_permissions_mock):1869 args = MagicMock()1870 args.upgrade_script_file = "/var/lib/"\1871 "ambari-server/resources/upgrade/ddl/"\1872 "Ambari-DDL-Postgres-UPGRADE-1.3.0.sql"1873 update_ambari_properties_mock.return_value = 01874 check_postgre_up_mock.return_value = 01875 execute_db_script_mock.return_value = 01876 check_db_consistency_mock.return_value = 01877 # Testing call under non-root1878 is_root_mock.return_value = False1879 try:1880 ambari_server.upgrade(args)1881 self.fail("Should throw exception")1882 except FatalException as fe:1883 # Expected1884 self.assertTrue("root-level" in fe.reason)1885 pass1886 # Testing calls under root1887 is_root_mock.return_value = True1888 # Testing with undefined custom user1889 read_ambari_user_mock.return_value = None1890 ambari_server.upgrade(args)1891 self.assertTrue(print_warning_msg_mock.called)1892 warning_args = print_warning_msg_mock.call_args[0][0]1893 self.assertTrue("custom ambari user" in warning_args)1894 # Testing with defined custom user1895 read_ambari_user_mock.return_value = "ambari-custom-user"1896 ambari_server.upgrade(args)1897 self.assertTrue(adjust_directory_permissions_mock.called)1898 def test_print_info_msg(self):1899 out = StringIO.StringIO()1900 sys.stdout = out1901 ambari_server.VERBOSE = True1902 ambari_server.print_info_msg("msg")1903 self.assertNotEqual("", out.getvalue())1904 sys.stdout = sys.__stdout__1905 def test_print_error_msg(self):1906 out = StringIO.StringIO()1907 sys.stdout = out1908 ambari_server.VERBOSE = True1909 ambari_server.print_error_msg("msg")1910 self.assertNotEqual("", out.getvalue())1911 sys.stdout = sys.__stdout__1912 def test_print_warning_msg(self):1913 out = StringIO.StringIO()1914 sys.stdout = out1915 ambari_server.VERBOSE = True1916 ambari_server.print_warning_msg("msg")1917 self.assertNotEqual("", out.getvalue())1918 sys.stdout = sys.__stdout__1919 @patch.object(ambari_server, "get_choice_string_input")1920 def test_get_YN_input(self, get_choice_string_input_mock):1921 ambari_server.get_YN_input("prompt", "default")1922 self.assertTrue(get_choice_string_input_mock.called)1923 self.assertEqual(4, len(get_choice_string_input_mock.call_args_list[0][0]))1924 def test_load_default_db_properties(self):1925 args = MagicMock()1926 ambari_server.load_default_db_properties(args)1927 self.assertEquals(args.database, "postgres")1928 self.assertEquals(args.database_host, "localhost")1929 self.assertEquals(args.database_port, "5432")1930 self.assertEquals(args.database_name, "ambari")1931 self.assertEquals(args.database_username, "ambari")1932 self.assertEquals(args.database_password, "bigdata")1933 args = MagicMock()1934 ambari_server.DATABASE_INDEX=11935 ambari_server.load_default_db_properties(args)1936 self.assertEquals(args.database, "oracle")1937 self.assertEquals(args.database_port, "1521")1938 failed = False1939 args = MagicMock()1940 ambari_server.DATABASE_INDEX=21941 try:1942 ambari_server.load_default_db_properties(args)1943 except:1944 failed = True1945 self.assertTrue(failed)1946 pass1947 @patch.object(ambari_server, 'setup')1948 def test_main_db_options(self, setup_mock):1949 base_args = ["ambari-server.py", "setup"]1950 db_args = ["--database", "postgres", "--databasehost", "somehost.net", "--databaseport", "12345",1951 "--databasename", "ambari", "--databaseusername", "ambari", "--databasepassword", "bigdata"]1952 #test no args1953 failed = False1954 sys.argv = list(base_args)1955 try:1956 ambari_server.main()1957 except SystemExit:1958 failed = True1959 pass1960 self.assertFalse(failed)1961 self.assertTrue(setup_mock.called)1962 setup_mock.reset_mock()1963 #test full args1964 sys.argv = list(base_args)1965 sys.argv.extend(db_args)1966 try:1967 ambari_server.main()1968 except SystemExit:1969 failed = True1970 pass1971 self.assertTrue(ambari_server.PROMPT_DATABASE_OPTIONS)1972 self.assertFalse(failed)1973 self.assertTrue(setup_mock.called)1974 setup_mock.reset_mock()1975 #test not full args1976 sys.argv = list(base_args)1977 sys.argv.extend(["--database", "postgres"])1978 try:1979 ambari_server.main()1980 except SystemExit:1981 failed = True1982 pass1983 self.assertFalse(setup_mock.called)1984 self.assertTrue(failed)1985 setup_mock.reset_mock()1986 #test wrong database1987 failed = False1988 sys.argv = list(base_args)1989 sys.argv.extend(["--database", "unknown"])1990 sys.argv.extend(db_args[2:])1991 try:1992 ambari_server.main()1993 except SystemExit:1994 failed = True1995 pass1996 self.assertTrue(failed)1997 self.assertFalse(setup_mock.called)1998 setup_mock.reset_mock()1999 #test wrong port check2000 failed = False2001 sys.argv = list(base_args)2002 sys.argv.extend(["--databaseport", "unknown"])2003 sys.argv.extend(db_args[:4])2004 sys.argv.extend(db_args[6:])2005 try:2006 ambari_server.main()2007 except SystemExit:2008 failed = True2009 pass2010 self.assertTrue(failed)2011 self.assertFalse(setup_mock.called)2012 setup_mock.reset_mock()2013 pass2014 @patch.object(ambari_server, "load_default_db_properties")2015 @patch.object(ambari_server, "get_YN_input")2016 @patch.object(ambari_server, "get_validated_string_input")2017 @patch.object(ambari_server, "setup_master_key")2018 @patch.object(ambari_server, "configure_database_password")2019 def test_prompt_db_properties(self, configure_database_password_mock, setup_master_key_mock,2020 get_validated_string_input_mock, get_YN_input_mock, load_default_db_properties_mock):2021 args = MagicMock()2022 #test not prompt2023 ambari_server.PROMPT_DATABASE_OPTIONS = False2024 ambari_server.prompt_db_properties(args)2025 self.assertFalse(load_default_db_properties_mock.called)2026 self.assertFalse(get_validated_string_input_mock.called)2027 self.assertFalse(get_YN_input_mock.called)2028 load_default_db_properties_mock.reset_mock()2029 get_validated_string_input_mock.reset_mock()2030 get_YN_input_mock.reset_mock()2031 #test prompt2032 get_YN_input_mock.return_value = False2033 ambari_server.PROMPT_DATABASE_OPTIONS = True2034 ambari_server.prompt_db_properties(args)2035 self.assertTrue(get_YN_input_mock.called)2036 self.assertFalse(get_validated_string_input_mock.called)2037 self.assertTrue(load_default_db_properties_mock.called)2038 load_default_db_properties_mock.reset_mock()2039 get_validated_string_input_mock.reset_mock()2040 get_YN_input_mock.reset_mock()2041 #test prompt advanced2042 get_YN_input_mock.return_value = True2043 ambari_server.PROMPT_DATABASE_OPTIONS = True2044 setup_master_key_mock.return_value = (None, False, True)2045 configure_database_password_mock.return_value = ("test", None)2046 ambari_server.prompt_db_properties(args)2047 self.assertTrue(get_YN_input_mock.called)2048 self.assertTrue(get_validated_string_input_mock.called)2049 self.assertTrue(load_default_db_properties_mock.called)2050 self.assertEquals(args.database, "postgres")2051 load_default_db_properties_mock.reset_mock()2052 get_validated_string_input_mock.reset_mock()2053 get_YN_input_mock.reset_mock()2054 pass2055 @patch.object(ambari_server, "get_db_cli_tool")2056 @patch.object(ambari_server, "run_in_shell")2057 def test_execute_remote_script(self, run_in_shell_mock, get_db_cli_tool_mock):2058 args = MagicMock()2059 script = "script"2060 #success2061 run_in_shell_mock.return_value=(0, None, None)2062 get_db_cli_tool_mock.return_value="tool"2063 args.database = "postgres"2064 args.database_password = "some_password"2065 retcode, out, err = ambari_server.execute_remote_script(args, script)2066 self.assertEquals(retcode, 0)2067 self.assertTrue(get_db_cli_tool_mock.called)2068 self.assertTrue(run_in_shell_mock.called)2069 #fail no cli tool2070 get_db_cli_tool_mock.return_value = None2071 get_db_cli_tool_mock.called = False2072 run_in_shell_mock.called = False2073 retcode, out, err = ambari_server.execute_remote_script(args, script)2074 self.assertEquals(retcode, -1)2075 self.assertTrue(get_db_cli_tool_mock.called)2076 self.assertFalse(run_in_shell_mock.called)2077 #fail execute2078 errcode = 12079 get_db_cli_tool_mock.return_value = "tool"2080 run_in_shell_mock.return_value=(errcode, None, None)2081 get_db_cli_tool_mock.called = False2082 run_in_shell_mock.called = False2083 retcode, out, err = ambari_server.execute_remote_script(args, script)2084 self.assertEquals(retcode, errcode)2085 self.assertTrue(get_db_cli_tool_mock.called)2086 self.assertTrue(run_in_shell_mock.called)2087 #fail wrong database - case should be completely impossible2088 get_db_cli_tool_mock.return_value = "tool"2089 run_in_shell_mock.return_value=(0, None, None)2090 get_db_cli_tool_mock.called = False2091 run_in_shell_mock.called = False2092 args.database = "unknown"2093 retcode, out, err = ambari_server.execute_remote_script(args, script)2094 self.assertEquals(retcode, -2)2095 self.assertTrue(get_db_cli_tool_mock.called)2096 self.assertFalse(run_in_shell_mock.called)2097 pass2098 @patch.object(ambari_server, "get_conf_dir")2099 def test_update_ambari_properties(self, get_conf_dir_mock):2100 properties = ["server.jdbc.user.name=ambari-server\n",2101 "server.jdbc.user.passwd=/etc/ambari-server/conf/password.dat\n",2102 "java.home=/usr/jdk64/jdk1.6.0_31\n",2103 "server.os_type=redhat6\n",2104 "ambari-server.user=ambari\n",2105 "agent.fqdn.service.url=URL\n"]2106 NEW_PROPERTY = 'some_new_property=some_value\n'2107 CHANGED_VALUE_PROPERTY = 'server.os_type=should_not_overwrite_value\n'2108 get_conf_dir_mock.return_value = '/etc/ambari-server/conf'2109 (tf1, fn1) = tempfile.mkstemp()2110 (tf2, fn2) = tempfile.mkstemp()2111 ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE = fn12112 ambari_server.AMBARI_PROPERTIES_FILE = fn22113 with open(ambari_server.AMBARI_PROPERTIES_FILE, "w") as f:2114 f.write(NEW_PROPERTY)2115 f.write(CHANGED_VALUE_PROPERTY)2116 with open(ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE, 'w') as f:2117 for line in properties:2118 f.write(line)2119 #Call tested method2120 ambari_server.update_ambari_properties()2121 timestamp = datetime.datetime.now()2122 #RPMSAVE_FILE wasn't found2123 self.assertFalse(os.path.exists(ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE))2124 #Renamed RPMSAVE_FILE exists2125 self.assertTrue(os.path.exists(ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE2126 + '.' + timestamp.strftime('%Y%m%d%H%M%S')))2127 with open(ambari_server.AMBARI_PROPERTIES_FILE, 'r') as f:2128 ambari_properties_content = f.readlines()2129 for line in properties:2130 if (line == "agent.fqdn.service.url=URL\n"):2131 if (not ambari_server.GET_FQDN_SERVICE_URL+"=URL\n" in ambari_properties_content) and (line in ambari_properties_content):2132 self.fail()2133 else:2134 if not line in ambari_properties_content:2135 self.fail()2136 if not NEW_PROPERTY in ambari_properties_content:2137 self.fail()2138 if CHANGED_VALUE_PROPERTY in ambari_properties_content:2139 self.fail()2140 # Command should not fail if *.rpmsave file is missing2141 result = ambari_server.update_ambari_properties()2142 self.assertEquals(result, 0)2143 os.unlink(fn2)2144 #if ambari.properties file is absent then "ambari-server upgrade" should2145 # fail2146 (tf, fn) = tempfile.mkstemp()2147 ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE = fn2148 result = ambari_server.update_ambari_properties()2149 self.assertNotEquals(result, 0)2150 @patch.object(ambari_server.Properties, '__init__')2151 @patch.object(ambari_server, 'search_file')2152 def test_update_ambari_properties_negative_case(self, search_file_mock, properties_mock):2153 search_file_mock.return_value = None2154 #Call tested method2155 self.assertEquals(0, ambari_server.update_ambari_properties())2156 self.assertFalse(properties_mock.called)2157 search_file_mock.return_value = False2158 #Call tested method2159 self.assertEquals(0, ambari_server.update_ambari_properties())2160 self.assertFalse(properties_mock.called)2161 search_file_mock.return_value = ''2162 #Call tested method2163 self.assertEquals(0, ambari_server.update_ambari_properties())2164 self.assertFalse(properties_mock.called)2165 @patch.object(ambari_server, "get_conf_dir")2166 def test_update_ambari_properties_without_user_property(self, get_conf_dir_mock):2167 '''2168 Checks: update_ambari_properties call should add ambari-server.user property if2169 it's absent2170 '''2171 properties = ["server.jdbc.user.name=ambari-server\n",2172 "server.jdbc.user.passwd=/etc/ambari-server/conf/password.dat\n",2173 "java.home=/usr/jdk64/jdk1.6.0_31\n",2174 "server.os_type=redhat6\n"]2175 get_conf_dir_mock.return_value = '/etc/ambari-server/conf'2176 (tf1, fn1) = tempfile.mkstemp()2177 (tf2, fn2) = tempfile.mkstemp()2178 ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE = fn12179 ambari_server.AMBARI_PROPERTIES_FILE = fn22180 with open(ambari_server.AMBARI_PROPERTIES_RPMSAVE_FILE, 'w') as f:2181 for line in properties:2182 f.write(line)2183 #Call tested method2184 ambari_server.update_ambari_properties()2185 ambari_properties = ambari_server.Properties()2186 ambari_properties.load(open(fn2))2187 self.assertTrue(ambari_server.NR_USER_PROPERTY in ambari_properties.keys())2188 value = ambari_properties[ambari_server.NR_USER_PROPERTY]2189 self.assertEqual(value, "root")2190 os.unlink(fn2)2191 @patch.object(ambari_server, 'verify_setup_allowed')2192 @patch("sys.exit")2193 @patch.object(ambari_server, "get_YN_input")2194 @patch.object(ambari_server, "get_db_cli_tool")2195 @patch.object(ambari_server, "store_remote_properties")2196 @patch.object(ambari_server, "is_local_database")2197 @patch.object(ambari_server, "check_iptables")2198 @patch.object(ambari_server, "check_jdbc_drivers")2199 @patch.object(ambari_server, "is_root")2200 @patch.object(ambari_server, "check_ambari_user")2201 @patch.object(ambari_server, "download_jdk")2202 @patch.object(ambari_server, "configure_os_settings")2203 @patch('__builtin__.raw_input')2204 def test_setup_remote_db_wo_client(self,raw_input, configure_os_settings_mock,2205 download_jdk_mock, check_ambari_user_mock, is_root_mock,2206 check_jdbc_drivers_mock, check_iptables_mock, is_local_db_mock,2207 store_remote_properties_mock, get_db_cli_tool_mock, get_YN_input,2208 exit_mock, verify_setup_allowed_method):2209 args = MagicMock()2210 raw_input.return_value =""2211 is_root_mock.return_value = True2212 is_local_db_mock.return_value = False2213 get_YN_input.return_value = False2214 check_iptables_mock.return_value = (0, "other")2215 store_remote_properties_mock.return_value = 02216 get_db_cli_tool_mock.return_value = None2217 check_jdbc_drivers_mock.return_value=02218 check_ambari_user_mock.return_value = 02219 download_jdk_mock.return_value = 02220 configure_os_settings_mock.return_value = 02221 verify_setup_allowed_method.return_value = 02222 try:2223 ambari_server.setup(args)2224 self.fail("Should throw exception")2225 except NonFatalException as fe:2226 # Expected2227 self.assertTrue("The cli was not found" in fe.reason)2228 @patch.object(ambari_server, 'verify_setup_allowed')2229 @patch("sys.exit")2230 @patch.object(ambari_server, "get_YN_input")2231 @patch.object(ambari_server, "get_db_cli_tool")2232 @patch.object(ambari_server, "is_local_database")2233 @patch.object(ambari_server, "check_iptables")2234 @patch.object(ambari_server, "check_jdbc_drivers")2235 @patch.object(ambari_server, "is_root")2236 @patch.object(ambari_server, "check_ambari_user")2237 @patch.object(ambari_server, "download_jdk")2238 @patch.object(ambari_server, "configure_os_settings")2239 @patch('__builtin__.raw_input')2240 def test_store_remote_properties(self,raw_input, configure_os_settings_mock,2241 download_jdk_mock, check_ambari_user_mock, is_root_mock,2242 check_jdbc_drivers_mock, check_iptables_mock, is_local_db_mock,2243 get_db_cli_tool_mock, get_YN_input, exit_mock, verify_setup_allowed_method):2244 raw_input.return_value =""2245 is_root_mock.return_value = True2246 is_local_db_mock.return_value = False2247 get_YN_input.return_value = False2248 check_iptables_mock.return_value = (0, "other")2249 get_db_cli_tool_mock.return_value = None2250 check_jdbc_drivers_mock.return_value=02251 check_ambari_user_mock.return_value = 02252 download_jdk_mock.return_value = 02253 configure_os_settings_mock.return_value = 02254 verify_setup_allowed_method.return_value = 02255 import optparse2256 args = optparse.Values()2257 args.database = "oracle"2258 args.database_host = "localhost"2259 args.database_port = "1234"2260 args.database_name = "ambari"2261 args.sid_or_sname = "foo"2262 args.database_username = "foo"2263 args.database_password = "foo"2264 tempdir = tempfile.gettempdir()2265 prop_file = os.path.join(tempdir, "ambari.properties")2266 with open(prop_file, "w") as f:2267 f.write("dummy=dummy")2268 f.close()2269 os.environ[ambari_server.AMBARI_CONF_VAR] = tempdir2270 ambari_server.DATABASE_INDEX = 12271 ambari_server.store_remote_properties(args)2272 properties = ambari_server.get_ambari_properties()2273 found = False2274 for n in properties.propertyNames():2275 if not found and n.startswith("server.jdbc.properties"):2276 found = True2277 ambari_server.DATABASE_INDEX = 02278 del os.environ[ambari_server.AMBARI_CONF_VAR]2279 os.remove(prop_file)2280 self.assertTrue(found)2281 @patch.object(ambari_server, "parse_properties_file")2282 @patch.object(ambari_server, "get_db_cli_tool")2283 @patch.object(ambari_server, "print_error_msg")2284 @patch.object(ambari_server, "get_YN_input")2285 @patch.object(ambari_server, "setup_db")2286 @patch.object(ambari_server, "run_os_command")2287 @patch.object(ambari_server, "is_root")2288 def test_reset_remote_db_wo_client(self, is_root_mock, run_os_command_mock, setup_db_mock,2289 get_YN_inputMock, print_error_msg_mock, get_db_cli_tool_mock, parse_properties_file_mock):2290 args = MagicMock()2291 get_YN_inputMock.return_value = True2292 run_os_command_mock.return_value = (0, None, None)2293 args.persistence_type="remote"2294 get_db_cli_tool_mock.return_value = None2295 is_root_mock.return_value = True2296 try:2297 ambari_server.reset(args)2298 self.fail("Should throw exception")2299 except NonFatalException as fe:2300 # Expected2301 self.assertTrue("Client wasn't found" in fe.reason)2302 pass2303 @patch.object(ambari_server, "find_properties_file")2304 def test_get_ambari_properties(self, find_properties_file_mock):2305 find_properties_file_mock.return_value = None2306 rcode = ambari_server.get_ambari_properties()2307 self.assertEqual(rcode, -1)2308 2309 tf1 = tempfile.NamedTemporaryFile()2310 find_properties_file_mock.return_value = tf1.name2311 prop_name='name'2312 prop_value='val'2313 2314 with open(tf1.name, 'w') as fout:2315 fout.write(prop_name + '=' + prop_value)2316 fout.close()2317 properties = ambari_server.get_ambari_properties()2318 self.assertEqual(properties[prop_name], prop_value)2319 @patch.object(ambari_server, "get_ambari_properties")2320 @patch.object(ambari_server, "find_jdbc_driver")2321 @patch.object(ambari_server, "copy_files")2322 @patch.object(ambari_server, "print_error_msg")2323 @patch.object(ambari_server, "print_warning_msg")2324 @patch('__builtin__.raw_input')2325 @patch("sys.exit")2326 def check_jdbc_drivers(self, exit_mock, raw_input_mock, print_warning_msg, print_error_msg_mock, copy_files_mock,2327 find_jdbc_driver_mock, get_ambari_properties_mock):2328 out = StringIO.StringIO()2329 sys.stdout = out2330 args = MagicMock()2331 # Check positive scenario2332 drivers_list = ['driver_file']2333 resources_dir = '/tmp'2334 get_ambari_properties_mock.return_value = {ambari_server.RESOURCES_DIR_PROPERTY : resources_dir}2335 find_jdbc_driver_mock.return_value = drivers_list2336 args.database = "oracle"2337 rcode = ambari_server.check_jdbc_drivers(args)2338 self.assertEqual(0, rcode)2339 copy_files_mock.assert_called_with(drivers_list, resources_dir)2340 # Check negative scenarios2341 # Silent option, no drivers2342 ambari_server.SILENT = True2343 find_jdbc_driver_mock.return_value = -12344 rcode = ambari_server.check_jdbc_drivers(args)2345 self.assertTrue(print_error_msg_mock.called)2346 self.assertTrue(exit_mock.called)2347 # Non-Silent option, no drivers2348 ambari_server.SILENT = False2349 find_jdbc_driver_mock.return_value = -12350 rcode = ambari_server.check_jdbc_drivers(args)2351 self.assertTrue(exit_mock.called)2352 self.assertTrue(print_error_msg_mock.called)2353 # Non-Silent option, no drivers at first ask, present drivers after that2354 find_jdbc_driver_mock.side_effect = [-1, drivers_list]2355 rcode = ambari_server.check_jdbc_drivers(args)2356 self.assertEqual(0, rcode)2357 copy_files_mock.assert_called_with(drivers_list, resources_dir)2358 # Non-Silent option, no drivers at first ask, present drivers after that2359 find_jdbc_driver_mock.reset()2360 find_jdbc_driver_mock.side_effect = [-1, -1]2361 rcode = ambari_server.check_jdbc_drivers(args)2362 self.assertTrue(exit_mock.called)2363 self.assertTrue(print_error_msg_mock.called)2364 sys.stdout = sys.__stdout__2365 @patch.object(ambari_server, "find_properties_file")2366 def test_get_ambari_properties(self, find_properties_file):2367 find_properties_file.return_value = None2368 rcode = ambari_server.get_ambari_properties()2369 self.assertEqual(rcode, -1)2370 tf1 = tempfile.NamedTemporaryFile()2371 find_properties_file.return_value = tf1.name2372 prop_name='name'2373 prop_value='val'2374 with open(tf1.name, 'w') as fout:2375 fout.write(prop_name + '=' + prop_value)2376 fout.close()2377 properties = ambari_server.get_ambari_properties()2378 self.assertEqual(properties[prop_name], prop_value)2379 self.assertEqual(properties.fileName, os.path.abspath(tf1.name))2380 sys.stdout = sys.__stdout__2381 2382 @patch("os.path.exists")2383 @patch("os.remove")2384 @patch.object(ambari_server,"print_warning_msg")2385 def test_remove_file(self, printWarningMsgMock, removeMock, pathExistsMock):2386 def side_effect():2387 raise Exception(-1, "Failed to delete!")2388 2389 removeMock.side_effect = side_effect2390 pathExistsMock.return_value = 12391 2392 res = ambari_server.remove_file("/someNonExsistantDir/filename")2393 self.assertEquals(res,1)2394 2395 removeMock.side_effect = None2396 res = ambari_server.remove_file("/someExsistantDir/filename")2397 self.assertEquals(res, 0)2398 2399 @patch("shutil.copyfile")2400 def test_copy_file(self, shutilCopyfileMock):2401 def side_effect():2402 raise Exception(-1, "Failed to copy!")2403 2404 shutilCopyfileMock.side_effect = side_effect2405 2406 try:2407 ambari_server.copy_file("/tmp/psswd","/someNonExsistantDir/filename")2408 self.fail("Exception on file not copied has not been thrown!")2409 except FatalException:2410 # Expected2411 pass2412 2413 self.assertTrue(shutilCopyfileMock.called)2414 2415 2416 shutilCopyfileMock.side_effect = None2417 try:2418 ambari_server.copy_file("/tmp/psswd","/root/psswd")2419 except FatalException:2420 self.fail("Exception on file copied should not be thrown!")2421 2422 self.assertTrue(shutilCopyfileMock.called)2423 @patch.object(ambari_server, "get_ambari_properties")2424 @patch.object(ambari_server, "find_jdbc_driver")2425 @patch.object(ambari_server, "copy_files")2426 @patch.object(ambari_server, "print_error_msg")2427 @patch.object(ambari_server, "print_warning_msg")2428 @patch('__builtin__.raw_input')2429 def test_check_jdbc_drivers(self, raw_input_mock, print_warning_msg, print_error_msg_mock, copy_files_mock,2430 find_jdbc_driver_mock, get_ambari_properties_mock):2431 out = StringIO.StringIO()2432 sys.stdout = out2433 args = MagicMock()2434 2435 # Check positive scenario2436 drivers_list = ['driver_file']2437 resources_dir = '/tmp'2438 2439 get_ambari_properties_mock.return_value = {ambari_server.RESOURCES_DIR_PROPERTY : resources_dir}2440 find_jdbc_driver_mock.return_value = drivers_list2441 copy_files_mock.return_value = 02442 2443 args.database = "oracle"2444 2445 rcode = ambari_server.check_jdbc_drivers(args)2446 2447 self.assertEqual(0, rcode)2448 copy_files_mock.assert_called_with(drivers_list, resources_dir)2449 2450 get_ambari_properties_mock.reset_mock()2451 find_jdbc_driver_mock.reset_mock()2452 copy_files_mock.reset_mock()2453 print_error_msg_mock.reset_mock()2454 print_warning_msg.reset_mock()2455 raw_input_mock.reset_mock()2456 2457 2458 2459 # Check negative scenarios2460 # Silent option, no drivers2461 ambari_server.SILENT = True2462 2463 find_jdbc_driver_mock.return_value = -12464 2465 failed = False2466 2467 try:2468 rcode = ambari_server.check_jdbc_drivers(args)2469 except FatalException:2470 failed = True2471 2472 self.assertTrue(print_error_msg_mock.called)2473 self.assertTrue(failed)2474 2475 get_ambari_properties_mock.reset_mock()2476 find_jdbc_driver_mock.reset_mock()2477 copy_files_mock.reset_mock()2478 print_error_msg_mock.reset_mock()2479 print_warning_msg.reset_mock()2480 raw_input_mock.reset_mock()2481 2482 # Non-Silent option, no drivers2483 ambari_server.SILENT = False2484 2485 find_jdbc_driver_mock.return_value = -12486 2487 failed = False2488 2489 try:2490 rcode = ambari_server.check_jdbc_drivers(args)2491 except FatalException:2492 failed = True2493 self.assertTrue(failed)2494 self.assertTrue(print_error_msg_mock.called)2495 2496 get_ambari_properties_mock.reset_mock()2497 find_jdbc_driver_mock.reset_mock()2498 copy_files_mock.reset_mock()2499 print_error_msg_mock.reset_mock()2500 print_warning_msg.reset_mock()2501 raw_input_mock.reset_mock()2502 2503 # Non-Silent option, no drivers at first ask, present drivers after that2504 2505 find_jdbc_driver_mock.side_effect = [-1, drivers_list]2506 2507 rcode = ambari_server.check_jdbc_drivers(args)2508 2509 self.assertEqual(0, rcode)2510 copy_files_mock.assert_called_with(drivers_list, resources_dir)2511 2512 get_ambari_properties_mock.reset_mock()2513 find_jdbc_driver_mock.reset_mock()2514 copy_files_mock.reset_mock()2515 print_error_msg_mock.reset_mock()2516 print_warning_msg.reset_mock()2517 raw_input_mock.reset_mock()2518 2519 # Non-Silent option, no drivers at first ask, no drivers after that2520 find_jdbc_driver_mock.side_effect = [-1, -1]2521 2522 failed = False2523 2524 try:2525 rcode = ambari_server.check_jdbc_drivers(args)2526 except FatalException:2527 failed = True2528 2529 self.assertTrue(failed)2530 self.assertTrue(print_error_msg_mock.called)2531 2532 get_ambari_properties_mock.reset_mock()2533 find_jdbc_driver_mock.reset_mock()2534 copy_files_mock.reset_mock()2535 print_error_msg_mock.reset_mock()2536 print_warning_msg.reset_mock()2537 raw_input_mock.reset_mock()2538 2539 2540 2541 2542 # Failed to copy_files 2543 2544 find_jdbc_driver_mock.side_effect = [drivers_list]2545 try:2546 rcode = ambari_server.check_jdbc_drivers(args)2547 except FatalException:2548 failed = True2549 2550 self.assertTrue(failed)2551 2552 sys.stdout = sys.__stdout__2553 2554 2555 @patch.object(ambari_server, "find_properties_file")2556 def test_get_ambari_properties(self, find_properties_file_mock):2557 find_properties_file_mock.return_value = None2558 rcode = ambari_server.get_ambari_properties()2559 self.assertEqual(rcode, -1)2560 2561 tf1 = tempfile.NamedTemporaryFile()2562 find_properties_file_mock.return_value = tf1.name2563 prop_name='name'2564 prop_value='val'2565 2566 with open(tf1.name, 'w') as fout:2567 fout.write(prop_name + '=' + prop_value)2568 fout.close()2569 properties = ambari_server.get_ambari_properties()2570 2571 self.assertEqual(properties[prop_name], prop_value)2572 self.assertEqual(properties.fileName, os.path.abspath(tf1.name))2573 2574 sys.stdout = sys.__stdout__2575 @patch.object(ambari_server, "find_properties_file")2576 def test_parse_properties_file(self, find_properties_file_mock):2577 tf1 = tempfile.NamedTemporaryFile(mode='r')2578 find_properties_file_mock.return_value = tf1.name2579 args = MagicMock()2580 ambari_server.parse_properties_file(args)2581 self.assertEquals(args.persistence_type, "local")2582 with open(tf1.name, 'w') as fout:2583 fout.write("\n")2584 fout.write(ambari_server.PERSISTENCE_TYPE_PROPERTY+"=remote")2585 args = MagicMock()2586 ambari_server.parse_properties_file(args)2587 self.assertEquals(args.persistence_type, "remote")2588 @patch.object(ambari_server, 'decrypt_password_for_alias')2589 @patch.object(ambari_server, 'is_alias_string')2590 @patch.object(ambari_server, 'get_ambari_properties')2591 def test_configure_database_username_password_masterkey_persisted(self,2592 get_ambari_properties_method, is_alias_string_method,2593 decrypt_password_for_alias_method):2594 out = StringIO.StringIO()2595 sys.stdout = out2596 configs = {ambari_server.JDBC_USER_NAME_PROPERTY: "fakeuser",2597 ambari_server.JDBC_PASSWORD_PROPERTY: "${alias=somealias}",2598 ambari_server.SECURITY_KEY_IS_PERSISTED: "True" }2599 get_ambari_properties_method.return_value = configs2600 is_alias_string_method.return_value = True2601 decrypt_password_for_alias_method.return_value = "falepasswd"2602 args = MagicMock()2603 args.master_key = None2604 ambari_server.configure_database_username_password(args)2605 self.assertTrue(decrypt_password_for_alias_method.called)2606 self.assertTrue(is_alias_string_method.called)2607 self.assertEquals("fakeuser", args.database_username)2608 self.assertEquals("falepasswd", args.database_password)2609 sys.stdout = sys.__stdout__2610 @patch.object(ambari_server, 'read_password')2611 def test_configure_database_password(self, read_password_method):2612 out = StringIO.StringIO()2613 sys.stdout = out2614 read_password_method.return_value = "fakepasswd"2615 result = ambari_server.configure_database_password(True)2616 self.assertTrue(read_password_method.called)2617 self.assertEquals("fakepasswd", result)2618 result = ambari_server.configure_database_password(True)2619 self.assertEquals("fakepasswd", result)2620 result = ambari_server.configure_database_password(True)2621 self.assertEquals("fakepasswd", result)2622 sys.stdout = sys.__stdout__2623 @patch("os.path.exists")2624 @patch.object(ambari_server, 'get_is_secure')2625 @patch.object(ambari_server, 'get_is_persisted')2626 @patch.object(ambari_server, 'remove_password_file')2627 @patch.object(ambari_server, 'save_passwd_for_alias')2628 @patch.object(ambari_server, 'read_master_key')2629 @patch.object(ambari_server, 'read_ambari_user')2630 @patch.object(ambari_server, 'get_master_key_location')2631 @patch.object(ambari_server, 'update_properties')2632 @patch.object(ambari_server, 'save_master_key')2633 @patch.object(ambari_server, 'get_YN_input')2634 @patch.object(ambari_server, 'search_file')2635 @patch.object(ambari_server, 'get_ambari_properties')2636 @patch.object(ambari_server, 'is_root')2637 def test_setup_master_key_not_persist(self, is_root_method,2638 get_ambari_properties_method, search_file_message,2639 get_YN_input_method, save_master_key_method,2640 update_properties_method, get_master_key_location_method,2641 read_ambari_user_method, read_master_key_method,2642 save_passwd_for_alias_method, remove_password_file_method,2643 get_is_persisted_method, get_is_secure_method, exists_mock):2644 is_root_method.return_value = True2645 p = get_ambari_properties_method.return_value2646 p.get_property.side_effect = [ "fakepasswd", "fakepasswd", "fakepasswd", "fakepasswd"]2647 read_master_key_method.return_value = "aaa"2648 get_YN_input_method.return_value = False2649 read_ambari_user_method.return_value = None2650 save_passwd_for_alias_method.return_value = 02651 get_is_persisted_method.return_value = (True, "filepath")2652 get_is_secure_method.return_value = False2653 exists_mock.return_value = False2654 ambari_server.setup_master_key()2655 self.assertTrue(get_YN_input_method.called)2656 self.assertTrue(read_master_key_method.called)2657 self.assertTrue(read_ambari_user_method.called)2658 self.assertTrue(update_properties_method.called)2659 self.assertFalse(save_master_key_method.called)2660 self.assertTrue(save_passwd_for_alias_method.called)2661 self.assertEquals(3, save_passwd_for_alias_method.call_count)2662 self.assertTrue(remove_password_file_method.called)2663 result_expected = {ambari_server.JDBC_PASSWORD_PROPERTY :2664 ambari_server.get_alias_string(ambari_server.JDBC_RCA_PASSWORD_ALIAS),2665 ambari_server.JDBC_RCA_PASSWORD_FILE_PROPERTY:2666 ambari_server.get_alias_string(ambari_server.JDBC_RCA_PASSWORD_ALIAS),2667 ambari_server.LDAP_MGR_PASSWORD_PROPERTY :2668 ambari_server.get_alias_string(ambari_server.LDAP_MGR_PASSWORD_ALIAS),2669 ambari_server.SSL_TRUSTSTORE_PASSWORD_PROPERTY :2670 ambari_server.get_alias_string(ambari_server.SSL_TRUSTSTORE_PASSWORD_ALIAS),2671 ambari_server.SECURITY_IS_ENCRYPTION_ENABLED : 'true'}2672 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))2673 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),2674 key=operator.itemgetter(0))2675 self.assertEquals(sorted_x, sorted_y)2676 @patch.object(ambari_server, 'save_passwd_for_alias')2677 @patch("os.path.exists")2678 @patch.object(ambari_server, 'get_is_secure')2679 @patch.object(ambari_server, 'get_is_persisted')2680 @patch.object(ambari_server, 'read_master_key')2681 @patch.object(ambari_server, 'read_ambari_user')2682 @patch.object(ambari_server, 'get_master_key_location')2683 @patch.object(ambari_server, 'update_properties')2684 @patch.object(ambari_server, 'save_master_key')2685 @patch.object(ambari_server, 'get_YN_input')2686 @patch.object(ambari_server, 'search_file')2687 @patch.object(ambari_server, 'get_ambari_properties')2688 @patch.object(ambari_server, 'is_root')2689 def test_setup_master_key_persist(self, is_root_method,2690 get_ambari_properties_method, search_file_message,2691 get_YN_input_method, save_master_key_method,2692 update_properties_method, get_master_key_location_method,2693 read_ambari_user_method, read_master_key_method,2694 get_is_persisted_method, get_is_secure_method, exists_mock,2695 save_passwd_for_alias_method):2696 is_root_method.return_value = True2697 p = get_ambari_properties_method.return_value2698 p.get_property.side_effect = [ "fakepasswd", None, None, None ]2699 read_master_key_method.return_value = "aaa"2700 get_YN_input_method.side_effect = [True, False]2701 read_ambari_user_method.return_value = None2702 get_is_persisted_method.return_value = (True, "filepath")2703 get_is_secure_method.return_value = False2704 exists_mock.return_value = False2705 save_passwd_for_alias_method.return_value = 02706 ambari_server.setup_master_key()2707 self.assertTrue(get_YN_input_method.called)2708 self.assertTrue(read_master_key_method.called)2709 self.assertTrue(read_ambari_user_method.called)2710 self.assertTrue(update_properties_method.called)2711 self.assertTrue(save_master_key_method.called)2712 result_expected = {ambari_server.JDBC_PASSWORD_PROPERTY:2713 ambari_server.get_alias_string(ambari_server.JDBC_RCA_PASSWORD_ALIAS),2714 ambari_server.SECURITY_IS_ENCRYPTION_ENABLED: 'true'}2715 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))2716 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),2717 key=operator.itemgetter(0))2718 self.assertEquals(sorted_x, sorted_y)2719 @patch.object(ambari_server, 'read_master_key')2720 @patch.object(ambari_server, 'remove_password_file')2721 @patch("os.path.exists")2722 @patch.object(ambari_server, 'read_ambari_user')2723 @patch.object(ambari_server, 'get_master_key_location')2724 @patch("ambari-server.Properties")2725 @patch.object(ambari_server, 'save_passwd_for_alias')2726 @patch.object(ambari_server, 'read_passwd_for_alias')2727 @patch.object(ambari_server, 'update_properties')2728 @patch.object(ambari_server, 'save_master_key')2729 @patch.object(ambari_server, 'get_validated_string_input')2730 @patch.object(ambari_server, 'get_YN_input')2731 @patch.object(ambari_server, 'search_file')2732 @patch.object(ambari_server, 'get_ambari_properties')2733 @patch.object(ambari_server, 'is_root')2734 def test_reset_master_key_persisted(self, is_root_method,2735 get_ambari_properties_method, search_file_message,2736 get_YN_input_method, get_validated_string_input_method,2737 save_master_key_method, update_properties_method,2738 read_passwd_for_alias_method, save_passwd_for_alias_method,2739 Properties_mock, get_master_key_location_method,2740 read_ambari_user_method, exists_mock,2741 remove_password_file_method, read_master_key_method):2742 # Testing call under non-root2743 is_root_method.return_value = False2744 try:2745 ambari_server.setup_master_key()2746 self.fail("Should throw exception")2747 except FatalException as fe:2748 # Expected2749 self.assertTrue("root-level" in fe.reason)2750 pass2751 # Testing call under root2752 is_root_method.return_value = True2753 search_file_message.return_value = "filepath"2754 read_ambari_user_method.return_value = None2755 p = get_ambari_properties_method.return_value2756 p.get_property.side_effect = [ 'true', '${alias=fakealias}',2757 '${alias=fakealias}',2758 '${alias=fakealias}', '${alias=fakealias}']2759 get_YN_input_method.side_effect = [ True, True ]2760 read_master_key_method.return_value = "aaa"2761 read_passwd_for_alias_method.return_value = "fakepassword"2762 save_passwd_for_alias_method.return_value = 02763 exists_mock.return_value = False2764 ambari_server.setup_master_key()2765 self.assertTrue(save_master_key_method.called)2766 self.assertTrue(get_YN_input_method.called)2767 self.assertTrue(read_master_key_method.called)2768 self.assertTrue(update_properties_method.called)2769 self.assertTrue(read_passwd_for_alias_method.called)2770 self.assertTrue(3, read_passwd_for_alias_method.call_count)2771 self.assertTrue(3, save_passwd_for_alias_method.call_count)2772 result_expected = {ambari_server.JDBC_PASSWORD_PROPERTY:2773 ambari_server.get_alias_string(ambari_server.JDBC_RCA_PASSWORD_ALIAS),2774 ambari_server.JDBC_RCA_PASSWORD_FILE_PROPERTY:2775 ambari_server.get_alias_string(ambari_server.JDBC_RCA_PASSWORD_ALIAS),2776 ambari_server.LDAP_MGR_PASSWORD_PROPERTY:2777 ambari_server.get_alias_string(ambari_server.LDAP_MGR_PASSWORD_ALIAS),2778 ambari_server.SSL_TRUSTSTORE_PASSWORD_PROPERTY:2779 ambari_server.get_alias_string(ambari_server.SSL_TRUSTSTORE_PASSWORD_ALIAS),2780 ambari_server.SECURITY_IS_ENCRYPTION_ENABLED: 'true'}2781 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))2782 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),2783 key=operator.itemgetter(0))2784 self.assertEquals(sorted_x, sorted_y)2785 @patch.object(ambari_server, 'get_is_persisted')2786 @patch.object(ambari_server, 'get_is_secure')2787 @patch.object(ambari_server, 'remove_password_file')2788 @patch("os.path.exists")2789 @patch.object(ambari_server, 'read_ambari_user')2790 @patch.object(ambari_server, 'get_master_key_location')2791 @patch("ambari-server.Properties")2792 @patch.object(ambari_server, 'save_passwd_for_alias')2793 @patch.object(ambari_server, 'read_passwd_for_alias')2794 @patch.object(ambari_server, 'update_properties')2795 @patch.object(ambari_server, 'save_master_key')2796 @patch.object(ambari_server, 'get_validated_string_input')2797 @patch.object(ambari_server, 'get_YN_input')2798 @patch.object(ambari_server, 'search_file')2799 @patch.object(ambari_server, 'get_ambari_properties')2800 @patch.object(ambari_server, 'is_root')2801 def test_reset_master_key_not_persisted(self, is_root_method,2802 get_ambari_properties_method,2803 search_file_message, get_YN_input_method,2804 get_validated_string_input_method, save_master_key_method,2805 update_properties_method, read_passwd_for_alias_method,2806 save_passwd_for_alias_method, Properties_mock,2807 get_master_key_location_method, read_ambari_user_method,2808 exists_mock, remove_password_file_method, get_is_secure_method,2809 get_is_persisted_method):2810 is_root_method.return_value = True2811 search_file_message.return_value = False2812 read_ambari_user_method.return_value = None2813 p = get_ambari_properties_method.return_value2814 p.get_property.side_effect = [ '${alias=fakealias}', '${alias=fakealias}',2815 '${alias=fakealias}', '${alias=fakealias}']2816 get_YN_input_method.side_effect = [ True, False ]2817 get_validated_string_input_method.return_value = "aaa"2818 read_passwd_for_alias_method.return_value = "fakepassword"2819 save_passwd_for_alias_method.return_value = 02820 exists_mock.return_value = False2821 get_is_secure_method.return_value = True2822 get_is_persisted_method.return_value = (True, "filePath")2823 ambari_server.setup_master_key()2824 self.assertFalse(save_master_key_method.called)2825 self.assertTrue(get_YN_input_method.called)2826 self.assertTrue(get_validated_string_input_method.called)2827 self.assertTrue(update_properties_method.called)2828 self.assertTrue(read_passwd_for_alias_method.called)2829 self.assertTrue(3, read_passwd_for_alias_method.call_count)2830 self.assertTrue(3, save_passwd_for_alias_method.call_count)2831 self.assertFalse(save_master_key_method.called)2832 result_expected = {ambari_server.JDBC_PASSWORD_PROPERTY:2833 ambari_server.get_alias_string(ambari_server.JDBC_RCA_PASSWORD_ALIAS),2834 ambari_server.JDBC_RCA_PASSWORD_FILE_PROPERTY:2835 ambari_server.get_alias_string(ambari_server.JDBC_RCA_PASSWORD_ALIAS),2836 ambari_server.LDAP_MGR_PASSWORD_PROPERTY:2837 ambari_server.get_alias_string(ambari_server.LDAP_MGR_PASSWORD_ALIAS),2838 ambari_server.SSL_TRUSTSTORE_PASSWORD_PROPERTY:2839 ambari_server.get_alias_string(ambari_server.SSL_TRUSTSTORE_PASSWORD_ALIAS),2840 ambari_server.SECURITY_IS_ENCRYPTION_ENABLED: 'true'}2841 sorted_x = sorted(result_expected.iteritems(), key=operator.itemgetter(0))2842 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),2843 key=operator.itemgetter(0))2844 self.assertEquals(sorted_x, sorted_y)2845 @patch('__builtin__.raw_input')2846 @patch.object(ambari_server, 'get_is_secure')2847 @patch.object(ambari_server, 'get_YN_input')2848 @patch.object(ambari_server, 'update_properties')2849 @patch.object(ambari_server, 'search_file')2850 @patch.object(ambari_server, 'get_ambari_properties')2851 @patch.object(ambari_server, 'is_root')2852 def test_setup_ldap_invalid_input(self, is_root_method, get_ambari_properties_method,2853 search_file_message,2854 update_properties_method,2855 get_YN_input_method,2856 get_is_secure_method,2857 raw_input_mock):2858 out = StringIO.StringIO()2859 sys.stdout = out2860 is_root_method.return_value = True2861 search_file_message.return_value = "filepath"2862 configs = { ambari_server.SECURITY_MASTER_KEY_LOCATION : "filepath",2863 ambari_server.SECURITY_KEYS_DIR : tempfile.gettempdir(),2864 ambari_server.SECURITY_IS_ENCRYPTION_ENABLED : "true"2865 }2866 get_ambari_properties_method.return_value = configs2867 raw_input_mock.side_effect = ['a:3', 'b:b', 'host', 'b:2', 'false', 'uid', 'base', 'true']2868 ambari_server.SILENT = False2869 get_YN_input_method.return_value = True2870 ambari_server.setup_ldap()2871 ldap_properties_map = \2872 {2873 "authentication.ldap.primaryUrl" : "a:3",2874 "authentication.ldap.secondaryUrl" : "b:2",2875 "authentication.ldap.useSSL" : "false",2876 "authentication.ldap.usernameAttribute" : "uid",2877 "authentication.ldap.baseDn" : "base",2878 "authentication.ldap.bindAnonymously" : "true",2879 "client.security" : "ldap"2880 }2881 sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))2882 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),2883 key=operator.itemgetter(0))2884 self.assertEquals(sorted_x, sorted_y)2885 self.assertTrue(get_YN_input_method.called)2886 self.assertTrue(8, raw_input_mock.call_count)2887 raw_input_mock.reset_mock()2888 raw_input_mock.side_effect = ['a:3', '', 'b:2', 'false', 'uid', 'base', 'true']2889 ambari_server.setup_ldap()2890 ldap_properties_map = \2891 {2892 "authentication.ldap.primaryUrl" : "a:3",2893 "authentication.ldap.useSSL" : "false",2894 "authentication.ldap.usernameAttribute" : "uid",2895 "authentication.ldap.baseDn" : "base",2896 "authentication.ldap.bindAnonymously" : "true",2897 "client.security" : "ldap"2898 }2899 sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))2900 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),2901 key=operator.itemgetter(0))2902 self.assertEquals(sorted_x, sorted_y)2903 self.assertTrue(5, raw_input_mock.call_count)2904 sys.stdout = sys.__stdout__2905 @patch.object(ambari_server, 'get_is_secure')2906 @patch.object(ambari_server, 'encrypt_password')2907 @patch.object(ambari_server, 'save_passwd_for_alias')2908 @patch.object(ambari_server, 'get_YN_input')2909 @patch.object(ambari_server, 'update_properties')2910 @patch.object(ambari_server, 'configure_ldap_password')2911 @patch.object(ambari_server, 'get_validated_string_input')2912 @patch.object(ambari_server, 'setup_master_key')2913 @patch.object(ambari_server, 'search_file')2914 @patch.object(ambari_server, 'get_ambari_properties')2915 @patch.object(ambari_server, 'is_root')2916 def test_setup_ldap(self, is_root_method, get_ambari_properties_method,2917 search_file_message, setup_master_key_method,2918 get_validated_string_input_method,2919 configure_ldap_password_method, update_properties_method,2920 get_YN_input_method, save_passwd_for_alias_method,2921 encrypt_password_method, get_is_secure_method):2922 out = StringIO.StringIO()2923 sys.stdout = out2924 # Testing call under non-root2925 is_root_method.return_value = False2926 try:2927 ambari_server.setup_ldap()2928 self.fail("Should throw exception")2929 except FatalException as fe:2930 # Expected2931 self.assertTrue("root-level" in fe.reason)2932 pass2933 # Testing call under root2934 is_root_method.return_value = True2935 search_file_message.return_value = "filepath"2936 configs = { ambari_server.SECURITY_MASTER_KEY_LOCATION : "filepath",2937 ambari_server.SECURITY_KEYS_DIR : tempfile.gettempdir(),2938 ambari_server.SECURITY_IS_ENCRYPTION_ENABLED : "true"2939 }2940 get_ambari_properties_method.return_value = configs2941 configure_ldap_password_method.return_value = "password"2942 setup_master_key_method.return_value = (None, True, True)2943 save_passwd_for_alias_method.return_value = 02944 encrypt_password_method.return_value = ambari_server.get_alias_string(2945 ambari_server.LDAP_MGR_PASSWORD_ALIAS)2946 def yn_input_side_effect(*args, **kwargs):2947 if 'TrustStore' in args[0]:2948 return False2949 else:2950 return True2951 #get_YN_input_method.side_effect = yn_input_side_effect()2952 get_YN_input_method.side_effect = [ True, ]2953 def valid_input_side_effect(*args, **kwargs):2954 if 'Bind anonymously' in args[0]:2955 return 'false'2956 if args[1] == "true" or args[1] == "false":2957 return args[1]2958 else:2959 return "test"2960 get_validated_string_input_method.side_effect = valid_input_side_effect2961 ambari_server.setup_ldap()2962 ldap_properties_map =\2963 {2964 "authentication.ldap.primaryUrl" : "test",2965 "authentication.ldap.secondaryUrl" : "test",2966 "authentication.ldap.useSSL" : "false",2967 "authentication.ldap.usernameAttribute" : "test",2968 "authentication.ldap.baseDn" : "test",2969 "authentication.ldap.bindAnonymously" : "false",2970 "authentication.ldap.managerDn" : "test",2971 "client.security" : "ldap",\2972 ambari_server.LDAP_MGR_PASSWORD_PROPERTY : ambari_server.get_alias_string(\2973 ambari_server.LDAP_MGR_PASSWORD_ALIAS)2974 }2975 sorted_x = sorted(ldap_properties_map.iteritems(), key=operator.itemgetter(0))2976 sorted_y = sorted(update_properties_method.call_args[0][1].iteritems(),2977 key=operator.itemgetter(0))2978 self.assertEquals(sorted_x, sorted_y)2979 self.assertTrue(update_properties_method.called)2980 self.assertTrue(configure_ldap_password_method.called)2981 self.assertTrue(get_validated_string_input_method.called)2982 self.assertTrue(get_YN_input_method.called)2983 sys.stdout = sys.__stdout__2984 @patch.object(ambari_server, 'read_password')2985 def test_configure_ldap_password(self, read_password_method):2986 out = StringIO.StringIO()2987 sys.stdout = out2988 read_password_method.return_value = "blah"2989 ambari_server.configure_ldap_password()2990 self.assertTrue(read_password_method.called)2991 sys.stdout = sys.__stdout__2992 @patch.object(ambari_server, 'get_validated_string_input')2993 def test_read_password(self, get_validated_string_input_method):2994 out = StringIO.StringIO()2995 sys.stdout = out2996 passwordDefault = ""2997 passwordPrompt = 'Enter Manager Password* : '2998 passwordPattern = ".*"2999 passwordDescr = "Invalid characters in password."3000 get_validated_string_input_method.side_effect = ['', 'aaa', 'aaa']3001 password = ambari_server.read_password(passwordDefault, passwordPattern,3002 passwordPrompt, passwordDescr)3003 self.assertTrue(3, get_validated_string_input_method.call_count)3004 self.assertEquals('aaa', password)3005 get_validated_string_input_method.reset_mock()3006 get_validated_string_input_method.side_effect = ['aaa', 'aaa']3007 password = ambari_server.read_password(passwordDefault, passwordPattern,3008 passwordPrompt, passwordDescr)3009 self.assertTrue(2, get_validated_string_input_method.call_count)3010 self.assertEquals('aaa', password)3011 get_validated_string_input_method.reset_mock()3012 get_validated_string_input_method.side_effect = ['aaa']3013 password = ambari_server.read_password('aaa', passwordPattern,3014 passwordPrompt, passwordDescr)3015 self.assertTrue(1, get_validated_string_input_method.call_count)3016 self.assertEquals('aaa', password)3017 sys.stdout = sys.__stdout__3018 def test_generate_random_string(self):3019 random_str_len = 1003020 str1 = ambari_server.generate_random_string(random_str_len)3021 self.assertTrue(len(str1) == random_str_len)3022 3023 str2 = ambari_server.generate_random_string(random_str_len)3024 self.assertTrue(str1 != str2)3025 @patch("__builtin__.open")3026 @patch.object(ambari_server, "search_file")3027 @patch.object(ambari_server, "backup_file_in_temp")3028 def test_update_properties(self, backup_file_in_temp_mock, search_file_mock, open_mock):3029 conf_file = "ambari.properties"3030 propertyMap = {"1":"1", "2":"2"}3031 properties = MagicMock()3032 f = MagicMock(name = "file")3033 # f.__enter__.return_value = f #mimic file behavior3034 search_file_mock.return_value = conf_file3035 open_mock.return_value = f3036 ambari_server.update_properties(properties, propertyMap)3037 properties.store.assert_called_with(f.__enter__.return_value)3038 backup_file_in_temp_mock.assert_called_with(conf_file)3039 self.assertEquals(2, properties.removeOldProp.call_count)3040 self.assertEquals(2, properties.process_pair.call_count)3041 properties = MagicMock()3042 backup_file_in_temp_mock.reset_mock()3043 open_mock.reset_mock()3044 ambari_server.update_properties(properties, None)3045 properties.store.assert_called_with(f.__enter__.return_value)3046 backup_file_in_temp_mock.assert_called_with(conf_file)3047 self.assertFalse(properties.removeOldProp.called)3048 self.assertFalse(properties.process_pair.called)3049 pass3050 def test_regexps(self):3051 res = re.search(ambari_server.REGEX_HOSTNAME_PORT, "")3052 self.assertTrue(res is None)3053 res = re.search(ambari_server.REGEX_HOSTNAME_PORT, "ddd")3054 self.assertTrue(res is None)3055 res = re.search(ambari_server.REGEX_HOSTNAME_PORT, "gg:ff")3056 self.assertTrue(res is None)3057 res = re.search(ambari_server.REGEX_HOSTNAME_PORT, "gg:55444325")3058 self.assertTrue(res is None)3059 res = re.search(ambari_server.REGEX_HOSTNAME_PORT, "gg:555")3060 self.assertTrue(res is not None)3061 res = re.search(ambari_server.REGEX_TRUE_FALSE, "")3062 self.assertTrue(res is not None)3063 res = re.search(ambari_server.REGEX_TRUE_FALSE, "t")3064 self.assertTrue(res is None)3065 res = re.search(ambari_server.REGEX_TRUE_FALSE, "trrrr")3066 self.assertTrue(res is None)3067 res = re.search(ambari_server.REGEX_TRUE_FALSE, "true|false")3068 self.assertTrue(res is None)3069 res = re.search(ambari_server.REGEX_TRUE_FALSE, "true")3070 self.assertTrue(res is not None)3071 res = re.search(ambari_server.REGEX_TRUE_FALSE, "false")3072 self.assertTrue(res is not None)3073 res = re.search(ambari_server.REGEX_ANYTHING, "")3074 self.assertTrue(res is not None)3075 res = re.search(ambari_server.REGEX_ANYTHING, "t")3076 self.assertTrue(res is not None)3077 res = re.search(ambari_server.REGEX_ANYTHING, "trrrr")3078 self.assertTrue(res is not None)3079 def get_sample(self, sample):3080 """3081 Returns sample file content as string with normalized line endings3082 """3083 path = self.get_samples_dir(sample)3084 return self.get_file_string(path)3085 def get_file_string(self, file):3086 """3087 Returns file content as string with normalized line endings3088 """3089 string = open(file, 'r').read()3090 return self.normalize(string)3091 def normalize(self, string):3092 """3093 Normalizes line ending in string according to platform-default encoding3094 """3095 return string.replace("\n", os.linesep)3096 def get_samples_dir(self, sample):3097 """3098 Returns full file path by sample name3099 """3100 testdir = os.path.dirname(__file__)3101 return os.path.dirname(testdir) + os.sep + "resources" + os.sep \...
test_plugin.py
Source:test_plugin.py
1########2# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# * See the License for the specific language governing permissions and14# * limitations under the License.15from os import path16import unittest17import re18from cloudify.test_utils import workflow_test19from plugin.srv_graph.graph_element import ServiceGraphElement20from plugin.srv_graph.graph_element import ComponentElement21from plugin.srv_graph.graph_element import ComponentDependencyElement22from plugin.srv_graph.graph_element import ComponentFactory23from plugin.srv_graph.graph_element import ComponentFactoryFacade24from plugin.srv_graph.graph_builder import GraphBuilder25from plugin.api.client_facade_api import ARCADIAClientFacade26from plugin.srv_graph.pretty_printer import DefaultXMLPrinter27from plugin.srv_graph.pretty_printer import ARCADIAXMLPrinter28from plugin.tests.mocks.nodes import CloudifyWorlkflowNodeInstanceMock29from plugin.tests.mocks.nodes import CloudifyWorkflowRelationshipInstanceMock30from plugin.tests.mocks.client import ARCADIAClientMock31from plugin.context import actx32from plugin.tests.utils.xml_compare import XmlTree33def flatten_str(string):34 return re.sub('[\n\s\t]', '', string)35class TestPlugin(unittest.TestCase):36 def setUp(self):37 actx._components = dict()38 actx._relationships = dict()39 actx._service_graph_instance = None40 actx._client = None41 actx._service_graph_tree = None42 @workflow_test(path.join('blueprint', 'blueprint.yaml'),43 resources_to_copy=[path.join('blueprint',44 'test_plugin.yaml')])45 def test_install_arcadia_workflow(self, cfy_local):46 expected_result = '''47 <ServiceGraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ArcadiaModellingArtefacts.xsd">48 <DescriptiveSGMetadata>49 <SGID>wordpress_mysql_service_graph_id</SGID>50 <SGName>SimpleWordPressServiceGraph</SGName>51 <SGDescription>SGDescription</SGDescription>52 </DescriptiveSGMetadata>53 <GraphNodeDescriptor>54 <GraphNode>55 <NID>graph_node_mysql_id</NID>56 <CNID>mysql_id</CNID>57 </GraphNode>58 <GraphNode>59 <NID>graph_node_wordpress_id</NID>60 <CNID>wordpress_id</CNID>61 <GraphDependency>62 <CEPCID>mysqltcp_cepcid</CEPCID>63 <ECEPID>mysqltcp</ECEPID>64 <NID>NID</NID>65 </GraphDependency>66 </GraphNode>67 </GraphNodeDescriptor>68 <RuntimePolicyDescriptor>69 <RuntimePolicy>70 <RPID>RPID</RPID>71 <RPName>RPName</RPName>72 </RuntimePolicy>73 </RuntimePolicyDescriptor>74 </ServiceGraph>'''75 actx.client = ARCADIAClientMock()76 77 cfy_local.execute('install_arcadia', task_retries=0)78 service_graph = actx.client._service_graph_tree79 self.assertTrue(isinstance(service_graph, ServiceGraphElement))80 self.assertTrue(len(service_graph.components) == 2)81 self.assertTrue(len(service_graph.policies) == 1)82 def find_component(array, component):83 for comp in array:84 if comp._instance._node_instance['name'] == component:85 return comp86 return87 mysql_comp = find_component(service_graph.components, 'mysql')88 wp_comp = find_component(service_graph.components, 'wordpress')89 self.assertTrue(mysql_comp)90 self.assertTrue(wp_comp)91 self.assertTrue(len(wp_comp.dependencies) == 1)92 self.assertTrue(wp_comp.dependencies[0].target._instance == mysql_comp._instance)93 expected_root = XmlTree.convert_string_to_tree(expected_result)94 actual_root = XmlTree.convert_string_to_tree(actx.client._service_graph_printed)95 self.assertTrue(XmlTree().xml_isomorphic(expected_root, actual_root))96 def test_pretty_print(self):97 expected_result = '''98 <service_graph> 99 service_graph100 <graph_node>101 mysql102 </graph_node>103 <graph_node>104 wordpress105 <graph_node_dependency>106 mysql-wordpress107 </graph_node_dependency>108 </graph_node>109 </service_graph>110 '''111 default_pretty_printer = DefaultXMLPrinter()112 service_graph = ServiceGraphElement(_instance="service_graph")113 mysql = ComponentElement(_instance="mysql")114 wordpress = ComponentElement(_instance="wordpress")115 dependency = ComponentDependencyElement(mysql, wordpress, "wordpress_to_mysql_realationship")116 wordpress.add_dependency(dependency)117 service_graph.add_component(mysql)118 service_graph.add_component(wordpress)119 result = service_graph.print_element(default_pretty_printer)120 self.assertEqual(flatten_str(result), flatten_str(expected_result))121 def test_graph_node_print(self):122 expected_rusult = '''123 <GraphNode>124 <NID>graph_node_mysql_id</NID>125 <CNID>mysql_id</CNID>126 </GraphNode> 127 '''128 mock_instance = CloudifyWorlkflowNodeInstanceMock()129 mock_instance._node_instance.runtime_properties['nid'] = 'graph_node_mysql_id'130 mock_instance._node_instance.runtime_properties['cnid'] = 'mysql_id'131 pretty_printer = ARCADIAXMLPrinter()132 ComponentFactoryFacade.set_factory(ComponentFactory())133 mysql = ComponentFactoryFacade.INSTANCE.create_component(mock_instance)134 result = mysql.print_element(pretty_printer)135 self.assertEqual(flatten_str(result), flatten_str(expected_rusult))136 def test_graph_node_dependency_print(self):137 expected_rusult = '''138 <GraphDependency>139 <CEPCID>mysqltcp_cepcid</CEPCID>140 <ECEPID>mysqltcp</ECEPID>141 <NID>NID</NID>142 </GraphDependency>143 '''144 wrap_comp_type = ['cloudify.nodes.Root', 'cloudify.nodes.SoftwareComponent', 'cloudify.arcadia.nodes.WrappedComponent']145 mock_instance = CloudifyWorlkflowNodeInstanceMock()146 mock_instance._node_instance.runtime_properties['nid'] = 'graph_node_mysql_id'147 mock_instance._node_instance.runtime_properties['cnid'] = 'mysql_id'148 mock_instance._node_instance.runtime_properties['cepcid'] = 'mysqltcp_cepcid'149 mock_instance._node_instance.runtime_properties['ecepid'] = 'mysqltcp'150 con_to_type = ['cloudify.relationships.depends_on', 'cloudify.relationships.connected_to', 151 'cloudify.arcadia.relationships.connected_to', 'wordpress_connected_to_mysql']152 mock_relationship = CloudifyWorkflowRelationshipInstanceMock(node_instance = mock_instance, 153 relationship_instance = {154 'runtime_properties' : {'nid': 'NID'}155 },156 type_hierarchy = con_to_type)157 158 ComponentFactoryFacade.set_factory(ComponentFactory())159 mysql_cmp = ComponentFactoryFacade.INSTANCE.create_component(_instance = mock_instance)160 mysql_dependency = ComponentFactoryFacade.INSTANCE.create_component_dependency(_instance = mock_relationship, _target = mysql_cmp)161 result = mysql_dependency.print_element(ARCADIAXMLPrinter())162 self.assertEqual(flatten_str(result), flatten_str(expected_rusult))163 def test_graph_with_dependencies(self):164 expected_rusult = '''165 <GraphNode>166 <NID>graph_node_wordpress_id</NID>167 <CNID>wordpress_id</CNID>168 <GraphDependency>169 <CEPCID>mysqltcp_cepcid</CEPCID>170 <ECEPID>mysqltcp</ECEPID>171 <NID>NID</NID>172 </GraphDependency>173 </GraphNode>174 '''175 wrap_comp_type = ['cloudify.nodes.Root', 'cloudify.nodes.SoftwareComponent', 'cloudify.arcadia.nodes.WrappedComponent']176 mock_wordpress = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = wrap_comp_type)177 mock_wordpress._node_instance.runtime_properties['nid'] = 'graph_node_wordpress_id'178 mock_wordpress._node_instance.runtime_properties['cnid'] = 'wordpress_id'179 mock_msql = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = wrap_comp_type)180 mock_msql._node_instance.runtime_properties['nid'] = 'graph_node_mysql_id'181 mock_msql._node_instance.runtime_properties['cnid'] = 'mysql_id'182 mock_msql._node_instance.runtime_properties['cepcid'] = 'mysqltcp_cepcid'183 mock_msql._node_instance.runtime_properties['ecepid'] = 'mysqltcp'184 con_to_type = ['cloudify.relationships.depends_on', 'cloudify.relationships.connected_to', 185 'cloudify.arcadia.relationships.connected_to', 'wordpress_connected_to_mysql']186 mock_relationship = CloudifyWorkflowRelationshipInstanceMock(node_instance = mock_msql, 187 relationship_instance = {188 'runtime_properties' : {'nid': 'NID'}189 },190 type_hierarchy = con_to_type)191 ComponentFactoryFacade.set_factory(ComponentFactory())192 wp_cmp = ComponentFactoryFacade.INSTANCE.create_component(_instance = mock_wordpress)193 mysql_cmp = ComponentFactoryFacade.INSTANCE.create_component(_instance = mock_msql)194 mysql_dep = ComponentFactoryFacade.INSTANCE.create_component_dependency(_instance = mock_relationship, _source = wp_cmp, _target = mysql_cmp)195 wp_cmp.add_dependency(mysql_dep)196 result = wp_cmp.print_element(ARCADIAXMLPrinter())197 self.assertEqual(flatten_str(result), flatten_str(expected_rusult))198 def test_complete_service_graph(self):199 expected_rusult = '''200 <ServiceGraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ArcadiaModellingArtefacts.xsd">201 <DescriptiveSGMetadata>202 <SGID>wordpress_mysql_service_graph_id</SGID>203 <SGName>SimpleWordPressServiceGraph</SGName>204 <SGDescription>SGDescription</SGDescription>205 </DescriptiveSGMetadata>206 <GraphNodeDescriptor>207 <GraphNode>208 <NID>graph_node_mysql_id</NID>209 <CNID>mysql_id</CNID>210 </GraphNode>211 <GraphNode>212 <NID>graph_node_wordpress_id</NID>213 <CNID>wordpress_id</CNID>214 <GraphDependency>215 <CEPCID>mysqltcp_cepcid</CEPCID>216 <ECEPID>mysqltcp</ECEPID>217 <NID>NID</NID>218 </GraphDependency>219 </GraphNode>220 </GraphNodeDescriptor>221 <RuntimePolicyDescriptor>222 <RuntimePolicy>223 <RPID>RPID</RPID>224 <RPName>RPName</RPName>225 </RuntimePolicy>226 </RuntimePolicyDescriptor>227 </ServiceGraph> 228 '''229 srv_graph_type = ['cloudify.nodes.Root', 'cloudify.arcadia.nodes.ServiceGraph']230 runtime_type = ['cloudify.nodes.Root', 'cloudify.arcadia.nodes.RuntimePolicy']231 wrap_comp_type = ['cloudify.nodes.Root', 'cloudify.nodes.SoftwareComponent', 'cloudify.arcadia.nodes.WrappedComponent']232 sg_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = srv_graph_type)233 sg_mock._node_instance.runtime_properties['sgid'] = 'wordpress_mysql_service_graph_id'234 sg_mock._node_instance.runtime_properties['sgname'] = 'SimpleWordPressServiceGraph'235 sg_mock._node_instance.runtime_properties['sgdesc'] = 'SGDescription'236 wp_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = wrap_comp_type)237 wp_mock._node_instance.runtime_properties['nid'] = 'graph_node_wordpress_id'238 wp_mock._node_instance.runtime_properties['cnid'] = 'wordpress_id'239 mysql_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = wrap_comp_type)240 mysql_mock._node_instance.runtime_properties['nid'] = 'graph_node_mysql_id'241 mysql_mock._node_instance.runtime_properties['cnid'] = 'mysql_id'242 mysql_mock._node_instance.runtime_properties['cepcid'] = 'mysqltcp_cepcid'243 mysql_mock._node_instance.runtime_properties['ecepid'] = 'mysqltcp'244 rp_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = runtime_type)245 rp_mock._node_instance.runtime_properties['rpid'] = 'RPID'246 rp_mock._node_instance.runtime_properties['rpname'] = 'RPName'247 sg_mock._contained_instances.append(mysql_mock)248 sg_mock._contained_instances.append(wp_mock)249 sg_mock._contained_instances.append(rp_mock)250 sg_mock._node_instance['id'] = 'service_graph_gm17g6'251 mysql_mock._node_instance['id'] = 'mysql_x1m8sh'252 wp_mock._node_instance['id'] = 'wordpress_k8lr3c'253 rp_mock._node_instance['id'] = 'runtime_policy_7vy2nn'254 con_in_type = ['cloudify.relationships.contained_in', 'cloudify.arcadia.relationships.contained_in']255 con_to_type = ['cloudify.relationships.depends_on', 'cloudify.relationships.connected_to', 256 'cloudify.arcadia.relationships.connected_to', 'wordpress_connected_to_mysql']257 mysql_mock._relationship_instances['service_graph_gm17g6'] = CloudifyWorkflowRelationshipInstanceMock(258 node_instance = sg_mock,259 type_hierarchy = con_in_type)260 wp_mock._relationship_instances['service_graph_gm17g6'] = CloudifyWorkflowRelationshipInstanceMock(261 node_instance = sg_mock, type_hierarchy = con_in_type)262 wp_mock._relationship_instances['mysql_x1m8sh'] = CloudifyWorkflowRelationshipInstanceMock(263 node_instance = mysql_mock, 264 relationship_instance = {265 'runtime_properties' : {'nid': 'NID'} 266 },267 type_hierarchy = con_to_type)268 rp_mock._relationship_instances['service_graph_gm17g6'] = CloudifyWorkflowRelationshipInstanceMock(269 node_instance = sg_mock,270 type_hierarchy = con_in_type)271 graph_builder = GraphBuilder(_comp_factory = ComponentFactory())272 service_graph = graph_builder.build(sg_mock)273 result = service_graph.print_element(ARCADIAXMLPrinter())274 self.assertEqual(flatten_str(result), flatten_str(expected_rusult))275 def test_service_graph_builder(self):276 srv_graph_type = ['cloudify.nodes.Root', 'cloudify.arcadia.nodes.ServiceGraph']277 runtime_type = ['cloudify.nodes.Root', 'cloudify.arcadia.nodes.RuntimePolicy']278 wrap_comp_type = ['cloudify.nodes.Root', 'cloudify.nodes.SoftwareComponent', 'cloudify.arcadia.nodes.WrappedComponent']279 sg_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = srv_graph_type)280 mysql_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = wrap_comp_type)281 wp_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = wrap_comp_type)282 rp_mock = CloudifyWorlkflowNodeInstanceMock(type_hierarchy = runtime_type)283 sg_mock._contained_instances.append(wp_mock)284 sg_mock._contained_instances.append(mysql_mock)285 sg_mock._contained_instances.append(rp_mock)286 sg_mock._node_instance['id'] = 'service_graph_gm17g6'287 mysql_mock._node_instance['id'] = 'mysql_x1m8sh'288 wp_mock._node_instance['id'] = 'wordpress_k8lr3c'289 rp_mock._node_instance['id'] = 'runtime_policy_7vy2nn'290 con_to_type = ['cloudify.relationships.depends_on', 'cloudify.relationships.connected_to',291 'cloudify.arcadia.relationships.connected_to', 'wordpress_connected_to_mysql']292 con_in_type = ['cloudify.relationships.contained_in', 'cloudify.arcadia.relationships.contained_in']293 mysql_mock._relationship_instances['service_graph_gm17g6'] = CloudifyWorkflowRelationshipInstanceMock(node_instance=sg_mock, type_hierarchy=con_in_type)294 wp_mock._relationship_instances['service_graph_gm17g6'] = CloudifyWorkflowRelationshipInstanceMock(node_instance=sg_mock, type_hierarchy=con_in_type)295 wp_mock._relationship_instances['mysql_x1m8sh'] = CloudifyWorkflowRelationshipInstanceMock(node_instance=mysql_mock, type_hierarchy=con_to_type)296 rp_mock._relationship_instances['service_graph_gm17g6'] = CloudifyWorkflowRelationshipInstanceMock(node_instance=sg_mock, type_hierarchy=con_in_type)297 graph_builder = GraphBuilder(_comp_factory = ComponentFactory())298 service_graph = graph_builder.build(sg_mock)299 self.assertTrue(isinstance(service_graph, ServiceGraphElement))300 self.assertTrue(len(service_graph.components) == 2)301 self.assertTrue(len(service_graph.policies) == 1)302 def find_component(array, component):303 for comp in array:304 if comp._instance == component:305 return comp306 return307 mysql_comp = find_component(service_graph.components, mysql_mock)308 wp_comp = find_component(service_graph.components, wp_mock)309 self.assertTrue(mysql_comp)310 self.assertTrue(wp_comp)311 self.assertTrue(len(wp_comp.dependencies) == 1)...
test_http_transfer.py
Source:test_http_transfer.py
1# Copyright 2015 VMware, Inc. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may not4# use this file except in compliance with the License. You may obtain a copy5# of the License at http://www.apache.org/licenses/LICENSE-2.06#7# Unless required by applicable law or agreed to in writing, software8# distributed under the License is distributed on an "AS IS" BASIS, without9# warranties or conditions of any kind, EITHER EXPRESS OR IMPLIED. See the10# License for then specific language governing permissions and limitations11# under the License.12import unittest13import uuid14from hamcrest import * # noqa15from mock import MagicMock16from mock import patch17from nose_parameterized import parameterized18from common import services19from common.service_name import ServiceName20from gen.host import Host21from gen.host.ttypes import ServiceTicketRequest22from gen.host.ttypes import ServiceTicketResultCode23from gen.host.ttypes import ServiceType24from host.hypervisor.esx.http_disk_transfer import HttpNfcTransferer25from host.hypervisor.esx.vim_client import VimClient26class TestHttpTransfer(unittest.TestCase):27 """Http Transferer tests."""28 @patch.object(VimClient, "acquire_credentials")29 @patch("pysdk.connect.Connect")30 def setUp(self, connect, creds):31 self.host_uuid = str(uuid.uuid4())32 VimClient.host_uuid = self.host_uuid33 self.image_ds = "image_ds"34 creds.return_value = ["username", "password"]35 self.vim_client = VimClient(auto_sync=False)36 self.patcher = patch("host.hypervisor.esx.vm_config.GetEnv")37 self.patcher.start()38 services.register(ServiceName.AGENT_CONFIG, MagicMock())39 self.http_transferer = HttpNfcTransferer(self.vim_client,40 self.image_ds)41 def tearDown(self):42 self.vim_client.disconnect(wait=True)43 self.patcher.stop()44 @parameterized.expand([45 (True,), (False,)46 ])47 @patch("host.hypervisor.esx.http_disk_transfer.VimClient")48 @patch("host.hypervisor.esx.http_disk_transfer.DirectClient")49 def test_get_remote_connections(self, get_svc_ticket_success, _client_cls,50 _vim_client_cls):51 host = "mock_host"52 port = 883553 get_service_ticket_mock = MagicMock()54 if get_svc_ticket_success:55 get_service_ticket_mock.result = ServiceTicketResultCode.OK56 else:57 get_service_ticket_mock.result = ServiceTicketResultCode.NOT_FOUND58 instance = _client_cls.return_value59 instance.get_service_ticket.return_value = get_service_ticket_mock60 if get_svc_ticket_success:61 (agent_conn,62 vim_conn) = self.http_transferer._get_remote_connections(63 host, port)64 _client_cls.assert_called_once_with(65 "Host", Host.Client, host, port)66 instance.connect.assert_called_once_with()67 request = ServiceTicketRequest(service_type=ServiceType.VIM)68 instance.get_service_ticket.assert_called_once_with(request)69 _vim_client_cls.assert_called_once_with(70 host=host, ticket=get_service_ticket_mock.vim_ticket,71 auto_sync=False)72 self.assertEqual(agent_conn, instance)73 self.assertEqual(vim_conn, _vim_client_cls.return_value)74 else:75 self.assertRaises(76 ValueError, self.http_transferer._get_remote_connections,77 host, port)78 @parameterized.expand([79 (None, "http://*/ha-nfc/x.vmdk", "http://actual_host/ha-nfc/x.vmdk"),80 (None, "https://*/foo", "https://actual_host/foo"),81 (None, "http://*:1234/foo", "http://actual_host:1234/foo"),82 (None, "https://host/foo", "https://host/foo")83 ])84 def test_ensure_host_in_url(self, _, url, replaced_url):85 host = "actual_host"86 result = self.http_transferer._ensure_host_in_url(url, host)87 self.assertEqual(result, replaced_url)88 def test_export_shadow_vm(self):89 self.http_transferer._get_disk_url_from_lease = MagicMock()90 self.http_transferer._wait_for_lease = MagicMock()91 mock_get_vm = MagicMock()92 mock_lease = MagicMock()93 self.vim_client.get_vm_obj_in_cache = mock_get_vm94 mock_get_vm.return_value.ExportVm.return_value = mock_lease95 lease, url = self.http_transferer._export_shadow_vm()96 mock_get_vm.assert_called_once_with(self.http_transferer._shadow_vm_id)97 mock_get_vm.return_value.ExportVm.assert_called_once_with()98 self.http_transferer._wait_for_lease.assert_called_once_with(99 mock_lease)100 self.http_transferer._get_disk_url_from_lease.assert_called_once_with(101 mock_lease)102 def test_ensure_shadow_vm(self):103 self.http_transferer._vm_manager.create_vm = MagicMock()104 self.http_transferer._ensure_shadow_vm()105 create_vm = self.http_transferer._vm_manager.create_vm106 self.assertTrue(create_vm.called)107 vm_id_arg, vm_spec_arg = create_vm.call_args_list[0][0]108 expected_vm_id = "shadow_%s" % self.host_uuid109 self.assertEqual(vm_id_arg, expected_vm_id)110 self.assertEqual(111 vm_spec_arg.files.vmPathName,112 '[] /vmfs/volumes/%s/vms/%s' % (self.image_ds, expected_vm_id[:2]))113 def test_configure_shadow_vm_with_disk(self):114 image_id = "fake_image_id"115 vm_mgr = self.http_transferer._vm_manager116 vm_mgr.update_vm = MagicMock()117 spec_mock = MagicMock()118 info_mock = MagicMock()119 vm_mgr.update_vm_spec = MagicMock(return_value=spec_mock)120 vm_mgr.get_vm_config = MagicMock(return_value=info_mock)121 vm_mgr.remove_all_disks = MagicMock()122 vm_mgr.add_disk = MagicMock()123 self.http_transferer._configure_shadow_vm_with_disk(image_id)124 expected_vm_id = "shadow_%s" % self.host_uuid125 vm_mgr.update_vm_spec.assert_called_once_with()126 vm_mgr.get_vm_config.assert_called_once_with(expected_vm_id)127 vm_mgr.remove_all_disks.assert_called_once_with(spec_mock, info_mock)128 vm_mgr.add_disk.assert_called_once_with(129 spec_mock, self.image_ds, image_id, info_mock, disk_is_image=True)130 def test_get_image_stream_from_shadow_vm(self):131 image_id = "fake_image_id"132 lease_mock = MagicMock()133 url_mock = MagicMock()134 xferer = self.http_transferer135 xferer._ensure_shadow_vm = MagicMock()136 xferer._export_shadow_vm = MagicMock(137 return_value=(lease_mock, url_mock))138 xferer._configure_shadow_vm_with_disk = MagicMock()139 xferer._ensure_host_in_url = MagicMock(140 return_value=url_mock)141 lease, url = self.http_transferer._get_image_stream_from_shadow_vm(142 image_id)143 xferer._ensure_shadow_vm.assert_called_once_with()144 xferer._export_shadow_vm.assert_called_once_with()145 xferer._configure_shadow_vm_with_disk.assert_called_once_with(image_id)146 xferer._ensure_host_in_url.assert_called_once_with(url_mock,147 "localhost")148 self.assertEqual(lease, lease_mock)149 self.assertEqual(url, url_mock)150 @patch("uuid.uuid4", return_value="fake_id")151 @patch("pyVmomi.vim.vm.VmImportSpec")152 def test_create_import_vm_spec(self, mock_vm_imp_spec_cls, mock_uuid):153 image_id = "fake_image_id"154 destination_datastore = "fake_datastore"155 create_spec_mock = MagicMock()156 create_empty_disk_mock = MagicMock()157 xferer = self.http_transferer158 xferer._vm_config.create_spec_for_import = create_spec_mock159 xferer._vm_manager.create_empty_disk = create_empty_disk_mock160 spec = xferer._create_import_vm_spec(image_id, destination_datastore)161 expected_vm_id = "h2h_fake_id"162 create_spec_mock.assert_called_once_with(163 vm_id=expected_vm_id, image_id=image_id,164 datastore=destination_datastore, memory=32, cpus=1)165 create_empty_disk_mock.assert_called_once_with(166 create_spec_mock.return_value, destination_datastore, None,167 size_mb=1)168 mock_vm_imp_spec_cls.assert_called_once_with(169 configSpec=create_empty_disk_mock.return_value)170 self.assertEqual(spec, mock_vm_imp_spec_cls.return_value)171 def test_get_url_from_import_vm(self):172 host = "mock_host"173 lease_mock = MagicMock()174 url_mock = MagicMock()175 import_spec = MagicMock()176 rp_mock = MagicMock()177 folder_mock = MagicMock()178 vim_client_mock = MagicMock()179 vim_client_mock.host = host180 xferer = self.http_transferer181 xferer._wait_for_lease = MagicMock()182 xferer._get_disk_url_from_lease = MagicMock(return_value=url_mock)183 xferer._ensure_host_in_url = MagicMock(return_value=url_mock)184 vim_client_mock.root_resource_pool = rp_mock185 vim_client_mock.vm_folder = folder_mock186 rp_mock.ImportVApp.return_value = lease_mock187 lease, url = xferer._get_url_from_import_vm(vim_client_mock,188 import_spec)189 rp_mock.ImportVApp.assert_called_once_with(190 import_spec, folder_mock)191 xferer._wait_for_lease.assert_called_once_with(192 lease_mock)193 xferer._get_disk_url_from_lease.assert_called_once_with(lease_mock)194 xferer._ensure_host_in_url.assert_called_once_with(url_mock, host)195 self.assertEqual(lease, lease_mock)196 self.assertEqual(url, url_mock)197 @patch("os.unlink")198 def test_send_image_to_host(self, mock_unlink):199 host = "mock_host"200 port = 8835201 image_id = "fake_image_id"202 destination_datastore = "fake_datastore"203 read_lease_mock = MagicMock()204 from_url_mock = MagicMock()205 write_lease_mock = MagicMock()206 to_url_mock = MagicMock()207 import_spec_mock = MagicMock()208 xferer = self.http_transferer209 xferer._get_image_stream_from_shadow_vm = MagicMock(210 return_value=(read_lease_mock, from_url_mock))211 xferer.download_file = MagicMock()212 vim_conn_mock = MagicMock()213 agent_conn_mock = MagicMock()214 xferer._get_remote_connections = MagicMock(215 return_value=(agent_conn_mock, vim_conn_mock))216 xferer._create_import_vm_spec = MagicMock(217 return_value=import_spec_mock)218 xferer._get_url_from_import_vm = MagicMock(219 return_value=(write_lease_mock, to_url_mock))220 xferer.upload_file = MagicMock()221 self.http_transferer.send_image_to_host(222 image_id, destination_datastore, host, port)223 xferer._get_image_stream_from_shadow_vm.assert_called_once_with(224 image_id)225 shadow_vm_id = "shadow_%s" % self.host_uuid226 expected_tmp_file = "/vmfs/volumes/%s/%s_transfer.vmdk" % (227 self.image_ds, shadow_vm_id)228 xferer.download_file.assert_called_once_with(229 from_url_mock, expected_tmp_file)230 read_lease_mock.Complete.assert_called_once_with()231 xferer._create_import_vm_spec.assert_called_once_with(232 image_id, destination_datastore)233 xferer._get_url_from_import_vm.assert_called_once_with(234 vim_conn_mock, import_spec_mock)235 xferer.upload_file.assert_called_once_with(236 expected_tmp_file, to_url_mock)237 write_lease_mock.Complete.assert_called_once_with()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!