class AWSReportDownloaderTest(MasuTestCase): """Test Cases for the AWS S3 functions.""" fake = Faker() @classmethod def setUpClass(cls): """Set up shared class variables.""" super().setUpClass() cls.fake_customer_name = CUSTOMER_NAME cls.fake_report_name = REPORT cls.fake_bucket_prefix = PREFIX cls.fake_bucket_name = BUCKET cls.selected_region = REGION cls.auth_credential = fake_arn(service="iam", generate_account_id=True) cls.manifest_accessor = ReportManifestDBAccessor() @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def setUp(self, fake_session): """Set up shared variables.""" super().setUp() os.makedirs(DATA_DIR, exist_ok=True) self.credentials = {"role_arn": self.auth_credential} self.data_source = {"bucket": self.fake_bucket_name} self.report_downloader = ReportDownloader( customer_name=self.fake_customer_name, credentials=self.credentials, data_source=self.data_source, provider_type=Provider.PROVIDER_AWS, provider_uuid=self.aws_provider_uuid, ) self.aws_report_downloader = AWSReportDownloader( **{ "customer_name": self.fake_customer_name, "credentials": self.credentials, "data_source": self.data_source, "report_name": self.fake_report_name, "provider_uuid": self.aws_provider_uuid, }) def tearDown(self): """Remove test generated data.""" shutil.rmtree(DATA_DIR, ignore_errors=True) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._check_size" ) @patch( "masu.external.downloader.aws.aws_report_downloader.ReportManifestDBAccessor.mark_s3_csv_cleared" ) @patch( "masu.external.downloader.aws.aws_report_downloader.ReportManifestDBAccessor.get_s3_csv_cleared" ) @patch( "masu.external.downloader.aws.aws_report_downloader.utils.remove_files_not_in_set_from_s3_bucket" ) @patch( "masu.external.downloader.aws.aws_report_downloader.utils.copy_local_report_file_to_s3_bucket" ) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_file(self, fake_session, mock_copy, mock_remove, mock_check_csvs, mock_mark_csvs, mock_check_size): """Test the download file method.""" mock_check_csvs.return_value = False mock_check_size.return_value = True downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.download_file(self.fake.file_path(), manifest_id=1) mock_check_csvs.assert_called() mock_copy.assert_called() mock_remove.assert_called() mock_mark_csvs.assert_called() @patch("masu.external.report_downloader.ReportStatsDBAccessor") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSessionDownloadError) def test_download_report_missing_bucket(self, mock_stats, fake_session): """Test download fails when bucket is missing.""" mock_stats.return_value.__enter__ = Mock() fake_report_date = self.fake.date_time().replace(day=1) fake_report_date_str = fake_report_date.strftime("%Y%m%dT000000.000Z") expected_assembly_id = "882083b7-ea62-4aab-aa6a-f0d08d65ee2b" input_key = f"/koku/20180701-20180801/{expected_assembly_id}/koku-1.csv.gz" mock_manifest = { "assemblyId": expected_assembly_id, "billingPeriod": { "start": fake_report_date_str }, "reportKeys": [input_key], } with patch.object(AWSReportDownloader, "_get_manifest", return_value=("", mock_manifest)): with self.assertRaises(AWSReportDownloaderError): report_downloader = ReportDownloader( customer_name=self.fake_customer_name, credentials=self.credentials, data_source=self.data_source, provider_type=Provider.PROVIDER_AWS, provider_uuid=self.aws_provider_uuid, ) report_context = { "date": fake_report_date.date(), "manifest_id": 1, "comporession": "GZIP", "current_file": "/my/file", } report_downloader.download_report(report_context) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_missing_report_name(self, fake_session): """Test downloading a report with an invalid report name.""" with self.assertRaises(MasuProviderError): AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source, "wrongreport") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_default_report(self, fake_session): """Test assume aws role works.""" downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) self.assertEqual(downloader.report_name, self.fake_report_name) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSessionNoReport) @patch("masu.util.aws.common.get_cur_report_definitions", return_value=[]) def test_download_default_report_no_report_found(self, fake_session, fake_report_list): """Test download fails when no reports are found.""" with self.assertRaises(MasuProviderError): AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_success(self, fake_session, fake_shutil): """Test _check_size is successful.""" fake_client = Mock() fake_client.get_object.return_value = { "ContentLength": 123456, "Body": Mock() } fake_shutil.disk_usage.return_value = (10, 10, 4096 * 1024 * 1024) downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice( ["json", "csv.gz"])) result = downloader._check_size(fakekey, check_inflate=False) self.assertTrue(result) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_fail_nospace(self, fake_session, fake_shutil): """Test _check_size fails if there is no more space.""" fake_client = Mock() fake_client.get_object.return_value = { "ContentLength": 123456, "Body": Mock() } fake_shutil.disk_usage.return_value = (10, 10, 10) downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice( ["json", "csv.gz"])) result = downloader._check_size(fakekey, check_inflate=False) self.assertFalse(result) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_fail_nosize(self, fake_session): """Test _check_size fails if there report has no size.""" fake_client = Mock() fake_client.get_object.return_value = {} downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice( ["json", "csv.gz"])) with self.assertRaises(AWSReportDownloaderError): downloader._check_size(fakekey, check_inflate=False) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_inflate_success(self, fake_session, fake_shutil): """Test _check_size inflation succeeds.""" fake_client = Mock() fake_client.get_object.return_value = { "ContentLength": 123456, "Body": io.BytesIO(b"\xd2\x02\x96I") } fake_shutil.disk_usage.return_value = (10, 10, 4096 * 1024 * 1024) downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension="csv.gz") result = downloader._check_size(fakekey, check_inflate=True) self.assertTrue(result) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_inflate_fail(self, fake_session, fake_shutil): """Test _check_size fails when inflation fails.""" fake_client = Mock() fake_client.get_object.return_value = { "ContentLength": 123456, "Body": io.BytesIO(b"\xd2\x02\x96I") } fake_shutil.disk_usage.return_value = (10, 10, 1234567) downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension="csv.gz") result = downloader._check_size(fakekey, check_inflate=True) self.assertFalse(result) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_file_check_size_fail(self, fake_session, fake_shutil): """Test _check_size fails when key is fake.""" fake_client = Mock() fake_client.get_object.return_value = { "ContentLength": 123456, "Body": io.BytesIO(b"\xd2\x02\x96I") } fake_shutil.disk_usage.return_value = (10, 10, 1234567) downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension="csv.gz") with self.assertRaises(AWSReportDownloaderError): downloader.download_file(fakekey) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_file_raise_downloader_err(self, fake_session): """Test _check_size fails when there is a downloader error.""" fake_response = {"Error": {"Code": self.fake.word()}} fake_client = Mock() fake_client.get_object.side_effect = ClientError( fake_response, "masu-test") downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client with self.assertRaises(AWSReportDownloaderError): downloader.download_file(self.fake.file_path()) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_file_raise_nofile_err(self, fake_session): """Test that downloading a nonexistent file fails with AWSReportDownloaderNoFileError.""" fake_response = {"Error": {"Code": "NoSuchKey"}} fake_client = Mock() fake_client.get_object.side_effect = ClientError( fake_response, "masu-test") downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source) downloader.s3_client = fake_client with self.assertRaises(AWSReportDownloaderNoFileError): downloader.download_file(self.fake.file_path()) def test_remove_manifest_file(self): """Test that we remove the manifest file.""" manifest_file = f"{DATA_DIR}/test_manifest.json" with open(manifest_file, "w") as f: f.write("Test") self.assertTrue(os.path.isfile(manifest_file)) self.aws_report_downloader._remove_manifest_file(manifest_file) self.assertFalse(os.path.isfile(manifest_file)) def test_delete_manifest_file_warning(self): """Test that an INFO is logged when removing a manifest file that does not exist.""" with self.assertLogs( logger="masu.external.downloader.aws.aws_report_downloader", level="INFO") as captured_logs: # Disable log suppression logging.disable(logging.NOTSET) self.aws_report_downloader._remove_manifest_file("None") self.assertTrue( captured_logs.output[0].startswith("INFO:"), msg= "The log is expected to start with 'INFO:' but instead was: " + captured_logs.output[0], ) self.assertTrue( "Could not delete manifest file at" in captured_logs.output[0], msg="""The log message is expected to contain 'Could not delete manifest file at' but instead was: """ + captured_logs.output[0], ) # Re-enable log suppression logging.disable(logging.CRITICAL) def test_init_with_demo_account(self): """Test init with the demo account.""" mock_task = Mock( request=Mock(id=str(self.fake.uuid4()), return_value={})) account_id = "123456" arn = fake_arn(service="iam", generate_account_id=True) credentials = {"role_arn": arn} report_name = FAKE.word() demo_accounts = { account_id: { arn: { "report_name": report_name, "report_prefix": FAKE.word() } } } with self.settings(DEMO_ACCOUNTS=demo_accounts): with patch("masu.util.aws.common.get_assume_role_session" ) as mock_session: AWSReportDownloader( **{ "task": mock_task, "customer_name": f"acct{account_id}", "credentials": credentials, "data_source": { "bucket": FAKE.word() }, "report_name": report_name, "provider_uuid": self.aws_provider_uuid, }) mock_session.assert_called_once() @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._remove_manifest_file" ) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._get_manifest" ) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_get_manifest_context_for_date(self, mock_session, mock_manifest, mock_delete): """Test that the manifest is read.""" current_month = DateAccessor().today().replace(day=1, second=1, microsecond=1) downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source, provider_uuid=self.aws_provider_uuid) start_str = current_month.strftime(downloader.manifest_date_format) assembly_id = "1234" compression = downloader.report.get("Compression") report_keys = ["file1", "file2"] mock_manifest.return_value = ( "", { "assemblyId": assembly_id, "Compression": compression, "reportKeys": report_keys, "billingPeriod": { "start": start_str }, }, DateAccessor().today(), ) result = downloader.get_manifest_context_for_date(current_month) self.assertEqual(result.get("assembly_id"), assembly_id) self.assertEqual(result.get("compression"), compression) self.assertIsNotNone(result.get("files")) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._remove_manifest_file" ) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._get_manifest" ) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_get_manifest_context_for_date_no_manifest(self, mock_session, mock_manifest, mock_delete): """Test that the manifest is read.""" current_month = DateAccessor().today().replace(day=1, second=1, microsecond=1) downloader = AWSReportDownloader(self.fake_customer_name, self.credentials, self.data_source, provider_uuid=self.aws_provider_uuid) mock_manifest.return_value = ("", { "reportKeys": [] }, DateAccessor().today()) result = downloader.get_manifest_context_for_date(current_month) self.assertEqual(result, {}) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.download_file" ) def test_get_manifest(self, mock_download_file): """Test _get_manifest method.""" mock_datetime = DateAccessor().today() mock_file_name = "testfile" mock_download_file.return_value = (mock_file_name, None, mock_datetime, []) fake_manifest_dict = {"foo": "bar"} with patch("masu.external.downloader.aws.aws_report_downloader.open"): with patch( "masu.external.downloader.aws.aws_report_downloader.json.load", return_value=fake_manifest_dict): manifest_file, manifest_json, manifest_modified_timestamp = self.aws_report_downloader._get_manifest( mock_datetime) self.assertEqual(manifest_file, mock_file_name) self.assertEqual(manifest_json, fake_manifest_dict) self.assertEqual(manifest_modified_timestamp, mock_datetime) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.download_file" ) def test_get_manifest_file_not_found(self, mock_download_file): """Test _get_manifest method when file is not found.""" mock_datetime = DateAccessor().today() mock_download_file.side_effect = AWSReportDownloaderNoFileError( "fake error") manifest_file, manifest_json, manifest_modified_timestamp = self.aws_report_downloader._get_manifest( mock_datetime) self.assertEqual(manifest_file, "") self.assertEqual(manifest_json, self.aws_report_downloader.empty_manifest) self.assertIsNone(manifest_modified_timestamp)
class AWSReportDownloaderTest(MasuTestCase): """Test Cases for the AWS S3 functions.""" fake = Faker() @classmethod def setUpClass(cls): """Set up shared class variables.""" super().setUpClass() cls.fake_customer_name = CUSTOMER_NAME cls.fake_report_name = REPORT cls.fake_bucket_prefix = PREFIX cls.fake_bucket_name = BUCKET cls.selected_region = REGION cls.auth_credential = fake_arn(service="iam", generate_account_id=True) cls.manifest_accessor = ReportManifestDBAccessor() @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def setUp(self, fake_session): """Set up shared variables.""" super().setUp() os.makedirs(DATA_DIR, exist_ok=True) self.mock_task = Mock(request=Mock(id=str(self.fake.uuid4()), return_value={})) self.report_downloader = ReportDownloader( task=self.mock_task, customer_name=self.fake_customer_name, access_credential=self.auth_credential, report_source=self.fake_bucket_name, provider_type=Provider.PROVIDER_AWS, provider_uuid=self.aws_provider_uuid, ) self.aws_report_downloader = AWSReportDownloader( **{ "task": self.mock_task, "customer_name": self.fake_customer_name, "auth_credential": self.auth_credential, "bucket": self.fake_bucket_name, "report_name": self.fake_report_name, "provider_uuid": self.aws_provider_uuid, } ) def tearDown(self): """Remove test generated data.""" shutil.rmtree(DATA_DIR, ignore_errors=True) @patch("masu.external.downloader.aws.aws_report_downloader.boto3.resource") @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.download_file", return_value=("mock_file_name", None), ) def test_download_bucket(self, mock_boto_resource, mock_download_file): """Test download bucket method.""" mock_resource = Mock() mock_bucket = Mock() mock_bucket.objects.all.return_value = [] mock_resource.Bucket.return_value = mock_bucket out = self.aws_report_downloader.download_bucket() expected_files = [] self.assertEqual(out, expected_files) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.download_file", side_effect=mock_download_file_error, ) def test_download_report_missing_manifest(self, mock_download_file): """Test download fails when manifest is missing.""" fake_report_date = self.fake.date_time().replace(day=1) out = self.report_downloader.download_report(fake_report_date) self.assertEqual(out, []) @patch("masu.external.report_downloader.ReportStatsDBAccessor") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSessionDownloadError) def test_download_report_missing_bucket(self, mock_stats, fake_session): """Test download fails when bucket is missing.""" mock_stats.return_value.__enter__ = Mock() fake_report_date = self.fake.date_time().replace(day=1) fake_report_date_str = fake_report_date.strftime("%Y%m%dT000000.000Z") expected_assembly_id = "882083b7-ea62-4aab-aa6a-f0d08d65ee2b" input_key = f"/koku/20180701-20180801/{expected_assembly_id}/koku-1.csv.gz" mock_manifest = { "assemblyId": expected_assembly_id, "billingPeriod": {"start": fake_report_date_str}, "reportKeys": [input_key], } with patch.object(AWSReportDownloader, "_get_manifest", return_value=("", mock_manifest)): with self.assertRaises(AWSReportDownloaderError): report_downloader = ReportDownloader( task=self.mock_task, customer_name=self.fake_customer_name, access_credential=self.auth_credential, report_source=self.fake_bucket_name, provider_type=Provider.PROVIDER_AWS, provider_uuid=self.aws_provider_uuid, ) AWSReportDownloader( **{ "task": self.mock_task, "customer_name": self.fake_customer_name, "auth_credential": self.auth_credential, "bucket": self.fake_bucket_name, "report_name": self.fake_report_name, "provider_uuid": self.aws_provider_uuid, } ) report_downloader.download_report(fake_report_date) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_missing_report_name(self, fake_session): """Test downloading a report with an invalid report name.""" auth_credential = fake_arn(service="iam", generate_account_id=True) with self.assertRaises(MasuProviderError): AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, "s3_bucket", "wrongreport") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_default_report(self, fake_session): """Test assume aws role works.""" # actual test auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) self.assertEqual(downloader.report_name, self.fake_report_name) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSessionNoReport) @patch("masu.util.aws.common.get_cur_report_definitions", return_value=[]) def test_download_default_report_no_report_found(self, fake_session, fake_report_list): """Test download fails when no reports are found.""" auth_credential = fake_arn(service="iam", generate_account_id=True) with self.assertRaises(MasuProviderError): AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_success(self, fake_session, fake_shutil): """Test _check_size is successful.""" fake_client = Mock() fake_client.get_object.return_value = {"ContentLength": 123456, "Body": Mock()} fake_shutil.disk_usage.return_value = (10, 10, 4096 * 1024 * 1024) auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice(["json", "csv.gz"])) result = downloader._check_size(fakekey, check_inflate=False) self.assertTrue(result) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_fail_nospace(self, fake_session, fake_shutil): """Test _check_size fails if there is no more space.""" fake_client = Mock() fake_client.get_object.return_value = {"ContentLength": 123456, "Body": Mock()} fake_shutil.disk_usage.return_value = (10, 10, 10) auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice(["json", "csv.gz"])) result = downloader._check_size(fakekey, check_inflate=False) self.assertFalse(result) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_fail_nosize(self, fake_session): """Test _check_size fails if there report has no size.""" fake_client = Mock() fake_client.get_object.return_value = {} auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice(["json", "csv.gz"])) with self.assertRaises(AWSReportDownloaderError): downloader._check_size(fakekey, check_inflate=False) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_inflate_success(self, fake_session, fake_shutil): """Test _check_size inflation succeeds.""" fake_client = Mock() fake_client.get_object.return_value = {"ContentLength": 123456, "Body": io.BytesIO(b"\xd2\x02\x96I")} fake_shutil.disk_usage.return_value = (10, 10, 4096 * 1024 * 1024) auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension="csv.gz") result = downloader._check_size(fakekey, check_inflate=True) self.assertTrue(result) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_check_size_inflate_fail(self, fake_session, fake_shutil): """Test _check_size fails when inflation fails.""" fake_client = Mock() fake_client.get_object.return_value = {"ContentLength": 123456, "Body": io.BytesIO(b"\xd2\x02\x96I")} fake_shutil.disk_usage.return_value = (10, 10, 1234567) auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension="csv.gz") result = downloader._check_size(fakekey, check_inflate=True) self.assertFalse(result) @patch("masu.external.downloader.aws.aws_report_downloader.shutil") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_file_check_size_fail(self, fake_session, fake_shutil): """Test _check_size fails when key is fake.""" fake_client = Mock() fake_client.get_object.return_value = {"ContentLength": 123456, "Body": io.BytesIO(b"\xd2\x02\x96I")} fake_shutil.disk_usage.return_value = (10, 10, 1234567) auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension="csv.gz") with self.assertRaises(AWSReportDownloaderError): downloader.download_file(fakekey) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_file_raise_downloader_err(self, fake_session): """Test _check_size fails when there is a downloader error.""" fake_response = {"Error": {"Code": self.fake.word()}} fake_client = Mock() fake_client.get_object.side_effect = ClientError(fake_response, "masu-test") auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client with self.assertRaises(AWSReportDownloaderError): downloader.download_file(self.fake.file_path()) @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_download_file_raise_nofile_err(self, fake_session): """Test that downloading a nonexistent file fails with AWSReportDownloaderNoFileError.""" fake_response = {"Error": {"Code": "NoSuchKey"}} fake_client = Mock() fake_client.get_object.side_effect = ClientError(fake_response, "masu-test") auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) downloader.s3_client = fake_client with self.assertRaises(AWSReportDownloaderNoFileError): downloader.download_file(self.fake.file_path()) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.check_if_manifest_should_be_downloaded" ) @patch("masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._remove_manifest_file") @patch("masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._get_manifest") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_get_report_context_for_date_should_download(self, mock_session, mock_manifest, mock_delete, mock_check): """Test that data is returned on the reports to process.""" current_month = DateAccessor().today().replace(day=1, second=1, microsecond=1) auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name, provider_uuid=self.aws_provider_uuid, ) start_str = current_month.strftime(downloader.manifest_date_format) assembly_id = "1234" compression = downloader.report.get("Compression") report_keys = ["file1", "file2"] mock_manifest.return_value = ( "", { "assemblyId": assembly_id, "Compression": compression, "reportKeys": report_keys, "billingPeriod": {"start": start_str}, }, ) mock_check.return_value = True expected = {"manifest_id": None, "assembly_id": assembly_id, "compression": compression, "files": report_keys} result = downloader.get_report_context_for_date(current_month) with ReportManifestDBAccessor() as manifest_accessor: manifest_entry = manifest_accessor.get_manifest(assembly_id, self.aws_provider_uuid) expected["manifest_id"] = manifest_entry.id self.assertIsInstance(result, dict) for key, value in result.items(): self.assertIn(key, expected) self.assertEqual(value, expected.get(key)) @patch( "masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.check_if_manifest_should_be_downloaded" ) @patch("masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._remove_manifest_file") @patch("masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._get_manifest") @patch("masu.util.aws.common.get_assume_role_session", return_value=FakeSession) def test_get_report_context_for_date_should_not_download( self, mock_session, mock_manifest, mock_delete, mock_check ): """Test that no data is returned when we don't want to process.""" current_month = DateAccessor().today().replace(day=1, second=1, microsecond=1) auth_credential = fake_arn(service="iam", generate_account_id=True) downloader = AWSReportDownloader( self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name ) start_str = current_month.strftime(downloader.manifest_date_format) assembly_id = "1234" compression = downloader.report.get("Compression") report_keys = ["file1", "file2"] mock_manifest.return_value = ( "", { "assemblyId": assembly_id, "Compression": compression, "reportKeys": report_keys, "billingPeriod": {"start": start_str}, }, ) mock_check.return_value = False expected = {} result = downloader.get_report_context_for_date(current_month) self.assertEqual(result, expected) def test_remove_manifest_file(self): """Test that we remove the manifest file.""" manifest_file = f"{DATA_DIR}/test_manifest.json" with open(manifest_file, "w") as f: f.write("Test") self.assertTrue(os.path.isfile(manifest_file)) self.aws_report_downloader._remove_manifest_file(manifest_file) self.assertFalse(os.path.isfile(manifest_file)) def test_delete_manifest_file_warning(self): """Test that an INFO is logged when removing a manifest file that does not exist.""" with self.assertLogs( logger="masu.external.downloader.aws.aws_report_downloader", level="INFO" ) as captured_logs: # Disable log suppression logging.disable(logging.NOTSET) self.aws_report_downloader._remove_manifest_file("None") self.assertTrue( captured_logs.output[0].startswith("INFO:"), msg="The log is expected to start with 'INFO:' but instead was: " + captured_logs.output[0], ) self.assertTrue( "Could not delete manifest file at" in captured_logs.output[0], msg="""The log message is expected to contain 'Could not delete manifest file at' but instead was: """ + captured_logs.output[0], ) # Re-enable log suppression logging.disable(logging.CRITICAL)
class AWSReportDownloaderTest(MasuTestCase): """Test Cases for the AWS S3 functions.""" fake = Faker() @classmethod def setUpClass(cls): super().setUpClass() cls.fake_customer_name = CUSTOMER_NAME cls.fake_report_name = REPORT cls.fake_bucket_prefix = PREFIX cls.fake_bucket_name = BUCKET cls.selected_region = REGION cls.auth_credential = fake_arn(service='iam', generate_account_id=True) cls.manifest_accessor = ReportManifestDBAccessor() @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def setUp(self, fake_session): super().setUp() os.makedirs(DATA_DIR, exist_ok=True) self.mock_task = Mock( request=Mock(id=str(self.fake.uuid4()), return_value={})) self.report_downloader = ReportDownloader( task=self.mock_task, customer_name=self.fake_customer_name, access_credential=self.auth_credential, report_source=self.fake_bucket_name, provider_type='AWS', provider_uuid=self.aws_provider_uuid, ) self.aws_report_downloader = AWSReportDownloader( **{ 'task': self.mock_task, 'customer_name': self.fake_customer_name, 'auth_credential': self.auth_credential, 'bucket': self.fake_bucket_name, 'report_name': self.fake_report_name, 'provider_uuid': self.aws_provider_uuid, }) def tearDown(self): shutil.rmtree(DATA_DIR, ignore_errors=True) @patch('masu.external.downloader.aws.aws_report_downloader.boto3.resource') @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.download_file', return_value=('mock_file_name', None), ) def test_download_bucket(self, mock_boto_resource, mock_download_file): """Test download bucket method.""" mock_resource = Mock() mock_bucket = Mock() mock_bucket.objects.all.return_value = [] mock_resource.Bucket.return_value = mock_bucket mock_boto_resource = mock_resource out = self.aws_report_downloader.download_bucket() expected_files = [] self.assertEqual(out, expected_files) @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.download_file', side_effect=mock_download_file_error, ) def test_download_report_missing_manifest(self, mock_download_file): fake_report_date = self.fake.date_time().replace(day=1) out = self.report_downloader.download_report(fake_report_date) self.assertEqual(out, []) @patch('masu.external.report_downloader.ReportStatsDBAccessor') @patch( 'masu.util.aws.common.get_assume_role_session', return_value=FakeSessionDownloadError, ) def test_download_report_missing_bucket(self, mock_stats, fake_session): mock_stats.return_value.__enter__ = Mock() fake_report_date = self.fake.date_time().replace(day=1) fake_report_date_str = fake_report_date.strftime('%Y%m%dT000000.000Z') expected_assembly_id = '882083b7-ea62-4aab-aa6a-f0d08d65ee2b' input_key = f'/koku/20180701-20180801/{expected_assembly_id}/koku-1.csv.gz' mock_manifest = { 'assemblyId': expected_assembly_id, 'billingPeriod': { 'start': fake_report_date_str }, 'reportKeys': [input_key], } with patch.object(AWSReportDownloader, '_get_manifest', return_value=('', mock_manifest)): with self.assertRaises(AWSReportDownloaderError): report_downloader = ReportDownloader( task=self.mock_task, customer_name=self.fake_customer_name, access_credential=self.auth_credential, report_source=self.fake_bucket_name, provider_type='AWS', provider_uuid=self.aws_provider_uuid, ) AWSReportDownloader( **{ 'task': self.mock_task, 'customer_name': self.fake_customer_name, 'auth_credential': self.auth_credential, 'bucket': self.fake_bucket_name, 'report_name': self.fake_report_name, 'provider_uuid': self.aws_provider_uuid, }) report_downloader.download_report(fake_report_date) @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_missing_report_name(self, fake_session): """Test downloading a report with an invalid report name.""" auth_credential = fake_arn(service='iam', generate_account_id=True) with self.assertRaises(MasuProviderError): AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, 's3_bucket', 'wrongreport') @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_download_default_report(self, fake_session): # actual test auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) self.assertEqual(downloader.report_name, self.fake_report_name) @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSessionNoReport) @patch('masu.util.aws.common.get_cur_report_definitions', return_value=[]) def test_download_default_report_no_report_found(self, fake_session, fake_report_list): auth_credential = fake_arn(service='iam', generate_account_id=True) with self.assertRaises(MasuProviderError): AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) @patch('masu.external.downloader.aws.aws_report_downloader.shutil') @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_check_size_success(self, fake_session, fake_shutil): fake_client = Mock() fake_client.get_object.return_value = { 'ContentLength': 123456, 'Body': Mock() } fake_shutil.disk_usage.return_value = (10, 10, 4096 * 1024 * 1024) auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice( ['json', 'csv.gz'])) result = downloader._check_size(fakekey, check_inflate=False) self.assertTrue(result) @patch('masu.external.downloader.aws.aws_report_downloader.shutil') @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_check_size_fail_nospace(self, fake_session, fake_shutil): fake_client = Mock() fake_client.get_object.return_value = { 'ContentLength': 123456, 'Body': Mock() } fake_shutil.disk_usage.return_value = (10, 10, 10) auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice( ['json', 'csv.gz'])) result = downloader._check_size(fakekey, check_inflate=False) self.assertFalse(result) @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_check_size_fail_nosize(self, fake_session): fake_client = Mock() fake_client.get_object.return_value = {} auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension=random.choice( ['json', 'csv.gz'])) with self.assertRaises(AWSReportDownloaderError): downloader._check_size(fakekey, check_inflate=False) @patch('masu.external.downloader.aws.aws_report_downloader.shutil') @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_check_size_inflate_success(self, fake_session, fake_shutil): fake_client = Mock() fake_client.get_object.return_value = { 'ContentLength': 123456, 'Body': io.BytesIO(b'\xd2\x02\x96I'), } fake_shutil.disk_usage.return_value = (10, 10, 4096 * 1024 * 1024) auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension='csv.gz') result = downloader._check_size(fakekey, check_inflate=True) self.assertTrue(result) @patch('masu.external.downloader.aws.aws_report_downloader.shutil') @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_check_size_inflate_fail(self, fake_session, fake_shutil): fake_client = Mock() fake_client.get_object.return_value = { 'ContentLength': 123456, 'Body': io.BytesIO(b'\xd2\x02\x96I'), } fake_shutil.disk_usage.return_value = (10, 10, 1234567) auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension='csv.gz') result = downloader._check_size(fakekey, check_inflate=True) self.assertFalse(result) @patch('masu.external.downloader.aws.aws_report_downloader.shutil') @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_download_file_check_size_fail(self, fake_session, fake_shutil): fake_client = Mock() fake_client.get_object.return_value = { 'ContentLength': 123456, 'Body': io.BytesIO(b'\xd2\x02\x96I'), } fake_shutil.disk_usage.return_value = (10, 10, 1234567) auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client fakekey = self.fake.file_path(depth=random.randint(1, 5), extension='csv.gz') with self.assertRaises(AWSReportDownloaderError): downloader.download_file(fakekey) @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_download_file_raise_downloader_err(self, fake_session): fake_response = {'Error': {'Code': self.fake.word()}} fake_client = Mock() fake_client.get_object.side_effect = ClientError( fake_response, 'masu-test') auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client with self.assertRaises(AWSReportDownloaderError): downloader.download_file(self.fake.file_path()) @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_download_file_raise_nofile_err(self, fake_session): fake_response = {'Error': {'Code': 'NoSuchKey'}} fake_client = Mock() fake_client.get_object.side_effect = ClientError( fake_response, 'masu-test') auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) downloader.s3_client = fake_client with self.assertRaises(AWSReportDownloaderNoFileError): downloader.download_file(self.fake.file_path()) @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.check_if_manifest_should_be_downloaded' ) @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._remove_manifest_file' ) @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._get_manifest' ) @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_get_report_context_for_date_should_download( self, mock_session, mock_manifest, mock_delete, mock_check): """Test that data is returned on the reports to process.""" current_month = DateAccessor().today().replace(day=1, second=1, microsecond=1) auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name, provider_uuid=self.aws_provider_uuid) start_str = current_month.strftime(downloader.manifest_date_format) assembly_id = '1234' compression = downloader.report.get('Compression') report_keys = ['file1', 'file2'] mock_manifest.return_value = ('', { 'assemblyId': assembly_id, 'Compression': compression, 'reportKeys': report_keys, 'billingPeriod': { 'start': start_str } }) mock_check.return_value = True expected = { 'manifest_id': None, 'assembly_id': assembly_id, 'compression': compression, 'files': report_keys } result = downloader.get_report_context_for_date(current_month) with ReportManifestDBAccessor() as manifest_accessor: manifest_entry = manifest_accessor.get_manifest( assembly_id, self.aws_provider_uuid) expected['manifest_id'] = manifest_entry.id self.assertIsInstance(result, dict) for key, value in result.items(): self.assertIn(key, expected) self.assertEqual(value, expected.get(key)) @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader.check_if_manifest_should_be_downloaded' ) @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._remove_manifest_file' ) @patch( 'masu.external.downloader.aws.aws_report_downloader.AWSReportDownloader._get_manifest' ) @patch('masu.util.aws.common.get_assume_role_session', return_value=FakeSession) def test_get_report_context_for_date_should_not_download( self, mock_session, mock_manifest, mock_delete, mock_check): """Test that no data is returned when we don't want to process.""" current_month = DateAccessor().today().replace(day=1, second=1, microsecond=1) auth_credential = fake_arn(service='iam', generate_account_id=True) downloader = AWSReportDownloader(self.mock_task, self.fake_customer_name, auth_credential, self.fake_bucket_name) start_str = current_month.strftime(downloader.manifest_date_format) assembly_id = '1234' compression = downloader.report.get('Compression') report_keys = ['file1', 'file2'] mock_manifest.return_value = ('', { 'assemblyId': assembly_id, 'Compression': compression, 'reportKeys': report_keys, 'billingPeriod': { 'start': start_str } }) mock_check.return_value = False expected = {} result = downloader.get_report_context_for_date(current_month) self.assertEqual(result, expected) def test_remove_manifest_file(self): manifest_file = f'{DATA_DIR}/test_manifest.json' with open(manifest_file, 'w') as f: f.write('Test') self.assertTrue(os.path.isfile(manifest_file)) self.aws_report_downloader._remove_manifest_file(manifest_file) self.assertFalse(os.path.isfile(manifest_file))