Skip to content

Commit 046b973

Browse files
mrm9084Copilot
andauthored
Fix App Config Tests (#44112)
* fix tests * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Update test_azure_appconfiguration_client.py * update tests --------- Co-authored-by: Copilot <[email protected]>
1 parent f3984ab commit 046b973

File tree

5 files changed

+300
-99
lines changed

5 files changed

+300
-99
lines changed

sdk/appconfiguration/azure-appconfiguration/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/appconfiguration/azure-appconfiguration",
5-
"Tag": "python/appconfiguration/azure-appconfiguration_adbb64a91f"
5+
"Tag": "python/appconfiguration/azure-appconfiguration_710e235678"
66
}

sdk/appconfiguration/azure-appconfiguration/tests/test_azure_appconfiguration_client.py

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import copy
88
import json
99
import re
10+
import time
1011
from datetime import datetime, timezone
1112
from azure.core import MatchConditions
1213
from azure.core.exceptions import (
@@ -81,7 +82,7 @@ def test_add_configuration_setting(self, appconfiguration_connection_string):
8182
# method: set_configuration_setting
8283
@app_config_decorator
8384
@recorded_by_proxy
84-
def test_set_configuration_setting(self, appconfiguration_connection_string):
85+
def test_set_existing_configuration_setting_label_etag(self, appconfiguration_connection_string):
8586
client = self.create_client(appconfiguration_connection_string)
8687
to_set_kv = self.create_config_setting()
8788
to_set_kv.value = to_set_kv.value + "a"
@@ -125,6 +126,7 @@ def test_get_configuration_setting_no_label(self, appconfiguration_connection_st
125126
assert fetched_kv.label is None
126127
client.delete_configuration_setting(key=compare_kv.key, label=compare_kv.label)
127128

129+
# method: get_configuration_setting
128130
@app_config_decorator
129131
@recorded_by_proxy
130132
def test_get_configuration_setting(self, appconfiguration_connection_string):
@@ -226,9 +228,9 @@ def test_list_configuration_settings_key_label(self, appconfiguration_connection
226228
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
227229
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
228230
self.set_up(appconfiguration_connection_string)
229-
items = list(self.client.list_configuration_settings(KEY, LABEL))
231+
items = list(self.client.list_configuration_settings(key_filter=KEY, label_filter=LABEL))
230232
assert len(items) == 1
231-
assert all(x.label == LABEL and x.label == LABEL for x in items)
233+
assert all(x.key == KEY and x.label == LABEL for x in items)
232234

233235
with pytest.raises(TypeError) as ex:
234236
self.client.list_configuration_settings("MyKey1", key_filter="MyKey2")
@@ -274,7 +276,7 @@ def test_list_configuration_settings_only_key(self, appconfiguration_connection_
274276
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
275277
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
276278
self.set_up(appconfiguration_connection_string)
277-
items = list(self.client.list_configuration_settings(KEY))
279+
items = list(self.client.list_configuration_settings(key_filter=KEY))
278280
assert len(items) == 2
279281
assert all(x.key == KEY for x in items)
280282
self.tear_down()
@@ -396,7 +398,7 @@ def test_list_configuration_settings_only_accepttime(self, appconfiguration_conn
396398
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
397399
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
398400
recorded_variables = kwargs.pop("variables", {})
399-
recorded_variables.setdefault("timestamp", str(datetime.utcnow()))
401+
recorded_variables.setdefault("timestamp", str(datetime.now(timezone.utc)))
400402

401403
with self.create_client(appconfiguration_connection_string) as client:
402404
# Confirm all configuration settings are cleaned up
@@ -421,10 +423,10 @@ def test_list_revisions_key_label(self, appconfiguration_connection_string):
421423
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
422424
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
423425
self.set_up(appconfiguration_connection_string)
424-
to_list1 = self.create_config_setting()
425-
items = list(self.client.list_revisions(label_filter=to_list1.label, key_filter=to_list1.key))
426+
to_list = self.create_config_setting()
427+
items = list(self.client.list_revisions(label_filter=to_list.label, key_filter=to_list.key))
426428
assert len(items) >= 2
427-
assert all(x.key == to_list1.key and x.label == to_list1.label for x in items)
429+
assert all(x.key == to_list.key and x.label == to_list.label for x in items)
428430
self.tear_down()
429431

430432
@app_config_decorator
@@ -678,6 +680,7 @@ def test_config_setting_secret_reference(self, appconfiguration_connection_strin
678680
set_flag = client.set_configuration_setting(secret_reference)
679681
self._assert_same_keys(secret_reference, set_flag)
680682

683+
set_flag.secret_id = "https://test-test.vault.azure.net/new_secrets/connectionString"
681684
updated_flag = client.set_configuration_setting(set_flag)
682685
self._assert_same_keys(set_flag, updated_flag)
683686

@@ -977,11 +980,16 @@ def test_breaking_with_secret_reference_configuration_setting(self, appconfigura
977980

978981
@app_config_decorator
979982
@recorded_by_proxy
980-
def test_create_snapshot(self, appconfiguration_connection_string):
983+
def test_create_snapshot(self, appconfiguration_connection_string, **kwargs):
981984
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
982985
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
983986
self.set_up(appconfiguration_connection_string)
984-
snapshot_name = self.get_resource_name("snapshot")
987+
988+
variables = kwargs.pop("variables", {})
989+
dynamic_snapshot_name_postfix = variables.setdefault("dynamic_snapshot_name_postfix", str(int(time.time())))
990+
991+
snapshot_name = f"{self.get_resource_name('snapshot')}_{dynamic_snapshot_name_postfix}"
992+
985993
filters = [ConfigurationSettingsFilter(key=KEY, label=LABEL)]
986994
response = self.client.begin_create_snapshot(name=snapshot_name, filters=filters)
987995
created_snapshot = response.result()
@@ -995,14 +1003,20 @@ def test_create_snapshot(self, appconfiguration_connection_string):
9951003
self._assert_snapshots(received_snapshot, created_snapshot)
9961004

9971005
self.tear_down()
1006+
return variables
9981007

9991008
@app_config_decorator
10001009
@recorded_by_proxy
1001-
def test_update_snapshot_status(self, appconfiguration_connection_string):
1010+
def test_update_snapshot_status(self, appconfiguration_connection_string, **kwargs):
10021011
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
10031012
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
10041013
self.set_up(appconfiguration_connection_string)
1005-
snapshot_name = self.get_resource_name("snapshot")
1014+
1015+
variables = kwargs.pop("variables", {})
1016+
dynamic_snapshot_name_postfix = variables.setdefault("dynamic_snapshot_name_postfix", str(int(time.time())))
1017+
1018+
snapshot_name = f"{self.get_resource_name('snapshot')}_{dynamic_snapshot_name_postfix}"
1019+
10061020
filters = [ConfigurationSettingsFilter(key=KEY, label=LABEL)]
10071021
response = self.client.begin_create_snapshot(name=snapshot_name, filters=filters)
10081022
created_snapshot = response.result()
@@ -1015,14 +1029,20 @@ def test_update_snapshot_status(self, appconfiguration_connection_string):
10151029
assert recovered_snapshot.status == "ready"
10161030

10171031
self.tear_down()
1032+
return variables
10181033

10191034
@app_config_decorator
10201035
@recorded_by_proxy
1021-
def test_update_snapshot_status_with_etag(self, appconfiguration_connection_string):
1036+
def test_update_snapshot_status_with_etag(self, appconfiguration_connection_string, **kwargs):
10221037
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
10231038
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
10241039
self.set_up(appconfiguration_connection_string)
1025-
snapshot_name = self.get_resource_name("snapshot")
1040+
1041+
variables = kwargs.pop("variables", {})
1042+
dynamic_snapshot_name_postfix = variables.setdefault("dynamic_snapshot_name_postfix", str(int(time.time())))
1043+
1044+
snapshot_name = f"{self.get_resource_name('snapshot')}_{dynamic_snapshot_name_postfix}"
1045+
10261046
filters = [ConfigurationSettingsFilter(key=KEY, label=LABEL)]
10271047
response = self.client.begin_create_snapshot(name=snapshot_name, filters=filters)
10281048
created_snapshot = response.result()
@@ -1037,19 +1057,24 @@ def test_update_snapshot_status_with_etag(self, appconfiguration_connection_stri
10371057
assert archived_snapshot.status == "archived"
10381058

10391059
self.tear_down()
1060+
return variables
10401061

10411062
@app_config_decorator
10421063
@recorded_by_proxy
1043-
def test_list_snapshots(self, appconfiguration_connection_string):
1064+
def test_list_snapshots(self, appconfiguration_connection_string, **kwargs):
10441065
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
10451066
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
10461067
self.set_up(appconfiguration_connection_string)
10471068

10481069
result = self.client.list_snapshots()
10491070
initial_snapshots = len(list(result))
10501071

1051-
snapshot_name1 = self.get_resource_name("snapshot1")
1052-
snapshot_name2 = self.get_resource_name("snapshot2")
1072+
variables = kwargs.pop("variables", {})
1073+
dynamic_snapshot_name_postfix = variables.setdefault("dynamic_snapshot_name_postfix", str(int(time.time())))
1074+
1075+
snapshot_name1 = f"{self.get_resource_name('snapshot1')}_{dynamic_snapshot_name_postfix}"
1076+
snapshot_name2 = f"{self.get_resource_name('snapshot2')}_{dynamic_snapshot_name_postfix}"
1077+
10531078
filters1 = [ConfigurationSettingsFilter(key=KEY)]
10541079
response1 = self.client.begin_create_snapshot(name=snapshot_name1, filters=filters1)
10551080
created_snapshot1 = response1.result()
@@ -1063,14 +1088,20 @@ def test_list_snapshots(self, appconfiguration_connection_string):
10631088
assert len(list(result)) == initial_snapshots + 2
10641089

10651090
self.tear_down()
1091+
return variables
10661092

10671093
@app_config_decorator
10681094
@recorded_by_proxy
1069-
def test_list_snapshot_configuration_settings(self, appconfiguration_connection_string):
1095+
def test_list_snapshot_configuration_settings(self, appconfiguration_connection_string, **kwargs):
10701096
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
10711097
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
10721098
self.set_up(appconfiguration_connection_string)
1073-
snapshot_name1 = self.get_resource_name("snapshot1")
1099+
1100+
variables = kwargs.pop("variables", {})
1101+
dynamic_snapshot_name_postfix = variables.setdefault("dynamic_snapshot_name_postfix", str(int(time.time())))
1102+
1103+
snapshot_name1 = f"{self.get_resource_name('snapshot1')}_{dynamic_snapshot_name_postfix}"
1104+
10741105
filters = [ConfigurationSettingsFilter(key=KEY, label=LABEL)]
10751106
response = self.client.begin_create_snapshot(name=snapshot_name1, filters=filters)
10761107
created_snapshot = response.result()
@@ -1079,7 +1110,8 @@ def test_list_snapshot_configuration_settings(self, appconfiguration_connection_
10791110
items = self.client.list_configuration_settings(snapshot_name=snapshot_name1)
10801111
assert len(list(items)) == 1
10811112

1082-
snapshot_name2 = self.get_resource_name("snapshot2")
1113+
snapshot_name2 = f"{self.get_resource_name('snapshot2')}_{dynamic_snapshot_name_postfix}"
1114+
10831115
filters = [ConfigurationSettingsFilter(key=KEY, label=LABEL, tags=["tag1=invalid"])]
10841116
response = self.client.begin_create_snapshot(name=snapshot_name2, filters=filters)
10851117
created_snapshot = response.result()
@@ -1089,16 +1121,18 @@ def test_list_snapshot_configuration_settings(self, appconfiguration_connection_
10891121
assert len(list(items)) == 0
10901122

10911123
self.tear_down()
1124+
return variables
10921125

10931126
@app_config_decorator
10941127
@recorded_by_proxy
10951128
def test_monitor_configuration_settings_by_page_etag(self, appconfiguration_connection_string):
10961129
# response header <x-ms-content-sha256> and <x-ms-date> are missing in python38.
10971130
set_custom_default_matcher(compare_bodies=False, excluded_headers="x-ms-content-sha256,x-ms-date")
10981131
with AzureAppConfigurationClient.from_connection_string(appconfiguration_connection_string) as client:
1132+
self.client = client
10991133
# prepare 200 configuration settings
11001134
for i in range(200):
1101-
client.add_configuration_setting(
1135+
self.client.add_configuration_setting(
11021136
ConfigurationSetting(
11031137
key=f"sample_key_{str(i)}",
11041138
label=f"sample_label_{str(i)}",
@@ -1108,7 +1142,7 @@ def test_monitor_configuration_settings_by_page_etag(self, appconfiguration_conn
11081142

11091143
# get page etags
11101144
page_etags = []
1111-
items = client.list_configuration_settings(key_filter="sample_key_*", label_filter="sample_label_*")
1145+
items = self.client.list_configuration_settings(key_filter="sample_key_*", label_filter="sample_label_*")
11121146
iterator = items.by_page()
11131147
for page in iterator:
11141148
etag = iterator.etag
@@ -1125,7 +1159,7 @@ def test_monitor_configuration_settings_by_page_etag(self, appconfiguration_conn
11251159
"Accept": "application/vnd.microsoft.appconfig.kvset+json, application/problem+json",
11261160
},
11271161
)
1128-
first_page_response = client.send_request(request)
1162+
first_page_response = self.client.send_request(request)
11291163
assert first_page_response.status_code == 304
11301164

11311165
link = first_page_response.headers.get("Link", None)
@@ -1136,14 +1170,14 @@ def test_monitor_configuration_settings_by_page_etag(self, appconfiguration_conn
11361170
method="GET", url=f"{continuation_token}", headers={"If-None-Match": page_etags[index]}
11371171
)
11381172
index += 1
1139-
response = client.send_request(request)
1173+
response = self.client.send_request(request)
11401174
assert response.status_code == 304
11411175

11421176
link = response.headers.get("Link", None)
11431177
continuation_token = link[1 : link.index(">")] if link else None
11441178

11451179
# do some changes
1146-
client.add_configuration_setting(
1180+
self.client.add_configuration_setting(
11471181
ConfigurationSetting(
11481182
key="sample_key_201",
11491183
label="sample_label_202",
@@ -1153,7 +1187,7 @@ def test_monitor_configuration_settings_by_page_etag(self, appconfiguration_conn
11531187

11541188
# get page etags after updates
11551189
new_page_etags = []
1156-
items = client.list_configuration_settings(key_filter="sample_key_*", label_filter="sample_label_*")
1190+
items = self.client.list_configuration_settings(key_filter="sample_key_*", label_filter="sample_label_*")
11571191
iterator = items.by_page()
11581192
for page in iterator:
11591193
etag = iterator.etag
@@ -1174,7 +1208,7 @@ def test_monitor_configuration_settings_by_page_etag(self, appconfiguration_conn
11741208
"Accept": "application/vnd.microsoft.appconfig.kvset+json, application/problem+json",
11751209
},
11761210
)
1177-
first_page_response = client.send_request(request)
1211+
first_page_response = self.client.send_request(request)
11781212
# 304 means the page doesn't have changes.
11791213
assert first_page_response.status_code == 304
11801214

@@ -1186,21 +1220,20 @@ def test_monitor_configuration_settings_by_page_etag(self, appconfiguration_conn
11861220
method="GET", url=f"{continuation_token}", headers={"If-None-Match": page_etags[index]}
11871221
)
11881222
index += 1
1189-
response = client.send_request(request)
1223+
response = self.client.send_request(request)
11901224

11911225
# 200 means the page has changes.
11921226
assert response.status_code == 200
11931227
items = response.json()["items"]
11941228
for item in items:
1195-
print(f"Key: {item['key']}, Label: {item['label']}")
1229+
# Read the keys
1230+
pass
11961231

11971232
link = response.headers.get("Link", None)
11981233
continuation_token = link[1 : link.index(">")] if link else None
11991234

12001235
# clean up
1201-
config_settings = client.list_configuration_settings()
1202-
for config_setting in config_settings:
1203-
client.delete_configuration_setting(key=config_setting.key, label=config_setting.label)
1236+
self.tear_down()
12041237

12051238
@app_config_decorator
12061239
@recorded_by_proxy

0 commit comments

Comments
 (0)