PRESUBMIT_test.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import os
  6. import sys
  7. import time
  8. import unittest
  9. # Add src/testing/ into sys.path for importing PRESUBMIT without pylint errors.
  10. sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
  11. from buildbot import PRESUBMIT
  12. class PresubmitError:
  13. def __init__(self, message):
  14. self.message = message
  15. def __eq__(self, other):
  16. return isinstance(other, PresubmitError) and self.message == other.message
  17. def __repr__(self):
  18. return 'PresubmitError({!r})'.format(self.message)
  19. class TestCheckFreeze(unittest.TestCase):
  20. def get_input_api(self, current_time, footers=None):
  21. """Get an input API to use for tests.
  22. Args:
  23. current_time - Current time expressed as seconds since the epoch.
  24. """
  25. class FakeTime:
  26. localtime = time.localtime
  27. strftime = time.strftime
  28. def time(self):
  29. return float(current_time)
  30. class FakeChange:
  31. def GitFootersFromDescription(self):
  32. return footers or []
  33. class FakeInputApi:
  34. time = FakeTime()
  35. change = FakeChange()
  36. return FakeInputApi()
  37. def get_output_api(self):
  38. class FakeOutputApi:
  39. PresubmitError = PresubmitError
  40. return FakeOutputApi
  41. def test_before_freeze(self):
  42. input_api = self.get_input_api(1639641599) # 2021/12/15 23:59:59 -0800
  43. output_api = self.get_output_api()
  44. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  45. self.assertEqual(errors, [])
  46. def test_start_of_freeze(self):
  47. input_api = self.get_input_api(1639641600) # 2021/12/16 00:00:00 -0800
  48. output_api = self.get_output_api()
  49. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  50. self.assertEqual(len(errors), 1)
  51. self.assertTrue(
  52. errors[0].message.startswith('There is a prod freeze in effect'))
  53. def test_end_of_freeze(self):
  54. input_api = self.get_input_api(1641196799) # 2022/01/02 23:59:59 -0800
  55. output_api = self.get_output_api()
  56. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  57. self.assertEqual(len(errors), 1)
  58. self.assertTrue(
  59. errors[0].message.startswith('There is a prod freeze in effect'))
  60. def test_after_freeze(self):
  61. input_api = self.get_input_api(1641196800) # 2022/01/03 00:00:00 -0800')
  62. output_api = self.get_output_api()
  63. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  64. self.assertEqual(errors, [])
  65. def test_ignore_freeze(self):
  66. input_api = self.get_input_api(
  67. 1639641600, # 2021/12/16 00:00:00 -0800
  68. footers={'Ignore-Freeze': 'testing'})
  69. output_api = self.get_output_api()
  70. errors = PRESUBMIT.CheckFreeze(input_api, output_api)
  71. self.assertEqual(errors, [])
  72. if __name__ == '__main__':
  73. unittest.main()