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

Go script is not accepting multiple tfvars to run for test case. #1447

Open
2 tasks
Kiba87 opened this issue Sep 20, 2024 · 0 comments
Open
2 tasks

Go script is not accepting multiple tfvars to run for test case. #1447

Kiba87 opened this issue Sep 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Kiba87
Copy link

Kiba87 commented Sep 20, 2024

Describe the bug
Not sure if it's a bug.. but let me explain what I am doing.. we have a test case to test terraform module with different parameters, we have list of parameters in csv file which has to iterate and generate tfvars and pass the tfvars to goscript to test terraform module. For example we have two different tfvars which it has to run and test using below script.. script is working fine but tfvars value for both test is same.. initially I thought it could be python issue which not iterating properly but we can see the correct parameters values in pipeline after python script runs even on go script both the tfvars run like generated_0.tfvars and generated_1.tfvars is running but generated_1.tfvars has same value as generated_0.tfvars.. please let me know if I'm missing something or is go script is not picking the value?

To Reproduce
Steps to reproduce the behavior, code snippets and examples which can be used to reproduce the issue.

// paste code snippets here
package test

import (
    "os"
    "testing"
    "fmt"
    "log"
    "io/ioutil"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestTerraformS3BucketCreation(t *testing.T) {
    t.Parallel()

    // Get the .tfvars file from the environment variable
    tfvarsFile := os.Getenv("TFVARS_FILE")
    if tfvarsFile == "" {
        t.Fatalf("TFVARS_FILE environment variable is not set")
    }

    fmt.Printf("Running Terraform test with %s\n", tfvarsFile)

    // Configure Terraform options
    terraformOptions := &terraform.Options{
        TerraformDir: "../",       // Path to Terraform directory
        VarFiles:     []string{tfvarsFile}, // Use the generated .tfvars file
    }

    // Ensure Terraform destroy runs at the end of the test
    defer terraform.Destroy(t, terraformOptions)

    // Run Terraform Init and Apply, and capture any errors
    terraformApplyErr := terraform.InitAndApplyE(t, terraformOptions)

    // Validate that Terraform apply succeeded and save the result
    var validationResult string
    if terraformApplyErr != nil {
        validationResult = fmt.Sprintf("Terraform apply failed for %s: %s\n", tfvarsFile, terraformApplyErr)
        writeValidationResultToFile(tfvarsFile, validationResult)
        t.Fatalf(validationResult) // Fail the test immediately if Terraform apply fails
    }

    validationResult = fmt.Sprintf("Terraform apply succeeded for %s\n", tfvarsFile)

    // Fetch the bucket name from the Terraform output
    bucketName := terraform.Output(t, terraformOptions, "bucket_name")
    assert.NotEmpty(t, bucketName, "Bucket name output should not be empty")

    // Validate the S3 bucket exists in AWS
    s3ValidationResult := validateS3BucketExists(t, bucketName)
    validationResult += s3ValidationResult

    // Write validation result to a file in the root of the repo
    writeValidationResultToFile(tfvarsFile, validationResult)
}

// Function to validate if the S3 bucket exists in AWS
func validateS3BucketExists(t *testing.T, bucketName string) string {
    // Create a new session in the default region (you can change the region if necessary)
    sess, err := session.NewSession(&aws.Config{
        Region: aws.String("us-west-2"),
    })
    if err != nil {
        t.Fatalf("Failed to create AWS session: %s", err)
    }

    // Create an S3 service client
    svc := s3.New(sess)

    // Check if the bucket exists by sending a HeadBucket request
    _, err = svc.HeadBucket(&s3.HeadBucketInput{
        Bucket: aws.String(bucketName),
    })

    if err != nil {
        return fmt.Sprintf("S3 Bucket %s does NOT exist: %s\n", bucketName, err)
    }

    fmt.Printf("S3 Bucket %s exists\n", bucketName)
    return fmt.Sprintf("S3 Bucket %s exists\n", bucketName)
}

// Function to write validation result to a file
func writeValidationResultToFile(tfvarsFile string, validationResult string) {
    // Use the tfvars file name for the validation result file name
    fileName := fmt.Sprintf("validation_results_%s.txt", tfvarsFile)

    // Write the validation result to a file in the root directory
    err := ioutil.WriteFile("../"+fileName, []byte(validationResult), 0644)
    if err != nil {
        log.Fatalf("Failed to write validation result to file: %s", err)
    }

    fmt.Printf("Validation result written to %s\n", fileName)
}

Expected behavior
A clear and concise description of what you expected to happen.

Nice to have

  • Terminal output
  • Screenshots

Versions

  • Terratest version:
  • Environment details (Ubuntu 20.04, Windows 10, etc.):

Additional context
Add any other context about the problem here.

@Kiba87 Kiba87 added the bug Something isn't working label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant