async def test_get_projects_with_whitelist(mocker, whitelist, expected, authority_config, get_gce_client, create_mock_coro, metrics): active_projects_mock, active_projects_coro = create_mock_coro() active_projects_mock.return_value = [{ 'projectId': "project_1" }, { 'projectId': "project_2" }] crm_client = get_gce_client(gcrm.GCRMClient) crm_client.list_all_active_projects = active_projects_coro gce_client = get_gce_client(gce.GCEClient) mock_rrset_channel = mocker.Mock() authority_config['project_whitelist'] = whitelist gce_authority = authority.GCEAuthority(authority_config, metrics, crm_client, gce_client, mock_rrset_channel) actual = await gce_authority._get_projects() assert actual == expected
async def test_run_fails_when_unexpected_error_raised( mocker, authority_config, get_gce_client, create_mock_coro, instance_data, exception_args, exception, metrics): instances = [instance_data] active_projects_mock, active_projects_coro = create_mock_coro() active_projects_mock.return_value = [{'projectId': 1}, {'projectId': 2}] crm_client = get_gce_client(gcrm.GCRMClient) crm_client.list_all_active_projects = active_projects_coro list_instances_mock, list_instances_coro = create_mock_coro() list_instances_mock.side_effect = [exception(*exception_args), instances] gce_client = get_gce_client(gce.GCEClient) gce_client.list_instances = list_instances_coro mock_rrset_channel = mocker.Mock() rrset_channel_put_mock, rrset_channel_put_coro = create_mock_coro() mock_rrset_channel.put = rrset_channel_put_coro gce_authority = authority.GCEAuthority(authority_config, metrics, crm_client, gce_client, mock_rrset_channel) with pytest.raises(exception): await gce_authority.run() rrset_channel_put_mock.assert_not_called()
async def test_run_continues_when_404_error_raised(mocker, caplog, authority_config, get_gce_client, create_mock_coro, instance_data, metrics): caplog.set_level(logging.WARN) instances = [instance_data] active_projects_mock, active_projects_coro = create_mock_coro() active_projects_mock.return_value = [{'projectId': 1}, {'projectId': 2}] crm_client = get_gce_client(gcrm.GCRMClient) crm_client.list_all_active_projects = active_projects_coro list_instances_mock, list_instances_coro = create_mock_coro() list_instances_mock.side_effect = [ exceptions.GCPHTTPResponseError('Error!', 404), instances ] gce_client = get_gce_client(gce.GCEClient) gce_client.list_instances = list_instances_coro mock_rrset_channel = mocker.Mock() rrset_channel_put_mock, rrset_channel_put_coro = create_mock_coro() mock_rrset_channel.put = rrset_channel_put_coro gce_authority = authority.GCEAuthority(authority_config, metrics, crm_client, gce_client, mock_rrset_channel) await gce_authority.run() # warning log project was skipped assert 1 == len(caplog.records) assert 2 == rrset_channel_put_mock.call_count
def fake_authority(mocker, monkeypatch, authority_config, auth_client, metrics): monkeypatch.setattr( 'gordon_gcp.plugins.janitor.authority.auth.GAuthClient', mocker.Mock(return_value=auth_client)) rrset_channel = asyncio.Queue() fake_authority = authority.GCEAuthority( authority_config, metrics, None, rrset_channel) fake_authority.session = auth_client._session return fake_authority
async def test_run_publishes_msg_to_channel(mocker, authority_config, get_gce_client, create_mock_coro, instance_data, metrics): instances = [] for i in range(1, 4): inst = copy.deepcopy(instance_data) inst['name'] = f'host-{i}' inst['networkInterfaces'][0]['accessConfigs'][0][ 'natIp'] = f'1.1.1.{i}' instances.append(inst) active_projects_mock, active_projects_coro = create_mock_coro() active_projects_mock.return_value = [{'projectId': 1}, {'projectId': 2}] crm_client = get_gce_client(gcrm.GCRMClient) crm_client.list_all_active_projects = active_projects_coro list_instances_mock, list_instances_coro = create_mock_coro() list_instances_mock.return_value = instances gce_client = get_gce_client(gce.GCEClient) gce_client.list_instances = list_instances_coro rrset_channel = asyncio.Queue() gce_authority = authority.GCEAuthority(authority_config, metrics, crm_client, gce_client, rrset_channel) await gce_authority.run() _expected_rrsets = [] for instance in instances: ip = instance['networkInterfaces'][0]['accessConfigs'][0]['natIP'] _expected_rrsets.append({ 'name': f"{instance['name']}.{authority_config['dns_zone']}", 'type': 'A', 'rrdatas': [ip], 'source': 'gceauthority' }) expected_rrsets = _expected_rrsets * 2 expected_msg = { 'zone': authority_config['dns_zone'], 'rrsets': expected_rrsets, } # one message includes data for all projects assert expected_msg == (await rrset_channel.get()) # run also calls self.cleanup at the end assert (await rrset_channel.get()) is None context = {'plugin': 'gceauthority'} gce_authority.metrics._timer_mock.assert_called_once_with('plugin-runtime', context=context) gce_authority.metrics.timer_stub.start_mock.assert_called_once_with() gce_authority.metrics.timer_stub.stop_mock.assert_called_once_with() context = { 'plugin': 'gceauthority', } gce_authority.metrics._set_mock.assert_has_calls( [mock.call('projects', 2, context=context)]) context = { 'plugin': 'gceauthority', 'zone': authority_config['dns_zone'], } gce_authority.metrics._set_mock.assert_has_calls( [mock.call('rrsets', len(expected_rrsets), context=context)])