TestAmbariAgent.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import unittest
  18. import subprocess
  19. import os
  20. import sys
  21. import AmbariConfig
  22. from mock.mock import MagicMock, patch, ANY
  23. with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
  24. from ambari_agent.Controller import AGENT_AUTO_RESTART_EXIT_CODE
  25. from ambari_agent import AmbariAgent
  26. class TestAmbariAgent(unittest.TestCase):
  27. @patch.object(subprocess, "Popen")
  28. @patch("os.path.isfile")
  29. @patch("os.remove")
  30. def test_main(self, os_remove_mock, os_path_isfile_mock, subprocess_popen_mock):
  31. facter1 = MagicMock()
  32. facter2 = MagicMock()
  33. subprocess_popen_mock.side_effect = [facter1, facter2]
  34. facter1.returncode = 77
  35. facter2.returncode = 55
  36. os_path_isfile_mock.return_value = True
  37. if not (os.environ.has_key("PYTHON")):
  38. os.environ['PYTHON'] = "test/python/path"
  39. sys.argv[0] = "test data"
  40. AmbariAgent.main()
  41. self.assertTrue(subprocess_popen_mock.called)
  42. self.assertTrue(subprocess_popen_mock.call_count == 2)
  43. self.assertTrue(facter1.communicate.called)
  44. self.assertTrue(facter2.communicate.called)
  45. self.assertTrue(os_path_isfile_mock.called)
  46. self.assertTrue(os_path_isfile_mock.call_count == 2)
  47. self.assertTrue(os_remove_mock.called)
  48. #
  49. # Test AmbariConfig.getLogFile() for ambari-agent
  50. #
  51. def test_logfile_location(self):
  52. #
  53. # Test without $AMBARI_AGENT_LOG_DIR
  54. #
  55. log_folder = '/var/log/ambari-agent'
  56. log_file = 'ambari-agent.log'
  57. with patch.dict('os.environ', {}):
  58. self.assertEqual(os.path.join(log_folder, log_file), AmbariConfig.AmbariConfig.getLogFile())
  59. #
  60. # Test with $AMBARI_AGENT_LOG_DIR
  61. #
  62. log_folder = '/myloglocation/log'
  63. log_file = 'ambari-agent.log'
  64. with patch.dict('os.environ', {'AMBARI_AGENT_LOG_DIR': log_folder}):
  65. self.assertEqual(os.path.join(log_folder, log_file), AmbariConfig.AmbariConfig.getLogFile())
  66. pass
  67. #
  68. # Test AmbariConfig.getOutFile() for ambari-agent
  69. #
  70. def test_outfile_location(self):
  71. #
  72. # Test without $AMBARI_AGENT_OUT_DIR
  73. #
  74. out_folder = '/var/log/ambari-agent'
  75. out_file = 'ambari-agent.out'
  76. with patch.dict('os.environ', {}):
  77. self.assertEqual(os.path.join(out_folder, out_file), AmbariConfig.AmbariConfig.getOutFile())
  78. #
  79. # Test with $AMBARI_AGENT_OUT_DIR
  80. #
  81. out_folder = '/myoutlocation/out'
  82. out_file = 'ambari-agent.out'
  83. with patch.dict('os.environ', {'AMBARI_AGENT_OUT_DIR': out_folder}):
  84. self.assertEqual(os.path.join(out_folder, out_file), AmbariConfig.AmbariConfig.getOutFile())
  85. pass