Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(testing): added TF templates for tests #2027

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vmyroslav
Copy link
Contributor

This PR introduces a template-based testing pkg that simplifies writing and maintaining Terraform provider acceptance tests. It provides a declarative and composable approach to constructing test configurations.

Key Features

  • Replaces string templates with structured templates
  • Pre-registered templates for all provider resources
  • Composable test configurations
  • Dynamic template modifications during tests

Benefits

  • Easy dynamic modifications during tests
  • Reusable templates across tests
  • Built-in validation of resource configurations

Usage Example

// Using the new template-based framework
func TestExpectedBehaviour(t *testing.T) {
    // Get template set with all provider resources pre-registered
    templateSet := GetTemplateSet(t)

    // Add custom template if needed
    templateSet.AddTemplate("aiven_kafka", `
resource "aiven_kafka" "bar" {
  project                 = data.aiven_project.foo.project
  cloud_name              = "google-europe-west1"
  plan                    = "startup-2"
  service_name            = "{{ .service_name }}"
  maintenance_window_dow  = "monday"
  maintenance_window_time = "10:00:00"
}`)

    // Add external provider templates
    templateSet.AddTemplate("gcp_provider", `
provider "google" {
    project = "{{ .gcp_project }}"
    region  = "{{ .gcp_region }}"
}`)

    // Build configuration using fluent interface
    builder := templateSet.NewBuilder().
       // Add data source with configuration
       AddDataSource("aiven_project", map[string]any{
          "resource_name": "foo",
          "project":       "test_project",
       }).
       // Add resources with different names
       AddResource("aiven_kafka_quota", map[string]any{
          "resource_name":      "example_quota_1",
          "project":            "test_project",
          "service_name":       "test-service",
          "user":               "test_user",
          "client_id":          "test_client_id",
          "consumer_byte_rate": 1000,
       }).
       AddResource("aiven_kafka_quota", map[string]any{
          "resource_name":      "example_quota_2",
          "project":            Reference("data.aiven_project.foo.project"), // Reference existing resource
          "service_name":       "test_service_2",
          "user":               "test_user_2",
          "client_id":          "test_client_id_2",
          "consumer_byte_rate": 1000,
       }).
       // Add custom templates
       Add("aiven_kafka", map[string]any{"service_name": "test-service"}).
       Add("gcp_provider", map[string]any{
          "gcp_project": "test-project", 
          "gcp_region": "us-central1",
       })

    // Render the full configuration
    str := builder.MustRender(t)
    assert.NotEmpty(t, str)

    // Modify configuration by removing resource
    builder.RemoveByResourceName("example_quota_1")

    // Render updated configuration
    nextStep := builder.MustRender(t)
    assert.NotEmpty(t, nextStep)
}

@vmyroslav vmyroslav added the no changelog No changelog entries are required for this PR label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no changelog No changelog entries are required for this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants