Exemplo n.º 1
0
    def test_raises_error_when_docker_api_error_encountered(self, mocker):
        stub_client = mocker.MagicMock(
            side_effect=docker.errors.APIError(None))
        mocker.patch('hostel_huptainer.containers.docker.client',
                     DockerClient=stub_client)

        with pytest.raises(ContainerError):
            list(MatchingContainers(None))
Exemplo n.º 2
0
    def test_error_encapsulates_docker_api_error_message(self, mocker):
        stub_client = mocker.MagicMock(
            side_effect=docker.errors.APIError("stub-message"))
        mocker.patch('hostel_huptainer.containers.docker.client',
                     DockerClient=stub_client)

        with pytest.raises(ContainerError) as error:
            list(MatchingContainers(None))

        assert "stub-message" in str(error)
Exemplo n.º 3
0
    def test_accesses_host_label_for_each_container(self, mocker, mock_list):
        stub_client = mocker.MagicMock()
        stub_client.return_value.containers.list.return_value = mock_list
        mocker.patch('hostel_huptainer.containers.docker.client',
                     DockerClient=stub_client)
        mocker.patch('hostel_huptainer.containers.csv_contains_value')

        list(MatchingContainers('stub-host.fqdn'))

        for mock_item in mock_list:
            mock_item.assert_has_calls(
                [mocker.call.labels.get('org.eff.certbot.cert_cns')])
Exemplo n.º 4
0
    def test___iter___does_not_yield_mismatching_containers(self, mocker):
        stub_item = mocker.MagicMock()
        stub_item.labels.get = lambda _: 'no-match.fqdn'
        stub_container_list = [stub_item]
        stub_client = mocker.MagicMock()
        stub_client.return_value.containers.list.return_value = (
            stub_container_list)
        mocker.patch('hostel_huptainer.containers.docker.client',
                     DockerClient=stub_client)

        matching_containers = MatchingContainers('stub-host.fqdn')

        assert stub_item not in list(matching_containers)
Exemplo n.º 5
0
def main():
    """Main program logic."""
    try:
        arguments = Arguments(sys.argv)
        environment = Environment(os.environ)
        containers = MatchingContainers(environment.hostname)
    except InputError as error:
        _handle_error(error)
    except ContainerError as error:
        _handle_error(error)
    else:
        for container in containers:
            send_signal(arguments.signal_method, container)
Exemplo n.º 6
0
    def test_passes_label_string_to_csv_contains_value(self, mocker,
                                                       label_list):
        stub_item = mocker.MagicMock()
        stub_item.labels.get = lambda _: label_list
        stub_container_list = [stub_item, stub_item]
        stub_client = mocker.MagicMock()
        stub_client.return_value.containers.list.return_value = (
            stub_container_list)
        mocker.patch('hostel_huptainer.containers.docker.client',
                     DockerClient=stub_client)
        mock_contains = mocker.patch(
            'hostel_huptainer.containers.csv_contains_value')

        _ = [item for item in MatchingContainers('stub-host.fqdn')]

        for _ in stub_container_list:
            mock_contains.assert_called_with(label_list, 'stub-host.fqdn')
Exemplo n.º 7
0
    def test_properly_passes_filter_to_container_list_method(self, mocker):
        stub_client = mocker.MagicMock()
        stub_client.return_value.containers.list.return_value = [
            mocker.MagicMock()
        ]
        mocker.patch('hostel_huptainer.containers.csv_contains_value')
        mock_docker = mocker.patch('hostel_huptainer.containers.docker.client',
                                   DockerClient=stub_client)

        list(MatchingContainers(None))

        expected_dict = {
            'label': 'org.eff.certbot.cert_cns',
            'status': 'running'
        }
        mock_docker.assert_has_calls([
            mocker.call.DockerClient().containers.list(filters=expected_dict)
        ])
Exemplo n.º 8
0
    def test_properly_sets_label_value(self, mocker, label):
        mocker.patch('hostel_huptainer.containers.docker')

        containers = MatchingContainers(label)

        assert containers.label_value == label