/src/python/tests/appengine/handlers/bots_test.py

https://github.com/google/clusterfuzz · Python · 121 lines · 81 code · 22 blank · 18 comment · 12 complexity · 68090c4005e6165031fe266f267ba180 MD5 · raw file

  1. # Copyright 2019 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Tests for bots."""
  15. import collections
  16. import datetime
  17. import string
  18. import unittest
  19. import flask
  20. import webtest
  21. from datastore import data_types
  22. from handlers import bots
  23. from tests.test_libs import helpers as test_helpers
  24. from tests.test_libs import test_utils
  25. @test_utils.with_cloud_emulators('datastore')
  26. class BotsTest(unittest.TestCase):
  27. """Jobs tests."""
  28. def setUp(self):
  29. test_helpers.patch(self, [
  30. 'libs.access.has_access',
  31. 'libs.access.get_access',
  32. 'libs.gcs.prepare_blob_upload',
  33. ])
  34. self.mock.prepare_blob_upload.return_value = (
  35. collections.namedtuple('GcsUpload', [])())
  36. flaskapp = flask.Flask('testflask')
  37. flaskapp.add_url_rule('/', view_func=bots.JsonHandler.as_view('/'))
  38. self.app = webtest.TestApp(flaskapp)
  39. def _create_bot(self,
  40. bot_name,
  41. last_beat_time=datetime.datetime(2020, 1, 25, 0, 0),
  42. task_payload='',
  43. task_end_time=datetime.datetime(2020, 1, 25, 0, 0),
  44. source_version='',
  45. platform_id=''):
  46. """Create a test job."""
  47. bot = data_types.Heartbeat()
  48. bot.bot_name = bot_name
  49. bot.last_beat_time = last_beat_time
  50. bot.task_payload = task_payload
  51. bot.task_end_time = task_end_time
  52. bot.source_version = source_version
  53. bot.platform_id = platform_id
  54. bot.put()
  55. return bot
  56. def test_pagination(self):
  57. """Test bots pagination and post method."""
  58. self.mock.has_access.return_value = True
  59. expected_items = {1: [], 2: [], 3: []}
  60. for bot_num, bot_suffix in enumerate(string.ascii_lowercase):
  61. bot_name = "test_bot_" + bot_suffix
  62. bot = self._create_bot(bot_name=bot_name)
  63. expected_items[(bot_num // bots.PAGE_SIZE) + 1].append(bot.bot_name)
  64. resp = self.app.post_json('/', {'page': 1})
  65. self.assertListEqual(expected_items[1],
  66. [item['bot_name'] for item in resp.json['items']])
  67. resp = self.app.post_json('/', {'page': 2})
  68. self.assertListEqual(expected_items[2],
  69. [item['bot_name'] for item in resp.json['items']])
  70. resp = self.app.post_json('/', {'page': 3})
  71. self.assertListEqual(expected_items[3],
  72. [item['bot_name'] for item in resp.json['items']])
  73. resp = self.app.post_json('/', {'page': 4})
  74. self.assertListEqual([], [item['bot_name'] for item in resp.json['items']])
  75. def test_search(self):
  76. """Test bots search."""
  77. self.mock.has_access.return_value = True
  78. bot_a = self._create_bot(bot_name='test_bot_a', task_payload='pay_x')
  79. bot_b = self._create_bot(bot_name='test_bot_b', task_payload='pay_y')
  80. resp = self.app.post_json('/', {'q': 'a'})
  81. self.assertListEqual([bot_a.bot_name],
  82. [item['bot_name'] for item in resp.json['items']])
  83. resp = self.app.post_json('/', {'q': 'b'})
  84. self.assertListEqual([bot_b.bot_name],
  85. [item['bot_name'] for item in resp.json['items']])
  86. resp = self.app.post_json('/', {'q': 'c'})
  87. self.assertListEqual([], [item['bot_name'] for item in resp.json['items']])
  88. resp = self.app.post_json('/', {'q': 'bot'})
  89. self.assertListEqual([bot_a.bot_name, bot_b.bot_name],
  90. [item['bot_name'] for item in resp.json['items']])
  91. resp = self.app.post_json('/', {'q': 'x'})
  92. self.assertListEqual([bot_a.bot_name],
  93. [item['bot_name'] for item in resp.json['items']])
  94. resp = self.app.post_json('/', {'q': 'y'})
  95. self.assertListEqual([bot_b.bot_name],
  96. [item['bot_name'] for item in resp.json['items']])
  97. resp = self.app.post_json('/', {'q': 'pay'})
  98. self.assertListEqual([bot_a.bot_name, bot_b.bot_name],
  99. [item['bot_name'] for item in resp.json['items']])