Repository: ambari Updated Branches: refs/heads/trunk 17ac0d69d -> 03a927ddb
http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/TestOSCheck.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/TestOSCheck.py b/ambari-server/src/test/python/TestOSCheck.py index ebef706..cf114a1 100644 --- a/ambari-server/src/test/python/TestOSCheck.py +++ b/ambari-server/src/test/python/TestOSCheck.py @@ -28,13 +28,15 @@ from unittest import TestCase from mock.mock import patch from mock.mock import MagicMock +from only_for_platform import os_distro_value, os_distro_value_linux + from ambari_commons import OSCheck import os_check_type utils = __import__('ambari_server.utils').utils # We have to use this import HACK because the filename contains a dash -with patch("platform.linux_distribution", return_value = ('Suse','11','Final')): - with patch.object(OSCheck, "os_distribution", return_value = ('Suse','11','Final')): +with patch("platform.linux_distribution", return_value = os_distro_value_linux): + with patch.object(OSCheck, "os_distribution", return_value = os_distro_value): with patch.object(utils, "get_postgre_hba_dir"): ambari_server = __import__('ambari-server') @@ -207,15 +209,21 @@ class TestOSCheck(TestCase): configDefaults.AMBARI_PROPERTIES_BACKUP_FILE = fn1 serverConfiguration.AMBARI_PROPERTIES_FILE = fn2 - with open(configDefaults.AMBARI_PROPERTIES_BACKUP_FILE, 'w') as f: + f = open(configDefaults.AMBARI_PROPERTIES_BACKUP_FILE, 'w') + try: for line in properties: f.write(line) + finally: + f.close() #Call tested method update_ambari_properties() - with open(serverConfiguration.AMBARI_PROPERTIES_FILE, 'r') as f: + f = open(serverConfiguration.AMBARI_PROPERTIES_FILE, 'r') + try: ambari_properties_content = f.readlines() + finally: + f.close() count = 0 for line in ambari_properties_content: @@ -230,6 +238,7 @@ class TestOSCheck(TestCase): # Command should not fail if *.rpmsave file is missing result = update_ambari_properties() self.assertEquals(result, 0) + pass @patch.object(OSCheck, "os_distribution") def test_os_type_check(self, mock_linux_distribution): http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/TestResourceFilesKeeper.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/TestResourceFilesKeeper.py b/ambari-server/src/test/python/TestResourceFilesKeeper.py index dff7ccb..6856124 100644 --- a/ambari-server/src/test/python/TestResourceFilesKeeper.py +++ b/ambari-server/src/test/python/TestResourceFilesKeeper.py @@ -29,45 +29,74 @@ from subprocess import Popen from mock.mock import MagicMock, call from mock.mock import patch from mock.mock import create_autospec +from only_for_platform import get_platform, not_for_platform, only_for_platform, os_distro_value, PLATFORM_WINDOWS + from ambari_server.resourceFilesKeeper import ResourceFilesKeeper, KeeperException class TestResourceFilesKeeper(TestCase): - TEST_RESOURCES_DIR = "../resources" - TEST_STACKS_DIR="../resources/stacks" + TEST_RESOURCES_DIR = ".." + os.sep + "resources" + TEST_STACKS_DIR = ".." + os.sep + "resources" + os.sep + "stacks" # Stack that is not expected to change - DUMMY_UNCHANGEABLE_STACK="../resources/TestAmbaryServer.samples/" \ - "dummy_stack/HIVE/" + DUMMY_UNCHANGEABLE_STACK = ".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ + "dummy_stack" + os.sep + "HIVE" - DUMMY_ACTIVE_STACK="../resources/TestAmbaryServer.samples/" \ - "active_stack/" + DUMMY_ACTIVE_STACK = ".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ + "active_stack" - DUMMY_INACTIVE_STACK="../resources/TestAmbaryServer.samples/" \ - "inactive_stack/" + DUMMY_INACTIVE_STACK = ".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ + "inactive_stack" DUMMY_UNCHANGEABLE_PACKAGE=os.path.join(DUMMY_UNCHANGEABLE_STACK, ResourceFilesKeeper.PACKAGE_DIR) - DUMMY_UNCHANGEABLE_PACKAGE_HASH="33a5f7d3bb6e7b4545431fc07ae87fa2d59a09c4" + if get_platform() != PLATFORM_WINDOWS: + DUMMY_UNCHANGEABLE_PACKAGE_HASH="33a5f7d3bb6e7b4545431fc07ae87fa2d59a09c4" + else: + DUMMY_UNCHANGEABLE_PACKAGE_HASH="2e438f4f9862420ed8930a56b8809b8aca359e87" DUMMY_HASH="dummy_hash" YA_HASH="yet_another_hash" SOME_PATH="some-path" - DUMMY_UNCHANGEABLE_COMMON_SERVICES="../resources/TestAmbaryServer.samples/" \ - "dummy_common_services/HIVE/0.11.0.2.0.5.0" + DUMMY_UNCHANGEABLE_COMMON_SERVICES=".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ + "dummy_common_services" + os.sep + "HIVE" + os.sep + "0.11.0.2.0.5.0" DUMMY_UNCHANGEABLE_COMMON_SERVICES_PACKAGE=os.path.join(DUMMY_UNCHANGEABLE_COMMON_SERVICES, ResourceFilesKeeper.PACKAGE_DIR) + if get_platform() != PLATFORM_WINDOWS: + UPDATE_DIRECTORY_ARCHIVE_CALL_LIST = \ + "[call('../resources/TestAmbaryServer.samples/" \ + "dummy_stack/HIVE/package'),\n " \ + "call('../resources/TestAmbaryServer.samples/" \ + "dummy_stack/HIVE/package'),\n " \ + "call('../resources/TestAmbaryServer.samples/" \ + "dummy_stack/HIVE/package'),\n " \ + "call('../resources/TestAmbaryServer.samples/" \ + "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " \ + "call('../resources/TestAmbaryServer.samples/" \ + "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " \ + "call('../resources/custom_actions'),\n " \ + "call('../resources/host_scripts')]" + else: + UPDATE_DIRECTORY_ARCHIVE_CALL_LIST = \ + "[call('..\\\\resources\\\\TestAmbaryServer.samples\\\\dummy_stack\\\\HIVE\\\\package'),\n " \ + "call('..\\\\resources\\\\TestAmbaryServer.samples\\\\dummy_stack\\\\HIVE\\\\package'),\n " \ + "call('..\\\\resources\\\\TestAmbaryServer.samples\\\\dummy_stack\\\\HIVE\\\\package'),\n " \ + "call('..\\\\resources\\\\TestAmbaryServer.samples\\\\dummy_common_services\\\\HIVE\\\\0.11.0.2.0.5.0\\\\package'),\n " \ + "call('..\\\\resources\\\\TestAmbaryServer.samples\\\\dummy_common_services\\\\HIVE\\\\0.11.0.2.0.5.0\\\\package'),\n " \ + "call('..\\\\resources\\\\custom_actions'),\n " \ + "call('..\\\\resources\\\\host_scripts')]" + def setUp(self): logging.basicConfig(level=logging.ERROR) @patch.object(ResourceFilesKeeper, "update_directory_archieves") def test_perform_housekeeping(self, update_directory_archieves_mock): - resource_files_keeper = ResourceFilesKeeper("/dummy-resources", "/dummy-path") + resource_files_keeper = ResourceFilesKeeper(os.sep + "dummy-resources", os.sep + "dummy-path") resource_files_keeper.perform_housekeeping() update_directory_archieves_mock.assertCalled() pass @@ -91,18 +120,7 @@ class TestResourceFilesKeeper(TestCase): resource_files_keeper.update_directory_archieves() self.assertEquals(pprint.pformat( update_directory_archive_mock.call_args_list), - "[call('../resources/TestAmbaryServer.samples/" - "dummy_stack/HIVE/package'),\n " - "call('../resources/TestAmbaryServer.samples/" - "dummy_stack/HIVE/package'),\n " - "call('../resources/TestAmbaryServer.samples/" - "dummy_stack/HIVE/package'),\n " - "call('../resources/TestAmbaryServer.samples/" - "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " - "call('../resources/TestAmbaryServer.samples/" - "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " - "call('../resources/custom_actions'),\n " - "call('../resources/host_scripts')]") + self.UPDATE_DIRECTORY_ARCHIVE_CALL_LIST) pass http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/TestSetupAgent.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/TestSetupAgent.py b/ambari-server/src/test/python/TestSetupAgent.py index fbbce71..5c943cf 100644 --- a/ambari-server/src/test/python/TestSetupAgent.py +++ b/ambari-server/src/test/python/TestSetupAgent.py @@ -22,14 +22,9 @@ from mock.mock import patch import sys from ambari_commons import OSCheck -from only_for_platform import get_platform, not_for_platform, only_for_platform, PLATFORM_WINDOWS, PLATFORM_LINUX +from only_for_platform import get_platform, not_for_platform, only_for_platform, os_distro_value, PLATFORM_WINDOWS from mock.mock import MagicMock, patch, ANY, Mock -if get_platform() != PLATFORM_WINDOWS: - os_distro_value = ('Suse','11','Final') -else: - os_distro_value = ('win2012serverr2','6.3','WindowsServer') - with patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)): # from ambari_agent import NetUtil, security @@ -69,6 +64,7 @@ class TestSetupAgent(TestCase): pass + @not_for_platform(PLATFORM_WINDOWS) @patch.object(setup_agent, 'execOsCommand') @patch("os.environ") @patch("subprocess.Popen") @@ -127,6 +123,36 @@ class TestSetupAgent(TestCase): execOsCommand_mock.reset_mock() pass + @only_for_platform(PLATFORM_WINDOWS) + @patch.object(setup_agent, 'run_os_command') + @patch("os.environ") + @patch("time.sleep") + def test_runAgent(self, sleep_mock, environ_mock, run_os_command_mock): + expected_hostname = "test.hst" + passphrase = "passphrase" + run_os_command_mock.return_value = (0, "log", "") + # Test if expected_hostname is passed + ret = setup_agent.runAgent(passphrase, expected_hostname, "root", False) + self.assertEqual(run_os_command_mock.call_count, 1) + cmdStr = str(run_os_command_mock.call_args_list[0][0]) + self.assertEqual(cmdStr, str((["cmd", "/c", "ambari-agent.cmd", "restart"],))) + self.assertFalse('-v' in cmdStr) + self.assertEqual(ret["exitstatus"], 0) + self.assertFalse(sleep_mock.called) + + run_os_command_mock.reset_mock() + sleep_mock.reset_mock() + + # Retry command + run_os_command_mock.side_effect = [(2, "log", "err"), (0, "log", "")] + ret = setup_agent.runAgent(passphrase, expected_hostname, "root", False) + self.assertEqual(run_os_command_mock.call_count, 2) + self.assertTrue("Retrying" in ret['log'][1]) + self.assertEqual(ret["exitstatus"], 0) + self.assertTrue(sleep_mock.called) + pass + + @not_for_platform(PLATFORM_WINDOWS) @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch.object(setup_agent, 'getAvailableAgentPackageVersions') @patch('ambari_commons.OSCheck.is_suse_family') @@ -144,6 +170,7 @@ class TestSetupAgent(TestCase): self.assertTrue(result_version["exitstatus"] == 1) pass + @not_for_platform(PLATFORM_WINDOWS) @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch.object(setup_agent, 'getAvailableAgentPackageVersions') @patch('ambari_commons.OSCheck.is_suse_family') @@ -161,6 +188,21 @@ class TestSetupAgent(TestCase): self.assertTrue(result_version["exitstatus"] == 1) pass + @only_for_platform(PLATFORM_WINDOWS) + @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) + @patch.object(setup_agent, 'getAvailableAgentPackageVersions') + @patch.object(setup_agent, 'findNearestAgentPackageVersion') + def test_returned_optimal_version_is_initial_on_suse(self, findNearestAgentPackageVersion_method, + getAvailableAgentPackageVersions_method): + getAvailableAgentPackageVersions_method.return_value = {"exitstatus": 0, "log": "1.1.1"} + + projectVersion = "1.1.1" + result_version = setup_agent.getOptimalVersion(projectVersion) + self.assertTrue(findNearestAgentPackageVersion_method.called) + self.assertTrue(result_version["exitstatus"] == 1) + pass + + @not_for_platform(PLATFORM_WINDOWS) @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch('ambari_commons.OSCheck.is_suse_family') @patch('ambari_commons.OSCheck.is_ubuntu_family') @@ -183,6 +225,7 @@ class TestSetupAgent(TestCase): self.assertTrue(result_version["exitstatus"] == 1) pass + @not_for_platform(PLATFORM_WINDOWS) @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch('ambari_commons.OSCheck.is_suse_family') @patch('ambari_commons.OSCheck.is_ubuntu_family') @@ -205,6 +248,25 @@ class TestSetupAgent(TestCase): self.assertTrue(result_version["exitstatus"] == 1) pass + @only_for_platform(PLATFORM_WINDOWS) + @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) + @patch.object(setup_agent, 'findNearestAgentPackageVersion') + @patch.object(setup_agent, 'getAvailableAgentPackageVersions') + def test_returned_optimal_version_is_nearest_on_windows(self, findNearestAgentPackageVersion_method, + getAvailableAgentPackageVersions_method): + projectVersion = "" + nearest_version = projectVersion + "1.1.1" + findNearestAgentPackageVersion_method.return_value = { + "exitstatus": 0, + "log": [nearest_version, ""] + } + getAvailableAgentPackageVersions_method.return_value = {"exitstatus": 0, "log": nearest_version} + + result_version = setup_agent.getOptimalVersion(projectVersion) + self.assertTrue(findNearestAgentPackageVersion_method.called) + self.assertTrue(result_version["exitstatus"] == 1) + pass + @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch.object(setup_agent, 'getAvailableAgentPackageVersions') @patch('ambari_commons.OSCheck.is_suse_family') @@ -239,18 +301,31 @@ class TestSetupAgent(TestCase): "log": ["1.1.1.1", ""] } - projectVersion = "1.1.1" + projectVersion = "1.1.2" result_version = setup_agent.getOptimalVersion(projectVersion) self.assertTrue(findNearestAgentPackageVersion_method.called) - self.assertTrue(result_version["exitstatus"] == 1) + self.assertEqual(result_version["exitstatus"], 1) pass + @not_for_platform(PLATFORM_WINDOWS) @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch.object(subprocess, 'Popen') def test_execOsCommand(self, Popen_mock): self.assertFalse(setup_agent.execOsCommand("hostname -f") == None) + @only_for_platform(PLATFORM_WINDOWS) + @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) + @patch.object(subprocess, 'Popen') + def test_execOsCommand(self, Popen_mock): + p = MagicMock() + p.communicate.return_value = ("", "") + p.returncode = 0 + Popen_mock.return_value = p + retval = setup_agent.execOsCommand("hostname -f") + self.assertEqual(retval["exitstatus"], 0) + pass + @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch.object(setup_agent, 'checkVerbose') @patch.object(setup_agent, 'isAgentPackageAlreadyInstalled') http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/TestUtils.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/TestUtils.py b/ambari-server/src/test/python/TestUtils.py index 06e0db4..ee310b7 100644 --- a/ambari-server/src/test/python/TestUtils.py +++ b/ambari-server/src/test/python/TestUtils.py @@ -20,11 +20,14 @@ import StringIO import sys from unittest import TestCase from mock.mock import patch, MagicMock +from only_for_platform import not_for_platform, PLATFORM_WINDOWS + from ambari_commons.os_check import OSCheck, OSConst utils = __import__('ambari_server.utils').utils +@not_for_platform(PLATFORM_WINDOWS) class TestUtils(TestCase): @patch.object(OSCheck, "get_os_family") @@ -36,7 +39,7 @@ class TestUtils(TestCase): os_listdir_mock.return_value = ['8.4', '9.1'] self.assertEqual('9.1', utils.get_ubuntu_pg_version()) - + @patch.object(OSCheck, "is_suse_family") @patch.object(OSCheck, "is_ubuntu_family") @patch.object(OSCheck, "is_redhat_family") http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/TestValidateConfigs.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/TestValidateConfigs.py b/ambari-server/src/test/python/TestValidateConfigs.py index 1dc88fb..427f02e 100644 --- a/ambari-server/src/test/python/TestValidateConfigs.py +++ b/ambari-server/src/test/python/TestValidateConfigs.py @@ -26,6 +26,7 @@ if get_platform() != PLATFORM_WINDOWS: from validate_configs import ValidateConfigs +@not_for_platform(PLATFORM_WINDOWS) class TestValidateConfigs(TestCase): @patch("os.geteuid") http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/custom_actions/TestCheckHost.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/custom_actions/TestCheckHost.py b/ambari-server/src/test/python/custom_actions/TestCheckHost.py index 6d57fed..024aab2 100644 --- a/ambari-server/src/test/python/custom_actions/TestCheckHost.py +++ b/ambari-server/src/test/python/custom_actions/TestCheckHost.py @@ -31,14 +31,12 @@ from unittest import TestCase from check_host import CheckHost -from only_for_platform import only_for_platform, get_platform, PLATFORM_LINUX, PLATFORM_WINDOWS -from ambari_agent.HostCheckReportFileHandler import HostCheckReportFileHandler +from only_for_platform import get_platform, not_for_platform, only_for_platform, os_distro_value, PLATFORM_WINDOWS -from only_for_platform import get_platform, not_for_platform, only_for_platform, os_distro_value, PLATFORM_LINUX, PLATFORM_WINDOWS +from ambari_agent.HostCheckReportFileHandler import HostCheckReportFileHandler @patch.object(HostCheckReportFileHandler, "writeHostChecksCustomActionsFile", new=MagicMock()) -@patch.object(HostCheckReportFileHandler, "resolve_ambari_config", new=MagicMock()) class TestCheckHost(TestCase): current_dir = os.path.dirname(os.path.realpath(__file__)) @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @@ -172,6 +170,7 @@ class TestCheckHost(TestCase): checkHost.actionexecute(None) self.assertEquals(structured_out_mock.call_args[0][0], {'db_connection_check': {'message': 'Custom java is not ' \ 'available on host. Please install it. Java home should be the same as on server. \n', 'exit_code': 1}}) + pass @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @@ -214,6 +213,7 @@ class TestCheckHost(TestCase): {'cause': (), 'host': u'!!!', 'type': 'FORWARD_LOOKUP'}], 'message': 'There were 5 host(s) that could not resolve to an IP address.', 'failed_count': 5, 'success_count': 0, 'exit_code': 0}}) + pass @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch.object(Script, 'get_config') @@ -234,8 +234,10 @@ class TestCheckHost(TestCase): # ensure the correct function was called self.assertTrue(structured_out_mock.called) structured_out_mock.assert_called_with({}) + pass + @not_for_platform(PLATFORM_WINDOWS) @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) @patch("platform.system") @patch.object(Script, 'get_config') @@ -288,6 +290,59 @@ class TestCheckHost(TestCase): #ensure the correct response is returned put_structured_out_mock.assert_called_with({'last_agent_env_check': {'message': 'test exception', 'exit_code': 1}}) + pass + + @only_for_platform(PLATFORM_WINDOWS) + @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) + @patch("platform.system") + @patch.object(Script, 'get_config') + @patch.object(Script, 'get_tmp_dir') + @patch('resource_management.libraries.script.Script.put_structured_out') + @patch('ambari_agent.HostInfo.HostInfoWindows.javaProcs') + @patch('ambari_agent.HostInfo.HostInfoWindows.checkLiveServices') + @patch('ambari_agent.HostInfo.HostInfoWindows.getUMask') + @patch('ambari_agent.HostInfo.HostInfoWindows.checkFirewall') + @patch('ambari_agent.HostInfo.HostInfoWindows.checkReverseLookup') + @patch('time.time') + def testLastAgentEnv(self, time_mock, checkReverseLookup_mock, checkFirewall_mock, + getUMask_mock, checkLiveServices_mock, javaProcs_mock, put_structured_out_mock, + get_tmp_dir_mock, get_config_mock, systemmock): + jsonFilePath = os.path.join(TestCheckHost.current_dir, "..", "..", "resources", "custom_actions", "check_last_agent_env.json") + with open(jsonFilePath, "r") as jsonFile: + jsonPayload = json.load(jsonFile) + + get_config_mock.return_value = ConfigDictionary(jsonPayload) + get_tmp_dir_mock.return_value = "/tmp" + + checkHost = CheckHost() + checkHost.actionexecute(None) + + # ensure the correct function was called + self.assertTrue(time_mock.called) + self.assertTrue(checkReverseLookup_mock.called) + self.assertTrue(checkFirewall_mock.called) + self.assertTrue(getUMask_mock.called) + self.assertTrue(checkLiveServices_mock.called) + self.assertTrue(javaProcs_mock.called) + self.assertTrue(put_structured_out_mock.called) + # ensure the correct keys are in the result map + last_agent_env_check_result = put_structured_out_mock.call_args[0][0] + self.assertTrue('last_agent_env_check' in last_agent_env_check_result) + self.assertTrue('hostHealth' in last_agent_env_check_result['last_agent_env_check']) + self.assertTrue('firewallRunning' in last_agent_env_check_result['last_agent_env_check']) + self.assertTrue('firewallName' in last_agent_env_check_result['last_agent_env_check']) + self.assertTrue('alternatives' in last_agent_env_check_result['last_agent_env_check']) + self.assertTrue('umask' in last_agent_env_check_result['last_agent_env_check']) + self.assertTrue('stackFoldersAndFiles' in last_agent_env_check_result['last_agent_env_check']) + self.assertTrue('existingUsers' in last_agent_env_check_result['last_agent_env_check']) + + # try it now with errors + javaProcs_mock.side_effect = Exception("test exception") + checkHost.actionexecute(None) + + #ensure the correct response is returned + put_structured_out_mock.assert_called_with({'last_agent_env_check': {'message': 'test exception', 'exit_code': 1}}) + pass @patch("resource_management.libraries.script.Script.put_structured_out") @@ -324,6 +379,3 @@ class TestCheckHost(TestCase): - - - http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/custom_actions/TestInstallPackages.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/custom_actions/TestInstallPackages.py b/ambari-server/src/test/python/custom_actions/TestInstallPackages.py index d5ae81d..5ddfaa8 100644 --- a/ambari-server/src/test/python/custom_actions/TestInstallPackages.py +++ b/ambari-server/src/test/python/custom_actions/TestInstallPackages.py @@ -44,6 +44,7 @@ subproc_mock.return_value = MagicMock() subproc_stdout = MagicMock() subproc_mock.return_value.stdout = subproc_stdout +@not_for_platform(PLATFORM_WINDOWS) @patch.object(os, "read", new=MagicMock(return_value=None)) @patch.object(select, "select", new=MagicMock(return_value=([subproc_stdout], None, None))) @patch("pty.openpty", new = MagicMock(return_value=(1,5))) http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/custom_actions/test_ru_execute_tasks.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/custom_actions/test_ru_execute_tasks.py b/ambari-server/src/test/python/custom_actions/test_ru_execute_tasks.py index 3fa29a6..31c1030 100644 --- a/ambari-server/src/test/python/custom_actions/test_ru_execute_tasks.py +++ b/ambari-server/src/test/python/custom_actions/test_ru_execute_tasks.py @@ -21,18 +21,21 @@ limitations under the License. # Python Imports import os import json +from ambari_commons.os_family_impl import OsFamilyFuncImpl, OsFamilyImpl from mock.mock import patch from mock.mock import MagicMock # Module imports from stacks.utils.RMFTestCase import * +from only_for_platform import not_for_platform, only_for_platform, os_distro_value, PLATFORM_WINDOWS + from resource_management import Script, ConfigDictionary from resource_management.libraries.functions.default import default from resource_management.core.logger import Logger from ambari_agent.AmbariConfig import AmbariConfig from ambari_agent.FileCache import FileCache -from ambari_commons.os_check import OSCheck +from ambari_commons.os_check import OSCheck, OSConst def fake_call(command, **kwargs): @@ -46,8 +49,9 @@ def fake_call(command, **kwargs): class TestRUExecuteTasks(RMFTestCase): def get_custom_actions_dir(self): - return os.path.join(self.get_src_folder(), "test/resources/custom_actions/") + return os.path.normpath(os.path.join(self.get_src_folder(), "test", "resources", "custom_actions")) + @OsFamilyFuncImpl(os_family=OsFamilyImpl.DEFAULT) @patch.object(Logger, "info") @patch.object(Logger, "error") @patch.object(OSCheck, "get_os_type") @@ -55,6 +59,25 @@ class TestRUExecuteTasks(RMFTestCase): # Must patch the logger and get_os_type function. os_type_mock.return_value = "redhat" + self.CACHE_MOCK_DIR = "/var/lib/ambari-agent/cache/common-services/HDFS/2.1.0.2.0/package" + self.AGENT_INI_FILE_PATH = os.path.normpath(os.path.join(self.get_src_folder(), "../../ambari-agent/conf/unix/ambari-agent.ini")) + + self.osIndependentSetUp() + + @OsFamilyFuncImpl(os_family=OSConst.WINSRV_FAMILY) + @patch.object(Logger, "info") + @patch.object(Logger, "error") + @patch.object(OSCheck, "get_os_type") + def setUp(self, os_type_mock, error_mock, info_mock): + # Must patch the logger and get_os_type function. + os_type_mock.return_value = "Windows" + + self.CACHE_MOCK_DIR = "/var/lib/ambari-agent/cache/common-services/HDFS/2.1.0.2.0/package" + self.AGENT_INI_FILE_PATH = os.path.normpath(os.path.join(self.get_src_folder(), "..", "..", "ambari-agent", "conf", "windows", "ambari-agent.ini")) + + self.osIndependentSetUp() + + def osIndependentSetUp(self): Logger.logger = MagicMock() # Import the class under test. This is done here as opposed to the rest of the imports because the get_os_type() @@ -80,10 +103,11 @@ class TestRUExecuteTasks(RMFTestCase): config_dict = ConfigDictionary(json_payload) - cache_mock.return_value = "/var/lib/ambari-agent/cache/common-services/HDFS/2.1.0.2.0/package" + cache_mock.return_value = self.CACHE_MOCK_DIR get_config_mock.return_value = config_dict get_tmp_dir_mock.return_value = "/tmp" - ambari_agent_ini_file_path = os.path.join(self.get_src_folder(), "../../ambari-agent/conf/unix/ambari-agent.ini") + + ambari_agent_ini_file_path = self.AGENT_INI_FILE_PATH self.assertTrue(os.path.isfile(ambari_agent_ini_file_path)) get_config_file_mock.return_value = ambari_agent_ini_file_path @@ -104,7 +128,10 @@ class TestRUExecuteTasks(RMFTestCase): ru_execute = ExecuteUpgradeTasks() ru_execute.actionexecute(None) - call_mock.assert_called_with("/usr/bin/ambari-python-wrap /var/lib/ambari-agent/cache/common-services/HDFS/2.1.0.2.0/package/scripts/namenode.py prepare_rolling_upgrade /tmp", logoutput=True, quiet=True) + call_mock.assert_called_with( + "/usr/bin/ambari-python-wrap /var/lib/ambari-agent/cache/common-services/HDFS/2.1.0.2.0/package" + os.sep + + "scripts/namenode.py prepare_rolling_upgrade /tmp", logoutput=True, quiet=True) + pass @patch("resource_management.core.shell.checked_call") @patch("os.path.exists") @@ -149,4 +176,6 @@ class TestRUExecuteTasks(RMFTestCase): ru_execute = ExecuteUpgradeTasks() ru_execute.actionexecute(None) - call_mock.assert_called_with("/usr/bin/ambari-python-wrap /var/lib/ambari-agent/cache/custom_actions/scripts/namenode.py prepare_rolling_upgrade /tmp", logoutput=True, quiet=True) + call_mock.assert_called_with("/usr/bin/ambari-python-wrap /var/lib/ambari-agent/cache/custom_actions" + os.sep + + "scripts/namenode.py prepare_rolling_upgrade /tmp", logoutput=True, quiet=True) + pass http://git-wip-us.apache.org/repos/asf/ambari/blob/03a927dd/ambari-server/src/test/python/unitTests.py ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/python/unitTests.py b/ambari-server/src/test/python/unitTests.py index f16c280..7bc258d 100644 --- a/ambari-server/src/test/python/unitTests.py +++ b/ambari-server/src/test/python/unitTests.py @@ -258,7 +258,11 @@ def main(): sys.stderr.write("Total errors:{0}\n".format(len(test_errors))) sys.stderr.write("Total failures:{0}\n".format(len(test_failures))) - shutil.rmtree(newtmpdirpath) + try: + shutil.rmtree(newtmpdirpath) + except: + #Swallow the errors, nothing to do if the dir is being held by a dangling process + pass tempfile.tempdir = oldtmpdirpath tempfile.oldtmpdirpath = None