Back to Repositories

Testing Scheduler Pod Disruption Budget Configuration in Apache Airflow

This test suite validates the Pod Disruption Budget (PDB) functionality for the Apache Airflow scheduler component in Helm deployments. It ensures proper configuration and behavior of PDB settings across different Kubernetes versions and validates component-specific label handling.

Test Coverage Overview

The test suite provides comprehensive coverage of scheduler PDB functionality in Helm charts.

Key areas tested include:
  • Basic PDB enablement validation for both v1 and v1beta1 API versions
  • Component-specific label propagation
  • Configuration validation with minAvailable parameter
Integration points focus on Kubernetes PDB API compatibility and Helm templating.

Implementation Analysis

The testing approach utilizes the render_chart helper function to validate Helm template generation and configuration. Tests employ systematic verification of PDB configurations and label handling, using both direct template rendering and jmespath queries for validation.

Key patterns include:
  • Template validation without raising exceptions
  • Kubernetes version-specific testing
  • Label verification using jmespath

Technical Details

Testing tools and components:
  • render_chart utility for Helm template testing
  • jmespath for JSON/YAML parsing and validation
  • Kubernetes API version compatibility testing (1.16.0)
  • PDB configuration validation for both v1 and v1beta1 APIs

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Kubernetes manifest validation.

Notable practices include:
  • Isolated template rendering for focused testing
  • Version-specific API compatibility checks
  • Structured validation of configuration parameters
  • Clear test case organization and naming

apache/airflow

helm_tests/airflow_core/test_pdb_scheduler.py

            
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import jmespath

from tests.charts.helm_template_generator import render_chart


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

    def test_should_pass_validation_with_just_pdb_enabled_v1(self):
        render_chart(
            values={"scheduler": {"podDisruptionBudget": {"enabled": True}}},
            show_only=["templates/scheduler/scheduler-poddisruptionbudget.yaml"],
        )  # checks that no validation exception is raised

    def test_should_pass_validation_with_just_pdb_enabled_v1beta1(self):
        render_chart(
            values={"scheduler": {"podDisruptionBudget": {"enabled": True}}},
            show_only=["templates/scheduler/scheduler-poddisruptionbudget.yaml"],
            kubernetes_version="1.16.0",
        )  # checks that no validation exception is raised

    def test_should_add_component_specific_labels(self):
        docs = render_chart(
            values={
                "scheduler": {
                    "podDisruptionBudget": {"enabled": True},
                    "labels": {"test_label": "test_label_value"},
                },
            },
            show_only=["templates/scheduler/scheduler-poddisruptionbudget.yaml"],
        )

        assert "test_label" in jmespath.search("metadata.labels", docs[0])
        assert jmespath.search("metadata.labels", docs[0])["test_label"] == "test_label_value"

    def test_should_pass_validation_with_pdb_enabled_and_min_available_param(self):
        render_chart(
            values={
                "scheduler": {
                    "podDisruptionBudget": {
                        "enabled": True,
                        "config": {"maxUnavailable": None, "minAvailable": 1},
                    }
                }
            },
            show_only=["templates/scheduler/scheduler-poddisruptionbudget.yaml"],
        )  # checks that no validation exception is raised