blob: 0d94a6d494c1990ee85642d5c5b646736023c159 [file] [log] [blame]
Xinan Lin3ba18a02019-08-13 15:44:55 -07001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module for buildbucket unittests."""
6
7import mock
8import os
9import unittest
10
11import build_lib
12import buildbucket
13import task_executor
14
15from chromite.api.gen.test_platform import request_pb2
16from google.appengine.ext import testbed
17from google.protobuf import json_format
18
19
20ADDR = u'http://localhost:1'
21
22
Prathmesh Prabhu91ef1622019-08-30 14:07:42 -070023def _get_default_suite_params():
Xinan Lin3ba18a02019-08-13 15:44:55 -070024 return {
25 'suite': 'fake_suite',
26 'board': 'fake_board',
27 'model': 'fake_model',
28 build_lib.BuildVersionKey.CROS_VERSION: 'fake_cros_build',
29 build_lib.BuildVersionKey.FW_RW_VERSION: 'fake_firmware_rw_build',
30 build_lib.BuildVersionKey.FW_RO_VERSION: 'fake_firmware_ro_build',
31 build_lib.BuildVersionKey.ANDROID_BUILD_VERSION: 'fake_android_build',
32 build_lib.BuildVersionKey.TESTBED_BUILD_VERSION: 'fake_testbed_build',
33 'num': 1,
34 'pool': 'DUT_POOL_SUITES',
35 'priority': 10,
Xinan Lin6e097382019-08-27 18:43:35 -070036 'timeout': 12,
37 'timeout_mins': 4320,
38 'max_runtime_mins': 4320,
Xinan Lin3ba18a02019-08-13 15:44:55 -070039 'no_wait_for_results': True,
40 'test_source_build': 'fake_test_source_build',
41 'job_retry': False,
42 'no_delay': False,
43 'force': False,
44 'run_prod_code': True,
45 'is_skylab': False,
46 }
47
48
49class FakeTestPlatformClient(object):
50
51 def __init__(self, test):
52 self._test = test
53 self.called_with_requests = []
54
55 def ScheduleBuild(self, req, credentials=None):
56 self.called_with_requests.append(req)
57 # No test_platform_response proto. Simply return '200, ok'.
58 return '200 ok'
59
60
61class TestPlatformClientTestCase(unittest.TestCase):
62
63 def setUp(self):
64 super(TestPlatformClientTestCase, self).setUp()
65 self.fake_client = FakeTestPlatformClient(self)
66 patcher = mock.patch('buildbucket._get_client',
67 return_value=self._get_client(ADDR))
68 patcher.start()
69 self.client = buildbucket.TestPlatformClient(ADDR,
70 'foo-proj',
71 'foo-bucket',
72 'foo-builder')
73 self.addCleanup(patcher.stop)
74 self.testbed = testbed.Testbed()
75 self.testbed.activate()
76 self.addCleanup(self.testbed.deactivate)
77 self.testbed.init_taskqueue_stub(
78 root_path=os.path.join(os.path.dirname(__file__)))
79 self.taskqueue_stub = self.testbed.get_stub(
80 testbed.TASKQUEUE_SERVICE_NAME)
81
Xinan Lin3ba18a02019-08-13 15:44:55 -070082 def testFormTestPlatformRequestSuccessfully(self):
Prathmesh Prabhu91ef1622019-08-30 14:07:42 -070083 tp = _get_default_suite_params()
Xinan Lin3ba18a02019-08-13 15:44:55 -070084 self.assertEqual(len(self.fake_client.called_with_requests), 0)
Prathmesh Prabhu6f8d2f92019-08-30 14:05:35 -070085 self.client.run(**self._marshal_to_tq_params(tp))
Xinan Lin3ba18a02019-08-13 15:44:55 -070086 self.assertEqual(len(self.fake_client.called_with_requests), 1)
87 req = _struct_pb2_to_request_pb2(
88 self.fake_client.called_with_requests[0].properties['request'])
89
90 self.assertEqual(req.params.scheduling.managed_pool,
91 request_pb2.Request.Params.Scheduling.MANAGED_POOL_SUITES)
Xinan Lin6e097382019-08-27 18:43:35 -070092 self.assertEqual(req.params.time.maximum_duration.seconds, 165600)
Xinan Lin3ba18a02019-08-13 15:44:55 -070093 self.assertEqual(req.params.metadata.test_metadata_url,
Xinan Lin6e097382019-08-27 18:43:35 -070094 'gs://chromeos-image-archive/' + tp['test_source_build'])
Xinan Lin3ba18a02019-08-13 15:44:55 -070095 self.assertEqual(req.test_plan.suite[0].name, tp['suite'])
96
Alex Zamorzaevc1935602019-08-28 14:37:35 -070097 def testNonStandardPoolName(self):
Prathmesh Prabhu91ef1622019-08-30 14:07:42 -070098 tp = _get_default_suite_params()
Alex Zamorzaevc1935602019-08-28 14:37:35 -070099 self.assertEqual(len(self.fake_client.called_with_requests), 0)
100 tp['pool'] = 'cts'
Prathmesh Prabhu6f8d2f92019-08-30 14:05:35 -0700101 self.client.run(**self._marshal_to_tq_params(tp))
Alex Zamorzaevc1935602019-08-28 14:37:35 -0700102 req = _struct_pb2_to_request_pb2(
103 self.fake_client.called_with_requests[0].properties['request'])
104
105 self.assertEqual(req.params.scheduling.managed_pool,
106 request_pb2.Request.Params.Scheduling.MANAGED_POOL_CTS)
107
Xinan Lin3ba18a02019-08-13 15:44:55 -0700108 def testShouldGoUnmanagedPool(self):
Prathmesh Prabhu91ef1622019-08-30 14:07:42 -0700109 tp = _get_default_suite_params()
Xinan Lin3ba18a02019-08-13 15:44:55 -0700110 self.assertEqual(len(self.fake_client.called_with_requests), 0)
111 tp['pool'] = 'wifi'
Prathmesh Prabhu6f8d2f92019-08-30 14:05:35 -0700112 self.client.run(**self._marshal_to_tq_params(tp))
Xinan Lin3ba18a02019-08-13 15:44:55 -0700113 req = _struct_pb2_to_request_pb2(
114 self.fake_client.called_with_requests[0].properties['request'])
115
116 self.assertEqual(req.params.scheduling.unmanaged_pool, tp['pool'])
117
118 def testShouldSchedulewithQuotaAccount(self):
Prathmesh Prabhu91ef1622019-08-30 14:07:42 -0700119 tp = _get_default_suite_params()
Xinan Lin3ba18a02019-08-13 15:44:55 -0700120 self.assertEqual(len(self.fake_client.called_with_requests), 0)
121 tp['override_qs_account'] = 'dummy@foo.com'
Prathmesh Prabhu6f8d2f92019-08-30 14:05:35 -0700122 self.client.run(**self._marshal_to_tq_params(tp))
Xinan Lin3ba18a02019-08-13 15:44:55 -0700123 req = _struct_pb2_to_request_pb2(
124 self.fake_client.called_with_requests[0].properties['request'])
125
126 self.assertEqual(req.params.scheduling.quota_account,
127 tp['override_qs_account'])
128
129 def testNoFinalBuild(self):
Prathmesh Prabhu91ef1622019-08-30 14:07:42 -0700130 tp = _get_default_suite_params()
Xinan Lin3ba18a02019-08-13 15:44:55 -0700131 tp[build_lib.BuildVersionKey.CROS_VERSION] = None
132 tp[build_lib.BuildVersionKey.ANDROID_BUILD_VERSION] = None
133 tp[build_lib.BuildVersionKey.TESTBED_BUILD_VERSION] = None
Prathmesh Prabhu6f8d2f92019-08-30 14:05:35 -0700134 params = self._marshal_to_tq_params(tp)
Xinan Lin3ba18a02019-08-13 15:44:55 -0700135 self.assertRaises(buildbucket.BuildbucketRunError,
136 self.client.run, **params)
137
Prathmesh Prabhu6f8d2f92019-08-30 14:05:35 -0700138 def _marshal_to_tq_params(self, suite_params):
139 """Marshal given suite parameters to a format returned from taskqueue.
140
141 buildbucket.TestPlatformClient.run() expects arguments pulled from a
142 taskqueue. This helper function marshals suite parameters into the format
143 they would be in when pulled from a taskqueue.
144 """
145 task_executor.push(task_executor.SUITES_QUEUE, **suite_params)
146 tasks = self.taskqueue_stub.get_filtered_tasks()
147 return tasks[0].extract_params()
Xinan Lin3ba18a02019-08-13 15:44:55 -0700148
Prathmesh Prabhu73801e12019-08-30 14:09:31 -0700149 def _get_client(self, addr):
150 self.assertEqual(ADDR, addr)
151 return self.fake_client
152
Xinan Lin3ba18a02019-08-13 15:44:55 -0700153def _struct_pb2_to_request_pb2(struct_pb2):
154 """Transform google struct proto to test_platform_request.
155
156 Args:
157 struct_pb2: A struct_pb2 instance.
158
159 Returns:
160 A request_pb2 instance.
161 """
162 json = json_format.MessageToJson(struct_pb2)
163 return json_format.Parse(json, request_pb2.Request())