/
bots_service.py
289 lines (201 loc) · 10.4 KB
/
bots_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Copyright (C) 2018 Codethink Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# <http://www.apache.org/licenses/LICENSE-2.0>
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors:
# Finn Ball <finn.ball@codethink.co.uk>
import copy
import grpc
import pytest
import uuid
from unittest import mock
from grpc._server import _Context
from buildgrid._protos.build.bazel.remote.execution.v2 import remote_execution_pb2
from buildgrid._protos.google.devtools.remoteworkers.v1test2 import bots_pb2, worker_pb2
from google.protobuf import any_pb2
from buildgrid.server import scheduler, job
from buildgrid.server.job import ExecuteStage, LeaseState
from buildgrid.server.worker import bots_interface, bots_service
# GRPC context
@pytest.fixture
def context():
yield mock.MagicMock(spec=_Context)
@pytest.fixture
def action_job():
action_digest = remote_execution_pb2.Digest()
j = job.Job(action_digest, None)
yield j
@pytest.fixture
def bot_session():
bot = bots_pb2.BotSession()
bot.bot_id = 'ana'
yield bot
@pytest.fixture
def schedule():
yield scheduler.Scheduler()
@pytest.fixture
def bots(schedule):
yield bots_interface.BotsInterface(schedule)
# Instance to test
@pytest.fixture
def instance(bots):
yield bots_service.BotsService(bots)
def test_create_bot_session(bot_session, context, instance):
parent = 'rach'
request = bots_pb2.CreateBotSessionRequest(parent=parent,
bot_session=bot_session)
response = instance.CreateBotSession(request, context)
assert isinstance(response, bots_pb2.BotSession)
assert uuid.UUID(response.name, version=4)
assert bot_session.bot_id == response.bot_id
def test_create_bot_session_bot_id_fail(context, instance):
bot = bots_pb2.BotSession()
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot)
instance.CreateBotSession(request, context)
context.set_code.assert_called_once_with(grpc.StatusCode.INVALID_ARGUMENT)
def test_update_bot_session(bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
bot = instance.CreateBotSession(request, context)
request = bots_pb2.UpdateBotSessionRequest(name=bot.name,
bot_session=bot)
response = instance.UpdateBotSession(request, context)
assert isinstance(response, bots_pb2.BotSession)
assert bot == response
def test_update_bot_session_zombie(bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
bot = instance.CreateBotSession(request, context)
# Update server with incorrect UUID by rotating it
bot.name = bot.name[len(bot.name): 0]
request = bots_pb2.UpdateBotSessionRequest(name=bot.name,
bot_session=bot)
response = instance.UpdateBotSession(request, context)
context.set_code.assert_called_once_with(grpc.StatusCode.INVALID_ARGUMENT)
def test_update_bot_session_bot_id_fail(bot_session, context, instance):
request = bots_pb2.UpdateBotSessionRequest(name='ana',
bot_session=bot_session)
instance.UpdateBotSession(request, context)
context.set_code.assert_called_once_with(grpc.StatusCode.INVALID_ARGUMENT)
@pytest.mark.parametrize("number_of_jobs", [0, 1, 3, 500])
def test_number_of_leases(number_of_jobs, bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
# Inject work
for n in range(0, number_of_jobs):
action_digest = remote_execution_pb2.Digest()
instance._instance._scheduler.append_job(job.Job(action_digest))
response = instance.CreateBotSession(request, context)
assert len(response.leases) == number_of_jobs
assert isinstance(response, bots_pb2.BotSession)
def test_update_leases_with_work(bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
# Inject work
action_digest = remote_execution_pb2.Digest(hash='gaff')
instance._instance._scheduler.append_job(job.Job(action_digest))
response = instance.CreateBotSession(request, context)
assert len(response.leases) == 1
response_action = remote_execution_pb2.Digest()
response.leases[0].payload.Unpack(response_action)
assert isinstance(response, bots_pb2.BotSession)
assert response.leases[0].state == LeaseState.PENDING.value
assert uuid.UUID(response.leases[0].id, version=4)
assert response_action == action_digest
def test_update_leases_work_complete(bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
# Create bot session
# Simulated the severed binding between client and server
response = copy.deepcopy(instance.CreateBotSession(request, context))
# Inject work
action_digest = remote_execution_pb2.Digest(hash='gaff')
instance._instance._scheduler.append_job(job.Job(action_digest))
request = bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response)
response = copy.deepcopy(instance.UpdateBotSession(request, context))
assert response.leases[0].state == LeaseState.PENDING.value
response.leases[0].state = LeaseState.ACTIVE.value
request = bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response)
response = copy.deepcopy(instance.UpdateBotSession(request, context))
response.leases[0].state = LeaseState.COMPLETED.value
request = bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response)
response = copy.deepcopy(instance.UpdateBotSession(request, context))
assert len(response.leases) == 0
def test_work_rejected_by_bot(bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
# Inject work
action_digest = remote_execution_pb2.Digest(hash='gaff')
instance._instance._scheduler.append_job(job.Job(action_digest))
# Simulated the severed binding between client and server
response = copy.deepcopy(instance.CreateBotSession(request, context))
# Reject work
assert response.leases[0].state == LeaseState.PENDING.value
response.leases[0].state = LeaseState.COMPLETED.value
request = bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response)
response = instance.UpdateBotSession(request, context)
context.set_code.assert_called_once_with(grpc.StatusCode.UNIMPLEMENTED)
@pytest.mark.parametrize("state", [LeaseState.LEASE_STATE_UNSPECIFIED, LeaseState.PENDING])
def test_work_out_of_sync_from_pending(state, bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
# Inject work
action_digest = remote_execution_pb2.Digest(hash='gaff')
instance._instance._scheduler.append_job(job.Job(action_digest))
# Simulated the severed binding between client and server
response = copy.deepcopy(instance.CreateBotSession(request, context))
response.leases[0].state = state.value
request = bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response)
response = instance.UpdateBotSession(request, context)
context.set_code.assert_called_once_with(grpc.StatusCode.DATA_LOSS)
@pytest.mark.parametrize("state", [LeaseState.LEASE_STATE_UNSPECIFIED, LeaseState.PENDING])
def test_work_out_of_sync_from_active(state, bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
# Inject work
action_digest = remote_execution_pb2.Digest(hash='gaff')
instance._instance._scheduler.append_job(job.Job(action_digest))
# Simulated the severed binding between client and server
response = copy.deepcopy(instance.CreateBotSession(request, context))
response.leases[0].state = LeaseState.ACTIVE.value
request = copy.deepcopy(bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response))
response = instance.UpdateBotSession(request, context)
response.leases[0].state = state.value
request = bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response)
response = instance.UpdateBotSession(request, context)
context.set_code.assert_called_once_with(grpc.StatusCode.DATA_LOSS)
def test_work_active_to_active(bot_session, context, instance):
request = bots_pb2.CreateBotSessionRequest(parent='',
bot_session=bot_session)
# Inject work
action_digest = remote_execution_pb2.Digest(hash='gaff')
instance._instance._scheduler.append_job(job.Job(action_digest))
# Simulated the severed binding between client and server
response = copy.deepcopy(instance.CreateBotSession(request, context))
response.leases[0].state = LeaseState.ACTIVE.value
request = bots_pb2.UpdateBotSessionRequest(name=response.name,
bot_session=response)
response = instance.UpdateBotSession(request, context)
assert response.leases[0].state == LeaseState.ACTIVE.value
def test_post_bot_event_temp(context, instance):
request = bots_pb2.PostBotEventTempRequest()
instance.PostBotEventTemp(request, context)
context.set_code.assert_called_once_with(grpc.StatusCode.UNIMPLEMENTED)