def test_run2(client): """ Tests that a docker executor raises HostUnavailableError when host is not available when running a container and deletes host and port metadata from execution """ with client.application.app_context(): task, job, execution = JobExecutionFixture.new_defaults() _, pool_mock, client_mock = PoolFixture.new_defaults(r"test-.+") client_mock.containers.run.side_effect = requests.exceptions.ConnectionError( "failed") exe = Executor(app=client.application, pool=pool_mock) msg = "Connection to host host:1234 failed with error: failed" with expect.error_to_happen(HostUnavailableError, message=msg): exe.run( task, job, execution, "mock-image", "latest", "command", blacklisted_hosts=set(), ) expect(execution.metadata).not_to_include("docker_host") expect(execution.metadata).not_to_include("docker_port")
def test_run1(client): """Tests that a docker executor can run containers""" with client.application.app_context(): task, job, execution = JobExecutionFixture.new_defaults() _, pool_mock, client_mock = PoolFixture.new_defaults(r"test-.+") client_mock.containers.run.return_value = MagicMock(id="job_id") exe = Executor(app=client.application, pool=pool_mock) exe.run( task, job, execution, "mock-image", "latest", "command", blacklisted_hosts=set(), ) expect(execution.metadata).to_include("container_id") expect(client_mock.containers.run.call_count).to_equal(1) client_mock.containers.run.assert_called_with( image=f"mock-image:latest", environment={}, command="command", detach=True, name=f"fastlane-job-{execution.execution_id}", extra_hosts={})
def test_run3(client): """ Tests that a docker executor raises RuntimeError if no docker_host and docker_port available in execution """ with client.application.app_context(): task, job, execution = JobExecutionFixture.new_defaults() _, pool_mock, _ = PoolFixture.new_defaults(r"test-.+") exe = Executor(app=client.application, pool=pool_mock) del execution.metadata["docker_host"] del execution.metadata["docker_port"] msg = "Can't run job without docker_host and docker_port in execution metadata." with expect.error_to_happen(RuntimeError, message=msg): exe.run( task, job, execution, "mock-image", "latest", "command", blacklisted_hosts=set(), ) expect(execution.metadata).not_to_include("docker_host") expect(execution.metadata).not_to_include("docker_port")
def test_circuit2(client): """ Tests that when running a container with a docker host that's not accessible, the circuit is open, the host and port are removed from the job's metadata 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) 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.run(task, job, execution, "ubuntu", "latest", "ls -la") expect(executor.get_circuit("localhost:4567").current_state).to_equal( "open")