Exemplo n.º 1
0
 def test_save_credentials_errors(self):
     """Test save credentials when get_credentials raises exception."""
     event = choice(EVENT_LIST)
     msg = msg_generator(event)
     processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID)
     with patch.multiple(
             SourcesHTTPClient,
             _get_aws_credentials=MagicMock(
                 side_effect=SourcesHTTPClientError),
             _get_azure_credentials=MagicMock(
                 side_effect=SourcesHTTPClientError),
             _get_gcp_credentials=MagicMock(
                 side_effect=SourcesHTTPClientError),
             _get_ocp_credentials=MagicMock(
                 side_effect=SourcesHTTPClientError),
     ):
         for provider in Provider.PROVIDER_LIST:
             with self.subTest(test=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_auth_info"
                         ) as mock_add:
                             with self.assertRaises(SourcesHTTPClientError):
                                 result = processor.save_credentials()
                                 self.assertIsNone(result)
                                 mock_set.assert_called_once()
                                 mock_add.assert_not_called()
Exemplo n.º 2
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()
Exemplo n.º 3
0
 def test_save_sources_details(self):
     """Test save_source_details calls storage method."""
     table = [{
         "return": None,
         "expected": False
     }, {
         "return": True,
         "expected": True
     }]
     event = choice(EVENT_LIST)
     msg = msg_generator(event)
     processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID)
     for test, provider in product(table, PROVIDER_LIST):
         mock_details = mock_details_generator(provider, FAKER.name(),
                                               uuid4(), FAKER.pyint())
         with self.subTest(
                 test=
                 f"(test={test}, provider={provider}, mock_details={mock_details.__dict__})"
         ):
             with patch.object(KafkaMessageProcessor,
                               "get_source_details",
                               return_value=mock_details):
                 with patch("sources.storage.add_provider_sources_details",
                            return_value=test.get(
                                "return")) as mock_details_save:
                     result = processor.save_sources_details()
                     self.assertEqual(result, test.get("expected"))
                     mock_details_save.assert_called_once()
Exemplo n.º 4
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()
Exemplo n.º 5
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)
                                })
Exemplo n.º 6
0
 def test_save_sources_details_unknown_source_type(self):
     """Test save_source_details does not call storage method."""
     provider_list = [Provider.PROVIDER_IBM, "unknown"]
     event = choice(EVENT_LIST)
     msg = msg_generator(event)
     processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID)
     for provider in provider_list:
         mock_details = mock_details_generator(provider, FAKER.name(),
                                               uuid4(), FAKER.pyint())
         with self.subTest(
                 test=
                 f"(provider={provider}, mock_details={mock_details.__dict__})"
         ):
             with patch.object(KafkaMessageProcessor,
                               "get_source_details",
                               return_value=mock_details):
                 with patch("sources.storage.add_provider_sources_details"
                            ) as mock_details_save:
                     result = processor.save_sources_details()
                     self.assertIsNone(result)
                     mock_details_save.assert_not_called()
Exemplo n.º 7
0
 def test_save_credentials(self):
     """Test save credentials calls add_provider_sources_auth_info."""
     event = choice(EVENT_LIST)
     msg = msg_generator(event)
     processor = KafkaMessageProcessor(msg, event, COST_MGMT_APP_TYPE_ID)
     with patch.multiple(  # Add all new providers here
             SourcesHTTPClient,
             _get_aws_credentials=MagicMock(
                 return_value=self.valid_creds.get(Provider.PROVIDER_AWS)),
             _get_azure_credentials=MagicMock(return_value=self.valid_creds.
                                              get(Provider.PROVIDER_AZURE)),
             _get_gcp_credentials=MagicMock(
                 return_value=self.valid_creds.get(Provider.PROVIDER_GCP)),
             _get_ocp_credentials=MagicMock(
                 return_value=self.valid_creds.get(Provider.PROVIDER_OCP)),
     ):
         table = [{
             "return": None,
             "expected": False
         }, {
             "return": True,
             "expected": True
         }]
         for test, provider in product(table, PROVIDER_LIST):
             with self.subTest(test=(test, provider)):
                 with patch("sources.storage.get_source_type",
                            return_value=provider):
                     with patch(
                             "sources.storage.add_provider_sources_auth_info",
                             return_value=test.get("return")) as mock_add:
                         result = processor.save_credentials()
                         self.assertEqual(result, test.get("expected"))
                         mock_add.assert_called_with(
                             None, {
                                 "credentials":
                                 self.valid_creds.get(provider)
                             })