diff --git a/tests/integration/fixtures/data.py b/tests/integration/fixtures/data.py index 7c8ba597..44e12b8b 100644 --- a/tests/integration/fixtures/data.py +++ b/tests/integration/fixtures/data.py @@ -15,7 +15,7 @@ from ..artifacts import CertKey def generate_signing_key(): signing_key = signedjson.key.generate_signing_key("0") - value = f"{signing_key.alg} {signing_key.version} " f"{signedjson.key.encode_signing_key_base64(signing_key)}" + value = f"{signing_key.alg} {signing_key.version} {signedjson.key.encode_signing_key_base64(signing_key)}" return value diff --git a/tests/integration/lib/synapse.py b/tests/integration/lib/synapse.py index 18ca21c2..3c33f3fc 100644 --- a/tests/integration/lib/synapse.py +++ b/tests/integration/lib/synapse.py @@ -80,15 +80,17 @@ async def upload_media(synapse_fqdn: str, user_access_token: str, file_path: Pat params = {"filename": file_path.name} with open(file_path, "rb") as f: - async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, RetryClient( - session, retry_options=retry_options, raise_for_status=True - ) as retry, retry.post( - "https://127.0.0.1/_matrix/media/v3/upload", - server_hostname=synapse_fqdn, - headers=headers, - params=params, - data=f.read(), - ) as response: + async with ( + aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, + RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, + retry.post( + "https://127.0.0.1/_matrix/media/v3/upload", + server_hostname=synapse_fqdn, + headers=headers, + params=params, + data=f.read(), + ) as response, + ): response_json = await response.json() assert response_json["content_uri"].startswith("mxc://") @@ -106,13 +108,15 @@ async def download_media( # Initialize SHA-256 hasher sha256_hash = hashlib.sha256() - async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, RetryClient( - session, retry_options=retry_options, raise_for_status=True - ) as retry, retry.get( - f"https://127.0.0.1/_matrix/client/v1/media/download/{server_name}/{content_id}", - headers=headers, - server_hostname=synapse_fqdn, - ) as response: + async with ( + aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, + RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, + retry.get( + f"https://127.0.0.1/_matrix/client/v1/media/download/{server_name}/{content_id}", + headers=headers, + server_hostname=synapse_fqdn, + ) as response, + ): # Process the stream in chunks while True: chunk = await response.content.read(8192) # 8KB chunks diff --git a/tests/integration/lib/utils.py b/tests/integration/lib/utils.py index 449aace6..6726006d 100644 --- a/tests/integration/lib/utils.py +++ b/tests/integration/lib/utils.py @@ -69,13 +69,15 @@ async def aiottp_get_json(url: str, ssl_context: SSLContext) -> Any: """ host = urlparse(url).hostname - async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, RetryClient( - session, retry_options=retry_options, raise_for_status=True - ) as retry, retry.get( - url.replace(host, "127.0.0.1"), - headers={"Host": host}, - server_hostname=host, - ) as response: + async with ( + aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, + RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, + retry.get( + url.replace(host, "127.0.0.1"), + headers={"Host": host}, + server_hostname=host, + ) as response, + ): return await response.json() @@ -96,11 +98,13 @@ async def aiohttp_post_json(url: str, data: dict, headers: dict, ssl_context: SS """ host = urlparse(url).hostname - async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, RetryClient( - session, retry_options=retry_options, raise_for_status=True - ) as retry, retry.post( - url.replace(host, "127.0.0.1"), headers=headers | {"Host": host}, server_hostname=host, json=data - ) as response: + async with ( + aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, + RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, + retry.post( + url.replace(host, "127.0.0.1"), headers=headers | {"Host": host}, server_hostname=host, json=data + ) as response, + ): return await response.json() @@ -124,9 +128,10 @@ def value_file_has(property_path, expected=None): a[key] = b[key] return a - with open(Path().resolve() / "charts" / "matrix-stack" / "values.yaml") as base_value_file, open( - os.environ["TEST_VALUES_FILE"] - ) as test_value_file: + with ( + open(Path().resolve() / "charts" / "matrix-stack" / "values.yaml") as base_value_file, + open(os.environ["TEST_VALUES_FILE"]) as test_value_file, + ): data = merge(yaml.safe_load(base_value_file), yaml.safe_load(test_value_file)) keys = property_path.split(".") diff --git a/tests/integration/test_networking.py b/tests/integration/test_networking.py index 57219ef3..6e1c52f1 100644 --- a/tests/integration/test_networking.py +++ b/tests/integration/test_networking.py @@ -138,16 +138,14 @@ async def test_pods_monitored( # Something monitored by multiple ServiceMonitors smells like a bug assert covered_pod.metadata.name not in monitored_pods, ( - f"Pod {covered_pod.metadata.name} " "is monitored multiple times" + f"Pod {covered_pod.metadata.name} is monitored multiple times" ) monitored_pods.add(covered_pod.metadata.name) service_monitor_is_useful = True - assert service_monitor_is_useful, ( - f"ServiceMonitor {service_monitor['metadata']['name']} " "does not cover any pod" - ) + assert service_monitor_is_useful, f"ServiceMonitor {service_monitor['metadata']['name']} does not cover any pod" assert all_monitorable_pods == monitored_pods, ( - f"Some pods are not monitored : " f"{', '.join(list(set(all_monitorable_pods) ^ set(monitored_pods)))}" + f"Some pods are not monitored : {', '.join(list(set(all_monitorable_pods) ^ set(monitored_pods)))}" ) diff --git a/tests/manifests/__init__.py b/tests/manifests/__init__.py index 6d920327..af10e9e6 100644 --- a/tests/manifests/__init__.py +++ b/tests/manifests/__init__.py @@ -41,7 +41,7 @@ def _enrich_components_to_test(details) -> Dict[str, Any]: _component_details[component].setdefault("hyphened_name", component) values_files = _component_details[component].setdefault("additional_values_files", []) - values_files.append(f"{_component_details[component]["hyphened_name"]}-minimal-values.yaml") + values_files.append(f"{_component_details[component]['hyphened_name']}-minimal-values.yaml") _component_details[component]["values_files"] = values_files del _component_details[component]["additional_values_files"] diff --git a/tests/manifests/test_ingresses.py b/tests/manifests/test_ingresses.py index 8a56dbe9..383a3f3d 100644 --- a/tests/manifests/test_ingresses.py +++ b/tests/manifests/test_ingresses.py @@ -234,11 +234,11 @@ async def test_ingress_services(templates): found_service = services_by_name[backend_service["name"]] if backend_service["port"].get("name"): port_names = [port["name"] for port in found_service["spec"]["ports"]] - assert ( - backend_service["port"]["name"] in port_names - ), f"Port name {backend_service['port']['name']} not found in service {backend_service['name']}" + assert backend_service["port"]["name"] in port_names, ( + f"Port name {backend_service['port']['name']} not found in service {backend_service['name']}" + ) else: port_numbers = [port["port"] for port in found_service["spec"]["ports"]] - assert ( - backend_service["port"]["number"] in port_numbers - ), f"Port number {backend_service['port']['number']} not found in service {backend_service['name']}" + assert backend_service["port"]["number"] in port_numbers, ( + f"Port number {backend_service['port']['number']} not found in service {backend_service['name']}" + ) diff --git a/tests/manifests/test_labels.py b/tests/manifests/test_labels.py index 7f5c957f..d94fd0e5 100644 --- a/tests/manifests/test_labels.py +++ b/tests/manifests/test_labels.py @@ -21,14 +21,14 @@ async def test_templates_have_expected_labels(templates): ] for template in templates: - id = f"{template["kind"]}/{template["metadata"]["name"]}" + id = f"{template['kind']}/{template['metadata']['name']}" labels = template["metadata"]["labels"] for expected_label in expected_labels: assert expected_label in labels, f"{expected_label} label not present in {id}" - assert ( - labels[expected_label] is not None - ), f"{expected_label} label is null in {id} and so won't be present in cluster" + assert labels[expected_label] is not None, ( + f"{expected_label} label is null in {id} and so won't be present in cluster" + ) assert labels["helm.sh/chart"].startswith("matrix-stack-") assert labels["app.kubernetes.io/managed-by"] == "Helm" @@ -39,10 +39,10 @@ async def test_templates_have_expected_labels(templates): f"The app.kubernetes.io/instance label for {id}" "does not start with the expected chart release name of 'pytest'. " ) - f"The label value is {labels["app.kubernetes.io/instance"]}" + f"The label value is {labels['app.kubernetes.io/instance']}" assert labels["app.kubernetes.io/instance"].replace("pytest-", "") == labels["app.kubernetes.io/name"], ( f"The app.kubernetes.io/name label for {id}" "is not a concatenation of the expected chart release name of 'pytest' and the instance label. " - f"The label value is {labels["app.kubernetes.io/instance"]} vs {labels["app.kubernetes.io/name"]}" + f"The label value is {labels['app.kubernetes.io/instance']} vs {labels['app.kubernetes.io/name']}" ) diff --git a/tests/manifests/test_pod_securityContext.py b/tests/manifests/test_pod_securityContext.py index ba87652e..ec25de0e 100644 --- a/tests/manifests/test_pod_securityContext.py +++ b/tests/manifests/test_pod_securityContext.py @@ -15,9 +15,9 @@ async def test_sets_nonRoot_uids_gids_in_pod_securityContext_by_default(template if template["kind"] in ["Deployment", "StatefulSet", "Job"]: id = f"{template['kind']}/{template['metadata']['name']}" - assert ( - "securityContext" in template["spec"]["template"]["spec"] - ), f"Pod securityContext unexpectedly absent for {id}" + assert "securityContext" in template["spec"]["template"]["spec"], ( + f"Pod securityContext unexpectedly absent for {id}" + ) pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] @@ -28,12 +28,12 @@ async def test_sets_nonRoot_uids_gids_in_pod_securityContext_by_default(template assert "runAsNonRoot" in pod_securityContext, f"No runAsNonRoot in {id}'s Pod securityContext" assert pod_securityContext["runAsNonRoot"], f"{id} is running as root" - assert ( - pod_securityContext["runAsUser"] == pod_securityContext["runAsGroup"] - ), f"{id} has distinct uid and gid in the Pod securityContext" - assert ( - pod_securityContext["runAsGroup"] == pod_securityContext["fsGroup"] - ), f"{id} has distinct run and FS gids in the Pod securityContext" + assert pod_securityContext["runAsUser"] == pod_securityContext["runAsGroup"], ( + f"{id} has distinct uid and gid in the Pod securityContext" + ) + assert pod_securityContext["runAsGroup"] == pod_securityContext["fsGroup"], ( + f"{id} has distinct run and FS gids in the Pod securityContext" + ) @pytest.mark.parametrize("values_file", values_files_to_test) @@ -51,9 +51,9 @@ async def test_can_nuke_pod_securityContext_ids(component, values, make_template if template["kind"] in ["Deployment", "StatefulSet", "Job"]: id = f"{template['kind']}/{template['metadata']['name']}" - assert ( - "securityContext" in template["spec"]["template"]["spec"] - ), f"Pod securityContext unexpectedly absent for {id}" + assert "securityContext" in template["spec"]["template"]["spec"], ( + f"Pod securityContext unexpectedly absent for {id}" + ) pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] @@ -68,19 +68,19 @@ async def test_sets_seccompProfile_in_pod_securityContext_by_default(templates): if template["kind"] in ["Deployment", "StatefulSet", "Job"]: id = f"{template['kind']}/{template['metadata']['name']}" - assert ( - "securityContext" in template["spec"]["template"]["spec"] - ), f"Pod securityContext unexpectedly absent for {id}" + assert "securityContext" in template["spec"]["template"]["spec"], ( + f"Pod securityContext unexpectedly absent for {id}" + ) pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] assert "seccompProfile" in pod_securityContext, f"No seccompProfile in {id}'s Pod securityContext" - assert ( - "type" in pod_securityContext["seccompProfile"] - ), f"No type in {id}'s Pod securityContext.seccompProfile" - assert ( - pod_securityContext["seccompProfile"]["type"] == "RuntimeDefault" - ), f"{id} has unexpected seccompProfile type" + assert "type" in pod_securityContext["seccompProfile"], ( + f"No type in {id}'s Pod securityContext.seccompProfile" + ) + assert pod_securityContext["seccompProfile"]["type"] == "RuntimeDefault", ( + f"{id} has unexpected seccompProfile type" + ) @pytest.mark.parametrize("values_file", values_files_to_test) @@ -94,9 +94,9 @@ async def test_can_nuke_pod_securityContext_seccompProfile(component, values, ma if template["kind"] in ["Deployment", "StatefulSet", "Job"]: id = f"{template['kind']}/{template['metadata']['name']}" - assert ( - "securityContext" in template["spec"]["template"]["spec"] - ), f"Pod securityContext unexpectedly absent for {id}" + assert "securityContext" in template["spec"]["template"]["spec"], ( + f"Pod securityContext unexpectedly absent for {id}" + ) pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] diff --git a/tests/manifests/test_secrets_consistency.py b/tests/manifests/test_secrets_consistency.py index 4e8e11e8..6846dfe8 100644 --- a/tests/manifests/test_secrets_consistency.py +++ b/tests/manifests/test_secrets_consistency.py @@ -54,9 +54,9 @@ def get_volume_from_mount(template, volume_mount): def match_in_content(container_name, mounted_secret_keys, mount_path, match_in): found_mount = False for match in re.findall(rf"{mount_path}/([^\s\n\");]+)", match_in): - assert ( - f"{mount_path}/{match}" in mounted_secret_keys - ), f"{mount_path}/{match} used in {container_name} but it is not found from any mounted secret" + assert f"{mount_path}/{match}" in mounted_secret_keys, ( + f"{mount_path}/{match} used in {container_name} but it is not found from any mounted secret" + ) found_mount = True return found_mount diff --git a/tests/manifests/test_service_monitors.py b/tests/manifests/test_service_monitors.py index 19bf2868..9d628e52 100644 --- a/tests/manifests/test_service_monitors.py +++ b/tests/manifests/test_service_monitors.py @@ -41,9 +41,9 @@ def workload_ids_for_service_monitor(service_monitor, templates) -> set[str]: workload_ids.extend(find_workload_ids_matching_selector(templates, service["spec"]["selector"])) assert len(workload_ids) > 0, f"No workloads behind ServiceMonitor {service_monitor['metadata']['name']}" - assert len(workload_ids) == len( - set(workload_ids) - ), f"ServiceMonitor {service_monitor['metadata']['name']} covers same workloads multiple times" + assert len(workload_ids) == len(set(workload_ids)), ( + f"ServiceMonitor {service_monitor['metadata']['name']} covers same workloads multiple times" + ) return set(workload_ids) @@ -52,9 +52,9 @@ def workload_ids_monitored(templates: Iterator[Any]) -> set[str]: for template in templates: if template["kind"] == "ServiceMonitor": these_monitored_workload_ids = workload_ids_for_service_monitor(template, templates) - assert ( - workload_ids_monitored.intersection(these_monitored_workload_ids) == set() - ), "Multiple ServiceMonitors cover the same workload" + assert workload_ids_monitored.intersection(these_monitored_workload_ids) == set(), ( + "Multiple ServiceMonitors cover the same workload" + ) workload_ids_monitored.update(these_monitored_workload_ids) return workload_ids_monitored @@ -99,9 +99,9 @@ async def test_service_monitored_as_appropriate(component, values: dict, make_te # We should now have no ServiceMonitors rendered workloads_to_cover = set() for template in await make_templates(values): - assert ( - template["kind"] != "ServiceMonitor" - ), f"{component} unexpectedly has a ServiceMonitor when all are turned off" + assert template["kind"] != "ServiceMonitor", ( + f"{component} unexpectedly has a ServiceMonitor when all are turned off" + ) if ( template["kind"] in ["Deployment", "StatefulSet", "Job"] and template["metadata"]["labels"].get("servicemonitor", "some") != "none" diff --git a/tests/manifests/test_serviceaccounts.py b/tests/manifests/test_serviceaccounts.py index c2dfa21f..af6dc9a8 100644 --- a/tests/manifests/test_serviceaccounts.py +++ b/tests/manifests/test_serviceaccounts.py @@ -17,9 +17,9 @@ async def test_dont_automount_serviceaccount_tokens(templates): if template["kind"] in ["Deployment", "StatefulSet"]: id = f"{template['kind']}/{template['metadata']['name']}" - assert not template["spec"]["template"]["spec"][ - "automountServiceAccountToken" - ], f"ServiceAccount token automounted for {id}" + assert not template["spec"]["template"]["spec"]["automountServiceAccountToken"], ( + f"ServiceAccount token automounted for {id}" + ) @pytest.mark.parametrize("values_file", values_files_to_test) @@ -35,9 +35,9 @@ async def test_uses_serviceaccount_named_as_per_pod_controller_by_default(templa serviceaccount_names.add(template["metadata"]["name"]) for id, template in workloads_by_id.items(): - assert ( - "serviceAccountName" in template["spec"]["template"]["spec"] - ), f"{id} does not set an explicit ServiceAccount" + assert "serviceAccountName" in template["spec"]["template"]["spec"], ( + f"{id} does not set an explicit ServiceAccount" + ) serviceaccount_name = template["spec"]["template"]["spec"]["serviceAccountName"] covered_serviceaccount_names.add(serviceaccount_name) @@ -73,12 +73,12 @@ async def test_uses_serviceaccount_named_as_values_if_specified(component, value serviceaccount_names.append(template["metadata"]["name"]) for id, template in workloads_by_id.items(): - assert ( - "serviceAccountName" in template["spec"]["template"]["spec"] - ), f"{id} does not set an explicit ServiceAccount" - assert ( - template["spec"]["template"]["spec"]["serviceAccountName"] in serviceaccount_names - ), f"{id} does not reference a created ServiceAccount" + assert "serviceAccountName" in template["spec"]["template"]["spec"], ( + f"{id} does not set an explicit ServiceAccount" + ) + assert template["spec"]["template"]["spec"]["serviceAccountName"] in serviceaccount_names, ( + f"{id} does not reference a created ServiceAccount" + ) assert ( template["metadata"]["labels"]["expected.name"] == template["spec"]["template"]["spec"]["serviceAccountName"] @@ -111,9 +111,9 @@ async def test_does_not_create_serviceaccounts_if_configured_not_to(component, v serviceaccount_names.add(template["metadata"]["name"]) for id, template in workloads_by_id.items(): - assert ( - "serviceAccountName" in template["spec"]["template"]["spec"] - ), f"{id} does not set an explicit ServiceAccount" + assert "serviceAccountName" in template["spec"]["template"]["spec"], ( + f"{id} does not set an explicit ServiceAccount" + ) serviceaccount_name = template["spec"]["template"]["spec"]["serviceAccountName"] if template["metadata"]["labels"].get("serviceAccount", "some") == "none": @@ -121,6 +121,6 @@ async def test_does_not_create_serviceaccounts_if_configured_not_to(component, v else: covered_serviceaccount_names.add(serviceaccount_name) - assert ( - serviceaccount_names == covered_serviceaccount_names - ), f"{id} created ServiceAccounts that it shouldn't have" + assert serviceaccount_names == covered_serviceaccount_names, ( + f"{id} created ServiceAccounts that it shouldn't have" + ) diff --git a/tests/manifests/test_tolerations.py b/tests/manifests/test_tolerations.py index 04bc8daa..97362c57 100644 --- a/tests/manifests/test_tolerations.py +++ b/tests/manifests/test_tolerations.py @@ -29,9 +29,9 @@ async def test_no_tolerations_by_default(templates): if template["kind"] in ["Deployment", "StatefulSet", "Job"]: id = f"{template['kind']}/{template['metadata']['name']}" - assert ( - "tolerations" not in template["spec"]["template"]["spec"] - ), f"Tolerations unexpectedly present for {id}" + assert "tolerations" not in template["spec"]["template"]["spec"], ( + f"Tolerations unexpectedly present for {id}" + ) @pytest.mark.parametrize("values_file", values_files_to_test)