Skip to content

Commit 5685b9c

Browse files
authored
Merge pull request #4737 from alphagov/BC-performance-fixtures
Adding new fixtures for performance tests
2 parents f170516 + fd9b189 commit 5685b9c

File tree

3 files changed

+313
-80
lines changed

3 files changed

+313
-80
lines changed

app/commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,10 @@ def functional_test_fixtures():
854854
855855
REQUEST_BIN_API_TOKEN - request bin token to be used by functional tests
856856
857-
FUNCTIONAL_TEST_ENV_FILE - (optional) output file for the environment variables
858-
859857
SSM_UPLOAD_PATH - (optional) path to upload the environment variables to AWS SSM
860858
859+
PERFORMANCE_SSM_UPLOAD_PATH - (optional) path to upload performance-only environment variables to AWS SSM
860+
861861
"""
862862
if current_app.config["REGISTER_FUNCTIONAL_TESTING_BLUEPRINT"]:
863863
apply_fixtures()

app/functional_tests_fixtures/__init__.py

Lines changed: 145 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@
7474
)
7575
from app.schemas import api_key_schema, template_schema
7676

77+
78+
def _upload_env_to_ssm(ssm_upload_path, env_config, description):
79+
ssm = boto3.client("ssm")
80+
payload_size = len(env_config)
81+
current_app.logger.info(
82+
"--> Uploading %s to SSM (path=%s, size=%s bytes)",
83+
description,
84+
ssm_upload_path,
85+
payload_size,
86+
)
87+
try:
88+
response = ssm.put_parameter(
89+
Name=ssm_upload_path,
90+
Value=env_config,
91+
Type="SecureString",
92+
Overwrite=True,
93+
)
94+
95+
if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
96+
raise Exception("Failed to upload to SSM")
97+
except Exception as exc:
98+
raise Exception(
99+
f"Failed to upload {description} to SSM path {ssm_upload_path} (payload size={payload_size} bytes): {exc}"
100+
) from exc
101+
102+
77103
"""
78104
This function creates a set of database fixtures that functional tests use to run against.
79105
@@ -96,12 +122,14 @@ def apply_fixtures():
96122
function_tests_govuk_key_name = "govuk_notify"
97123
function_tests_live_key_name = "functional_tests_service_live_key"
98124
function_tests_test_key_name = "functional_tests_service_test_key"
125+
performance_tests_live_key_name = "performance_tests_service_live_key"
126+
performance_tests_test_key_name = "performance_tests_service_test_key"
99127
govuk_service_id = current_app.config["NOTIFY_SERVICE_ID"]
100128

101129
org_name = os.getenv("FUNCTIONAL_TEST_ORGANISATION_NAME", "Functional Tests Org")
102130
incoming_number = os.getenv("FUNCTIONAL_TEST_INBOUND_NUMBER", "07700900500")
103131

104-
env_var_dict = _create_db_objects(
132+
functional_env_var_dict, performance_env_var_dict = _create_db_objects(
105133
functional_test_password,
106134
request_bin_api_token,
107135
environment,
@@ -113,27 +141,24 @@ def apply_fixtures():
113141
govuk_service_id,
114142
org_name,
115143
incoming_number,
144+
performance_tests_live_key_name,
145+
performance_tests_test_key_name,
146+
performance_test_rate_limit=12000000,
147+
performance_test_sms_limit=5000000000,
148+
performance_test_email_limit=5000000000,
149+
performance_test_letter_limit=5000000000,
116150
)
117151

118-
functional_test_config = "\n".join(f"export {k}='{v}'" for k, v in env_var_dict.items())
119-
120-
functional_test_env_file = os.getenv("FUNCTIONAL_TEST_ENV_FILE", "/tmp/functional_test_env.sh")
121-
if functional_test_env_file != "":
122-
with open(functional_test_env_file, "w") as f:
123-
f.write(functional_test_config)
152+
functional_test_config = "\n".join(f"export {k}='{v}'" for k, v in functional_env_var_dict.items())
153+
performance_test_config = "\n".join(f"export {k}='{v}'" for k, v in performance_env_var_dict.items())
124154

125155
ssm_upload_path = os.getenv("SSM_UPLOAD_PATH")
126156
if ssm_upload_path:
127-
ssm = boto3.client("ssm")
128-
response = ssm.put_parameter(
129-
Name=ssm_upload_path,
130-
Value=functional_test_config,
131-
Type="SecureString",
132-
Overwrite=True,
133-
)
157+
_upload_env_to_ssm(ssm_upload_path, functional_test_config, "functional test environment")
134158

135-
if response["ResponseMetadata"]["HTTPStatusCode"] != 200:
136-
raise Exception("Failed to upload to SSM")
159+
performance_ssm_upload_path = os.getenv("PERFORMANCE_SSM_UPLOAD_PATH")
160+
if performance_ssm_upload_path:
161+
_upload_env_to_ssm(performance_ssm_upload_path, performance_test_config, "performance test environment")
137162

138163
current_app.logger.info("--> Functional test fixtures completed successfully")
139164

@@ -150,7 +175,13 @@ def _create_db_objects(
150175
govuk_service_id,
151176
org_name,
152177
incoming_number,
153-
) -> dict[str, str]:
178+
performance_tests_live_key_name="performance_tests_service_live_key",
179+
performance_tests_test_key_name="performance_tests_service_test_key",
180+
performance_test_rate_limit=12000000,
181+
performance_test_sms_limit=5000000000,
182+
performance_test_email_limit=5000000000,
183+
performance_test_letter_limit=5000000000,
184+
) -> tuple[dict[str, str], dict[str, str]]:
154185
current_app.logger.info("Creating functional test fixtures for %s:", environment)
155186

156187
current_app.logger.info("--> Ensure organisation exists")
@@ -179,17 +210,40 @@ def _create_db_objects(
179210

180211
current_app.logger.info("--> Ensure service exists")
181212
service = _create_service(org.id, service_admin_user)
213+
performance_service = _create_service(
214+
org.id,
215+
service_admin_user,
216+
service_name="Performance Tests",
217+
rate_limit=performance_test_rate_limit,
218+
sms_message_limit=performance_test_sms_limit,
219+
letter_message_limit=performance_test_letter_limit,
220+
email_message_limit=performance_test_email_limit,
221+
)
182222

183223
current_app.logger.info("--> Ensure users are added to service")
184224
dao_add_user_to_service(service, service_admin_user)
185225
dao_add_user_to_service(service, email_auth_user)
226+
dao_add_user_to_service(performance_service, service_admin_user)
227+
dao_add_user_to_service(performance_service, email_auth_user)
186228

187229
_grant_permissions(service, email_auth_user)
188230

189231
current_app.logger.info("--> Ensure api keys exists")
190232
api_key_notify = _create_api_key(function_tests_govuk_key_name, govuk_service_id, service_admin_user.id, "normal")
191233
api_key_live_key = _create_api_key(function_tests_live_key_name, service.id, service_admin_user.id, "normal")
192234
api_key_test_key = _create_api_key(function_tests_test_key_name, service.id, service_admin_user.id, "test")
235+
performance_api_key_live_key = _create_api_key(
236+
performance_tests_live_key_name,
237+
performance_service.id,
238+
service_admin_user.id,
239+
"normal",
240+
)
241+
_create_api_key(
242+
performance_tests_test_key_name,
243+
performance_service.id,
244+
service_admin_user.id,
245+
"test",
246+
)
193247

194248
current_app.logger.info("--> Ensure inbound number exists")
195249
inbound_number_id = _create_inbound_numbers(service.id, service_admin_user.id, incoming_number)
@@ -207,6 +261,18 @@ def _create_db_objects(
207261
),
208262
True,
209263
)
264+
performance_letter_contact = _create_service_letter_contact(
265+
performance_service.id,
266+
(
267+
"Government Digital Service\n"
268+
"The White Chapel Building\n"
269+
"10 Whitechapel High Street\n"
270+
"London\n"
271+
"E1 8QS\n"
272+
"United Kingdom"
273+
),
274+
True,
275+
)
210276

211277
email_template = _create_email_template(
212278
service=service,
@@ -244,6 +310,35 @@ def _create_db_objects(
244310
content="The quick brown fox jumped over the lazy dog.",
245311
)
246312

313+
performance_email_template = _create_email_template(
314+
service=performance_service,
315+
user_id=service_admin_user.id,
316+
name="Performance Tests - Email Template",
317+
subject="Performance Tests - Email",
318+
content="Performance test email for ((name)).",
319+
)
320+
performance_email_with_file_template = _create_email_template(
321+
service=performance_service,
322+
user_id=service_admin_user.id,
323+
name="Performance Tests - Email With File Template",
324+
subject="Performance Tests - Email With File",
325+
content="Performance test file for ((name)): ((link_to_file))",
326+
)
327+
performance_sms_template = _create_sms_template(
328+
service=performance_service,
329+
user_id=service_admin_user.id,
330+
name="Performance Tests - SMS Template",
331+
content="Performance test sms for ((name)).",
332+
)
333+
performance_letter_template = _create_letter_template(
334+
service=performance_service,
335+
user_id=service_admin_user.id,
336+
name="Performance Tests - Letter Template",
337+
subject="Performance Tests - Letter",
338+
content="Performance test letter for ((name)).",
339+
letter_contact_id=performance_letter_contact.id,
340+
)
341+
247342
api_client_integration_test_email_template = _create_email_template(
248343
service=service,
249344
user_id=service_admin_user.id,
@@ -298,18 +393,20 @@ def _create_db_objects(
298393

299394
current_app.logger.info("--> Ensure service permissions exists")
300395
_create_service_permissions(service.id)
396+
_create_service_permissions(performance_service.id)
301397

302398
current_app.logger.info("--> Ensure service sms senders exists")
303399
_create_service_sms_senders(service.id, incoming_number, True, inbound_number_id)
304400
sms_sender = _create_service_sms_senders(service.id, "func tests", False, None)
401+
_create_service_sms_senders(performance_service.id, incoming_number, True, None)
305402

306403
current_app.logger.info("--> Ensure service inbound api exists")
307404
_create_service_inbound_api(service.id, service_admin_user.id)
308405

309406
current_app.logger.info("--> Ensure dummy inbound SMS objects exist")
310407
_create_inbound_sms(service, 3)
311408

312-
return {
409+
functional_env_var_dict = {
313410
"FUNCTIONAL_TESTS_API_HOST": current_app.config["API_HOST_NAME"],
314411
"FUNCTIONAL_TESTS_ADMIN_HOST": current_app.config["ADMIN_BASE_URL"],
315412
"ENVIRONMENT": environment,
@@ -357,6 +454,17 @@ def _create_db_objects(
357454
"INBOUND_SMS_QUERY_KEY": f"{function_tests_test_key_name}-{service.id}-{api_key_test_key.secret}",
358455
}
359456

457+
performance_env_var_dict = {
458+
"PERFORMANCE_TESTS_API_HOST": current_app.config["API_HOST_NAME"],
459+
"PERFORMANCE_TESTS_SERVICE_API_KEY": f"{performance_tests_live_key_name}-{performance_service.id}-{performance_api_key_live_key.secret}", # noqa: E501
460+
"PERFORMANCE_TEST_SMS_TEMPLATE_ID": performance_sms_template.id,
461+
"PERFORMANCE_TEST_EMAIL_TEMPLATE_ID": performance_email_template.id,
462+
"PERFORMANCE_TEST_EMAIL_WITH_FILE_TEMPLATE_ID": performance_email_with_file_template.id,
463+
"PERFORMANCE_TEST_LETTER_TEMPLATE_ID": performance_letter_template.id,
464+
}
465+
466+
return functional_env_var_dict, performance_env_var_dict
467+
360468

361469
def _create_user(name, email_address, password, auth_type="sms_auth", organisations=None, mobile_number=None):
362470
if organisations is None:
@@ -400,7 +508,15 @@ def _create_organiation(email_domain, org_name):
400508
return org
401509

402510

403-
def _create_service(org_id, user, service_name="Functional Tests"):
511+
def _create_service(
512+
org_id,
513+
user,
514+
service_name="Functional Tests",
515+
rate_limit=3000,
516+
sms_message_limit=1000,
517+
letter_message_limit=1000,
518+
email_message_limit=1000,
519+
):
404520
services = get_services_by_partial_name(service_name)
405521

406522
service = None
@@ -416,14 +532,21 @@ def _create_service(org_id, user, service_name="Functional Tests"):
416532
"organisation_id": org_id,
417533
"organisation_type": "central",
418534
"created_by": user.id,
419-
"sms_message_limit": 1000,
420-
"letter_message_limit": 1000,
421-
"email_message_limit": 1000,
535+
"rate_limit": rate_limit,
536+
"sms_message_limit": sms_message_limit,
537+
"letter_message_limit": letter_message_limit,
538+
"email_message_limit": email_message_limit,
422539
"contact_link": current_app.config["ADMIN_BASE_URL"],
423540
}
424541
)
425542
dao_create_service(service, user)
426543

544+
service.rate_limit = rate_limit
545+
service.sms_message_limit = sms_message_limit
546+
service.letter_message_limit = letter_message_limit
547+
service.email_message_limit = email_message_limit
548+
service.contact_link = current_app.config["ADMIN_BASE_URL"]
549+
427550
set_default_free_allowance_for_service(service=service, year_start=None)
428551

429552
return service

0 commit comments

Comments
 (0)