Skip to content

Add Integration Test using new Terraform framework #63

@mayank0202

Description

@mayank0202

Is your feature request related to a problem? Please describe.
Terraform tests let authors validate that module configuration updates do not introduce breaking changes. Tests run against test-specific, short-lived resources, preventing any risk to your existing infrastructure or state.

Describe the solution you'd like
The following example demonstrates a simple Terraform configuration that creates an AWS S3 bucket, using an input variable to modify the name. We will create an example test file (below) that validates the buckets name is created as expected.

# main.tf
 
provider "aws" {
    region = "eu-central-1"
}
 
variable "bucket_prefix" {
  type = string
}
 
resource "aws_s3_bucket" "bucket" {
  bucket = "${var.bucket_prefix}-bucket"
}
 
output "bucket_name" {
  value = aws_s3_bucket.bucket.bucket
}

The following test file runs a single Terraform plan command which creates the S3 bucket, and then validates the logic for calculating the name is correct by checking the actual name matches the expected name.

# valid_string_concat.tftest.hcl
 
variables {
  bucket_prefix = "test"
}
 
run "valid_string_concat" {
 
  command = plan
 
  assert {
    condition     = aws_s3_bucket.bucket.bucket == "test-bucket"
    error_message = "S3 bucket name did not match expected"
  }
 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions