TestUpgradeHelper.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. '''
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. 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 and
  14. limitations under the License.
  15. '''
  16. from mock.mock import MagicMock, call
  17. from mock.mock import patch
  18. from unittest import TestCase
  19. import sys
  20. import unittest
  21. import upgradeHelper
  22. import StringIO
  23. import logging
  24. class TestUpgradeHelper(TestCase):
  25. original_curl = None
  26. out = None
  27. def setUp(self):
  28. # replace original curl call to mock
  29. self.original_curl = upgradeHelper.curl
  30. upgradeHelper.curl = self.magic_curl
  31. # mock logging methods
  32. upgradeHelper.logging.getLogger = MagicMock()
  33. upgradeHelper.logging.FileHandler = MagicMock()
  34. self.out = StringIO.StringIO()
  35. sys.stdout = self.out
  36. def magic_curl(self, *args, **kwargs):
  37. def ret_object():
  38. return ""
  39. def communicate():
  40. return "{}", ""
  41. ret_object.returncode = 0
  42. ret_object.communicate = communicate
  43. with patch("upgradeHelper.subprocess") as subprocess:
  44. subprocess.Popen.return_value = ret_object
  45. self.original_curl(*args, **kwargs)
  46. def tearDown(self):
  47. sys.stdout = sys.__stdout__
  48. @patch("optparse.OptionParser")
  49. @patch("upgradeHelper.modify_configs")
  50. @patch("upgradeHelper.backup_file")
  51. @patch("__builtin__.open")
  52. def test_ParseOptions(self, open_mock, backup_file_mock, modify_action_mock, option_parser_mock):
  53. class options(object):
  54. user = "test_user"
  55. hostname = "127.0.0.1"
  56. clustername = "test1"
  57. password = "test_password"
  58. upgrade_json = "catalog_file"
  59. from_stack = "0.0"
  60. to_stack = "1.3"
  61. logfile = "test.log"
  62. report = "report.txt"
  63. warnings = []
  64. printonly = False
  65. repo_version = None
  66. args = ["update-configs"]
  67. modify_action_mock.return_value = MagicMock()
  68. backup_file_mock.return_value = MagicMock()
  69. test_mock = MagicMock()
  70. test_mock.parse_args = lambda: (options, args)
  71. option_parser_mock.return_value = test_mock
  72. upgradeHelper.main()
  73. self.assertEqual(backup_file_mock.call_count, 0)
  74. self.assertEqual(modify_action_mock.call_count, 1)
  75. self.assertEqual({"user": options.user, "pass": options.password}, upgradeHelper.Options.API_TOKENS)
  76. self.assertEqual(options.clustername, upgradeHelper.Options.CLUSTER_NAME)
  77. @patch("optparse.OptionParser")
  78. @patch("upgradeHelper.finalize_ru")
  79. @patch("__builtin__.open")
  80. def test_Finalize_options(self, open_mock, finalize_ru_mock, option_parser_mock):
  81. class options(object):
  82. user = "test_user"
  83. hostname = "127.0.0.1"
  84. clustername = "test1"
  85. password = "test_password"
  86. upgrade_json = None
  87. from_stack = None
  88. to_stack = None
  89. logfile = "test.log"
  90. report = None
  91. warnings = []
  92. printonly = False
  93. repo_version = None
  94. args = ["finalize-ru"]
  95. test_mock = MagicMock()
  96. test_mock.parse_args = lambda: (options, args)
  97. option_parser_mock.return_value = test_mock
  98. try:
  99. upgradeHelper.main()
  100. except upgradeHelper.FatalException:
  101. # Expected
  102. pass
  103. class options(object):
  104. user = "test_user"
  105. hostname = "127.0.0.1"
  106. clustername = "test1"
  107. password = "test_password"
  108. upgrade_json = None
  109. from_stack = None
  110. to_stack = None
  111. logfile = "test.log"
  112. report = None
  113. warnings = []
  114. printonly = False
  115. repo_version = 'HDP-2.2.2.0-2561'
  116. args = ["finalize-ru"]
  117. test_mock = MagicMock()
  118. test_mock.parse_args = lambda: (options, args)
  119. option_parser_mock.return_value = test_mock
  120. upgradeHelper.main()
  121. self.assertTrue(finalize_ru_mock.called)
  122. if __name__ == "__main__":
  123. unittest.main()