TestServerUtils.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import os
  17. os.environ["ROOT"] = ""
  18. from mock.mock import patch, MagicMock
  19. from unittest import TestCase
  20. import platform
  21. from ambari_commons import os_utils
  22. os_utils.search_file = MagicMock(return_value="/tmp/ambari.properties")
  23. import shutil
  24. project_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)),os.path.normpath("../../../../"))
  25. shutil.copyfile(project_dir+"/ambari-server/conf/unix/ambari.properties", "/tmp/ambari.properties")
  26. with patch.object(platform, "linux_distribution", return_value = MagicMock(return_value=('Redhat', '6.4', 'Final'))):
  27. with patch("os.path.isdir", return_value = MagicMock(return_value=True)):
  28. with patch("os.access", return_value = MagicMock(return_value=True)):
  29. with patch.object(os_utils, "parse_log4j_file", return_value={'ambari.log.dir': '/var/log/ambari-server'}):
  30. from ambari_server.serverUtils import get_ambari_server_api_base
  31. from ambari_server.serverConfiguration import CLIENT_API_PORT, CLIENT_API_PORT_PROPERTY, SSL_API, DEFAULT_SSL_API_PORT, SSL_API_PORT
  32. @patch.object(platform, "linux_distribution", new = MagicMock(return_value=('Redhat', '6.4', 'Final')))
  33. class TestServerUtils(TestCase):
  34. def test_get_ambari_server_api_base(self):
  35. # Test case of using http protocol
  36. properties = FakeProperties({
  37. SSL_API: "false",
  38. CLIENT_API_PORT_PROPERTY: None
  39. })
  40. result = get_ambari_server_api_base(properties)
  41. self.assertEquals(result, 'http://127.0.0.1:8080/api/v1/')
  42. # Test case of using http protocol and custom port
  43. properties = FakeProperties({
  44. SSL_API: "false",
  45. CLIENT_API_PORT_PROPERTY: "8033"
  46. })
  47. result = get_ambari_server_api_base(properties)
  48. self.assertEquals(result, 'http://127.0.0.1:8033/api/v1/')
  49. # Test case of using https protocol (and ssl port)
  50. properties = FakeProperties({
  51. SSL_API: "true",
  52. SSL_API_PORT : "8443",
  53. CLIENT_API_PORT_PROPERTY: None
  54. })
  55. result = get_ambari_server_api_base(properties)
  56. self.assertEquals(result, 'https://127.0.0.1:8443/api/v1/')
  57. class FakeProperties(object):
  58. def __init__(self, prop_map):
  59. self.prop_map = prop_map
  60. def get_property(self, prop_name):
  61. return self.prop_map[prop_name]