Example #1
0
    async def test_set_table_acl_with_empty_signed_identifiers(self, tables_storage_account_name, tables_primary_storage_account_key):
        # Arrange
        account_url = self.account_url(tables_storage_account_name, "table")
        ts = TableServiceClient(credential=tables_primary_storage_account_key, endpoint=account_url)

        table = await self._create_table(ts)
        try:
            # Act
            await table.set_table_access_policy(signed_identifiers={})

            # Assert
            acl = await table.get_table_access_policy()
            assert acl is not None
            assert len(acl) ==  0
        finally:
            await ts.delete_table(table.table_name)
Example #2
0
    async def test_set_table_acl_too_many_ids(self, tables_storage_account_name, tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        ts = TableServiceClient(url, credential=tables_primary_storage_account_key)
        table = await self._create_table(ts)
        try:
            # Act
            identifiers = dict()
            for i in range(0, 6):
                identifiers['id{}'.format(i)] = None

            # Assert
            with pytest.raises(ValueError):
                await table.set_table_access_policy(signed_identifiers=identifiers)
        finally:
            await ts.delete_table(table.table_name)
Example #3
0
    async def test_create_table_fail_on_exist(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
        # Arrange
        ts = TableServiceClient(self.account_url(tables_cosmos_account_name, "cosmos"), tables_primary_cosmos_account_key)
        table_name = self._get_table_reference()

        # Act
        created = await ts.create_table(table_name=table_name)
        with pytest.raises(ResourceExistsError):
            await ts.create_table(table_name=table_name)

        # Assert
        assert created
        await ts.delete_table(table_name=table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_set_hour_metrics_async(self, resource_group, location, cosmos_account, cosmos_account_key):
        # Arrange
        url = self.account_url(cosmos_account, "cosmos")
        if 'cosmos' in url:
            pytest.skip("Cosmos Tables does not yet support service properties")
        tsc = TableServiceClient(url, cosmos_account_key)
        hour_metrics = Metrics(enabled=True, include_apis=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        await tsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        received_props = await tsc.get_service_properties()
        self._assert_metrics_equal(received_props['hour_metrics'], hour_metrics)
        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_create_client_with_api_version(self):
        url = self.account_url(self.tables_storage_account_name, "table")
        client = TableServiceClient(url, credential=self.credential)
        assert client._client._config.version == "2019-02-02"
        table = client.get_table_client('tablename')
        assert table._client._config.version == "2019-02-02"

        client = TableServiceClient(url, credential=self.credential, api_version="2019-07-07")
        assert client._client._config.version == "2019-07-07"
        table = client.get_table_client('tablename')
        assert table._client._config.version == "2019-07-07"

        with pytest.raises(ValueError):
            TableServiceClient(url, credential=self.credential, api_version="foo")
    async def test_set_logging_async(self, resource_group, location, cosmos_account, cosmos_account_key):
        # Arrange
        url = self.account_url(cosmos_account, "cosmos")
        if 'cosmos' in url:
            pytest.skip("Cosmos Tables does not yet support service properties")
        tsc = TableServiceClient(url, cosmos_account_key)
        logging = TableAnalyticsLogging(read=True, write=True, delete=True, retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        await tsc.set_service_properties(analytics_logging=logging)

        # Assert
        received_props = await tsc.get_service_properties()
        self._assert_logging_equal(received_props['analytics_logging'], logging)
        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_table_service_stats_when_unavailable(
            self, resource_group, location, cosmos_account,
            cosmos_account_key):
        # Arrange
        tsc = TableServiceClient(self.account_url(cosmos_account, "cosmos"),
                                 cosmos_account_key)

        # Act
        stats = await tsc.get_service_stats(
            raw_response_hook=self.
            override_response_body_with_unavailable_status)

        # Assert
        self._assert_stats_unavailable(stats)

        if self.is_live:
            sleep(SLEEP_DELAY)
Example #8
0
    async def test_get_table_acl(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
        # Arrange
        url = self.account_url(tables_cosmos_account_name, "cosmos")
        ts = TableServiceClient(self.account_url(tables_cosmos_account_name, "cosmos"), tables_primary_cosmos_account_key)
        table = await self._create_table(ts)
        try:
            # Act
            acl = await table.get_table_access_policy()

            # Assert
            assert acl is not None
            assert len(acl) ==  0
        finally:
            await ts.delete_table(table.table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_unicode_create_table_unicode_name(self, resource_group, location, storage_account,
                                                     storage_account_key):
        # Arrange
        url = self.account_url(storage_account, "table")
        if 'cosmos' in url:
            pytest.skip("Cosmos URLs support unicode table names")
        ts = TableServiceClient(url, storage_account_key)
        table_name = u'啊齄丂狛狜'

        # Act
        # with self.assertRaises(HttpResponseError):

        with pytest.raises(ValueError) as excinfo:
            await ts.create_table(table_name=table_name)

        assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long.""" in str(
            excinfo)
    async def test_retention_too_long_async(self, resource_group, location,
                                            cosmos_account,
                                            cosmos_account_key):
        # Arrange
        tsc = TableServiceClient(self.account_url(cosmos_account, "cosmos"),
                                 cosmos_account_key)
        minute_metrics = Metrics(enabled=True,
                                 include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True,
                                                                  days=366))

        await tsc.set_service_properties(None, None, minute_metrics)
        # Assert
        self.assertRaises(HttpResponseError, tsc.set_service_properties, None,
                          None, minute_metrics)
        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_create_table(self, resource_group, location, cosmos_account,
                                cosmos_account_key):
        # Arrange
        ts = TableServiceClient(self.account_url(cosmos_account, "cosmos"),
                                cosmos_account_key)
        table_name = self._get_table_reference()

        # Act
        created = await ts.create_table(table_name=table_name)

        # Assert
        assert created.table_name == table_name

        await ts.delete_table(table_name=table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
Example #12
0
    async def test_set_minute_metrics_async(self, resource_group, location, storage_account, storage_account_key):
        # Arrange
        url = self.account_url(storage_account, "table")
        if 'cosmos' in url:
            pytest.skip("Cosmos Tables does not yet support service properties")
        tsc = TableServiceClient(url, storage_account_key)
        minute_metrics = Metrics(enabled=True, include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True, days=5))

        # Act
        await tsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        if self.is_live:
            time.sleep(30)
        received_props = await tsc.get_service_properties()
        self._assert_metrics_equal(received_props['minute_metrics'], minute_metrics)
Example #13
0
    async def test_delete_table_invalid_name(
            self, tables_cosmos_account_name,
            tables_primary_cosmos_account_key):
        # Arrange
        ts = TableServiceClient(
            self.account_url(tables_cosmos_account_name, "cosmos"),
            tables_primary_cosmos_account_key)
        invalid_table_name = "my_table"

        with pytest.raises(ValueError) as excinfo:
            await ts.create_table(invalid_table_name)

        assert "Table names must be alphanumeric, cannot begin with a number, and must be between 3-63 characters long." "" in str(
            excinfo)

        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_delete_table_with_existing_table(self, resource_group,
                                                    location, cosmos_account,
                                                    cosmos_account_key):
        # Arrange
        ts = TableServiceClient(self.account_url(cosmos_account, "cosmos"),
                                cosmos_account_key)
        table = await self._create_table(ts)

        # Act
        # deleted = table.delete_table()
        deleted = await ts.delete_table(table_name=table.table_name)

        # Assert
        assert deleted is None

        if self.is_live:
            sleep(SLEEP_DELAY)
Example #15
0
    async def test_create_service_with_custom_account_endpoint_path_async(
            self):
        self.sas_token = self.generate_sas_token()
        self.sas_token = AzureSasCredential(self.sas_token)
        custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token.signature
        for service_type in SERVICES.items():
            conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format(
                self.tables_cosmos_account_name,
                self.tables_primary_cosmos_account_key, custom_account_url)

            # Act
            service = service_type[0].from_connection_string(conn_string,
                                                             table_name="foo")

            # Assert
            assert service.account_name == self.tables_cosmos_account_name
            assert service.credential.named_key.name == self.tables_cosmos_account_name
            assert service.credential.named_key.key == self.tables_primary_cosmos_account_key
            assert service._primary_hostname == 'local-machine:11002/custom/account/path'

        service = TableServiceClient(endpoint=custom_account_url)
        assert service.account_name == "custom"
        assert service.credential == None
        assert service._primary_hostname == 'local-machine:11002/custom/account/path'
        # mine doesnt have a question mark at the end
        assert service.url.startswith(
            'http://local-machine:11002/custom/account/path')

        service = TableClient(endpoint=custom_account_url, table_name="foo")
        assert service.account_name == "custom"
        assert service.table_name == "foo"
        assert service.credential == None
        assert service._primary_hostname == 'local-machine:11002/custom/account/path'
        assert service.url.startswith(
            'http://local-machine:11002/custom/account/path')

        service = TableClient.from_table_url(
            "http://local-machine:11002/custom/account/path/foo" +
            self.sas_token.signature)
        assert service.account_name == "custom"
        assert service.table_name == "foo"
        assert service.credential == None
        assert service._primary_hostname == 'local-machine:11002/custom/account/path'
        assert service.url.startswith(
            'http://local-machine:11002/custom/account/path')
Example #16
0
    async def test_table_service_stats_when_unavailable(
            self, tables_cosmos_account_name,
            tables_primary_cosmos_account_key):
        # Arrange
        tsc = TableServiceClient(
            self.account_url(tables_cosmos_account_name, "cosmos"),
            tables_primary_cosmos_account_key)

        # Act
        stats = await tsc.get_service_stats(
            raw_response_hook=self.
            override_response_body_with_unavailable_status)

        # Assert
        self._assert_stats_unavailable(stats)

        if self.is_live:
            sleep(SLEEP_DELAY)
 async def test_set_table_acl_with_empty_signed_identifier(self, tables_storage_account_name, tables_primary_storage_account_key):
     # Arrange
     url = self.account_url(tables_storage_account_name, "table")
     ts = TableServiceClient(url, tables_primary_storage_account_key)
     table = await self._create_table(ts)
     try:
         # Act
         await table.set_table_access_policy(signed_identifiers={'empty': None})
         # Assert
         acl = await table.get_table_access_policy()
         assert acl is not None
         assert len(acl) ==  1
         assert acl['empty'] is not None
         assert acl['empty'].permission is None
         assert acl['empty'].expiry is None
         assert acl['empty'].start is None
     finally:
         await ts.delete_table(table.table_name)
Example #18
0
    async def test_create_table(self, tables_cosmos_account_name,
                                tables_primary_cosmos_account_key):
        # Arrange
        ts = TableServiceClient(
            self.account_url(tables_cosmos_account_name, "cosmos"),
            tables_primary_cosmos_account_key)
        table_name = self._get_table_reference()

        # Act
        created = await ts.create_table(table_name=table_name)

        # Assert
        assert created.table_name == table_name

        await ts.delete_table(table_name=table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_set_table_acl_too_many_ids(self, resource_group, location, storage_account, storage_account_key):
        # Arrange
        url = self.account_url(storage_account, "table")
        if 'cosmos' in url:
            pytest.skip("Cosmos endpoint does not support this")
        ts = TableServiceClient(url, storage_account_key)
        table = await self._create_table(ts)
        try:
            # Act
            identifiers = dict()
            for i in range(0, 6):
                identifiers['id{}'.format(i)] = None

            # Assert
            with pytest.raises(ValueError):
                await table.set_table_access_policy(table_name=table.table_name, signed_identifiers=identifiers)
        finally:
            await ts.delete_table(table.table_name)
Example #20
0
    async def test_delete_table_with_existing_table(
            self, tables_cosmos_account_name,
            tables_primary_cosmos_account_key):
        # Arrange
        ts = TableServiceClient(
            self.account_url(tables_cosmos_account_name, "cosmos"),
            tables_primary_cosmos_account_key)
        table = await self._create_table(ts)

        # Act
        # deleted = table.delete_table()
        deleted = await ts.delete_table(table_name=table.table_name)

        # Assert
        assert deleted is None

        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_get_table_acl(self, resource_group, location,
                                 storage_account, storage_account_key):
        # Arrange
        url = self.account_url(storage_account, "table")
        if 'cosmos' in url:
            pytest.skip("Cosmos endpoint does not support this")
        ts = TableServiceClient(self.account_url(storage_account, "table"),
                                storage_account_key)
        table = await self._create_table(ts)
        try:
            # Act
            acl = await table.get_table_access_policy()

            # Assert
            self.assertIsNotNone(acl)
            self.assertEqual(len(acl), 0)
        finally:
            await ts.delete_table(table.table_name)
Example #22
0
    async def test_table_service_properties_async(self, resource_group, location, storage_account, storage_account_key):
        # Arrange
        url = self.account_url(storage_account, "table")
        if 'cosmos' in url:
            pytest.skip("Cosmos Tables does not yet support service properties")
        tsc = TableServiceClient(url, storage_account_key, logging_enable=True)
        # Act
        resp = await tsc.set_service_properties(
            analytics_logging=TableAnalyticsLogging(),
            hour_metrics=Metrics(),
            minute_metrics=Metrics(),
            cors=list())

        # Assert
        self.assertIsNone(resp)
        if self.is_live:
            time.sleep(30)
        self._assert_properties_default(await tsc.get_service_properties())
Example #23
0
    async def test_set_table_acl_with_empty_signed_identifier(self, tables_storage_account_name, tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        ts = TableServiceClient(url, credential=tables_primary_storage_account_key)
        table = await self._create_table(ts)
        try:
            dt = datetime(2021, 6, 8, 2, 10, 9)
            signed_identifiers={
                'null': None,
                'empty': TableAccessPolicy(start=None, expiry=None, permission=None),
                'partial': TableAccessPolicy(permission='r'),
                'full': TableAccessPolicy(start=dt, expiry=dt, permission='r')
                }
            await table.set_table_access_policy(signed_identifiers)
            acl = await table.get_table_access_policy()
            assert acl is not None
            assert len(acl) ==  4
            assert acl['null'] is None
            assert acl['empty'] is None
            assert acl['partial'] is not None
            assert acl['partial'].permission == 'r'
            assert acl['partial'].expiry is None
            assert acl['partial'].start is None
            assert acl['full'] is not None
            assert acl['full'].permission == 'r'
            self._assert_policy_datetime(dt, acl['full'].expiry)
            self._assert_policy_datetime(dt, acl['full'].start)

            signed_identifiers.pop('empty')
            signed_identifiers['partial'] = None

            await table.set_table_access_policy(signed_identifiers)
            acl = await table.get_table_access_policy()
            assert acl is not None
            assert len(acl) ==  3
            assert 'empty' not in acl
            assert acl['null'] is None
            assert acl['partial'] is None
            assert acl['full'] is not None
            assert acl['full'].permission == 'r'
            self._assert_policy_datetime(dt, acl['full'].expiry)
            self._assert_policy_datetime(dt, acl['full'].start)
        finally:
            await ts.delete_table(table.table_name)
Example #24
0
    async def authentication_by_shared_access_signature(self):
        # Instantiate a TableServiceClient using a connection string
        # [START auth_by_sas]
        from azure.data.tables.aio import TableServiceClient

        # Create a SAS token to use for authentication of a client
        from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions
        sas_token = generate_account_sas(
            self.account_name,
            self.access_key,
            resource_types=ResourceTypes(service=True),
            permission=AccountSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1))

        async with TableServiceClient(
                account_url=self.account_url,
                credential=sas_token) as token_auth_table_service:
            properties = await table_service.get_service_properties()
            print("Shared Access Signature: {}".format(properties))
Example #25
0
    async def test_create_table_fail_on_exist(self, resource_group, location,
                                              cosmos_account,
                                              cosmos_account_key):
        # Arrange
        ts = TableServiceClient(self.account_url(cosmos_account, "cosmos"),
                                cosmos_account_key)
        table_name = self._get_table_reference()

        # Act
        created = await ts.create_table(table_name=table_name)
        with self.assertRaises(ResourceExistsError):
            await ts.create_table(table_name=table_name)

        # Assert
        self.assertTrue(created)
        await ts.delete_table(table_name=table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
Example #26
0
    async def test_set_table_acl_too_many_ids(self, tables_cosmos_account_name, tables_primary_cosmos_account_key):
        # Arrange
        url = self.account_url(tables_cosmos_account_name, "cosmos")
        ts = TableServiceClient(url, tables_primary_cosmos_account_key)
        table = await self._create_table(ts)
        try:
            # Act
            identifiers = dict()
            for i in range(0, 6):
                identifiers['id{}'.format(i)] = None

            # Assert
            with pytest.raises(ValueError):
                await table.set_table_access_policy(table_name=table.table_name, signed_identifiers=identifiers)
        finally:
            await ts.delete_table(table.table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
    async def test_create_service_with_custom_account_endpoint_path_async(
            self, tables_storage_account_name,
            tables_primary_storage_account_key):
        custom_account_url = "http://local-machine:11002/custom/account/path/" + self.sas_token
        for service_type in SERVICES.items():
            conn_string = 'DefaultEndpointsProtocol=http;AccountName={};AccountKey={};TableEndpoint={};'.format(
                tables_storage_account_name,
                tables_primary_storage_account_key, custom_account_url)

            # Act
            service = service_type[0].from_connection_string(conn_string,
                                                             table_name="foo")

            # Assert
            assert service.account_name == tables_storage_account_name
            assert service.credential.account_name == tables_storage_account_name
            assert service.credential.account_key == tables_primary_storage_account_key
            assert service._primary_hostname == 'local-machine:11002/custom/account/path'

        service = TableServiceClient(account_url=custom_account_url)
        assert service.account_name == None
        assert service.credential == None
        assert service._primary_hostname == 'local-machine:11002/custom/account/path'
        assert service.url.startswith(
            'http://local-machine:11002/custom/account/path')

        service = TableClient(account_url=custom_account_url, table_name="foo")
        assert service.account_name == None
        assert service.table_name == "foo"
        assert service.credential == None
        assert service._primary_hostname == 'local-machine:11002/custom/account/path'
        assert service.url.startswith(
            'http://local-machine:11002/custom/account/path')

        service = TableClient.from_table_url(
            "http://local-machine:11002/custom/account/path/foo" +
            self.sas_token)
        assert service.account_name == None
        assert service.table_name == "foo"
        assert service.credential == None
        assert service._primary_hostname == 'local-machine:11002/custom/account/path'
        assert service.url.startswith(
            'http://local-machine:11002/custom/account/path')
Example #28
0
    async def test_set_hour_metrics_async(self, tables_storage_account_name,
                                          tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        tsc = TableServiceClient(url, tables_primary_storage_account_key)
        hour_metrics = Metrics(enabled=True,
                               include_apis=True,
                               retention_policy=RetentionPolicy(enabled=True,
                                                                days=5))

        # Act
        await tsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        if self.is_live:
            time.sleep(30)
        received_props = await tsc.get_service_properties()
        self._assert_metrics_equal(received_props['hour_metrics'],
                                   hour_metrics)
    async def test_set_table_acl_with_empty_signed_identifiers(
            self, resource_group, location, cosmos_account,
            cosmos_account_key):
        # Arrange
        url = self.account_url(cosmos_account, "cosmos")
        ts = TableServiceClient(url, cosmos_account_key)
        table = await self._create_table(ts)
        try:
            # Act
            await table.set_table_access_policy(signed_identifiers={})

            # Assert
            acl = await table.get_table_access_policy()
            assert acl is not None
            assert len(acl) == 0
        finally:
            await ts.delete_table(table.table_name)

        if self.is_live:
            sleep(SLEEP_DELAY)
Example #30
0
    async def test_set_logging_async(self, tables_storage_account_name,
                                     tables_primary_storage_account_key):
        # Arrange
        url = self.account_url(tables_storage_account_name, "table")
        tsc = TableServiceClient(url, tables_primary_storage_account_key)
        logging = TableAnalyticsLogging(read=True,
                                        write=True,
                                        delete=True,
                                        retention_policy=RetentionPolicy(
                                            enabled=True, days=5))

        # Act
        await tsc.set_service_properties(analytics_logging=logging)

        # Assert
        if self.is_live:
            time.sleep(30)
        received_props = await tsc.get_service_properties()
        self._assert_logging_equal(received_props['analytics_logging'],
                                   logging)