Back to Repositories

Testing Terraform Infrastructure Management in Apache Airflow

This test suite implements system testing infrastructure for Terraform operations within Apache Airflow. It provides a base class for managing Terraform infrastructure lifecycle including initialization, planning, applying, and destroying resources programmatically.

Test Coverage Overview

The test coverage focuses on managing Terraform infrastructure through automated test workflows. Key functionality includes:

  • Infrastructure initialization and planning
  • Resource provisioning and configuration
  • Output retrieval and validation
  • Clean resource teardown

Integration points cover Terraform CLI commands and infrastructure state management.

Implementation Analysis

The testing approach utilizes a system test base class pattern to encapsulate Terraform operations. The implementation leverages Python’s subprocess handling to execute Terraform commands, with specific focus on non-interactive automation through -input=false flags and auto-approve options.

The class provides reusable methods for common Terraform workflows while allowing test-specific directory configuration.

Technical Details

Testing tools and configuration:

  • Python subprocess management for CLI execution
  • Terraform CLI integration
  • SystemTest base class inheritance
  • UTF-8 output handling
  • Directory-based test isolation

Best Practices Demonstrated

The test implementation showcases several quality testing practices including proper resource cleanup, command output validation, and infrastructure lifecycle management. Notable practices include:

  • Automated setup and teardown methods
  • Non-interactive execution safety
  • Isolated test environments
  • Standardized command execution patterns

apache/airflow

tests_common/test_utils/terraform.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

from tests_common.test_utils.system_tests_class import SystemTest


class Terraform(SystemTest):
    """Base class for Terraform tests."""

    TERRAFORM_DIR: str

    def setup_method(self) -> None:
        self.execute_cmd(["terraform", "init", "-input=false", self.TERRAFORM_DIR])
        self.execute_cmd(["terraform", "plan", "-input=false", self.TERRAFORM_DIR])
        self.execute_cmd(["terraform", "apply", "-input=false", "-auto-approve", self.TERRAFORM_DIR])

    def get_tf_output(self, name):
        return "".join(self.check_output(["terraform", "output", name]).decode("utf-8").splitlines())

    def teardown_method(self) -> None:
        self.execute_cmd(["terraform", "plan", "-destroy", "-input=false", self.TERRAFORM_DIR])
        self.execute_cmd(["terraform", "destroy", "-input=false", "-auto-approve", self.TERRAFORM_DIR])