예제 #1
0
def test_get_gpubsub_publisher_config_raises(
        config_key, exp_msg, config, auth_client, mock_pubsub_client,
        caplog, emulator, metrics):
    """Raise with improper configuration."""
    changes_chnl = asyncio.Queue()
    config.pop(config_key)

    with pytest.raises(exceptions.GCPConfigError) as e:
        client = janitor.get_gpubsub_publisher(config, changes_chnl, metrics)
        client.publisher.create_topic.assert_not_called()

    e.match(exp_msg)
    assert 1 == len(caplog.records)
예제 #2
0
def test_get_gpubsub_publisher_raises(
        config, auth_client, mock_pubsub_client, caplog, emulator, metrics):
    """Raise when there's an issue creating a Google Pub/Sub topic."""
    changes_chnl = asyncio.Queue()
    mock_pubsub_client.return_value.create_topic.side_effect = [
        Exception('fooo')
    ]

    with pytest.raises(exceptions.GCPGordonJanitorError) as e:
        client = janitor.get_gpubsub_publisher(config, changes_chnl, metrics)

        client.publisher.create_topic.assert_called_once_with(client.topic)
        e.match(f'Error trying to create topic "{client.topic}"')

    assert 1 == len(caplog.records)
예제 #3
0
def test_get_gpubsub_publisher_topic_exists(
        config, auth_client, mock_pubsub_client, emulator, metrics):
    """Do not raise if topic already exists."""
    changes_chnl = asyncio.Queue()
    exp = google_exceptions.AlreadyExists('foo')
    mock_pubsub_client.return_value.create_topic.side_effect = [exp]

    short_topic = config['topic']
    client = janitor.get_gpubsub_publisher(config, changes_chnl, metrics)

    exp_topic = f'projects/{config["project"]}/topics/{short_topic}'
    assert 60 == client.cleanup_timeout
    assert client.publisher is not None
    assert not client._messages
    assert exp_topic == client.topic

    client.publisher.create_topic.assert_called_once_with(exp_topic)
예제 #4
0
def test_get_gpubsub_publisher(local, timeout, exp_timeout, topic, config,
                               auth_client, emulator, mock_pubsub_client,
                               monkeypatch, metrics):
    """Happy path to initialize a GPubsubPublisher client."""
    changes_chnl = asyncio.Queue()

    if local:
        monkeypatch.setenv('PUBSUB_EMULATOR_HOST', True)

    if timeout:
        config['cleanup_timeout'] = timeout

    config['topic'] = topic
    client = janitor.get_gpubsub_publisher(config, changes_chnl, metrics)

    topic = topic.split('/')[-1]
    exp_topic = f'projects/{config["project"]}/topics/{topic}'
    assert exp_timeout == client.cleanup_timeout
    assert client.publisher is not None
    assert not client._messages

    client.publisher.create_topic.assert_called_once_with(exp_topic)