gardeningserver_unittest.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (C) 2011 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import json
  29. import sys
  30. import unittest2 as unittest
  31. from webkitpy.common.system.outputcapture import OutputCapture
  32. from webkitpy.layout_tests.models.test_configuration import *
  33. from webkitpy.port import builders
  34. from webkitpy.thirdparty.mock import Mock
  35. from webkitpy.tool.mocktool import MockTool
  36. from webkitpy.common.system.executive_mock import MockExecutive
  37. from webkitpy.common.host_mock import MockHost
  38. from webkitpy.tool.servers.gardeningserver import *
  39. class TestPortFactory(object):
  40. # FIXME: Why is this a class method?
  41. @classmethod
  42. def create(cls):
  43. host = MockHost()
  44. return host.port_factory.get("test-win-xp")
  45. @classmethod
  46. def path_to_test_expectations_file(cls):
  47. return cls.create().path_to_test_expectations_file()
  48. class MockServer(object):
  49. def __init__(self):
  50. self.tool = MockTool()
  51. self.tool.executive = MockExecutive(should_log=True)
  52. self.tool.filesystem.files[TestPortFactory.path_to_test_expectations_file()] = ""
  53. # The real GardeningHTTPRequestHandler has a constructor that's too hard to
  54. # call in a unit test, so we create a subclass that's easier to constrcut.
  55. class TestGardeningHTTPRequestHandler(GardeningHTTPRequestHandler):
  56. def __init__(self, server):
  57. self.server = server
  58. self.body = None
  59. def _expectations_updater(self):
  60. return GardeningExpectationsUpdater(self.server.tool, TestPortFactory.create())
  61. def read_entity_body(self):
  62. return self.body if self.body else ''
  63. def _serve_text(self, text):
  64. print "== Begin Response =="
  65. print text
  66. print "== End Response =="
  67. def _serve_json(self, json_object):
  68. print "== Begin JSON Response =="
  69. print json.dumps(json_object)
  70. print "== End JSON Response =="
  71. class GardeningServerTest(unittest.TestCase):
  72. def _post_to_path(self, path, body=None, expected_stderr=None, expected_stdout=None, server=None):
  73. handler = TestGardeningHTTPRequestHandler(server or MockServer())
  74. handler.path = path
  75. handler.body = body
  76. OutputCapture().assert_outputs(self, handler.do_POST, expected_stderr=expected_stderr, expected_stdout=expected_stdout)
  77. def disabled_test_rollout(self):
  78. expected_stderr = "MOCK run_command: ['echo', 'rollout', '--force-clean', '--non-interactive', '2314', 'MOCK rollout reason'], cwd=/mock-checkout\n"
  79. expected_stdout = "== Begin Response ==\nsuccess\n== End Response ==\n"
  80. self._post_to_path("/rollout?revision=2314&reason=MOCK+rollout+reason", expected_stderr=expected_stderr, expected_stdout=expected_stdout)
  81. def disabled_test_rebaselineall(self):
  82. expected_stderr = "MOCK run_command: ['echo', 'rebaseline-json'], cwd=/mock-checkout, input={\"user-scripts/another-test.html\":{\"%s\": [%s]}}\n"
  83. expected_stdout = "== Begin Response ==\nsuccess\n== End Response ==\n"
  84. server = MockServer()
  85. self.output = ['{"add": [], "delete": []}', '']
  86. def run_command(args, cwd=None, input=None, **kwargs):
  87. print >> sys.stderr, "MOCK run_command: %s, cwd=%s, input=%s" % (args, cwd, input)
  88. return self.output.pop(0)
  89. server.tool.executive.run_command = run_command
  90. self._post_to_path("/rebaselineall", body='{"user-scripts/another-test.html":{"MOCK builder": ["txt","png"]}}', expected_stderr=expected_stderr % ('MOCK builder', '"txt","png"'), expected_stdout=expected_stdout, server=server)
  91. self._post_to_path("/rebaselineall", body='{"user-scripts/another-test.html":{"MOCK builder (Debug)": ["txt","png"]}}', expected_stderr=expected_stderr % ('MOCK builder (Debug)', '"txt","png"'), expected_stdout=expected_stdout)