BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Terraform 1.7 Adds Config-Driven Remove and Test Mocking Ahead of OpenTofu

Terraform 1.7 Adds Config-Driven Remove and Test Mocking Ahead of OpenTofu

HashiCorp announced the release of Terraform 1.7, a new version of the popular Infrastructure as Code (IaC) tool. Terraform now supports config-driven remove capability, a safer way to remove resources from the managed stack’s state data. The new version also comes with mock providers and overrides, as well as several other enhancements in the test framework.

Terraform previously introduced config-driven blocks for moving and importing resources. With the 1.7 release, a new removed block is available for resources and modules that can be used for state removal as part of the Terraform plan and apply commands. This approach is preferable to using terraform state rm command, as using the command requires manual tasks on individual resources.

removed {
  # The resource address to remove from state
  from = aws_instance.example

  # The lifecycle block instructs Terraform not to destroy the underlying resource
  lifecycle {
    destroy = false
  }
}

Terraform 1.7 also includes an enhancement for config-driven import. It is possible now to use for_each loops inside import blocks. The new approach allows importing multiple instances of the resource or a module in a single block.

In the earlier 1.6 version, the Terraform team released a new testing framework, which allows for creating and running unit and integration tests validating Terraform configurations. The new version enhances the testing framework, especially with a new mocking capability.

Dan Barr, technical product marketing manager at HashiCorp, explains the benefits of using mocking in Terraform tests:

Previously, all tests were executed by making actual provider calls using either a plan or apply operation. This is a great way to observe the real behavior of a module. But it can also be useful to mock provider calls to model more advanced situations and to test without having to create actual infrastructure or requiring credentials. This can be especially useful with cloud resources that take a long time to provision, such as databases and higher-level platform services. Mocking can significantly reduce the time required to run a test suite with many different permutations, giving module authors the ability to thoroughly test their code without slowing down the development process.

Engineers using Terraform can now mock providers and resources to generate fake data for all computed attributes. The new mock_provider block offers the ability to specify values for computed attributes of resources and data sources. Test suite creators can mix mocked and real providers to create flexible test setups.

mock_provider "aws" {
  mock_resource "aws_s3_bucket" {
    defaults = {
      arn = "arn:aws:s3:::test-bucket-name"
    }
  }
}

run "sets_bucket_name" {
  variables {
    bucket_name = "test-bucket-name"
  }

  # Validates a known attribute set in the resource configuration
  assert {
    condition     = output.bucket == "test-bucket-name"
    error_message = "Wrong ARN value"
  }

  # Validates a computed attribute using the mocked resource
  assert {
    condition     = output.arn == "arn:aws:s3:::test-bucket-name"
    error_message = "Wrong ARN value"
  }
}

The new Terraform version additionally supports overrides that allow replacing specific instances of resources, data sources, and modules for testing purposes. Overrides allow engineers to reduce the execution time of resource provisioning or child module execution and simulate different outputs to exercise different test scenarios comprehensively.

mock_provider "aws" {}

override_module {
  target = module.big_database
  outputs = {
    endpoint = "big_database.012345678901.us-east-1.rds.amazonaws.com:3306"
    db_name  = "test_db"
    username = "fakeuser"
    password = "fakepassword"
  }
}

run "test" {
  assert {
    condition     = module.big_database.username == "fakeuser"
    error_message = "Incorrect username"
  }
}

The latest version ships with further enhancements to the testing framework, including the ability to reference variables and run outputs in test provider blocks, use HashiCorp Configuration Language (HCL) functions in variable and provider blocks, and load variable values for tests from *.tfvars files.

Terraform 1.7 can be downloaded from the HashiCorp website. The full changelog and the upgrade guide are available on GitHub.

OpenTofu, the fork of Terraform, created in response to HishiCorp’s decision to adopt the Business Source Licence, also included the module testing feature in its first GA release (1.6). According to the roadmap, the project plans to support mocking providers and overrides, as well as config-driven remove feature, in the subsequent 1.7 release. Additionally, OpenTofu focuses on shipping state encryption, a highly anticipated capability within the community, as a flagship feature of the new release.

About the Author

Rate this Article

Adoption
Style

BT