def test_circuit4(client): """ Tests that when getting the result for a container with a docker host that's not accessible, the circuit is open and a HostUnavailableError is raised """ with client.application.app_context(): client.application.config["DOCKER_CIRCUIT_BREAKER_MAX_FAILS"] = 1 task, job, execution = JobExecutionFixture.new_defaults( docker_host="localhost", docker_port=4567, container_id=str(uuid4())) pool = DockerPool(([None, ["localhost:4567"], 2], )) executor = Executor(client.application, pool) expect(executor.get_circuit("localhost:4567").current_state).to_equal( "closed") with expect.error_to_happen(HostUnavailableError): executor.get_result(task, job, execution) expect(executor.get_circuit("localhost:4567").current_state).to_equal( "open")
def test_get_result3(client): app = client.application with app.app_context(): container_mock = ContainerFixture.new_with_status( container_id="fastlane-job-123", name="fastlane-job-123") _, pool_mock, client_mock = PoolFixture.new_defaults( r"test[-].+", max_running=1, containers=[container_mock]) client_mock.containers.get.side_effect = requests.exceptions.ConnectionError( "failed") executor = Executor(app, pool_mock) _, job, execution = JobExecutionFixture.new_defaults( container_id="fastlane-job-123") msg = "Connection to host host:1234 failed with error: failed" with expect.error_to_happen(HostUnavailableError, message=msg): executor.get_result(job.task, job, execution)
def verify_get_result(client, status, exit_code, stdout, stderr, custom_error, started_at, finished_at): app = client.application with app.app_context(): container_mock = ContainerFixture.new_with_status( container_id="fastlane-job-123", name="fastlane-job-123", status=status, exit_code=exit_code, started_at=started_at, finished_at=finished_at, custom_error=custom_error, stdout=stdout, stderr=stderr, ) _, pool_mock, _ = PoolFixture.new_defaults(r"test[-].+", max_running=1, containers=[container_mock]) executor = Executor(app, pool_mock) _, job, execution = JobExecutionFixture.new_defaults( container_id="fastlane-job-123") result = executor.get_result(job.task, job, execution) expect(result.status).to_equal(STATUS.get(status)) expect(result.exit_code).to_equal(exit_code) if stdout is None: expect(result.log).to_be_empty() else: expect(result.log).to_equal(stdout) if stderr is not None and custom_error != "": expect( result.error).to_equal(f"{custom_error}\n\nstderr:\n{stderr}") else: if stderr is not None: expect(result.error).to_equal(stderr) else: expect(result.error).to_equal(custom_error) parsed_started_at = parse(started_at) expect(result.started_at).to_equal(parsed_started_at) if finished_at is not None: parsed_finished_at = parse(finished_at) else: parsed_finished_at = finished_at expect(result.finished_at).to_equal(parsed_finished_at)