def test_create_unknown_image_raises_docker_error_no_such_image( profile_unknown_image, config): utils._DOCKER_CLIENTS = {} # clear clients cache config.DOCKER_MAX_TOTAL_RETRIES = 0 with pytest.raises(DockerError) as excinfo: create(profile_unknown_image.name) error = str(excinfo.value) # Error message depends on whether Docker Swarm is used assert any(("No such image" in error, "not exist" in error)) assert 'unknown_image' in error
def test_destroy_not_started(profile, docker_client): sandbox = create(profile.name) assert docker_client.containers.get(sandbox.container.id) destroy(sandbox) with pytest.raises(docker.errors.NotFound): docker_client.containers.get(sandbox.container.id)
def test_create_start_destroy_with_context_manager(profile, docker_client): with create(profile.name, 'cat') as sandbox: result = start(sandbox) assert result['stdout'] == b'' result = start(sandbox, stdin=b'stdin data') assert result['stdout'] == b'stdin data' with pytest.raises(docker.errors.NotFound): docker_client.containers.get(sandbox.container.id)
def test_destroy_exited(profile, docker_client): sandbox = create(profile.name, 'true') result = start(sandbox) assert result['exit_code'] == 0 assert docker_client.containers.get(sandbox.container.id) destroy(sandbox) with pytest.raises(docker.errors.NotFound): docker_client.containers.get(sandbox.container.id)
def test_destroy_running(profile, docker_client): sandbox = create(profile.name, 'sleep 10', limits={'realtime': 1}) result = start(sandbox) assert result['timeout'] is True container = docker_client.containers.get(sandbox.container.id) assert container.status == 'running' destroy(sandbox) with pytest.raises(docker.errors.NotFound): docker_client.containers.get(sandbox.container.id)
def test_create(profile, docker_client): test_containers = docker_client.containers.list( filters={'name': 'hlbox-test-'}, all=True) assert test_containers == [] sandbox = create(profile.name, 'true') container = docker_client.containers.get(sandbox.container.id) assert container.name.startswith('hlbox-test-') assert container.status == 'created' assert 'true' in container.attrs['Args'] assert sandbox.realtime_limit == 5
def test_start_with_stdin_data_str(profile): sandbox = create(profile.name, 'cat') result = start(sandbox, stdin="stdin data\n") expected_result = { 'exit_code': 0, 'stdout': b'stdin data\n', 'stderr': b'', 'duration': ANY, 'timeout': False, 'oom_killed': False, } assert result == expected_result assert result['duration'] > 0
def test_start_no_stdin_data(profile): command = 'echo "stdout data" && echo "stderr data" >&2' sandbox = create(profile.name, command) result = start(sandbox) expected_result = { 'exit_code': 0, 'stdout': b'stdout data\n', 'stderr': b'stderr data\n', 'duration': ANY, 'timeout': False, 'oom_killed': False, } assert result == expected_result assert result['duration'] > 0
def test_start_same_sandbox_multiple_times(profile): sandbox = create(profile.name, 'cat', limits={'cputime': 20}) expected_result = { 'exit_code': 0, 'stdout': b'', 'stderr': b'', 'duration': ANY, 'timeout': False, 'oom_killed': False, } result = start(sandbox) assert result == expected_result result = start(sandbox, stdin=b'stdin data') assert result == dict(expected_result, stdout=b'stdin data') long_data = b'stdin long data' + b'a b c d e\n' * 100000 result = start(sandbox, stdin=long_data) assert result == dict(expected_result, stdout=long_data) result = start(sandbox) assert result == expected_result