test_ru_execute_tasks.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # !/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. # Python Imports
  18. import os
  19. import json
  20. from mock.mock import patch
  21. from mock.mock import MagicMock
  22. # Module imports
  23. from stacks.utils.RMFTestCase import *
  24. from resource_management import Script, ConfigDictionary
  25. from resource_management.libraries.functions.default import default
  26. from resource_management.core.logger import Logger
  27. from ambari_agent.AmbariConfig import AmbariConfig
  28. from ambari_commons.os_check import OSCheck
  29. def fake_call(command):
  30. """
  31. Instead of shell.call, call a command whose output equals the command.
  32. :param command: Command that will be echoed.
  33. :return: Returns a tuple of (process output code, output)
  34. """
  35. return (0, command)
  36. class TestRUExecuteTasks(RMFTestCase):
  37. CUSTOM_ACTIONS_DIR = os.path.join(os.getcwd(), "../resources/custom_actions/")
  38. @patch.object(Logger, "info")
  39. @patch.object(Logger, "error")
  40. @patch.object(OSCheck, "get_os_type")
  41. def setUp(self, os_type_mock, error_mock, info_mock):
  42. # Must patch the logger and get_os_type function.
  43. os_type_mock.return_value = "redhat"
  44. Logger.logger = MagicMock()
  45. # Import the class under test. This is done here as opposed to the rest of the imports because the get_os_type()
  46. # method needs to be patched first.
  47. from ru_execute_tasks import ExecuteUpgradeTasks
  48. global ExecuteUpgradeTasks
  49. def tearDown(self):
  50. Logger.logger = None
  51. @patch("resource_management.core.shell.call")
  52. @patch("os.path.exists")
  53. @patch.object(AmbariConfig, "getConfigFile")
  54. @patch.object(Script, 'get_tmp_dir')
  55. @patch.object(Script, 'get_config')
  56. def test_execution(self, get_config_mock, get_tmp_dir_mock, get_config_file_mock, os_path_exists_mock, call_mock):
  57. # Mock the config objects
  58. json_file_path = os.path.join(self.CUSTOM_ACTIONS_DIR, "ru_execute_tasks_namenode_prepare.json")
  59. self.assertTrue(os.path.isfile(json_file_path))
  60. with open(json_file_path, "r") as json_file:
  61. json_payload = json.load(json_file)
  62. config_dict = ConfigDictionary(json_payload)
  63. get_config_mock.return_value = config_dict
  64. get_tmp_dir_mock.return_value = "/tmp"
  65. ambari_agent_ini_file_path = os.path.join(os.getcwd(), "../../../../ambari-agent/conf/unix/ambari-agent.ini")
  66. self.assertTrue(os.path.isfile(ambari_agent_ini_file_path))
  67. get_config_file_mock.return_value = ambari_agent_ini_file_path
  68. # Mock os calls
  69. os_path_exists_mock.return_value = True
  70. call_mock.side_effect = fake_call # echo the command
  71. # Ensure that the json file was actually read.
  72. stack_name = default("/hostLevelParams/stack_name", None)
  73. stack_version = default("/hostLevelParams/stack_version", None)
  74. service_package_folder = default('/roleParams/service_package_folder', None)
  75. self.assertEqual(stack_name, "HDP")
  76. self.assertEqual(stack_version, 2.2)
  77. self.assertEqual(service_package_folder, "common-services/HDFS/2.1.0.2.0/package")
  78. # Begin the test
  79. ru_execute = ExecuteUpgradeTasks()
  80. ru_execute.actionexecute(None)
  81. 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")