Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions contributing-docs/testing/helm_unit_tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,58 @@
Helm Unit Tests
---------------

On the Airflow Project, we have decided to stick with pythonic testing for our Helm chart. This makes our chart
On the Apache 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 ``helm-tests``.
add them under ``helm-tests/tests/helm_tests`` directory.

.. code-block:: python

class TestBaseChartTest: ...

To render the chart create a YAML string with the nested dictionary of options you wish to test. You can then
use our ``render_chart`` function to render the object of interest into a testable Python dictionary. Once the chart
has been rendered, you can use the ``render_k8s_object`` function to create a k8s model object. It simultaneously
ensures that the object created properly conforms to the expected resource spec and allows you to use object values
instead of nested dictionaries.
To render the chart create a dictionary with options you wish to test. You can then use them with our ``render_chart``
function to render the object of interest as a testable Python dictionary. Once the chart has been rendered,
you can use ``jmespath.search`` function for searching desirable value of rendered chart to test.

Example test here:

.. code-block:: python

from chart_utils.helm_template_generator import render_chart, render_k8s_object

git_sync_basic = """
dags:
gitSync:
enabled: true
"""
from chart_utils.helm_template_generator import render_chart


class TestGitSyncScheduler:
def test_basic(self):
helm_settings = yaml.safe_load(git_sync_basic)
res = render_chart(
"GIT-SYNC",
helm_settings,
docs = render_chart(
name="GIT-SYNC",
values={"dags": {"gitSync": {"enabled": True}}},
show_only=["templates/scheduler/scheduler-deployment.yaml"],
)
dep: k8s.V1Deployment = render_k8s_object(res[0], k8s.V1Deployment)
assert "dags" == dep.spec.template.spec.volumes[1].name

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


To execute all Helm tests using breeze command and utilize parallel pytest tests, you can run the
following command (but it takes quite a long time even in a multi-processor machine).
following command (it takes quite a long time even on a multi-processor machine).

.. code-block:: bash

breeze testing helm-tests

You can also execute tests from a selected package only. Tests in ``tests/chart`` are grouped by packages
so rather than running all tests, you can run only tests from a selected package. For example:
You can also execute tests from a selected package only. Tests in ``helm-tests/tests/helm_tests`` are grouped
by packages so rather than running all tests, you can run only tests from a selected package. For example:

.. code-block:: bash

breeze testing helm-tests --test-type basic
breeze testing helm-tests --test-type airflow_aux

Will run all tests from ``tests-charts/basic`` package.
Will run all tests from ``helm-tests/tests/helm_tests/airflow_aux`` package.


You can also run Helm tests individually via the usual ``breeze`` command. Just enter breeze and run the
tests with pytest as you would do with regular unit tests (you can add ``-n auto`` command to run Helm
tests in parallel - unlike most of the regular unit tests of ours that require a database, the Helm tests are
perfectly safe to be run in parallel (and if you have multiple processors, you can gain significant
speedups when using parallel runs):
speedups when using parallel runs)):

.. code-block:: bash

Expand All @@ -95,13 +86,13 @@ This runs all chart tests using all processors you have available.

.. code-block:: bash

pytest helm-tests/tests/other/test_airflow_common.py -n auto
pytest helm-tests/tests/helm_tests/airflow_aux/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 helm-tests/tests/other/test_airflow_common.py
pytest helm-tests/tests/helm_tests/airflow_aux/test_airflow_common.py

This will run all tests from ``tests_airflow_common.py`` file sequentially.

Expand Down
10 changes: 0 additions & 10 deletions helm-tests/tests/chart_utils/helm_template_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
import jmespath
import jsonschema
import yaml
from kubernetes.client.api_client import ApiClient

api_client = ApiClient()

AIRFLOW_ROOT = Path(__file__).resolve().parents[3]
CHART_DIR = AIRFLOW_ROOT / "chart"
Expand Down Expand Up @@ -185,10 +182,3 @@ def prepare_k8s_lookup_dict(k8s_objects) -> dict[tuple[str, str], dict[str, Any]
(k8s_object["kind"], k8s_object["metadata"]["name"]): k8s_object for k8s_object in k8s_objects
}
return k8s_obj_by_key


def render_k8s_object(obj, type_to_render):
"""
Function that renders dictionaries into k8s objects. For helm chart testing only.
"""
return api_client._ApiClient__deserialize_model(obj, type_to_render)
Loading