Skip to content

Commit

Permalink
Use plain asserts in tests. (apache#12951)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarp authored Jan 17, 2021
1 parent 8af5a33 commit 39d9057
Show file tree
Hide file tree
Showing 534 changed files with 9,942 additions and 10,443 deletions.
4 changes: 2 additions & 2 deletions TESTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Example test here:
res = render_chart('GIT-SYNC', helm_settings,
show_only=["templates/scheduler/scheduler-deployment.yaml"])
dep: k8s.V1Deployment = render_k8s_object(res[0], k8s.V1Deployment)
self.assertEqual("dags", dep.spec.template.spec.volumes[1].name)
assert "dags" == dep.spec.template.spec.volumes[1].name
To run tests using breeze run the following command

Expand Down Expand Up @@ -330,7 +330,7 @@ Example of the ``redis`` integration test:
hook = RedisHook(redis_conn_id='redis_default')
redis = hook.get_conn()
self.assertTrue(redis.ping(), 'Connection to Redis with PING works.')
assert redis.ping(), 'Connection to Redis with PING works.'
The markers can be specified at the test level or the class level (then all tests in this class
require an integration). You can add multiple markers with different integrations for tests that
Expand Down
69 changes: 32 additions & 37 deletions chart/tests/test_basic_helm_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,52 +39,47 @@ def test_basic_deployments(self):
list_of_kind_names_tuples = [
(k8s_object['kind'], k8s_object['metadata']['name']) for k8s_object in k8s_objects
]
self.assertEqual(
list_of_kind_names_tuples,
[
('ServiceAccount', 'TEST-BASIC-scheduler'),
('ServiceAccount', 'TEST-BASIC-webserver'),
('ServiceAccount', 'TEST-BASIC-worker'),
('Secret', 'TEST-BASIC-postgresql'),
('Secret', 'TEST-BASIC-airflow-metadata'),
('Secret', 'TEST-BASIC-airflow-result-backend'),
('ConfigMap', 'TEST-BASIC-airflow-config'),
('Role', 'TEST-BASIC-pod-launcher-role'),
('Role', 'TEST-BASIC-pod-log-reader-role'),
('RoleBinding', 'TEST-BASIC-pod-launcher-rolebinding'),
('RoleBinding', 'TEST-BASIC-pod-log-reader-rolebinding'),
('Service', 'TEST-BASIC-postgresql-headless'),
('Service', 'TEST-BASIC-postgresql'),
('Service', 'TEST-BASIC-statsd'),
('Service', 'TEST-BASIC-webserver'),
('Deployment', 'TEST-BASIC-scheduler'),
('Deployment', 'TEST-BASIC-statsd'),
('Deployment', 'TEST-BASIC-webserver'),
('StatefulSet', 'TEST-BASIC-postgresql'),
('Secret', 'TEST-BASIC-fernet-key'),
('Job', 'TEST-BASIC-create-user'),
('Job', 'TEST-BASIC-run-airflow-migrations'),
],
)
self.assertEqual(OBJECT_COUNT_IN_BASIC_DEPLOYMENT, len(k8s_objects))
assert list_of_kind_names_tuples == [
('ServiceAccount', 'TEST-BASIC-scheduler'),
('ServiceAccount', 'TEST-BASIC-webserver'),
('ServiceAccount', 'TEST-BASIC-worker'),
('Secret', 'TEST-BASIC-postgresql'),
('Secret', 'TEST-BASIC-airflow-metadata'),
('Secret', 'TEST-BASIC-airflow-result-backend'),
('ConfigMap', 'TEST-BASIC-airflow-config'),
('Role', 'TEST-BASIC-pod-launcher-role'),
('Role', 'TEST-BASIC-pod-log-reader-role'),
('RoleBinding', 'TEST-BASIC-pod-launcher-rolebinding'),
('RoleBinding', 'TEST-BASIC-pod-log-reader-rolebinding'),
('Service', 'TEST-BASIC-postgresql-headless'),
('Service', 'TEST-BASIC-postgresql'),
('Service', 'TEST-BASIC-statsd'),
('Service', 'TEST-BASIC-webserver'),
('Deployment', 'TEST-BASIC-scheduler'),
('Deployment', 'TEST-BASIC-statsd'),
('Deployment', 'TEST-BASIC-webserver'),
('StatefulSet', 'TEST-BASIC-postgresql'),
('Secret', 'TEST-BASIC-fernet-key'),
('Job', 'TEST-BASIC-create-user'),
('Job', 'TEST-BASIC-run-airflow-migrations'),
]
assert OBJECT_COUNT_IN_BASIC_DEPLOYMENT == len(k8s_objects)
for k8s_object in k8s_objects:
labels = jmespath.search('metadata.labels', k8s_object) or {}
if 'postgresql' in labels.get('chart'):
continue
k8s_name = k8s_object['kind'] + ":" + k8s_object['metadata']['name']
self.assertEqual(
'TEST-VALUE',
labels.get("TEST-LABEL"),
f"Missing label TEST-LABEL on {k8s_name}. Current labels: {labels}",
)
assert 'TEST-VALUE' == labels.get(
"TEST-LABEL"
), f"Missing label TEST-LABEL on {k8s_name}. Current labels: {labels}"

def test_basic_deployment_without_default_users(self):
k8s_objects = render_chart("TEST-BASIC", {"webserver": {'defaultUser': {'enabled': False}}})
list_of_kind_names_tuples = [
(k8s_object['kind'], k8s_object['metadata']['name']) for k8s_object in k8s_objects
]
self.assertNotIn(('Job', 'TEST-BASIC-create-user'), list_of_kind_names_tuples)
self.assertEqual(OBJECT_COUNT_IN_BASIC_DEPLOYMENT - 1, len(k8s_objects))
assert ('Job', 'TEST-BASIC-create-user') not in list_of_kind_names_tuples
assert OBJECT_COUNT_IN_BASIC_DEPLOYMENT - 1 == len(k8s_objects)

def test_network_policies_are_valid(self):
k8s_objects = render_chart(
Expand All @@ -109,7 +104,7 @@ def test_network_policies_are_valid(self):
('NetworkPolicy', 'TEST-BASIC-worker-policy'),
]
for kind_name in expected_kind_names:
self.assertIn(kind_name, kind_names_tuples)
assert kind_name in kind_names_tuples

def test_chart_is_consistent_with_official_airflow_image(self):
def get_k8s_objs_with_image(obj: Union[List[Any], Dict[str, Any]]) -> List[Dict[str, Any]]:
Expand Down Expand Up @@ -137,4 +132,4 @@ def get_k8s_objs_with_image(obj: Union[List[Any], Dict[str, Any]]) -> List[Dict[
image: str = obj["image"] # pylint: disable=invalid-sequence-index
if image.startswith(image_repo):
# Make sure that a command is not specified
self.assertNotIn("command", obj)
assert "command" not in obj
8 changes: 4 additions & 4 deletions chart/tests/test_celery_kubernetes_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_should_create_a_worker_deployment_with_the_celery_executor(self):
show_only=["templates/workers/worker-deployment.yaml"],
)

self.assertEqual("config", jmespath.search("spec.template.spec.volumes[0].name", docs[0]))
self.assertEqual("dags", jmespath.search("spec.template.spec.volumes[1].name", docs[0]))
assert "config" == jmespath.search("spec.template.spec.volumes[0].name", docs[0])
assert "dags" == jmespath.search("spec.template.spec.volumes[1].name", docs[0])

def test_should_create_a_worker_deployment_with_the_celery_kubernetes_executor(self):
docs = render_chart(
Expand All @@ -44,5 +44,5 @@ def test_should_create_a_worker_deployment_with_the_celery_kubernetes_executor(s
show_only=["templates/workers/worker-deployment.yaml"],
)

self.assertEqual("config", jmespath.search("spec.template.spec.volumes[0].name", docs[0]))
self.assertEqual("dags", jmespath.search("spec.template.spec.volumes[1].name", docs[0]))
assert "config" == jmespath.search("spec.template.spec.volumes[0].name", docs[0])
assert "dags" == jmespath.search("spec.template.spec.volumes[1].name", docs[0])
35 changes: 14 additions & 21 deletions chart/tests/test_cleanup_pods.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,21 @@ def test_should_create_cronjob_for_enabled_cleanup(self):
show_only=["templates/cleanup/cleanup-cronjob.yaml"],
)

self.assertEqual(
"airflow-cleanup-pods",
jmespath.search("spec.jobTemplate.spec.template.spec.containers[0].name", docs[0]),
assert "airflow-cleanup-pods" == jmespath.search(
"spec.jobTemplate.spec.template.spec.containers[0].name", docs[0]
)
self.assertEqual(
"apache/airflow:2.0.0",
jmespath.search("spec.jobTemplate.spec.template.spec.containers[0].image", docs[0]),
assert "apache/airflow:2.0.0" == jmespath.search(
"spec.jobTemplate.spec.template.spec.containers[0].image", docs[0]
)
self.assertIn(
{"name": "config", "configMap": {"name": "RELEASE-NAME-airflow-config"}},
jmespath.search("spec.jobTemplate.spec.template.spec.volumes", docs[0]),
)
self.assertIn(
{
"name": "config",
"mountPath": "/opt/airflow/airflow.cfg",
"subPath": "airflow.cfg",
"readOnly": True,
},
jmespath.search("spec.jobTemplate.spec.template.spec.containers[0].volumeMounts", docs[0]),
assert {"name": "config", "configMap": {"name": "RELEASE-NAME-airflow-config"}} in jmespath.search(
"spec.jobTemplate.spec.template.spec.volumes", docs[0]
)
assert {
"name": "config",
"mountPath": "/opt/airflow/airflow.cfg",
"subPath": "airflow.cfg",
"readOnly": True,
} in jmespath.search("spec.jobTemplate.spec.template.spec.containers[0].volumeMounts", docs[0])

def test_should_change_image_when_set_airflow_image(self):
docs = render_chart(
Expand All @@ -62,7 +56,6 @@ def test_should_change_image_when_set_airflow_image(self):
show_only=["templates/cleanup/cleanup-cronjob.yaml"],
)

self.assertEqual(
"airflow:test",
jmespath.search("spec.jobTemplate.spec.template.spec.containers[0].image", docs[0]),
assert "airflow:test" == jmespath.search(
"spec.jobTemplate.spec.template.spec.containers[0].image", docs[0]
)
19 changes: 8 additions & 11 deletions chart/tests/test_dags_persistent_volume_claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ def test_should_not_generate_a_document_if_persistence_is_disabled(self):
show_only=["templates/dags-persistent-volume-claim.yaml"],
)

self.assertEqual(0, len(docs))
assert 0 == len(docs)

def test_should_not_generate_a_document_when_using_an_existing_claim(self):
docs = render_chart(
values={"dags": {"persistence": {"enabled": True, "existingClaim": "test-claim"}}},
show_only=["templates/dags-persistent-volume-claim.yaml"],
)

self.assertEqual(0, len(docs))
assert 0 == len(docs)

def test_should_generate_a_document_if_persistence_is_enabled_and_not_using_an_existing_claim(self):
docs = render_chart(
values={"dags": {"persistence": {"enabled": True, "existingClaim": None}}},
show_only=["templates/dags-persistent-volume-claim.yaml"],
)

self.assertEqual(1, len(docs))
assert 1 == len(docs)

def test_should_set_pvc_details_correctly(self):
docs = render_chart(
Expand All @@ -63,11 +63,8 @@ def test_should_set_pvc_details_correctly(self):
show_only=["templates/dags-persistent-volume-claim.yaml"],
)

self.assertEqual(
{
"accessModes": ["ReadWriteMany"],
"resources": {"requests": {"storage": "1G"}},
"storageClassName": "MyStorageClass",
},
jmespath.search("spec", docs[0]),
)
assert {
"accessModes": ["ReadWriteMany"],
"resources": {"requests": {"storage": "1G"}},
"storageClassName": "MyStorageClass",
} == jmespath.search("spec", docs[0])
10 changes: 5 additions & 5 deletions chart/tests/test_extra_configmaps_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ def test_extra_configmaps(self):
("ConfigMap", f"{RELEASE_NAME}-airflow-variables"),
("ConfigMap", f"{RELEASE_NAME}-other-variables"),
]
self.assertEqual(set(k8s_objects_by_key.keys()), set(all_expected_keys))
assert set(k8s_objects_by_key.keys()) == set(all_expected_keys)

all_expected_data = [
{"AIRFLOW_VAR_HELLO_MESSAGE": "Hi!", "AIRFLOW_VAR_KUBERNETES_NAMESPACE": "default"},
{"HELLO_WORLD": "Hi again!"},
]
for expected_key, expected_data in zip(all_expected_keys, all_expected_data):
configmap_obj = k8s_objects_by_key[expected_key]
self.assertEqual(configmap_obj["data"], expected_data)
assert configmap_obj["data"] == expected_data

def test_extra_secrets(self):
values_str = textwrap.dedent(
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_extra_secrets(self):
("Secret", f"{RELEASE_NAME}-airflow-connections"),
("Secret", f"{RELEASE_NAME}-other-secrets"),
]
self.assertEqual(set(k8s_objects_by_key.keys()), set(all_expected_keys))
assert set(k8s_objects_by_key.keys()) == set(all_expected_keys)

all_expected_data = [
{"AIRFLOW_CON_AWS": b64encode(b"aws_connection_string").decode("utf-8")},
Expand All @@ -106,5 +106,5 @@ def test_extra_secrets(self):
all_expected_keys, all_expected_data, all_expected_string_data
):
configmap_obj = k8s_objects_by_key[expected_key]
self.assertEqual(configmap_obj["data"], expected_data)
self.assertEqual(configmap_obj["stringData"], expected_string_data)
assert configmap_obj["data"] == expected_data
assert configmap_obj["stringData"] == expected_string_data
4 changes: 2 additions & 2 deletions chart/tests/test_extra_env_env_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_extra_env(self, k8s_obj_key, env_paths):
k8s_object = self.k8s_objects_by_key[k8s_obj_key]
for path in env_paths:
env = jmespath.search(f"{path}.env", k8s_object)
self.assertIn(expected_env_as_str, yaml.dump(env))
assert expected_env_as_str in yaml.dump(env)

@parameterized.expand(PARAMS)
def test_extra_env_from(self, k8s_obj_key, env_from_paths):
Expand All @@ -114,4 +114,4 @@ def test_extra_env_from(self, k8s_obj_key, env_from_paths):
k8s_object = self.k8s_objects_by_key[k8s_obj_key]
for path in env_from_paths:
env_from = jmespath.search(f"{path}.envFrom", k8s_object)
self.assertIn(expected_env_from_as_str, yaml.dump(env_from))
assert expected_env_from_as_str in yaml.dump(env_from)
30 changes: 12 additions & 18 deletions chart/tests/test_flower_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ def test_should_create_flower_deployment_with_authorization(self):
show_only=["templates/flower/flower-deployment.yaml"],
)

self.assertEqual(
"AIRFLOW__CELERY__FLOWER_BASIC_AUTH",
jmespath.search("spec.template.spec.containers[0].env[0].name", docs[0]),
assert "AIRFLOW__CELERY__FLOWER_BASIC_AUTH" == jmespath.search(
"spec.template.spec.containers[0].env[0].name", docs[0]
)
self.assertEqual(
['curl', '--user', '$AIRFLOW__CELERY__FLOWER_BASIC_AUTH', 'localhost:7777'],
jmespath.search("spec.template.spec.containers[0].livenessProbe.exec.command", docs[0]),
assert ['curl', '--user', '$AIRFLOW__CELERY__FLOWER_BASIC_AUTH', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].livenessProbe.exec.command", docs[0]
)
self.assertEqual(
['curl', '--user', '$AIRFLOW__CELERY__FLOWER_BASIC_AUTH', 'localhost:7777'],
jmespath.search("spec.template.spec.containers[0].readinessProbe.exec.command", docs[0]),
assert ['curl', '--user', '$AIRFLOW__CELERY__FLOWER_BASIC_AUTH', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].readinessProbe.exec.command", docs[0]
)

def test_should_create_flower_deployment_without_authorization(self):
Expand All @@ -55,15 +52,12 @@ def test_should_create_flower_deployment_without_authorization(self):
show_only=["templates/flower/flower-deployment.yaml"],
)

self.assertEqual(
"AIRFLOW__CORE__FERNET_KEY",
jmespath.search("spec.template.spec.containers[0].env[0].name", docs[0]),
assert "AIRFLOW__CORE__FERNET_KEY" == jmespath.search(
"spec.template.spec.containers[0].env[0].name", docs[0]
)
self.assertEqual(
['curl', 'localhost:7777'],
jmespath.search("spec.template.spec.containers[0].livenessProbe.exec.command", docs[0]),
assert ['curl', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].livenessProbe.exec.command", docs[0]
)
self.assertEqual(
['curl', 'localhost:7777'],
jmespath.search("spec.template.spec.containers[0].readinessProbe.exec.command", docs[0]),
assert ['curl', 'localhost:7777'] == jmespath.search(
"spec.template.spec.containers[0].readinessProbe.exec.command", docs[0]
)
Loading

0 comments on commit 39d9057

Please sign in to comment.