Пример #1
0
 def test_get_latest_cost_export_for_path_exception(self, mock_factory):
     """Test that function handles a raised exception."""
     mock_factory.return_value = Mock(
         spec=AzureClientFactory,
         cloud_storage_account=Mock(return_value=Mock(
             spec=BlobServiceClient,
             get_container_client=Mock(return_value=Mock(
                 spec=ContainerClient,
                 list_blobs=Mock(side_effect=AdalError("test error")))),
         )),
     )
     with self.assertRaises(AzureServiceError):
         service = AzureService(self.tenant_id, self.client_id,
                                self.client_secret,
                                self.resource_group_name,
                                self.storage_account_name)
         service.get_latest_cost_export_for_path(report_path=FAKE.word(),
                                                 container_name=FAKE.word())
Пример #2
0
class AzureServiceTest(MasuTestCase):
    """Test Cases for the AzureService object."""
    @patch("masu.external.downloader.azure.azure_service.AzureClientFactory")
    def setUp(self, mock_factory):
        """Set up each test."""
        super().setUp()
        self.subscription_id = FAKE.uuid4()
        self.tenant_id = FAKE.uuid4()
        self.client_id = FAKE.uuid4()
        self.client_secret = FAKE.word()
        self.resource_group_name = FAKE.word()
        self.storage_account_name = FAKE.word()

        self.container_name = FAKE.word()
        self.current_date_time = datetime.today()
        self.export_directory = FAKE.word()
        mock_factory.return_value = MockAzureClientFactory(
            self.subscription_id,
            self.container_name,
            self.current_date_time,
            self.export_directory,
            self.resource_group_name,
            self.storage_account_name,
        )

        self.client = AzureService(
            subscription_id=self.subscription_id,
            tenant_id=self.tenant_id,
            client_id=self.client_id,
            client_secret=self.client_secret,
            resource_group_name=self.resource_group_name,
            storage_account_name=self.storage_account_name,
        )

    def test_initializer(self):
        """Test the AzureService initializer."""
        self.assertIsNotNone(self.client._factory)
        self.assertIsNotNone(self.client._cloud_storage_account)

    def test_get_cost_export_for_key(self):
        """Test that a cost export is retrieved by a key."""
        today = self.current_date_time
        yesterday = today - relativedelta(days=1)
        test_matrix = [
            {
                "key":
                "{}_{}_day_{}".format(self.container_name, "blob", today.day),
                "expected_date":
                today.date()
            },
            {
                "key":
                "{}_{}_day_{}".format(self.container_name, "blob",
                                      yesterday.day),
                "expected_date":
                yesterday.date(),
            },
        ]

        for test in test_matrix:
            key = test.get("key")
            expected_modified_date = test.get("expected_date")
            cost_export = self.client.get_cost_export_for_key(
                key, self.container_name)
            self.assertIsNotNone(cost_export)
            self.assertEquals(cost_export.name, key)
            self.assertEquals(cost_export.properties.last_modified.date(),
                              expected_modified_date)

    def test_get_cost_export_for_missing_key(self):
        """Test that a cost export is not retrieved by an incorrect key."""
        key = "{}_{}_wrong".format(self.container_name, "blob")
        with self.assertRaises(AzureCostReportNotFound):
            self.client.get_cost_export_for_key(key, self.container_name)

    def test_get_latest_cost_export_for_path(self):
        """Test that the latest cost export is returned for a given path."""
        report_path = "{}_{}".format(self.container_name, "blob")
        cost_export = self.client.get_latest_cost_export_for_path(
            report_path, self.container_name)
        self.assertEquals(cost_export.properties.last_modified.date(),
                          self.current_date_time.date())

    def test_get_latest_cost_export_for_path_missing(self):
        """Test that the no cost export is returned for a missing path."""
        report_path = FAKE.word()
        with self.assertRaises(AzureCostReportNotFound):
            self.client.get_latest_cost_export_for_path(
                report_path, self.container_name)

    def test_describe_cost_management_exports(self):
        """Test that cost management exports are returned for the account."""
        exports = self.client.describe_cost_management_exports()
        self.assertEquals(len(exports), 2)
        for export in exports:
            self.assertEquals(export.get("container"), self.container_name)
            self.assertEquals(export.get("directory"), self.export_directory)
            self.assertIn("{}_{}".format(self.container_name, "blob"),
                          export.get("name"))

    @patch("masu.external.downloader.azure.azure_service.AzureClientFactory")
    def test_describe_cost_management_exports_wrong_account(
            self, mock_factory):
        """Test that cost management exports are not returned from incorrect account."""
        mock_factory.return_value = MockAzureClientFactory(
            self.subscription_id,
            self.container_name,
            self.current_date_time,
            self.export_directory,
            self.resource_group_name,
            self.storage_account_name,
        )

        client = AzureService(
            subscription_id=self.subscription_id,
            tenant_id=self.tenant_id,
            client_id=self.client_id,
            client_secret=self.client_secret,
            resource_group_name=self.resource_group_name,
            storage_account_name="wrongaccount",
        )

        exports = client.describe_cost_management_exports()
        self.assertEquals(exports, [])

    def test_download_cost_export(self):
        """Test that cost management exports are downloaded."""
        key = "{}_{}_day_{}".format(self.container_name, "blob",
                                    self.current_date_time.day)
        file_path = self.client.download_cost_export(key, self.container_name)
        self.assertTrue(file_path.endswith(".csv"))