def test_execute_koku_provider_op_destroy(self):
        """Test to execute Koku Operations to sync with Sources for destruction."""
        source_id = 1
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        mock_koku_uuid = faker.uuid4()

        provider = Sources(source_id=source_id,
                           auth_header=auth_header,
                           offset=offset,
                           koku_uuid=mock_koku_uuid)
        provider.save()

        with requests_mock.mock() as m:
            m.delete(
                f'http://www.koku.com/api/cost-management/v1/providers/{mock_koku_uuid}/',
                status_code=204)
            msg = {
                'operation': 'destroy',
                'provider': provider,
                'offset': provider.offset
            }
            source_integration.execute_koku_provider_op(msg)
            self.assertEqual(
                Sources.objects.filter(source_id=source_id).exists(), False)
    def test_execute_koku_provider_op_destroy_non_recoverable_error(self):
        """Test to execute Koku Operations to sync with Sources with non-recoverable error."""
        source_id = 1
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2

        provider = Sources(source_id=source_id,
                           auth_header=auth_header,
                           offset=offset)
        provider.save()

        logging.disable(logging.NOTSET)
        with requests_mock.mock() as m:
            m.post('http://www.koku.com/api/cost-management/v1/providers/',
                   status_code=400,
                   json={'uuid': faker.uuid4()})
            with self.assertLogs('sources.kafka_listener',
                                 level='ERROR') as logger:
                msg = {
                    'operation': 'create',
                    'provider': provider,
                    'offset': provider.offset
                }
                source_integration.execute_koku_provider_op(msg)
                self.assertIn(':Unable to create provider for Source ID: 1',
                              logger.output[0])
示例#3
0
    def test_execute_koku_provider_op_destroy_provider_not_found(self):
        """Test to execute Koku Operations to sync with Sources for destruction with provider missing.

        First, raise ProviderBuilderError. Check that provider and source still exists.
        Then, re-call provider destroy without exception, then see both source and provider are gone.

        """
        source_id = self.source_ids.get(Provider.PROVIDER_AWS)
        provider = Sources(**self.sources.get(Provider.PROVIDER_AWS))
        provider.save()
        # check that the source exists
        self.assertTrue(Sources.objects.filter(source_id=source_id).exists())

        with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True):
            builder = SourcesProviderCoordinator(source_id, provider.auth_header)
            builder.create_account(provider)

        self.assertTrue(Provider.objects.filter(uuid=provider.source_uuid).exists())
        provider = Sources.objects.get(source_id=source_id)

        msg = {"operation": "destroy", "provider": provider, "offset": provider.offset}
        with patch.object(SourcesHTTPClient, "set_source_status"):
            with patch.object(ProviderBuilder, "destroy_provider", side_effect=raise_provider_manager_error):
                source_integration.execute_koku_provider_op(msg)
                self.assertTrue(Provider.objects.filter(uuid=provider.source_uuid).exists())
                self.assertTrue(Sources.objects.filter(source_uuid=provider.source_uuid).exists())
                self.assertTrue(Sources.objects.filter(koku_uuid=provider.source_uuid).exists())

        with patch.object(SourcesHTTPClient, "set_source_status"):
            source_integration.execute_koku_provider_op(msg)
        self.assertFalse(Provider.objects.filter(uuid=provider.source_uuid).exists())
    def test_execute_koku_provider_op_update(self):
        """Test to execute Koku Operations to sync with Sources for destruction."""
        source_id = 1
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        mock_koku_uuid = faker.uuid4()

        provider = Sources(source_id=source_id,
                           auth_header=auth_header,
                           offset=offset,
                           koku_uuid=mock_koku_uuid,
                           pending_update=True)
        provider.save()

        with requests_mock.mock() as m:
            m.put(
                f'http://www.koku.com/api/cost-management/v1/providers/{mock_koku_uuid}/',
                status_code=200,
                json={})
            msg = {
                'operation': 'update',
                'provider': provider,
                'offset': provider.offset
            }
            source_integration.execute_koku_provider_op(msg)
            response = Sources.objects.get(source_id=source_id)
            self.assertEquals(response.pending_update, False)
示例#5
0
    def test_execute_koku_provider_op_destroy_non_recoverable_error(self):
        """Test to execute Koku Operations to sync with Sources with non-recoverable error."""
        source_id = 1
        app_id = 1
        application_type_id = 2
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2

        provider = Sources(source_id=source_id, auth_header=auth_header, offset=offset)
        provider.save()

        logging.disable(logging.NOTSET)
        with requests_mock.mock() as m:
            m.post(
                "http://www.koku.com/api/cost-management/v1/providers/",
                status_code=400,
                json={"errors": [{"detail": "koku check failed"}]},
            )
            m.get(
                SOURCES_APPS.format(application_type_id, source_id), status_code=200, json={"data": [{"id": app_id}]}
            )
            m.patch(f"http://www.sources.com/api/v1.0/applications/{app_id}", status_code=204)
            with self.assertLogs("sources.kafka_listener", level="ERROR") as logger:
                msg = {"operation": "create", "provider": provider, "offset": provider.offset}
                source_integration.execute_koku_provider_op(msg, application_type_id)
                self.assertIn(":Unable to create provider for Source ID: 1", logger.output[0])
示例#6
0
    def test_execute_koku_provider_op_update(self):
        """Test to execute Koku Operations to sync with Sources for destruction."""
        source_id = 1
        app_id = 1
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        mock_koku_uuid = faker.uuid4()
        application_type_id = 2

        provider = Sources(
            source_id=source_id, auth_header=auth_header, offset=offset, koku_uuid=mock_koku_uuid, pending_update=True
        )
        provider.save()

        with requests_mock.mock() as m:
            m.put(f"http://www.koku.com/api/cost-management/v1/providers/{mock_koku_uuid}/", status_code=200, json={})
            m.get(
                f"http://www.sources.com/api/v1.0/applications?filter[source_id]={source_id}",
                status_code=200,
                json={"data": [{"id": app_id}]},
            )
            m.patch(f"http://www.sources.com/api/v1.0/applications/{app_id}", status_code=204)
            msg = {"operation": "update", "provider": provider, "offset": provider.offset}
            source_integration.execute_koku_provider_op(msg, application_type_id)
            response = Sources.objects.get(source_id=source_id)
            self.assertEquals(response.pending_update, False)
示例#7
0
    def test_execute_koku_provider_op_destroy_non_recoverable_error(self):
        """Test to execute Koku Operations to sync with Sources with non-recoverable error."""
        source_id = 1
        app_id = 1
        application_type_id = 2
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2

        provider = Sources(source_id=source_id, auth_header=auth_header, offset=offset)
        provider.save()

        logging.disable(logging.NOTSET)
        with requests_mock.mock() as m:
            m.post('http://www.koku.com/api/cost-management/v1/providers/',
                   status_code=400,
                   json={'errors': [{'detail': 'koku check failed'}]})
            m.get('http://www.sources.com/api/v1.0/applications?filter[application_type_id]={}&filter[source_id]={}'.
                  format(application_type_id, source_id),
                  status_code=200, json={'data': [{'id': app_id}]})
            m.patch(f'http://www.sources.com/api/v1.0/applications/{app_id}',
                    status_code=204)
            with self.assertLogs('sources.kafka_listener', level='ERROR') as logger:
                msg = {'operation': 'create', 'provider': provider, 'offset': provider.offset}
                source_integration.execute_koku_provider_op(msg, application_type_id)
                self.assertIn(':Unable to create provider for Source ID: 1', logger.output[0])
示例#8
0
    def test_execute_koku_provider_op_destroy(self):
        """Test to execute Koku Operations to sync with Sources for destruction."""
        source_id = self.source_ids.get(Provider.PROVIDER_AWS)
        provider = Sources(**self.sources.get(Provider.PROVIDER_AWS))
        provider.save()

        msg = {"operation": "destroy", "provider": provider, "offset": provider.offset}
        with patch.object(SourcesHTTPClient, "set_source_status"):
            source_integration.execute_koku_provider_op(msg)
        self.assertEqual(Sources.objects.filter(source_id=source_id).exists(), False)
示例#9
0
    def test_execute_koku_provider_op_skip_status(self):
        """Test to execute Koku Operations to sync with Sources and not push status."""
        source_id = self.source_ids.get(Provider.PROVIDER_AWS)
        provider = Sources(**self.sources.get(Provider.PROVIDER_AWS))
        provider.save()

        msg = {"operation": "create", "provider": provider, "offset": provider.offset}
        with patch.object(SourcesHTTPClient, "set_source_status"):
            with patch.object(ProviderAccessor, "cost_usage_source_ready", side_effect=SkipStatusPush):
                source_integration.execute_koku_provider_op(msg)
        self.assertEqual(Sources.objects.get(source_id=source_id).status, {})
示例#10
0
    def test_execute_koku_provider_op_create(self):
        """Test to execute Koku Operations to sync with Sources for creation."""
        source_id = self.source_ids.get(Provider.PROVIDER_AWS)
        provider = Sources(**self.sources.get(Provider.PROVIDER_AWS))
        provider.save()

        msg = {"operation": "create", "provider": provider, "offset": provider.offset}
        with patch.object(SourcesHTTPClient, "set_source_status"):
            with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True):
                source_integration.execute_koku_provider_op(msg)
        self.assertIsNotNone(Sources.objects.get(source_id=source_id).koku_uuid)
        self.assertFalse(Sources.objects.get(source_id=source_id).pending_update)
        self.assertEqual(Sources.objects.get(source_id=source_id).koku_uuid, str(provider.source_uuid))
示例#11
0
    def test_execute_koku_provider_op_destroy_recoverable_error(self):
        """Test to execute Koku Operations to sync with Sources with recoverable error."""
        source_id = 1
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        application_type_id = 2

        provider = Sources(source_id=source_id, auth_header=auth_header, offset=offset)
        provider.save()

        with requests_mock.mock() as m:
            m.post("http://www.koku.com/api/cost-management/v1/providers/", exc=requests.exceptions.RequestException)
            with self.assertRaises(source_integration.SourcesIntegrationError):
                msg = {"operation": "create", "provider": provider, "offset": provider.offset}
                source_integration.execute_koku_provider_op(msg, application_type_id)
示例#12
0
    def test_execute_koku_provider_op_update(self):
        """Test to execute Koku Operations to sync with Sources for update."""

        def set_status_helper(*args, **kwargs):
            """helper to clear update flag."""
            storage.clear_update_flag(source_id)

        source_id = self.source_ids.get(Provider.PROVIDER_AWS)
        provider = Sources(**self.sources.get(Provider.PROVIDER_AWS))
        provider.save()

        msg = {"operation": "create", "provider": provider, "offset": provider.offset}
        with patch.object(SourcesHTTPClient, "set_source_status"):
            with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True):
                source_integration.execute_koku_provider_op(msg)

        builder = SourcesProviderCoordinator(source_id, provider.auth_header)

        source = storage.get_source_instance(source_id)
        uuid = source.koku_uuid

        with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True):
            builder.update_account(source)

        self.assertEqual(
            Provider.objects.get(uuid=uuid).billing_source.data_source,
            self.sources.get(Provider.PROVIDER_AWS).get("billing_source").get("data_source"),
        )

        provider.billing_source = {"data_source": {"bucket": "new-bucket"}}
        provider.koku_uuid = uuid
        provider.pending_update = True
        provider.save()

        msg = {"operation": "update", "provider": provider, "offset": provider.offset}
        with patch.object(SourcesHTTPClient, "set_source_status", side_effect=set_status_helper):
            with patch.object(ProviderAccessor, "cost_usage_source_ready", returns=True):
                source_integration.execute_koku_provider_op(msg)
        response = Sources.objects.get(source_id=source_id)
        self.assertEqual(response.pending_update, False)
        self.assertEqual(response.billing_source, {"data_source": {"bucket": "new-bucket"}})

        response = Provider.objects.get(uuid=uuid)
        self.assertEqual(response.billing_source.data_source.get("bucket"), "new-bucket")
示例#13
0
    def test_execute_koku_provider_op_destroy(self):
        """Test to execute Koku Operations to sync with Sources for destruction."""
        source_id = 1
        app_id = 1
        application_type_id = 2
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        mock_koku_uuid = faker.uuid4()

        provider = Sources(source_id=source_id, auth_header=auth_header, offset=offset, koku_uuid=mock_koku_uuid)
        provider.save()

        with requests_mock.mock() as m:
            m.delete(f"http://www.koku.com/api/cost-management/v1/providers/{mock_koku_uuid}/", status_code=204)
            m.get(
                SOURCES_APPS.format(application_type_id, source_id), status_code=200, json={"data": [{"id": app_id}]}
            )
            m.patch(f"http://www.sources.com/api/v1.0/applications/{app_id}", status_code=204)
            msg = {"operation": "destroy", "provider": provider, "offset": provider.offset}
            source_integration.execute_koku_provider_op(msg, application_type_id)
            self.assertEqual(Sources.objects.filter(source_id=source_id).exists(), False)
示例#14
0
    def test_execute_koku_provider_op_destroy_provider_not_found(self):
        """Test to execute Koku Operations to sync with Sources for destruction with provider missing."""
        source_id = 1
        app_id = 1
        application_type_id = 2
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        mock_koku_uuid = faker.uuid4()

        provider = Sources(source_id=source_id,
                           auth_header=auth_header,
                           offset=offset,
                           koku_uuid=mock_koku_uuid)
        provider.save()

        with requests_mock.mock() as m:
            m.delete(
                f'http://www.koku.com/api/cost-management/v1/providers/{mock_koku_uuid}/',
                status_code=404,
                json={})
            m.get(
                'http://www.sources.com/api/v1.0/applications?filter[application_type_id]={}&filter[source_id]={}'
                .format(application_type_id, source_id),
                status_code=200,
                json={'data': [{
                    'id': app_id
                }]})
            m.patch(f'http://www.sources.com/api/v1.0/applications/{app_id}',
                    status_code=204)
            msg = {
                'operation': 'destroy',
                'provider': provider,
                'offset': provider.offset
            }
            source_integration.execute_koku_provider_op(
                msg, application_type_id)
            self.assertEqual(
                Sources.objects.filter(source_id=source_id).exists(), False)
示例#15
0
    def test_execute_koku_provider_op_create(self):
        """Test to execute Koku Operations to sync with Sources for creation."""
        source_id = 1
        app_id = 1
        application_type_id = 2
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        provider = Sources(source_id=source_id, auth_header=auth_header, offset=offset)
        provider.save()

        mock_koku_uuid = faker.uuid4()
        with requests_mock.mock() as m:
            m.post('http://www.koku.com/api/cost-management/v1/providers/',
                   status_code=201,
                   json={'uuid': mock_koku_uuid})
            m.get('http://www.sources.com/api/v1.0/applications?filter[application_type_id]={}&filter[source_id]={}'.
                  format(application_type_id, source_id),
                  status_code=200, json={'data': [{'id': app_id}]})
            m.patch(f'http://www.sources.com/api/v1.0/applications/{app_id}',
                    status_code=204)
            msg = {'operation': 'create', 'provider': provider, 'offset': provider.offset}
            source_integration.execute_koku_provider_op(msg, application_type_id)
            self.assertEqual(Sources.objects.get(source_id=source_id).koku_uuid, mock_koku_uuid)
    def test_execute_koku_provider_op_create(self):
        """Test to execute Koku Operations to sync with Sources for creation."""
        source_id = 1
        auth_header = Config.SOURCES_FAKE_HEADER
        offset = 2
        provider = Sources(source_id=source_id,
                           auth_header=auth_header,
                           offset=offset)
        provider.save()

        mock_koku_uuid = faker.uuid4()
        with requests_mock.mock() as m:
            m.post('http://www.koku.com/api/cost-management/v1/providers/',
                   status_code=201,
                   json={'uuid': mock_koku_uuid})
            msg = {
                'operation': 'create',
                'provider': provider,
                'offset': provider.offset
            }
            source_integration.execute_koku_provider_op(msg)
            self.assertEqual(
                Sources.objects.get(source_id=source_id).koku_uuid,
                mock_koku_uuid)