TestHostModel.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python2.6
  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. import logging
  18. from mock.mock import MagicMock, patch
  19. from HttpClientInvoker import HttpClientInvoker
  20. from ambari_client.ambari_api import AmbariClient
  21. import unittest
  22. class TestHostModel(unittest.TestCase):
  23. def setUp(self):
  24. http_client_logger = logging.getLogger()
  25. http_client_logger.info('Running test:' + self.id())
  26. def create_host(self, http_client_mock = MagicMock()):
  27. http_client_mock.invoke.side_effect = HttpClientInvoker.http_client_invoke_side_effects
  28. client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock)
  29. cluster = client.get_cluster('test1')
  30. host = cluster.get_host('myhost')
  31. return host
  32. def test_get_host_components(self):
  33. http_client_mock = MagicMock()
  34. expected_path = '//clusters/test1/hosts/myhost/host_components?fields=HostRoles/state'
  35. host = self.create_host(http_client_mock)
  36. host_components = host.get_host_components()
  37. self.assertEqual(host_components[0].component_name,"DATANODE")
  38. self.assertEqual(host_components[0].state,"STARTED")
  39. self.assertEqual(host_components[3].component_name,"HBASE_MASTER")
  40. self.assertEqual(host_components[3].state,"STARTED")
  41. http_client_mock.invoke.assert_called_with('GET', expected_path, headers=None, payload=None)
  42. def test_get_host_component(self):
  43. http_client_mock = MagicMock()
  44. expected_path = '//clusters/test1/hosts/myhost/host_components/DATANODE'
  45. host = self.create_host(http_client_mock)
  46. component = host.get_host_component("DATANODE")
  47. self.assertEqual(component.component_name,"DATANODE")
  48. self.assertEqual(component.state,"STARTED")
  49. self.assertEqual(component.host_name,"myhost")
  50. http_client_mock.invoke.assert_called_with('GET', expected_path, headers=None, payload=None)
  51. def test_assign_role(self):
  52. http_client_mock = MagicMock()
  53. expected_path = '//clusters/test1/hosts?Hosts/host_name=myhost'
  54. expected_payload = {'host_components': [{'HostRoles': {'component_name': 'GANGLIA_SERVER'}}]}
  55. host = self.create_host(http_client_mock)
  56. status = host.assign_role("GANGLIA_SERVER")
  57. self.assertTrue(status.status, 201)
  58. http_client_mock.invoke.assert_called_with('POST', expected_path, headers=None, payload=expected_payload)