TestRegistration.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. from unittest import TestCase
  18. import os
  19. import tempfile
  20. from mock.mock import patch
  21. from mock.mock import MagicMock
  22. from only_for_platform import not_for_platform, PLATFORM_WINDOWS
  23. from ambari_commons.os_check import OSCheck
  24. from ambari_agent.Register import Register
  25. from ambari_agent.AmbariConfig import AmbariConfig
  26. from ambari_agent.Hardware import Hardware
  27. @not_for_platform(PLATFORM_WINDOWS)
  28. class TestRegistration(TestCase):
  29. @patch.object(Hardware, "_chk_mount", new = MagicMock(return_value=True))
  30. @patch("ambari_commons.firewall.run_os_command")
  31. @patch.object(OSCheck, "get_os_type")
  32. @patch.object(OSCheck, "get_os_version")
  33. def test_registration_build(self, get_os_version_mock, get_os_type_mock, run_os_cmd_mock):
  34. config = AmbariConfig().getConfig()
  35. tmpdir = tempfile.gettempdir()
  36. config.set('agent', 'prefix', tmpdir)
  37. config.set('agent', 'current_ping_port', '33777')
  38. get_os_type_mock.return_value = "suse"
  39. get_os_version_mock.return_value = "11"
  40. run_os_cmd_mock.return_value = (3, "", "")
  41. ver_file = os.path.join(tmpdir, "version")
  42. with open(ver_file, "w") as text_file:
  43. text_file.write("1.3.0")
  44. register = Register(config)
  45. data = register.build(1)
  46. #print ("Register: " + pprint.pformat(data))
  47. self.assertEquals(len(data['hardwareProfile']) > 0, True, "hardwareProfile should contain content")
  48. self.assertEquals(data['hostname'] != "", True, "hostname should not be empty")
  49. self.assertEquals(data['publicHostname'] != "", True, "publicHostname should not be empty")
  50. self.assertEquals(data['responseId'], 1)
  51. self.assertEquals(data['timestamp'] > 1353678475465L, True, "timestamp should not be empty")
  52. self.assertEquals(len(data['agentEnv']) > 0, True, "agentEnv should not be empty")
  53. self.assertEquals(data['agentVersion'], '1.3.0', "agentVersion should not be empty")
  54. print data['agentEnv']['umask']
  55. self.assertEquals(not data['agentEnv']['umask']== "", True, "agents umask should not be empty")
  56. self.assertEquals(data['currentPingPort'] == 33777, True, "current ping port should be 33777")
  57. self.assertEquals(data['prefix'], config.get('agent', 'prefix'), 'The prefix path does not match')
  58. self.assertEquals(len(data), 9)
  59. os.remove(ver_file)