TestVersionSelectUtil.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 unittest import TestCase
  17. import os
  18. from mock.mock import patch, MagicMock
  19. from resource_management.core.logger import Logger
  20. class TestVersionSelectUtil(TestCase):
  21. """
  22. Class that tests the method of the version_select_util.py file.
  23. """
  24. def setUp(self):
  25. import imp
  26. Logger.logger = MagicMock()
  27. self.test_directory = os.path.dirname(os.path.abspath(__file__))
  28. test_file_path = os.path.join(self.test_directory, '../../../../ambari-common/src/main/python/resource_management/libraries/functions/version_select_util.py')
  29. with open(test_file_path, 'rb') as fp:
  30. self.module = imp.load_module('module', fp, test_file_path, ('.py', 'rb', imp.PY_SOURCE))
  31. @patch('__builtin__.open')
  32. @patch("resource_management.core.shell.call")
  33. def test_get_component_version(self, call_mock, open_mock):
  34. stack_expected_version = "2.2.1.0-2175"
  35. # Mock classes for reading from a file
  36. class MagicFile(object):
  37. allowed_names = set(["hadoop-hdfs-namenode",
  38. "hadoop-hdfs-datanode",
  39. "zookeeper-server",
  40. "zookeeper-client"
  41. ])
  42. def read(self, value):
  43. return (value + " - " + stack_expected_version) if value in self.allowed_names else ("ERROR: Invalid package - " + value)
  44. def __exit__(self, exc_type, exc_val, exc_tb):
  45. pass
  46. def __enter__(self):
  47. return self
  48. pass
  49. class MagicFile1(MagicFile):
  50. def read(self):
  51. return super(MagicFile1, self).read("hadoop-nonexistent-component-name")
  52. class MagicFile2(MagicFile):
  53. def read(self):
  54. return super(MagicFile2, self).read("hadoop-hdfs-namenode")
  55. class MagicFile3(MagicFile):
  56. def read(self):
  57. return super(MagicFile3, self).read("hadoop-hdfs-datanode")
  58. open_mock.side_effect = [MagicFile1(), MagicFile2(), MagicFile3()]
  59. call_mock.side_effect = [(0, "value will come from MagicFile"), ] * 3
  60. # Missing Stack
  61. version = self.module.get_component_version(None, "hadoop-hdfs-datanode")
  62. self.assertEquals(version, None)
  63. version = self.module.get_component_version("StackDoesNotExist", "hadoop-hdfs-datanode")
  64. self.assertEquals(version, None)
  65. # Invalid request
  66. version = self.module.get_component_version("HDP", None)
  67. self.assertEquals(version, None)
  68. # Incorrect name
  69. version = self.module.get_component_version("HDP", "hadoop-nonexistent-component-name")
  70. self.assertEquals(version, None)
  71. # Pass
  72. version = self.module.get_component_version("HDP", "hadoop-hdfs-namenode")
  73. self.assertEquals(version, stack_expected_version)
  74. version = self.module.get_component_version("HDP", "hadoop-hdfs-datanode")
  75. self.assertEquals(version, stack_expected_version)