Skip to content

Commit

Permalink
Migrating helm chart tests to helm_tests to prevent test DB init (apa…
Browse files Browse the repository at this point in the history
…che#32394)



---------

Co-authored-by: Amogh Desai <[email protected]>
  • Loading branch information
amoghrajesh and Amogh Desai authored Jul 7, 2023
1 parent 3ed5b7a commit 907aeeb
Show file tree
Hide file tree
Showing 59 changed files with 211 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
# Add tests and kubernetes_tests to context.
!tests
!kubernetes_tests
!helm_tests
!docker_tests

!.coveragerc
!.rat-excludes
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.ci
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ else
"tests/utils"
)
WWW_TESTS=("tests/www")
HELM_CHART_TESTS=("tests/charts")
HELM_CHART_TESTS=("helm_tests")
INTEGRATION_TESTS=("tests/integration")
SYSTEM_TESTS=("tests/system")
ALL_TESTS=("tests")
Expand Down Expand Up @@ -1068,7 +1068,7 @@ else
SELECTED_TESTS=("${WWW_TESTS[@]}")
elif [[ ${TEST_TYPE:=""} == "Helm" ]]; then
if [[ ${HELM_TEST_PACKAGE=} != "" ]]; then
SELECTED_TESTS=("tests/charts/${HELM_TEST_PACKAGE}")
SELECTED_TESTS=("helm_tests/${HELM_TEST_PACKAGE}")
else
SELECTED_TESTS=("${HELM_CHART_TESTS[@]}")
fi
Expand Down
10 changes: 5 additions & 5 deletions TESTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ Helm Unit Tests

On the Airflow Project, we have decided to stick with pythonic testing for our Helm chart. This makes our chart
easier to test, easier to modify, and able to run with the same testing infrastructure. To add Helm unit tests
add them in ``tests/charts``.
add them in ``helm_tests``.

.. code-block:: python
Expand Down Expand Up @@ -738,7 +738,7 @@ so rather than running all tests, you can run only tests from a selected package
breeze testing helm-tests --helm-test-package basic
Will run all tests from ``tests/charts/basic`` package.
Will run all tests from ``tests-charts/basic`` package.


You can also run Helm tests individually via the usual ``breeze`` command. Just enter breeze and run the
Expand All @@ -755,19 +755,19 @@ This enters breeze container.

.. code-block:: bash
pytest tests/charts -n auto
pytest helm_tests -n auto
This runs all chart tests using all processors you have available.

.. code-block:: bash
pytest tests/charts/test_airflow_common.py -n auto
pytest helm_tests/test_airflow_common.py -n auto
This will run all tests from ``tests_airflow_common.py`` file using all processors you have available.

.. code-block:: bash
pytest tests/charts/test_airflow_common.py
pytest helm_tests/test_airflow_common.py
This will run all tests from ``tests_airflow_common.py`` file sequentially.

Expand Down
4 changes: 2 additions & 2 deletions dev/breeze/src/airflow_breeze/global_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def all_helm_test_packages() -> list[str]:
return sorted(
[
candidate.name
for candidate in (AIRFLOW_SOURCES_ROOT / "tests" / "charts").iterdir()
if candidate.is_dir()
for candidate in (AIRFLOW_SOURCES_ROOT / "helm_tests").iterdir()
if candidate.is_dir() and candidate.name != "__pycache__"
]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
("setup.cfg", "/opt/airflow/setup.cfg"),
("setup.py", "/opt/airflow/setup.py"),
("tests", "/opt/airflow/tests"),
("helm_tests", "/opt/airflow/helm_tests"),
("kubernetes_tests", "/opt/airflow/kubernetes_tests"),
("docker_tests", "/opt/airflow/docker_tests"),
("chart", "/opt/airflow/chart"),
Expand Down
2 changes: 1 addition & 1 deletion dev/breeze/src/airflow_breeze/utils/selective_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __hash__(self):
r"^chart",
r"^airflow/kubernetes",
r"^tests/kubernetes",
r"^tests/charts",
r"^helm_tests",
],
FileGroupForCi.SETUP_FILES: [
r"^pyproject.toml",
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class TestAirflowCommon:
"""
This class holds tests that apply to more than 1 Airflow component so
we don't have to repeat tests everywhere
we don't have to repeat tests everywhere.
The one general exception will be the KubernetesExecutor PodTemplateFile,
as it requires extra test setup.
Expand Down Expand Up @@ -162,9 +162,7 @@ def test_annotations(self):
assert "true" in annotations["test-annotation/safe-to-evict"]

def test_global_affinity_tolerations_topology_spread_constraints_and_node_selector(self):
"""
Test affinity, tolerations, etc are correctly applied on all pods created
"""
"""Test affinity, tolerations, etc are correctly applied on all pods created."""
k8s_objects = render_chart(
values={
"cleanup": {"enabled": True},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def get_object_annotations(obj):


class TestServiceAccountAnnotations:
"""Tests Service Account Annotations."""

@pytest.mark.parametrize(
"values,show_only,expected_annotations",
[
Expand Down Expand Up @@ -383,6 +385,8 @@ def test_annotations_are_added(self, values, show_only, expected_annotations):
],
)
class TestPerComponentPodAnnotations:
"""Tests Per Component Pod Annotations."""

def test_annotations_are_added(self, values, show_only, expected_annotations):
k8s_objects = render_chart(
values=values,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@


class TestBaseChartTest:
"""Tests basic helm chart tests."""

def _get_values_with_version(self, values, version):
if version != "default":
values["airflowVersion"] = version
Expand Down Expand Up @@ -255,7 +257,7 @@ def test_network_policies_are_valid(self):
assert kind_name in kind_names_tuples

def test_labels_are_valid(self):
"""Test labels are correctly applied on all objects created by this chart"""
"""Test labels are correctly applied on all objects created by this chart."""
release_name = "test-basic"
k8s_objects = render_chart(
name=release_name,
Expand Down Expand Up @@ -368,7 +370,7 @@ def test_labels_are_valid(self):
warnings.warn(f"Unchecked objects: {kind_k8s_obj_labels_tuples.keys()}")

def test_labels_are_valid_on_job_templates(self):
"""Test labels are correctly applied on all job templates created by this chart"""
"""Test labels are correctly applied on all job templates created by this chart."""
release_name = "test-basic"
k8s_objects = render_chart(
name=release_name,
Expand Down Expand Up @@ -441,7 +443,7 @@ def test_chart_is_consistent_with_official_airflow_image(self):
def get_k8s_objs_with_image(obj: list[Any] | dict[str, Any]) -> list[dict[str, Any]]:
"""
Recursive helper to retrieve all the k8s objects that have an "image" key
inside k8s obj or list of k8s obj
inside k8s obj or list of k8s obj.
"""
out = []
if isinstance(obj, list):
Expand Down Expand Up @@ -511,7 +513,7 @@ def test_invalid_dags_access_mode(self):

@pytest.mark.parametrize("namespace", ["abc", "123", "123abc", "123-abc"])
def test_namespace_names(self, namespace):
"""Test various namespace names to make sure they render correctly in templates"""
"""Test various namespace names to make sure they render correctly in templates."""
render_chart(namespace=namespace)

def test_postgres_connection_url_no_override(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@


class TestCeleryKubernetesExecutor:
"""Tests celery kubernetes executor."""

def test_should_create_a_worker_deployment_with_the_celery_executor(self):
docs = render_chart(
values={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import yaml
from jsonschema import validate

CHART_DIR = Path(__file__).resolve().parents[3] / "chart"
CHART_DIR = Path(__file__).parents[2] / "chart"


class TestChartQuality:
"""Tests chart quality."""

def test_values_validate_schema(self):
values = yaml.safe_load((CHART_DIR / "values.yaml").read_text())
schema = json.loads((CHART_DIR / "values.schema.json").read_text())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class TestCleanupPods:
"""Tests cleanup of pods."""

def test_should_create_cronjob_for_enabled_cleanup(self):
docs = render_chart(
values={
Expand Down Expand Up @@ -296,6 +298,8 @@ def test_airflow_local_settings(self):


class TestCleanupServiceAccount:
"""Tests cleanup of service accounts."""

def test_should_add_component_specific_labels(self):
docs = render_chart(
values={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class TestConfigmap:
"""Tests configmaps."""

def test_single_annotation(self):
docs = render_chart(
values={
Expand Down Expand Up @@ -103,7 +105,7 @@ def test_pod_template_is_templated(self):
apiVersion: v1
kind: Pod
metadata:
name: dummy-name
name: example-name
labels:
mylabel: {{ .Release.Name }}
""",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class TestCreateUserJob:
"""Tests create user job."""

def test_should_run_by_default(self):
docs = render_chart(show_only=["templates/jobs/create-user-job.yaml"])
assert "Job" == docs[0]["kind"]
Expand Down Expand Up @@ -380,6 +382,8 @@ def test_airflow_local_settings(self):


class TestCreateUserJobServiceAccount:
"""Tests create user job service account."""

def test_should_add_component_specific_labels(self):
docs = render_chart(
values={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@


class TestExtraEnvEnvFrom:
"""Tests extra env from."""

k8s_objects: list[dict[str, Any]]
k8s_objects_by_key: dict[tuple[str, str], dict[str, Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@


class TestLogsPersistentVolumeClaim:
"""Tests logs PVC."""

def test_should_not_generate_a_document_if_persistence_is_disabled(self):
docs = render_chart(
values={"logs": {"persistence": {"enabled": False}}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class TestMigrateDatabaseJob:
"""Tests migrate DB job."""

def test_should_run_by_default(self):
docs = render_chart(show_only=["templates/jobs/migrate-database-job.yaml"])
assert "Job" == docs[0]["kind"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@


class TestPodLauncher:
"""Tests pod launcher."""

@pytest.mark.parametrize(
"executor, rbac, allow, expected_accounts",
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

@pytest.fixture(scope="class", autouse=True)
def isolate_chart(request):
chart_dir = Path(__file__).parents[3] / "chart"
chart_dir = Path(__file__).parents[2] / "chart"
with TemporaryDirectory(prefix=request.cls.__name__) as tmp_dir:
temp_chart_dir = Path(tmp_dir) / "chart"
copytree(chart_dir, temp_chart_dir)
Expand All @@ -42,6 +42,8 @@ def isolate_chart(request):


class TestPodTemplateFile:
"""Tests pod template file."""

def test_should_work(self):
docs = render_chart(
values={},
Expand Down Expand Up @@ -430,7 +432,7 @@ def test_should_create_valid_affinity_tolerations_topology_spread_constraints_an
)

def test_affinity_tolerations_topology_spread_constraints_and_node_selector_precedence(self):
"""When given both global and worker affinity etc, worker affinity etc is used"""
"""When given both global and worker affinity etc, worker affinity etc is used."""
expected_affinity = {
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@


class TestDagProcessor:
"""Tests DAG processor."""

@pytest.mark.parametrize(
"airflow_version, num_docs",
[
Expand All @@ -32,7 +34,7 @@ class TestDagProcessor:
],
)
def test_only_exists_on_new_airflow_versions(self, airflow_version, num_docs):
"""Standalone Dag Processor was only added from Airflow 2.3 onwards"""
"""Standalone Dag Processor was only added from Airflow 2.3 onwards."""
docs = render_chart(
values={
"airflowVersion": airflow_version,
Expand All @@ -44,9 +46,7 @@ def test_only_exists_on_new_airflow_versions(self, airflow_version, num_docs):
assert num_docs == len(docs)

def test_can_be_disabled(self):
"""
Standalone Dag Processor is disabled by default.
"""
"""Standalone Dag Processor is disabled by default."""
docs = render_chart(
values={"dagProcessor": {"enabled": False}},
show_only=["templates/dag-processor/dag-processor-deployment.yaml"],
Expand Down Expand Up @@ -223,7 +223,7 @@ def test_should_create_valid_affinity_tolerations_and_node_selector(self):
)

def test_affinity_tolerations_topology_spread_constraints_and_node_selector_precedence(self):
"""When given both global and triggerer affinity etc, triggerer affinity etc is used"""
"""When given both global and triggerer affinity etc, triggerer affinity etc is used."""
expected_affinity = {
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
Expand Down Expand Up @@ -411,7 +411,7 @@ def test_resources_are_not_added_by_default(self):
],
)
def test_strategy(self, strategy, expected_strategy):
"""strategy should be used when we aren't using both LocalExecutor and workers.persistence"""
"""Strategy should be used when we aren't using both LocalExecutor and workers.persistence."""
docs = render_chart(
values={
"dagProcessor": {"enabled": True, "strategy": strategy},
Expand Down Expand Up @@ -563,5 +563,7 @@ def test_should_add_component_specific_annotations(self):


class TestDagProcessorLogGroomer(LogGroomerTestBase):
"""DAG processor log groomer."""

obj_name = "dag-processor"
folder = "dag-processor"
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@


class TestSchedulerPdb:
"""Tests Scheduler PDB."""

def test_should_pass_validation_with_just_pdb_enabled_v1(self):
render_chart(
values={"scheduler": {"podDisruptionBudget": {"enabled": True}}},
Expand Down
Loading

0 comments on commit 907aeeb

Please sign in to comment.