Example #1
0
 def test_save_billing_source_errors(self):
     """Test save billing source when get_data_source raises exception."""
     event = choice(EVENT_LIST)
     msg = msg_generator(event)
     processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID)
     with patch.object(SourcesHTTPClient,
                       "get_data_source",
                       side_effect=SourcesHTTPClientError):
         new_list = copy.copy(Provider.PROVIDER_LIST)
         new_list.remove(
             Provider.PROVIDER_OCP
         )  # OCP is the oddball and does not save billing data
         for saved, provider in product([True, None], new_list):
             with self.subTest(test=(saved, provider)):
                 with patch("sources.storage.get_source_type",
                            return_value=provider):
                     with patch.object(SourcesHTTPClient,
                                       "set_source_status") as mock_set:
                         with patch(
                                 "sources.storage.add_provider_sources_billing_info"
                         ) as mock_add:
                             with self.assertRaises(SourcesHTTPClientError):
                                 result = processor.save_billing_source()
                                 self.assertIsNone(result)
                                 mock_set.assert_called_once()
                                 mock_add.assert_not_called()
Example #2
0
 def test_save_billing_source_skipped_for_OCP(self):
     """Test that save_billing doesn't do anything for an OCP source."""
     event = choice(EVENT_LIST)
     msg = msg_generator(event)
     processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID)
     provider = Provider.PROVIDER_OCP
     for saved in [True, None]:
         with self.subTest(test=saved):
             with patch("sources.storage.get_source_type",
                        return_value=provider):
                 with patch(
                         "sources.storage.add_provider_sources_billing_info",
                         return_value=saved) as mock_add:
                     result = processor.save_billing_source()
                     self.assertIsNone(result)
                     mock_add.assert_not_called()
Example #3
0
    def test_save_billing_source(self):
        """Test save billing source calls add_provider_sources_billing_info."""
        def side_effect_func(arg, _):
            """Helper func to mock client.get_data_source call."""
            values = {  # add new sources here
                Provider.PROVIDER_AWS: self.valid_billing.get(Provider.PROVIDER_AWS),
                Provider.PROVIDER_AZURE: self.valid_billing.get(Provider.PROVIDER_AZURE),
                Provider.PROVIDER_GCP: self.valid_billing.get(Provider.PROVIDER_GCP),
                Provider.PROVIDER_OCP: self.valid_billing.get(Provider.PROVIDER_OCP),
            }
            return values.get(arg)

        event = choice(EVENT_LIST)
        msg = msg_generator(event)
        processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID)
        with patch.object(SourcesHTTPClient,
                          "get_data_source",
                          side_effect=side_effect_func):
            new_list = copy.copy(PROVIDER_LIST)
            new_list.remove(
                Provider.PROVIDER_OCP
            )  # OCP is the oddball and does not save billing data
            table = [{
                "return": None,
                "expected": False
            }, {
                "return": True,
                "expected": True
            }]
            for test, provider in product(table, new_list):
                with self.subTest(test=(test, provider)):
                    with patch("sources.storage.get_source_type",
                               return_value=provider):
                        with patch(
                                "sources.storage.add_provider_sources_billing_info",
                                return_value=test.get("return")) as mock_add:
                            result = processor.save_billing_source()
                            self.assertEqual(result, test.get("expected"))
                            mock_add.assert_called_with(
                                None, {
                                    "data_source":
                                    self.valid_billing.get(provider)
                                })