PageRenderTime 52ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/master/buildbot/test/unit/test_steps_master.py

https://gitlab.com/murder187ss/buildbot
Python | 239 lines | 214 code | 11 blank | 14 comment | 12 complexity | 5691699485ac4ac8f9a5ab443f6bb081 MD5 | raw file
  1. # This file is part of Buildbot. Buildbot is free software: you can
  2. # redistribute it and/or modify it under the terms of the GNU General Public
  3. # License as published by the Free Software Foundation, version 2.
  4. #
  5. # This program is distributed in the hope that it will be useful, but WITHOUT
  6. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  7. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  8. # details.
  9. #
  10. # You should have received a copy of the GNU General Public License along with
  11. # this program; if not, write to the Free Software Foundation, Inc., 51
  12. # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. #
  14. # Copyright Buildbot Team Members
  15. import os
  16. import pprint
  17. import sys
  18. from twisted.internet import error
  19. from twisted.internet import reactor
  20. from twisted.python import failure
  21. from twisted.python import runtime
  22. from twisted.trial import unittest
  23. from buildbot.process.properties import Interpolate
  24. from buildbot.process.properties import WithProperties
  25. from buildbot.process.results import EXCEPTION
  26. from buildbot.process.results import FAILURE
  27. from buildbot.process.results import SUCCESS
  28. from buildbot.steps import master
  29. from buildbot.test.util import steps
  30. _COMSPEC_ENV = 'COMSPEC'
  31. class TestMasterShellCommand(steps.BuildStepMixin, unittest.TestCase):
  32. def setUp(self):
  33. if runtime.platformType == 'win32':
  34. self.comspec = os.environ.get(_COMSPEC_ENV)
  35. os.environ[_COMSPEC_ENV] = r'C:\WINDOWS\system32\cmd.exe'
  36. return self.setUpBuildStep()
  37. def tearDown(self):
  38. if runtime.platformType == 'win32':
  39. if self.comspec:
  40. os.environ[_COMSPEC_ENV] = self.comspec
  41. else:
  42. del os.environ[_COMSPEC_ENV]
  43. return self.tearDownBuildStep()
  44. def patchSpawnProcess(self, exp_cmd, exp_argv, exp_path, exp_usePTY,
  45. exp_env, outputs):
  46. def spawnProcess(pp, cmd, argv, path, usePTY, env):
  47. self.assertEqual([cmd, argv, path, usePTY, env],
  48. [exp_cmd, exp_argv, exp_path, exp_usePTY, exp_env])
  49. for output in outputs:
  50. if output[0] == 'out':
  51. pp.outReceived(output[1])
  52. elif output[0] == 'err':
  53. pp.errReceived(output[1])
  54. elif output[0] == 'rc':
  55. if output[1] != 0:
  56. so = error.ProcessTerminated(exitCode=output[1])
  57. else:
  58. so = error.ProcessDone(None)
  59. pp.processEnded(failure.Failure(so))
  60. self.patch(reactor, 'spawnProcess', spawnProcess)
  61. def test_real_cmd(self):
  62. cmd = [sys.executable, '-c', 'print "hello"']
  63. self.setupStep(
  64. master.MasterShellCommand(command=cmd))
  65. if runtime.platformType == 'win32':
  66. self.expectLogfile('stdio', "hello\r\n")
  67. else:
  68. self.expectLogfile('stdio', "hello\n")
  69. self.expectOutcome(result=SUCCESS, state_string="Ran")
  70. return self.runStep()
  71. def test_real_cmd_interrupted(self):
  72. cmd = [sys.executable, '-c', 'while True: pass']
  73. self.setupStep(
  74. master.MasterShellCommand(command=cmd))
  75. self.expectLogfile('stdio', "")
  76. if runtime.platformType == 'win32':
  77. # windows doesn't have signals, so we don't get 'killed',
  78. # but the "exception" part still works.
  79. self.expectOutcome(result=EXCEPTION,
  80. state_string="failed (1) (exception)")
  81. else:
  82. self.expectOutcome(result=EXCEPTION,
  83. state_string="killed (9) (exception)")
  84. d = self.runStep()
  85. self.step.interrupt("KILL")
  86. return d
  87. def test_real_cmd_fails(self):
  88. cmd = [sys.executable, '-c', 'import sys; sys.exit(1)']
  89. self.setupStep(
  90. master.MasterShellCommand(command=cmd))
  91. self.expectLogfile('stdio', "")
  92. self.expectOutcome(result=FAILURE, state_string="failed (1) (failure)")
  93. return self.runStep()
  94. def test_constr_args(self):
  95. self.setupStep(
  96. master.MasterShellCommand(description='x', descriptionDone='y',
  97. env={'a': 'b'}, workdir='build', usePTY=True,
  98. command='true'))
  99. if runtime.platformType == 'win32':
  100. exp_argv = [r'C:\WINDOWS\system32\cmd.exe', '/c', 'true']
  101. else:
  102. exp_argv = ['/bin/sh', '-c', 'true']
  103. self.patchSpawnProcess(
  104. exp_cmd=exp_argv[0], exp_argv=exp_argv,
  105. exp_path='build', exp_usePTY=True, exp_env={'a': 'b'},
  106. outputs=[
  107. ('out', 'hello!\n'),
  108. ('err', 'world\n'),
  109. ('rc', 0),
  110. ])
  111. self.expectOutcome(result=SUCCESS, state_string='y')
  112. return self.runStep()
  113. def test_env_subst(self):
  114. cmd = [sys.executable, '-c', 'import os; print os.environ["HELLO"]']
  115. os.environ['WORLD'] = 'hello'
  116. self.setupStep(
  117. master.MasterShellCommand(command=cmd,
  118. env={'HELLO': '${WORLD}'}))
  119. if runtime.platformType == 'win32':
  120. self.expectLogfile('stdio', "hello\r\n")
  121. else:
  122. self.expectLogfile('stdio', "hello\n")
  123. self.expectOutcome(result=SUCCESS)
  124. d = self.runStep()
  125. @d.addBoth
  126. def _restore_env(res):
  127. del os.environ['WORLD']
  128. return res
  129. return d
  130. def test_env_list_subst(self):
  131. cmd = [sys.executable, '-c', 'import os; print os.environ["HELLO"]']
  132. os.environ['WORLD'] = 'hello'
  133. os.environ['LIST'] = 'world'
  134. self.setupStep(
  135. master.MasterShellCommand(command=cmd,
  136. env={'HELLO': ['${WORLD}', '${LIST}']}))
  137. if runtime.platformType == 'win32':
  138. self.expectLogfile('stdio', "hello;world\r\n")
  139. else:
  140. self.expectLogfile('stdio', "hello:world\n")
  141. self.expectOutcome(result=SUCCESS)
  142. d = self.runStep()
  143. @d.addBoth
  144. def _restore_env(res):
  145. del os.environ['WORLD']
  146. del os.environ['LIST']
  147. return res
  148. return d
  149. def test_prop_rendering(self):
  150. cmd = [sys.executable, '-c', WithProperties(
  151. 'import os; print "%s"; print os.environ[\"BUILD\"]',
  152. 'project')]
  153. self.setupStep(
  154. master.MasterShellCommand(command=cmd,
  155. env={'BUILD': WithProperties('%s', "project")}))
  156. self.properties.setProperty("project", "BUILDBOT-TEST", "TEST")
  157. if runtime.platformType == 'win32':
  158. self.expectLogfile('stdio', "BUILDBOT-TEST\r\nBUILDBOT-TEST\r\n")
  159. else:
  160. self.expectLogfile('stdio', "BUILDBOT-TEST\nBUILDBOT-TEST\n")
  161. self.expectOutcome(result=SUCCESS)
  162. return self.runStep()
  163. def test_constr_args_descriptionSuffix(self):
  164. self.setupStep(
  165. master.MasterShellCommand(description='x', descriptionDone='y',
  166. descriptionSuffix='z',
  167. env={'a': 'b'}, workdir='build', usePTY=True,
  168. command='true'))
  169. if runtime.platformType == 'win32':
  170. exp_argv = [r'C:\WINDOWS\system32\cmd.exe', '/c', 'true']
  171. else:
  172. exp_argv = ['/bin/sh', '-c', 'true']
  173. self.patchSpawnProcess(
  174. exp_cmd=exp_argv[0], exp_argv=exp_argv,
  175. exp_path='build', exp_usePTY=True, exp_env={'a': 'b'},
  176. outputs=[
  177. ('out', 'hello!\n'),
  178. ('err', 'world\n'),
  179. ('rc', 0),
  180. ])
  181. self.expectOutcome(result=SUCCESS, state_string='y z')
  182. return self.runStep()
  183. class TestSetProperty(steps.BuildStepMixin, unittest.TestCase):
  184. def setUp(self):
  185. return self.setUpBuildStep()
  186. def tearDown(self):
  187. return self.tearDownBuildStep()
  188. def test_simple(self):
  189. self.setupStep(master.SetProperty(property="testProperty", value=Interpolate("sch=%(prop:scheduler)s, worker=%(prop:workername)s")))
  190. self.properties.setProperty('scheduler', 'force', source='SetProperty', runtime=True)
  191. self.properties.setProperty('workername', 'testWorker', source='SetProperty', runtime=True)
  192. self.expectOutcome(result=SUCCESS, state_string="Set")
  193. self.expectProperty('testProperty', 'sch=force, worker=testWorker', source='SetProperty')
  194. return self.runStep()
  195. class TestLogRenderable(steps.BuildStepMixin, unittest.TestCase):
  196. def setUp(self):
  197. return self.setUpBuildStep()
  198. def tearDown(self):
  199. return self.tearDownBuildStep()
  200. def test_simple(self):
  201. self.setupStep(master.LogRenderable(content=Interpolate('sch=%(prop:scheduler)s, worker=%(prop:workername)s')))
  202. self.properties.setProperty('scheduler', 'force', source='TestSetProperty', runtime=True)
  203. self.properties.setProperty('workername', 'testWorker', source='TestSetProperty', runtime=True)
  204. self.expectOutcome(result=SUCCESS, state_string='Logged')
  205. self.expectLogfile('Output', pprint.pformat('sch=force, worker=testWorker'))
  206. return self.runStep()