def setUp(self): """Set up each test.""" super().setUp() billing_start = self.date_accessor.this_month_start self.manifest_dict = { "assembly_id": "1234", "billing_period_start_datetime": billing_start, "num_total_files": 2, "provider_uuid": self.azure_provider_uuid, } self.today = DateAccessor().today # bill = self.creator.create_azure_cost_entry_bill(provider_uuid=self.azure_provider_uuid, bill_date=self.today) # product = self.creator.create_azure_cost_entry_product(provider_uuid=self.azure_provider_uuid) # meter = self.creator.create_azure_meter(provider_uuid=self.azure_provider_uuid) # self.creator.create_azure_cost_entry_line_item(bill, product, meter) self.manifest = CostUsageReportManifest.objects.filter( provider_id=self.azure_provider_uuid, billing_period_start_datetime=billing_start).first() # self.manifest = self.manifest_accessor.add(**self.manifest_dict) # with ProviderDBAccessor(self.azure_test_provider_uuid) as provider_accessor: # self.provider = provider_accessor.get_provider() self.updater = AzureReportSummaryUpdater(self.schema, self.azure_provider, self.manifest)
def setUp(self): """Set up each test.""" super().setUp() billing_start = self.date_accessor.today_with_timezone("UTC").replace( day=1) self.manifest_dict = { "assembly_id": "1234", "billing_period_start_datetime": billing_start, "num_total_files": 2, "provider_uuid": self.azure_provider_uuid, } self.today = DateAccessor().today_with_timezone("UTC") bill = self.creator.create_azure_cost_entry_bill( provider_uuid=self.azure_provider_uuid, bill_date=self.today) product = self.creator.create_azure_cost_entry_product( provider_uuid=self.azure_provider_uuid) meter = self.creator.create_azure_meter( provider_uuid=self.azure_provider_uuid) self.creator.create_azure_cost_entry_line_item(bill, product, meter) self.manifest = self.manifest_accessor.add(**self.manifest_dict) with ProviderDBAccessor( self.azure_test_provider_uuid) as provider_accessor: self.provider = provider_accessor.get_provider() self.updater = AzureReportSummaryUpdater(self.schema, self.azure_provider, self.manifest)
def setUp(self): """Set up each test.""" super().setUp() billing_start = self.date_accessor.today_with_timezone('UTC').replace(day=1) self.manifest_dict = { 'assembly_id': '1234', 'billing_period_start_datetime': billing_start, 'num_total_files': 2, 'provider_id': self.azure_provider.id, } today = DateAccessor().today_with_timezone('UTC') bill = self.creator.create_azure_cost_entry_bill(provider_id=self.azure_provider_id, bill_date=today) product = self.creator.create_azure_cost_entry_product() meter = self.creator.create_azure_meter() service = self.creator.create_azure_service() self.creator.create_azure_cost_entry_line_item(bill, product, meter, service) self.manifest = self.manifest_accessor.add(**self.manifest_dict) self.manifest_accessor.commit() with ProviderDBAccessor(self.azure_test_provider_uuid) as provider_accessor: self.provider = provider_accessor.get_provider() self.updater = AzureReportSummaryUpdater( self.schema, self.azure_provider, self.manifest )
def _set_updater(self): """ Create the report summary updater object. Object is specific to the report provider. Args: None Returns: (Object) : Provider-specific report summary updater """ if self._provider.type in (AMAZON_WEB_SERVICES, AWS_LOCAL_SERVICE_PROVIDER): return (AWSReportSummaryUpdater(self._schema, self._provider, self._manifest), OCPCloudReportSummaryUpdater(self._schema, self._provider, self._manifest)) if self._provider.type in (AZURE, AZURE_LOCAL_SERVICE_PROVIDER): return (AzureReportSummaryUpdater(self._schema, self._provider, self._manifest), OCPCloudReportSummaryUpdater(self._schema, self._provider, self._manifest)) if self._provider.type in (OPENSHIFT_CONTAINER_PLATFORM, ): return (OCPReportSummaryUpdater(self._schema, self._provider, self._manifest), OCPCloudReportSummaryUpdater(self._schema, self._provider, self._manifest)) return None
def _set_updater(self): """ Create the report summary updater object. Object is specific to the report provider. Args: None Returns: (Object) : Provider-specific report summary updater """ if self._provider.type in (Provider.PROVIDER_AWS, Provider.PROVIDER_AWS_LOCAL): return (AWSReportSummaryUpdater(self._schema, self._provider, self._manifest), OCPCloudReportSummaryUpdater(self._schema, self._provider, self._manifest)) if self._provider.type in (Provider.PROVIDER_AZURE, Provider.PROVIDER_AZURE_LOCAL): return (AzureReportSummaryUpdater(self._schema, self._provider, self._manifest), OCPCloudReportSummaryUpdater(self._schema, self._provider, self._manifest)) if self._provider.type in (Provider.PROVIDER_OCP, ): return (OCPReportSummaryUpdater(self._schema, self._provider, self._manifest), OCPCloudReportSummaryUpdater(self._schema, self._provider, self._manifest)) return (None, None)
class AzureReportSummaryUpdaterTest(MasuTestCase): """Test Cases for the AzureReportSummaryUpdater object.""" @classmethod def setUpClass(cls): """Set up the test class with required objects.""" super().setUpClass() with ReportingCommonDBAccessor() as report_common_db: cls.column_map = report_common_db.column_map cls.accessor = AzureReportDBAccessor("acct10001", cls.column_map) cls.report_schema = cls.accessor.report_schema cls.all_tables = list(AZURE_REPORT_TABLE_MAP.values()) cls.creator = ReportObjectCreator(cls.schema, cls.column_map) cls.date_accessor = DateAccessor() cls.manifest_accessor = ReportManifestDBAccessor() def setUp(self): """Set up each test.""" super().setUp() billing_start = self.date_accessor.today_with_timezone("UTC").replace( day=1) self.manifest_dict = { "assembly_id": "1234", "billing_period_start_datetime": billing_start, "num_total_files": 2, "provider_uuid": self.azure_provider_uuid, } self.today = DateAccessor().today_with_timezone("UTC") bill = self.creator.create_azure_cost_entry_bill( provider_uuid=self.azure_provider_uuid, bill_date=self.today) product = self.creator.create_azure_cost_entry_product( provider_uuid=self.azure_provider_uuid) meter = self.creator.create_azure_meter( provider_uuid=self.azure_provider_uuid) self.creator.create_azure_cost_entry_line_item(bill, product, meter) self.manifest = self.manifest_accessor.add(**self.manifest_dict) with ProviderDBAccessor( self.azure_test_provider_uuid) as provider_accessor: self.provider = provider_accessor.get_provider() self.updater = AzureReportSummaryUpdater(self.schema, self.azure_provider, self.manifest) @patch( "masu.processor.azure.azure_report_summary_updater.AzureReportDBAccessor.populate_line_item_daily_summary_table" ) def test_azure_update_summary_tables_with_manifest(self, mock_summary): """Test that summary tables are properly run.""" self.manifest.num_processed_files = self.manifest.num_total_files start_date = self.date_accessor.today_with_timezone("UTC") end_date = start_date + datetime.timedelta(days=1) bill_date = start_date.replace(day=1).date() with AzureReportDBAccessor(self.schema, self.column_map) as accessor: bill = accessor.get_cost_entry_bills_by_date(bill_date)[0] bill.summary_data_creation_datetime = start_date bill.save() start_date_str = start_date.strftime("%Y-%m-%d") end_date_str = end_date.strftime("%Y-%m-%d") expected_start_date = start_date.date() expected_end_date = end_date.date() self.assertIsNone(bill.summary_data_updated_datetime) self.updater.update_daily_tables(start_date_str, end_date_str) mock_summary.assert_not_called() self.updater.update_summary_tables(start_date_str, end_date_str) mock_summary.assert_called_with(expected_start_date, expected_end_date, [str(bill.id)]) with AzureReportDBAccessor(self.schema, self.column_map) as accessor: bill = accessor.get_cost_entry_bills_by_date(bill_date)[0] self.assertIsNotNone(bill.summary_data_creation_datetime) self.assertIsNotNone(bill.summary_data_updated_datetime) @patch( "masu.processor.azure.azure_report_summary_updater.AzureReportDBAccessor.populate_line_item_daily_summary_table" ) def test_azure_update_summary_tables_new_bill(self, mock_summary): """Test that summary tables are run for a full month.""" self.manifest.num_processed_files = self.manifest.num_total_files start_date = self.date_accessor.today_with_timezone("UTC") end_date = start_date bill_date = start_date.replace(day=1).date() with schema_context(self.schema): bill = self.accessor.get_cost_entry_bills_by_date(bill_date)[0] last_day_of_month = calendar.monthrange(bill_date.year, bill_date.month)[1] start_date_str = start_date.strftime("%Y-%m-%d") end_date_str = end_date.strftime("%Y-%m-%d") expected_start_date = start_date.replace(day=1) expected_end_date = end_date.replace(day=last_day_of_month) dates = list( rrule(freq=DAILY, dtstart=expected_start_date, until=expected_end_date, interval=5)) if expected_end_date not in dates: dates.append(expected_end_date) # Remove the first date since it's the start date dates.pop(0) expected_calls = [] for date in dates: expected_calls.append( call(expected_start_date.date(), date.date(), [str(bill.id)])) expected_start_date = date + datetime.timedelta(days=1) self.assertIsNone(bill.summary_data_creation_datetime) self.assertIsNone(bill.summary_data_updated_datetime) self.updater.update_daily_tables(start_date_str, end_date_str) mock_summary.assert_not_called() self.updater.update_summary_tables(start_date_str, end_date_str) self.assertEqual(mock_summary.call_args_list, expected_calls) with AzureReportDBAccessor(self.schema, self.column_map) as accessor: bill = accessor.get_cost_entry_bills_by_date(bill_date)[0] self.assertIsNotNone(bill.summary_data_creation_datetime) self.assertIsNotNone(bill.summary_data_updated_datetime)
class AzureReportSummaryUpdaterTest(MasuTestCase): """Test Cases for the AzureReportSummaryUpdater object.""" @classmethod def setUpClass(cls): """Set up the test class with required objects.""" super().setUpClass() with ReportingCommonDBAccessor() as report_common_db: cls.column_map = report_common_db.column_map cls.accessor = AzureReportDBAccessor('acct10001', cls.column_map) cls.report_schema = cls.accessor.report_schema cls.all_tables = list(AZURE_REPORT_TABLE_MAP.values()) cls.creator = ReportObjectCreator(cls.schema, cls.column_map) cls.date_accessor = DateAccessor() cls.manifest_accessor = ReportManifestDBAccessor() def setUp(self): """Set up each test.""" super().setUp() billing_start = self.date_accessor.today_with_timezone('UTC').replace(day=1) self.manifest_dict = { 'assembly_id': '1234', 'billing_period_start_datetime': billing_start, 'num_total_files': 2, 'provider_uuid': self.azure_provider_uuid, } today = DateAccessor().today_with_timezone('UTC') bill = self.creator.create_azure_cost_entry_bill( provider_uuid=self.azure_provider_uuid, bill_date=today ) product = self.creator.create_azure_cost_entry_product( provider_uuid=self.azure_provider_uuid ) meter = self.creator.create_azure_meter(provider_uuid=self.azure_provider_uuid) self.creator.create_azure_cost_entry_line_item(bill, product, meter) self.manifest = self.manifest_accessor.add(**self.manifest_dict) with ProviderDBAccessor(self.azure_test_provider_uuid) as provider_accessor: self.provider = provider_accessor.get_provider() self.updater = AzureReportSummaryUpdater(self.schema, self.azure_provider, self.manifest) @patch( 'masu.processor.azure.azure_report_summary_updater.AzureReportDBAccessor.populate_line_item_daily_summary_table' ) def test_azure_update_summary_tables_with_manifest(self, mock_summary): """Test that summary tables are properly run.""" self.manifest.num_processed_files = self.manifest.num_total_files start_date = self.date_accessor.today_with_timezone('UTC') end_date = start_date + datetime.timedelta(days=1) bill_date = start_date.replace(day=1).date() with AzureReportDBAccessor(self.schema, self.column_map) as accessor: bill = accessor.get_cost_entry_bills_by_date(bill_date)[0] bill.summary_data_creation_datetime = start_date bill.save() start_date_str = start_date.strftime('%Y-%m-%d') end_date_str = end_date.strftime('%Y-%m-%d') expected_start_date = start_date.strftime('%Y-%m-%d') expected_end_date = end_date.strftime('%Y-%m-%d') self.assertIsNone(bill.summary_data_updated_datetime) self.updater.update_daily_tables(start_date_str, end_date_str) mock_summary.assert_not_called() self.updater.update_summary_tables(start_date_str, end_date_str) mock_summary.assert_called_with(expected_start_date, expected_end_date, [str(bill.id)]) with AzureReportDBAccessor(self.schema, self.column_map) as accessor: bill = accessor.get_cost_entry_bills_by_date(bill_date)[0] self.assertIsNotNone(bill.summary_data_creation_datetime) self.assertIsNotNone(bill.summary_data_updated_datetime) @patch( 'masu.processor.azure.azure_report_summary_updater.AzureReportDBAccessor.populate_line_item_daily_summary_table' ) def test_azure_update_summary_tables_new_bill(self, mock_summary): """Test that summary tables are run for a full month.""" self.manifest.num_processed_files = self.manifest.num_total_files start_date = self.date_accessor.today_with_timezone('UTC') end_date = start_date bill_date = start_date.replace(day=1).date() with schema_context(self.schema): bill = self.accessor.get_cost_entry_bills_by_date(bill_date)[0] last_day_of_month = calendar.monthrange(bill_date.year, bill_date.month)[1] start_date_str = start_date.strftime('%Y-%m-%d') end_date_str = end_date.strftime('%Y-%m-%d') expected_start_date = start_date.replace(day=1).strftime('%Y-%m-%d') expected_end_date = end_date.replace(day=last_day_of_month).strftime('%Y-%m-%d') self.assertIsNone(bill.summary_data_creation_datetime) self.assertIsNone(bill.summary_data_updated_datetime) self.updater.update_daily_tables(start_date_str, end_date_str) mock_summary.assert_not_called() self.updater.update_summary_tables(start_date_str, end_date_str) mock_summary.assert_called_with(expected_start_date, expected_end_date, [str(bill.id)]) with AzureReportDBAccessor(self.schema, self.column_map) as accessor: bill = accessor.get_cost_entry_bills_by_date(bill_date)[0] self.assertIsNotNone(bill.summary_data_creation_datetime) self.assertIsNotNone(bill.summary_data_updated_datetime)