Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • edbaunton/buildgrid
  • BuildGrid/buildgrid
  • bloomberg/buildgrid
  • devcurmudgeon/buildgrid
  • mhadjimichael/buildgrid
  • jmacarthur/buildgrid
  • rkothur/buildgrid
  • valentindavid/buildgrid
  • jjardon/buildgrid
  • RichKen/buildgrid
  • jbonney/buildgrid
  • onsha_alexander/buildgrid
  • santigl/buildgrid
  • mostynb/buildgrid
  • hoffbrinkle/buildgrid
  • Malinskiy/buildgrid
  • coldtom/buildgrid
  • azeemb_a/buildgrid
  • pointswaves/buildgrid
  • BenjaminSchubert/buildgrid
  • michaellee8/buildgrid
  • anil-anil/buildgrid
  • seanborg/buildgrid
  • jdelong12/buildgrid
  • jclay/buildgrid
  • bweston92/buildgrid
  • zchen723/buildgrid
  • cpratt34/buildgrid
  • armbiant/apache-buildgrid
  • armbiant/android-buildgrid
  • itsme300/buildgrid
  • sbairoliya/buildgrid
32 results
Show changes
Commits on Source (2)
#!/usr/bin/env python3
#
# Copyright (C) 2018 Codethink Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
......
# 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 pytest
from unittest import mock
from buildgrid.server import scheduler, job
from buildgrid.server.execution import execution_instance
from buildgrid.server.execution._exceptions import InvalidArgumentError
# Mock of the scheduler class
@pytest.fixture
def schedule():
sched = mock.MagicMock(spec = scheduler.Scheduler)
sched.configure_mock(jobs = mock.MagicMock(spec = {}))
yield sched
# Actual instance to test
@pytest.fixture
def instance(schedule):
yield execution_instance.ExecutionInstance(schedule)
@pytest.mark.parametrize("skip_cache_lookup", [True, False])
@mock.patch.object(execution_instance,'Job', autospec = True)
def test_execute(mock_job, skip_cache_lookup, instance):
return_operation = 'rick'
action = 'pris'
mock_job.return_value.name = ''
mock_job.return_value.get_operation.return_value = return_operation
if skip_cache_lookup is False:
with pytest.raises(NotImplementedError):
instance.execute(action=action,
skip_cache_lookup=skip_cache_lookup)
else:
assert instance.execute(action=action,
skip_cache_lookup=skip_cache_lookup) is return_operation
# Action must be stored in the job
mock_job.assert_called_once_with(action)
# Only create one job per execute
instance._scheduler.append_job.assert_called_once_with(mock_job.return_value)
def test_get_operation(instance):
return_operation = mock.MagicMock(spec = job.Job)
instance._scheduler.jobs.get.return_value = return_operation
assert instance.get_operation('') is return_operation.get_operation.return_value
def test_get_operation_fail(instance):
instance._scheduler.jobs.get.return_value = None
with pytest.raises(InvalidArgumentError):
instance.get_operation('')
def test_list_operations(instance):
# List interface hasn't been fully implemented yet
instance.list_operations('change', 'me', 'in', 'future')
assert instance._scheduler.get_operations.call_count is 1
def test_delete_operation(instance):
name = 'roy'
instance._scheduler.jobs = mock.MagicMock(spec = {})
instance.delete_operation(name)
instance._scheduler.jobs.pop.assert_called_once_with(name)
def test_delete_operation_fail(instance):
name = 'roy'
instance._scheduler.jobs.pop.side_effect = KeyError()
with pytest.raises(InvalidArgumentError):
instance.delete_operation(name)
def test_cancel_operation(instance):
with pytest.raises(NotImplementedError):
instance.cancel_operation('')
# 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 pytest
from unittest import mock
from buildgrid.server import job
from buildgrid.server.job import ExecuteStage, LeaseState
from google.devtools.remoteexecution.v1test import remote_execution_pb2
from google.longrunning import operations_pb2
# Instance to test
@pytest.fixture
def instance():
j = job.Job('skin')
j._operation = mock.MagicMock(spec = operations_pb2.Operation)
j._operation.response = mock.Mock()
j._operation.metadata = mock.MagicMock(spec = remote_execution_pb2.ExecuteOperationMetadata)
yield j
@pytest.mark.parametrize("execute_stage", [ExecuteStage.QUEUED, ExecuteStage.COMPLETED])
@mock.patch.object(job.Job,'_pack_any', autospec = True)
@mock.patch.object(job.Job,'get_operation_meta', autospec = True)
def test_get_opertation(mock_get_meta, mock_pack, execute_stage, instance):
instance.execute_stage = execute_stage
result = 'orion'
instance.result = result
assert instance.get_operation() is instance._operation
instance._operation.metadata.CopyFrom.assert_called_once_with(mock_pack.return_value)
mock_get_meta.assert_called_once_with(instance)
if execute_stage is ExecuteStage.COMPLETED:
assert instance._operation.done
instance._operation.response.CopyFrom.assert_called_once_with(mock_pack.return_value)
mock_pack.assert_called_with(instance, result)
assert mock_pack.call_count is 2
else:
mock_pack.assert_called_once_with(instance, mock_get_meta.return_value)
@mock.patch.object(job,'ExecuteOperationMetadata', autospec = True)
def test_get_operation_meta(mock_meta, instance):
instance.execute_stage = ExecuteStage.COMPLETED
response = instance.get_operation_meta()
assert response is mock_meta.return_value
assert response.stage is instance.execute_stage.value
@mock.patch.object(job.bots_pb2, 'Lease', autospec = True)
@mock.patch.object(job.Job,'_pack_any', autospec = True)
def test_create_lease(mock_pack, mock_lease, instance):
action='harry'
name = 'bryant'
lease_state = LeaseState.PENDING
instance.action = action
instance.name = name
instance.lease_state = lease_state
assert instance.create_lease() is mock_lease.return_value
mock_pack.assert_called_once_with(instance, action)
mock_lease.assert_called_once_with(assignment=name,
inline_assignment=mock_pack.return_value,
state=lease_state.value)
@mock.patch.object(job.Job, 'get_operation', autospec = True)
@mock.patch.object(job.operations_pb2,'ListOperationsResponse', autospec = True)
def test_get_operations(mock_response, mock_get_operation, instance):
assert instance.get_operations() is mock_response.return_value
mock_get_operation.assert_called_once_with(instance)
mock_response.assert_called_once_with(operations=[mock_get_operation.return_value])
@mock.patch.object(job.any_pb2, 'Any', autospec = True)
def test__pack_any(mock_any, instance):
pack = 'rach'
assert instance._pack_any(pack) is mock_any.return_value
mock_any.assert_called_once_with()
# 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 pytest
from collections import deque
from unittest import mock
from buildgrid.server import scheduler
from buildgrid.server.job import ExecuteStage, LeaseState
# Instance to test
@pytest.fixture
def instance():
sched = scheduler.Scheduler()
sched.jobs = mock.MagicMock(spec = {})
sched.queue = mock.MagicMock(spec = deque)
yield sched
def test_append_job(instance):
mock_job = mock.MagicMock()
mock_job.return_value.name = ''
instance.append_job(mock_job)
instance.jobs.__setitem__.assert_called_once_with(mock_job.name, mock_job)
instance.queue.append.assert_called_once_with(mock_job)
def test_retry_job(instance):
n_tries = instance.MAX_N_TRIES - 1
name = 'eldon'
mock_job = mock.MagicMock()
mock_job.n_tries = n_tries
instance.jobs.__getitem__.return_value = mock_job
instance.retry_job(name)
assert mock_job.execute_stage is ExecuteStage.QUEUED
assert mock_job.n_tries is n_tries + 1
instance.jobs.__getitem__.assert_called_once_with(name)
instance.queue.appendleft.assert_called_once_with(mock_job)
instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
def test_retry_job_fail(instance):
n_tries = instance.MAX_N_TRIES
name = 'eldon'
mock_job = mock.MagicMock()
mock_job.n_tries = n_tries
instance.jobs.__getitem__.return_value = mock_job
instance.retry_job(name)
assert mock_job.execute_stage is ExecuteStage.COMPLETED
assert mock_job.n_tries is n_tries
instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
def test_create_job(instance):
name = 'eldon'
mock_job = mock.MagicMock()
mock_job.name = name
instance.queue.popleft.return_value = mock_job
instance.queue.__len__.return_value = 1
assert instance.create_job() is mock_job
assert mock_job.execute_stage is ExecuteStage.EXECUTING
instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
def test_job_complete(instance):
name = 'eldon'
result = 'bright'
mock_job = mock.MagicMock()
mock_job.name = name
instance.jobs.__getitem__.return_value = mock_job
instance.job_complete(name, result)
assert mock_job.execute_stage is ExecuteStage.COMPLETED
assert mock_job.result is result
instance.jobs.__getitem__.assert_called_once_with(name)
instance.jobs.__setitem__.assert_called_once_with(name, mock_job)
@pytest.mark.parametrize("state", [LeaseState.LEASE_STATE_UNSPECIFIED,
LeaseState.PENDING,
LeaseState.ACTIVE,
LeaseState.COMPLETED,
mock.Mock(value = 100)
])
@mock.patch.object(scheduler.Scheduler,'job_complete', autospec = True)
@mock.patch.object(scheduler.Scheduler,'create_job', autospec = True)
def test_update_lease_state_with_work(mock_create_job, mock_job_complete, state, instance):
name = 'orion'
mock_lease = mock.Mock()
mock_lease.assignment = name
mock_lease.state = state.value
if state == LeaseState.LEASE_STATE_UNSPECIFIED or \
state == LeaseState.COMPLETED:
assert instance.update_lease(mock_lease) is mock_create_job.return_value.lease
mock_create_job.assert_called_once_with(instance)
mock_create_job.return_value.create_lease.assert_called_once_with()
instance.jobs.__setitem__.assert_called_once_with(name, mock_create_job.return_value)
elif state == LeaseState.PENDING or \
state == LeaseState.ACTIVE or \
state == LeaseState.CANCELLED:
assert instance.update_lease(mock_lease) is mock_lease
else:
with pytest.raises(Exception):
instance.update_lease(mock_lease)
instance.jobs.get.assert_called_once_with(name)
@pytest.mark.parametrize("state", [LeaseState.LEASE_STATE_UNSPECIFIED,
LeaseState.PENDING,
LeaseState.ACTIVE,
LeaseState.COMPLETED,
mock.Mock(value = 100)
])
@mock.patch.object(scheduler.Scheduler,'job_complete', autospec = True)
@mock.patch.object(scheduler.Scheduler,'create_job', autospec = True)
def test_update_lease_state_without_work(mock_create_job, mock_job_complete, state, instance):
name = 'orion'
mock_lease = mock.Mock()
mock_lease.assignment = name
mock_lease.state = state.value
mock_create_job.return_value = None
if state == LeaseState.LEASE_STATE_UNSPECIFIED or \
state == LeaseState.COMPLETED:
assert instance.update_lease(mock_lease) is mock_lease
mock_create_job.assert_called_once_with(instance)
elif state == LeaseState.PENDING or \
state == LeaseState.ACTIVE or \
state == LeaseState.CANCELLED:
assert instance.update_lease(mock_lease) is mock_lease
else:
with pytest.raises(Exception):
instance.update_lease(mock_lease)
instance.jobs.get.assert_called_once_with(name)
@mock.patch.object(scheduler, 'operations_pb2', autospec = True)
def test_get_operations(mock_pb2, instance):
value = 'eldon'
response_value = mock.Mock()
response_value.get_operation.return_value = value
response_list = mock.MagicMock(spec = [])
response_list.return_value = [response_value]
instance.jobs.configure_mock(values = response_list)
response = mock.MagicMock()
mock_pb2.configure_mock(ListOperationsResponse = response)
assert instance.get_operations() is response.return_value
response_value.get_operation.assert_called_once()
response.return_value.operations.extend.assert_called_once_with([value])
response.assert_called_once_with()