TestActionQueue.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. from unittest import TestCase
  18. from ambari_agent.ActionQueue import ActionQueue
  19. from ambari_agent.AmbariConfig import AmbariConfig
  20. from ambari_agent.FileUtil import getFilePath
  21. import os, errno, time, pprint, tempfile, threading
  22. event = threading.Event()
  23. class TestActionQueue(TestCase):
  24. def test_ActionQueueStartStop(self):
  25. actionQueue = ActionQueue(AmbariConfig().getConfig())
  26. actionQueue.IDLE_SLEEP_TIME = 0.01
  27. actionQueue.start()
  28. actionQueue.stop()
  29. actionQueue.join()
  30. self.assertEqual(actionQueue.stopped(), True, 'Action queue is not stopped.')
  31. #This feature is not yet implemented in ActionQueue
  32. def test_RetryAction(self):
  33. pass
  34. def test_command_in_progress(self):
  35. config = AmbariConfig().getConfig()
  36. tmpfile = tempfile.gettempdir()
  37. config.set('puppet', 'puppetmodules', tmpfile)
  38. actionQueue = ActionQueue(config)
  39. actionQueue.IDLE_SLEEP_TIME = 0.01
  40. executor_started_event = threading.Event()
  41. end_executor_event = threading.Event()
  42. actionQueue.executor = FakeExecutor(executor_started_event, end_executor_event)
  43. before_start_result = actionQueue.result()
  44. command = {
  45. 'commandId': 17,
  46. 'role' : "role",
  47. 'taskId' : "taskId",
  48. 'clusterName' : "clusterName",
  49. 'serviceName' : "serviceName",
  50. 'status' : 'IN_PROGRESS',
  51. 'hostname' : "localhost.localdomain",
  52. 'hostLevelParams': "hostLevelParams",
  53. 'clusterHostInfo': "clusterHostInfo",
  54. 'roleCommand': "roleCommand",
  55. 'configurations': "configurations",
  56. 'commandType': "EXECUTION_COMMAND"
  57. }
  58. actionQueue.put(command)
  59. actionQueue.start()
  60. executor_started_event.wait()
  61. #print ("ii: " + pprint.pformat(actionQueue.commandInProgress))
  62. in_progress_result = actionQueue.result()
  63. end_executor_event.set()
  64. actionQueue.stop()
  65. actionQueue.join()
  66. after_start_result = actionQueue.result()
  67. self.assertEquals(len(before_start_result['componentStatus']), 0)
  68. self.assertEquals(len(before_start_result['reports']), 0)
  69. self.assertEquals(len(in_progress_result['componentStatus']), 0)
  70. self.assertEquals(len(in_progress_result['reports']), 1)
  71. self.assertEquals(in_progress_result['reports'][0]['status'], "IN_PROGRESS")
  72. self.assertEquals(in_progress_result['reports'][0]['stdout'], "Dummy output")
  73. self.assertEquals(in_progress_result['reports'][0]['exitCode'], 777)
  74. self.assertEquals(in_progress_result['reports'][0]['stderr'], 'Dummy err')
  75. self.assertEquals(len(after_start_result['componentStatus']), 0)
  76. self.assertEquals(len(after_start_result['reports']), 1)
  77. self.assertEquals(after_start_result['reports'][0]['status'], "COMPLETED")
  78. self.assertEquals(after_start_result['reports'][0]['stdout'], "returned stdout")
  79. self.assertEquals(after_start_result['reports'][0]['exitCode'], 0)
  80. self.assertEquals(after_start_result['reports'][0]['stderr'], 'returned stderr')
  81. #print("tmpout: " + pprint.pformat(actionQueue.tmpdir))
  82. #print("before: " + pprint.pformat(before_start_result))
  83. #print("in_progress: " + pprint.pformat(in_progress_result))
  84. #print("after: " + pprint.pformat(after_start_result))
  85. class FakeExecutor():
  86. def __init__(self, executor_started_event, end_executor_event):
  87. self.executor_started_event = executor_started_event
  88. self.end_executor_event = end_executor_event
  89. pass
  90. def runCommand(self, command, tmpoutpath, tmperrpath):
  91. tmpout= open(tmpoutpath, 'w')
  92. tmpout.write("Dummy output")
  93. tmpout.flush()
  94. tmperr= open(tmperrpath, 'w')
  95. tmperr.write("Dummy err")
  96. tmperr.flush()
  97. self.executor_started_event.set()
  98. self.end_executor_event.wait()
  99. return {
  100. "exitcode": 0,
  101. "stdout": "returned stdout",
  102. "stderr": "returned stderr"
  103. }