finnball pushed to branch master at BuildGrid / buildgrid
Commits:
-
a06b3eac
by finn at 2018-07-26T11:02:09Z
3 changed files:
Changes:
1 |
-# Copyright (C) 2018 Codethink Limited
|
|
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 |
-#
|
|
15 |
-# Authors:
|
|
16 |
-# Finn Ball <finn ball codethink co uk>
|
|
17 |
- |
|
18 |
-import pytest
|
|
19 |
- |
|
20 |
-from unittest import mock
|
|
21 |
- |
|
22 |
-from buildgrid.server import scheduler, job
|
|
23 |
-from buildgrid.server.execution import execution_instance
|
|
24 |
-from buildgrid.server.execution._exceptions import InvalidArgumentError
|
|
25 |
- |
|
26 |
-# Mock of the scheduler class
|
|
27 |
-@pytest.fixture
|
|
28 |
-def schedule():
|
|
29 |
- sched = mock.MagicMock(spec = scheduler.Scheduler)
|
|
30 |
- sched.configure_mock(jobs = mock.MagicMock(spec = {}))
|
|
31 |
- yield sched
|
|
32 |
- |
|
33 |
-# Actual instance to test
|
|
34 |
-@pytest.fixture
|
|
35 |
-def instance(schedule):
|
|
36 |
- yield execution_instance.ExecutionInstance(schedule)
|
|
37 |
- |
|
38 |
-@pytest.mark.parametrize("skip_cache_lookup", [True, False])
|
|
39 |
-@mock.patch.object(execution_instance,'Job', autospec = True)
|
|
40 |
-def test_execute(mock_job, skip_cache_lookup, instance):
|
|
41 |
- return_operation = 'rick'
|
|
42 |
- action = 'pris'
|
|
43 |
- mock_job.return_value.name = ''
|
|
44 |
- mock_job.return_value.get_operation.return_value = return_operation
|
|
45 |
- |
|
46 |
- if skip_cache_lookup is False:
|
|
47 |
- with pytest.raises(NotImplementedError):
|
|
48 |
- instance.execute(action=action,
|
|
49 |
- skip_cache_lookup=skip_cache_lookup)
|
|
50 |
- else:
|
|
51 |
- assert instance.execute(action=action,
|
|
52 |
- skip_cache_lookup=skip_cache_lookup) is return_operation
|
|
53 |
- # Action must be stored in the job
|
|
54 |
- mock_job.assert_called_once_with(action)
|
|
55 |
- # Only create one job per execute
|
|
56 |
- instance._scheduler.append_job.assert_called_once_with(mock_job.return_value)
|
|
57 |
- |
|
58 |
-def test_get_operation(instance):
|
|
59 |
- return_operation = mock.MagicMock(spec = job.Job)
|
|
60 |
- instance._scheduler.jobs.get.return_value = return_operation
|
|
61 |
- |
|
62 |
- assert instance.get_operation('') is return_operation.get_operation.return_value
|
|
63 |
- |
|
64 |
-def test_get_operation_fail(instance):
|
|
65 |
- instance._scheduler.jobs.get.return_value = None
|
|
66 |
- |
|
67 |
- with pytest.raises(InvalidArgumentError):
|
|
68 |
- instance.get_operation('')
|
|
69 |
- |
|
70 |
-def test_list_operations(instance):
|
|
71 |
- # List interface hasn't been fully implemented yet
|
|
72 |
- instance.list_operations('change', 'me', 'in', 'future')
|
|
73 |
- assert instance._scheduler.get_operations.call_count is 1
|
|
74 |
- |
|
75 |
-def test_delete_operation(instance):
|
|
76 |
- name = 'roy'
|
|
77 |
- instance._scheduler.jobs = mock.MagicMock(spec = {})
|
|
78 |
- instance.delete_operation(name)
|
|
79 |
- instance._scheduler.jobs.pop.assert_called_once_with(name)
|
|
80 |
- |
|
81 |
-def test_delete_operation_fail(instance):
|
|
82 |
- name = 'roy'
|
|
83 |
- instance._scheduler.jobs.pop.side_effect = KeyError()
|
|
84 |
- with pytest.raises(InvalidArgumentError):
|
|
85 |
- instance.delete_operation(name)
|
|
86 |
- |
|
87 |
-def test_cancel_operation(instance):
|
|
88 |
- with pytest.raises(NotImplementedError):
|
|
89 |
- instance.cancel_operation('')
|
1 |
-# Copyright (C) 2018 Codethink Limited
|
|
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 |
-#
|
|
15 |
-# Authors:
|
|
16 |
-# Finn Ball <finn ball codethink co uk>
|
|
17 |
- |
|
18 |
-import pytest
|
|
19 |
- |
|
20 |
-from unittest import mock
|
|
21 |
- |
|
22 |
-from buildgrid.server import job
|
|
23 |
-from buildgrid.server.job import ExecuteStage, LeaseState
|
|
24 |
- |
|
25 |
-from google.devtools.remoteexecution.v1test import remote_execution_pb2
|
|
26 |
-from google.longrunning import operations_pb2
|
|
27 |
- |
|
28 |
-# Instance to test
|
|
29 |
-@pytest.fixture
|
|
30 |
-def instance():
|
|
31 |
- j = job.Job('skin')
|
|
32 |
- j._operation = mock.MagicMock(spec = operations_pb2.Operation)
|
|
33 |
- j._operation.response = mock.Mock()
|
|
34 |
- j._operation.metadata = mock.MagicMock(spec = remote_execution_pb2.ExecuteOperationMetadata)
|
|
35 |
- yield j
|
|
36 |
- |
|
37 |
-@pytest.mark.parametrize("execute_stage", [ExecuteStage.QUEUED, ExecuteStage.COMPLETED])
|
|
38 |
-@mock.patch.object(job.Job,'_pack_any', autospec = True)
|
|
39 |
-@mock.patch.object(job.Job,'get_operation_meta', autospec = True)
|
|
40 |
-def test_get_opertation(mock_get_meta, mock_pack, execute_stage, instance):
|
|
41 |
- instance.execute_stage = execute_stage
|
|
42 |
- result = 'orion'
|
|
43 |
- instance.result = result
|
|
44 |
- |
|
45 |
- assert instance.get_operation() is instance._operation
|
|
46 |
- |
|
47 |
- instance._operation.metadata.CopyFrom.assert_called_once_with(mock_pack.return_value)
|
|
48 |
- mock_get_meta.assert_called_once_with(instance)
|
|
49 |
- |
|
50 |
- if execute_stage is ExecuteStage.COMPLETED:
|
|
51 |
- assert instance._operation.done
|
|
52 |
- instance._operation.response.CopyFrom.assert_called_once_with(mock_pack.return_value)
|
|
53 |
- mock_pack.assert_called_with(instance, result)
|
|
54 |
- assert mock_pack.call_count is 2
|
|
55 |
- else:
|
|
56 |
- mock_pack.assert_called_once_with(instance, mock_get_meta.return_value)
|
|
57 |
- |
|
58 |
-@mock.patch.object(job,'ExecuteOperationMetadata', autospec = True)
|
|
59 |
-def test_get_operation_meta(mock_meta, instance):
|
|
60 |
- instance.execute_stage = ExecuteStage.COMPLETED
|
|
61 |
- response = instance.get_operation_meta()
|
|
62 |
- |
|
63 |
- assert response is mock_meta.return_value
|
|
64 |
- assert response.stage is instance.execute_stage.value
|
|
65 |
- |
|
66 |
-@mock.patch.object(job.bots_pb2, 'Lease', autospec = True)
|
|
67 |
-@mock.patch.object(job.Job,'_pack_any', autospec = True)
|
|
68 |
-def test_create_lease(mock_pack, mock_lease, instance):
|
|
69 |
- action='harry'
|
|
70 |
- name = 'bryant'
|
|
71 |
- lease_state = LeaseState.PENDING
|
|
72 |
- |
|
73 |
- instance.action = action
|
|
74 |
- instance.name = name
|
|
75 |
- instance.lease_state = lease_state
|
|
76 |
- |
|
77 |
- assert instance.create_lease() is mock_lease.return_value
|
|
78 |
- mock_pack.assert_called_once_with(instance, action)
|
|
79 |
- mock_lease.assert_called_once_with(assignment=name,
|
|
80 |
- inline_assignment=mock_pack.return_value,
|
|
81 |
- state=lease_state.value)
|
|
82 |
- |
|
83 |
-@mock.patch.object(job.Job, 'get_operation', autospec = True)
|
|
84 |
-@mock.patch.object(job.operations_pb2,'ListOperationsResponse', autospec = True)
|
|
85 |
-def test_get_operations(mock_response, mock_get_operation, instance):
|
|
86 |
- assert instance.get_operations() is mock_response.return_value
|
|
87 |
- mock_get_operation.assert_called_once_with(instance)
|
|
88 |
- mock_response.assert_called_once_with(operations=[mock_get_operation.return_value])
|
|
89 |
- |
|
90 |
-@mock.patch.object(job.any_pb2, 'Any', autospec = True)
|
|
91 |
-def test__pack_any(mock_any, instance):
|
|
92 |
- pack = 'rach'
|
|
93 |
- assert instance._pack_any(pack) is mock_any.return_value
|
|
94 |
- |
|
95 |
- mock_any.assert_called_once_with()
|
1 |
-# Copyright (C) 2018 Codethink Limited
|
|
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 |
-#
|
|
15 |
-# Authors:
|
|
16 |
-# Finn Ball <finn ball codethink co uk>
|
|
17 |
- |
|
18 |
-import pytest
|
|
19 |
- |
|
20 |
-from collections import deque
|
|
21 |
-from unittest import mock
|
|
22 |
- |
|
23 |
-from buildgrid.server import scheduler
|
|
24 |
-from buildgrid.server.job import ExecuteStage, LeaseState
|
|
25 |
- |
|
26 |
-# Instance to test
|
|
27 |
-@pytest.fixture
|
|
28 |
-def instance():
|
|
29 |
- sched = scheduler.Scheduler()
|
|
30 |
- sched.jobs = mock.MagicMock(spec = {})
|
|
31 |
- sched.queue = mock.MagicMock(spec = deque)
|
|
32 |
- yield sched
|
|
33 |
- |
|
34 |
-def test_append_job(instance):
|
|
35 |
- mock_job = mock.MagicMock()
|
|
36 |
- mock_job.return_value.name = ''
|
|
37 |
- instance.append_job(mock_job)
|
|
38 |
- instance.jobs.__setitem__.assert_called_once_with(mock_job.name, mock_job)
|
|
39 |
- instance.queue.append.assert_called_once_with(mock_job)
|
|
40 |
- |
|
41 |
-def test_retry_job(instance):
|
|
42 |
- n_tries = instance.MAX_N_TRIES - 1
|
|
43 |
- name = 'eldon'
|
|
44 |
- |
|
45 |
- mock_job = mock.MagicMock()
|
|
46 |
- mock_job.n_tries = n_tries
|
|
47 |
- instance.jobs.__getitem__.return_value = mock_job
|
|
48 |
- |
|
49 |
- instance.retry_job(name)
|
|
50 |
- |
|
51 |
- assert mock_job.execute_stage is ExecuteStage.QUEUED
|
|
52 |
- assert mock_job.n_tries is n_tries + 1
|
|
53 |
- instance.jobs.__getitem__.assert_called_once_with(name)
|
|
54 |
- instance.queue.appendleft.assert_called_once_with(mock_job)
|
|
55 |
- instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
|
|
56 |
- |
|
57 |
-def test_retry_job_fail(instance):
|
|
58 |
- n_tries = instance.MAX_N_TRIES
|
|
59 |
- name = 'eldon'
|
|
60 |
- |
|
61 |
- mock_job = mock.MagicMock()
|
|
62 |
- mock_job.n_tries = n_tries
|
|
63 |
- instance.jobs.__getitem__.return_value = mock_job
|
|
64 |
- |
|
65 |
- instance.retry_job(name)
|
|
66 |
- |
|
67 |
- assert mock_job.execute_stage is ExecuteStage.COMPLETED
|
|
68 |
- assert mock_job.n_tries is n_tries
|
|
69 |
- instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
|
|
70 |
- |
|
71 |
-def test_create_job(instance):
|
|
72 |
- name = 'eldon'
|
|
73 |
- |
|
74 |
- mock_job = mock.MagicMock()
|
|
75 |
- mock_job.name = name
|
|
76 |
- instance.queue.popleft.return_value = mock_job
|
|
77 |
- instance.queue.__len__.return_value = 1
|
|
78 |
- |
|
79 |
- assert instance.create_job() is mock_job
|
|
80 |
- |
|
81 |
- assert mock_job.execute_stage is ExecuteStage.EXECUTING
|
|
82 |
- instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
|
|
83 |
- |
|
84 |
-def test_job_complete(instance):
|
|
85 |
- name = 'eldon'
|
|
86 |
- result = 'bright'
|
|
87 |
- |
|
88 |
- mock_job = mock.MagicMock()
|
|
89 |
- mock_job.name = name
|
|
90 |
- instance.jobs.__getitem__.return_value = mock_job
|
|
91 |
- |
|
92 |
- instance.job_complete(name, result)
|
|
93 |
- |
|
94 |
- assert mock_job.execute_stage is ExecuteStage.COMPLETED
|
|
95 |
- assert mock_job.result is result
|
|
96 |
- instance.jobs.__getitem__.assert_called_once_with(name)
|
|
97 |
- instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
|
|
98 |
- |
|
99 |
-@pytest.mark.parametrize("state", [LeaseState.LEASE_STATE_UNSPECIFIED,
|
|
100 |
- LeaseState.PENDING,
|
|
101 |
- LeaseState.ACTIVE,
|
|
102 |
- LeaseState.COMPLETED,
|
|
103 |
- mock.Mock(value = 100)
|
|
104 |
- ])
|
|
105 |
-@mock.patch.object(scheduler.Scheduler,'job_complete', autospec = True)
|
|
106 |
-@mock.patch.object(scheduler.Scheduler,'create_job', autospec = True)
|
|
107 |
-def test_update_lease_state_with_work(mock_create_job, mock_job_complete, state, instance):
|
|
108 |
- name = 'orion'
|
|
109 |
- |
|
110 |
- mock_lease = mock.Mock()
|
|
111 |
- mock_lease.assignment = name
|
|
112 |
- mock_lease.state = state.value
|
|
113 |
- |
|
114 |
- if state == LeaseState.LEASE_STATE_UNSPECIFIED or \
|
|
115 |
- state == LeaseState.COMPLETED:
|
|
116 |
- assert instance.update_lease(mock_lease) is mock_create_job.return_value.lease
|
|
117 |
- mock_create_job.assert_called_once_with(instance)
|
|
118 |
- mock_create_job.return_value.create_lease.assert_called_once_with()
|
|
119 |
- instance.jobs.__setitem__.assert_called_once_with(name, mock_create_job.return_value)
|
|
120 |
- |
|
121 |
- elif state == LeaseState.PENDING or \
|
|
122 |
- state == LeaseState.ACTIVE or \
|
|
123 |
- state == LeaseState.CANCELLED:
|
|
124 |
- assert instance.update_lease(mock_lease) is mock_lease
|
|
125 |
- |
|
126 |
- else:
|
|
127 |
- with pytest.raises(Exception):
|
|
128 |
- instance.update_lease(mock_lease)
|
|
129 |
- |
|
130 |
- instance.jobs.get.assert_called_once_with(name)
|
|
131 |
- |
|
132 |
-@pytest.mark.parametrize("state", [LeaseState.LEASE_STATE_UNSPECIFIED,
|
|
133 |
- LeaseState.PENDING,
|
|
134 |
- LeaseState.ACTIVE,
|
|
135 |
- LeaseState.COMPLETED,
|
|
136 |
- mock.Mock(value = 100)
|
|
137 |
- ])
|
|
138 |
-@mock.patch.object(scheduler.Scheduler,'job_complete', autospec = True)
|
|
139 |
-@mock.patch.object(scheduler.Scheduler,'create_job', autospec = True)
|
|
140 |
-def test_update_lease_state_without_work(mock_create_job, mock_job_complete, state, instance):
|
|
141 |
- name = 'orion'
|
|
142 |
- |
|
143 |
- mock_lease = mock.Mock()
|
|
144 |
- mock_lease.assignment = name
|
|
145 |
- mock_lease.state = state.value
|
|
146 |
- |
|
147 |
- mock_create_job.return_value = None
|
|
148 |
- |
|
149 |
- if state == LeaseState.LEASE_STATE_UNSPECIFIED or \
|
|
150 |
- state == LeaseState.COMPLETED:
|
|
151 |
- assert instance.update_lease(mock_lease) is mock_lease
|
|
152 |
- mock_create_job.assert_called_once_with(instance)
|
|
153 |
- |
|
154 |
- elif state == LeaseState.PENDING or \
|
|
155 |
- state == LeaseState.ACTIVE or \
|
|
156 |
- state == LeaseState.CANCELLED:
|
|
157 |
- assert instance.update_lease(mock_lease) is mock_lease
|
|
158 |
- |
|
159 |
- else:
|
|
160 |
- with pytest.raises(Exception):
|
|
161 |
- instance.update_lease(mock_lease)
|
|
162 |
- |
|
163 |
- instance.jobs.get.assert_called_once_with(name)
|
|
164 |
- |
|
165 |
-@mock.patch.object(scheduler, 'operations_pb2', autospec = True)
|
|
166 |
-def test_get_operations(mock_pb2, instance):
|
|
167 |
- value = 'eldon'
|
|
168 |
- response_value = mock.Mock()
|
|
169 |
- response_value.get_operation.return_value = value
|
|
170 |
- response_list = mock.MagicMock(spec = [])
|
|
171 |
- response_list.return_value = [response_value]
|
|
172 |
- instance.jobs.configure_mock(values = response_list)
|
|
173 |
- |
|
174 |
- response = mock.MagicMock()
|
|
175 |
- mock_pb2.configure_mock(ListOperationsResponse = response)
|
|
176 |
- |
|
177 |
- assert instance.get_operations() is response.return_value
|
|
178 |
- response_value.get_operation.assert_called_once()
|
|
179 |
- response.return_value.operations.extend.assert_called_once_with([value])
|
|
180 |
- response.assert_called_once_with()
|