1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import time
import pytest
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy
from azure.data.tables import (
TableServiceClient,
TableAnalyticsLogging,
TableMetrics,
TableRetentionPolicy,
TableCorsRule,
)
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
from _shared.testcase import TableTestCase
from preparers import tables_decorator
# ------------------------------------------------------------------------------
class TestTableServiceProperties(AzureRecordedTestCase, TableTestCase):
@tables_decorator
@recorded_by_proxy
def test_table_service_properties(self, tables_storage_account_name, tables_primary_storage_account_key):
# Arrange
url = self.account_url(tables_storage_account_name, "table")
tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
# Act
resp = tsc.set_service_properties(
analytics_logging=TableAnalyticsLogging(),
hour_metrics=TableMetrics(),
minute_metrics=TableMetrics(),
cors=list(),
)
# Assert
assert resp is None
if self.is_live:
time.sleep(45)
self._assert_properties_default(tsc.get_service_properties())
# --Test cases per feature ---------------------------------------
@tables_decorator
@recorded_by_proxy
def test_set_logging(self, tables_storage_account_name, tables_primary_storage_account_key):
# Arrange
url = self.account_url(tables_storage_account_name, "table")
tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
logging = TableAnalyticsLogging(
read=True, write=True, delete=True, retention_policy=TableRetentionPolicy(enabled=True, days=5)
)
# Act
tsc.set_service_properties(analytics_logging=logging)
# Assert
if self.is_live:
time.sleep(45)
received_props = tsc.get_service_properties()
self._assert_logging_equal(received_props["analytics_logging"], logging)
@tables_decorator
@recorded_by_proxy
def test_set_hour_metrics(self, tables_storage_account_name, tables_primary_storage_account_key):
# Arrange
url = self.account_url(tables_storage_account_name, "table")
tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
hour_metrics = TableMetrics(
enabled=True, include_apis=True, retention_policy=TableRetentionPolicy(enabled=True, days=5)
)
# Act
tsc.set_service_properties(hour_metrics=hour_metrics)
# Assert
if self.is_live:
time.sleep(45)
received_props = tsc.get_service_properties()
self._assert_metrics_equal(received_props["hour_metrics"], hour_metrics)
@tables_decorator
@recorded_by_proxy
def test_set_minute_metrics(self, tables_storage_account_name, tables_primary_storage_account_key):
# Arrange
url = self.account_url(tables_storage_account_name, "table")
tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
minute_metrics = TableMetrics(
enabled=True, include_apis=True, retention_policy=TableRetentionPolicy(enabled=True, days=5)
)
# Act
tsc.set_service_properties(minute_metrics=minute_metrics)
# Assert
if self.is_live:
time.sleep(45)
received_props = tsc.get_service_properties()
self._assert_metrics_equal(received_props["minute_metrics"], minute_metrics)
@tables_decorator
@recorded_by_proxy
def test_set_cors(self, tables_storage_account_name, tables_primary_storage_account_key):
# Arrange
url = self.account_url(tables_storage_account_name, "table")
tsc = TableServiceClient(url, credential=tables_primary_storage_account_key)
cors_rule1 = TableCorsRule(["www.xyz.com"], ["GET"])
allowed_origins = ["www.xyz.com", "www.ab.com", "www.bc.com"]
allowed_methods = ["GET", "PUT"]
max_age_in_seconds = 500
exposed_headers = ["x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc", "x-ms-meta-bcd"]
allowed_headers = ["x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz", "x-ms-meta-foo"]
cors_rule2 = TableCorsRule(allowed_origins, allowed_methods)
cors_rule2.max_age_in_seconds = max_age_in_seconds
cors_rule2.exposed_headers = exposed_headers
cors_rule2.allowed_headers = allowed_headers
cors = [cors_rule1, cors_rule2]
# Act
tsc.set_service_properties(cors=cors)
# Assert
if self.is_live:
time.sleep(45)
received_props = tsc.get_service_properties()
self._assert_cors_equal(received_props["cors"], cors)
# --Test cases for errors ---------------------------------------
@tables_decorator
@recorded_by_proxy
def test_too_many_cors_rules(self, tables_storage_account_name, tables_primary_storage_account_key):
# Arrange
tsc = TableServiceClient(
self.account_url(tables_storage_account_name, "table"), credential=tables_primary_storage_account_key
)
cors = []
for i in range(0, 6):
cors.append(TableCorsRule(["www.xyz.com"], ["GET"]))
# Assert
pytest.raises(HttpResponseError, tsc.set_service_properties, cors=cors)
@tables_decorator
@recorded_by_proxy
def test_retention_too_long(self, tables_storage_account_name, tables_primary_storage_account_key):
# Arrange
tsc = TableServiceClient(
self.account_url(tables_storage_account_name, "table"), credential=tables_primary_storage_account_key
)
minute_metrics = TableMetrics(
enabled=True, include_apis=True, retention_policy=TableRetentionPolicy(enabled=True, days=366)
)
# Assert
pytest.raises(HttpResponseError, tsc.set_service_properties, minute_metrics=minute_metrics)
@tables_decorator
@recorded_by_proxy
def test_client_with_url_ends_with_table_name(
self, tables_storage_account_name, tables_primary_storage_account_key
):
url = self.account_url(tables_storage_account_name, "table")
table_name = self.get_resource_name("mytable")
invalid_url = url + "/" + table_name
tsc = TableServiceClient(invalid_url, credential=tables_primary_storage_account_key)
with pytest.raises(ResourceNotFoundError) as exc:
tsc.create_table(table_name)
assert ("table specified does not exist") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
with pytest.raises(ResourceNotFoundError) as exc:
tsc.create_table_if_not_exists(table_name)
assert ("table specified does not exist") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
with pytest.raises(HttpResponseError) as exc:
tsc.set_service_properties(analytics_logging=TableAnalyticsLogging(write=True))
assert ("URI is invalid") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
with pytest.raises(HttpResponseError) as exc:
tsc.get_service_properties()
assert ("URI is invalid") in str(exc.value)
assert ("Please check your account URL.") in str(exc.value)
tsc.delete_table(table_name)
class TestTableUnitTest(TableTestCase):
def test_retention_no_days(self):
# Assert
pytest.raises(ValueError, TableRetentionPolicy, enabled=True)
|