gruntwork-io / terratest

Terratest is a Go library that makes it easier to write automated tests for your infrastructure code.
https://terratest.gruntwork.io/
Apache License 2.0
7.47k stars 1.32k forks source link

error while importing github.com/gruntwork-io/terratest/modules/terraform: import cycle not allowed in test #1382

Closed BenjaminCloudIam closed 8 months ago

BenjaminCloudIam commented 8 months ago

Hi,

I'am trying to use terratest but i got the following error when I run my test:

go test -v
# github.com/gruntwork-io/terratest/modules/terraform
package github.com/gruntwork-io/terratest/modules/terraform
        imports github.com/gruntwork-io/terratest/modules/terraform: import cycle not allowed in test
FAIL    github.com/gruntwork-io/terratest/modules/terraform [setup failed]

This is my code:

package test

import (
    "crypto/rand"
    "math/big"
    "testing"

    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func generateRandomString(length int) (string, error) {
    const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
    randomString := make([]byte, length)

    // Seed the random number generator with the current time

    for i := range randomString {
        // Generate a random index within the charset
        randomIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
        if err != nil {
            return "", err
        }
        randomString[i] = charset[randomIndex.Int64()]
    }

    return string(randomString), nil
}

func TestModuleOutCloudGouvEuWest(t *testing.T) {
    // t.Parallel()

    randomString, err := generateRandomString(4)
    assert.NoError(t, err)

    expectedPublicSubRegion := "cloudgouv-eu-west-1a"
    expectedPrivateSubRegion := "cloudgouv-eu-west-1a"
    expectedDatabaseSubRegion := "cloudgouv-eu-west-1a"

    commonTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples/cloudgouv-eu-west-1-common",

        // Variables to pass to our Terraform code using -var options
        Vars: map[string]interface{}{
            "create_database":     "false",
            "create_common":       "true",
            "create_keycloak":     "false",
            "create_loadbalancer": "false",
            "random_string":       randomString,
        },

        // MigrateState: true,
        // Variables to pass to our Terraform code using -var-file options
        // VarFiles: []string{"varfile.tfvars"},

        // Disable colors in Terraform commands so its easier to parse stdout/stderr
        NoColor: false,
    })

    instanceLoadBalancerTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples/cloudgouv-eu-west-1-instance",

        // Variables to pass to our Terraform code using -var options
        Vars: map[string]interface{}{
            "create_database":     "false",
            "create_common":       "false",
            "create_keycloak":     "false",
            "create_loadbalancer": "true",
            "random_string":       randomString,
        },

        // MigrateState: true,
        // Variables to pass to our Terraform code using -var-file options
        // VarFiles: []string{"varfile.tfvars"},

        // Disable colors in Terraform commands so its easier to parse stdout/stderr
        NoColor: false,
    })

    instanceTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples/cloudgouv-eu-west-1-instance",

        // Variables to pass to our Terraform code using -var options
        Vars: map[string]interface{}{
            "create_database":     "true",
            "create_common":       "false",
            "create_keycloak":     "true",
            "create_loadbalancer": "true",
            "random_string":       randomString,
        },

        // MigrateState: true,
        // Variables to pass to our Terraform code using -var-file options
        // VarFiles: []string{"varfile.tfvars"},

        // Disable colors in Terraform commands so its easier to parse stdout/stderr
        NoColor: false,
    })

    destroyInstanceTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples/cloudgouv-eu-west-1-instance",

        // Variables to pass to our Terraform code using -var options
        Vars: map[string]interface{}{
            "create_database":     "false",
            "create_common":       "false",
            "create_keycloak":     "false",
            "create_loadbalancer": "false",
            "random_string":       randomString,
        },

        // MigrateState: true,
        // Variables to pass to our Terraform code using -var-file options
        // VarFiles: []string{"varfile.tfvars"},

        // Disable colors in Terraform commands so its easier to parse stdout/stderr
        NoColor: false,
    })

    destroyCommonTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "../examples/cloudgouv-eu-west-1-common",

        // Variables to pass to our Terraform code using -var options
        Vars: map[string]interface{}{
            "create_database":     "false",
            "create_common":       "false",
            "create_keycloak":     "false",
            "create_loadbalancer": "false",
            "random_string":       randomString,
        },

        // MigrateState: true,
        // Variables to pass to our Terraform code using -var-file options
        // VarFiles: []string{"varfile.tfvars"},

        // Disable colors in Terraform commands so its easier to parse stdout/stderr
        NoColor: false,
    })

    terraform.InitAndApply(t, commonTerraformOptions)
    terraform.InitAndApply(t, instanceLoadBalancerTerraformOptions)
    terraform.InitAndApply(t, instanceTerraformOptions)
    defer terraform.Destroy(t, destroyInstanceTerraformOptions)
    defer terraform.Destroy(t, destroyCommonTerraformOptions)

    // Run `terraform output` to get the values of output variables
    actualPublicSubRegion := terraform.Output(t, commonTerraformOptions, "cloud_iam_db_public_subregion")
    actualPrivateSubRegion := terraform.Output(t, commonTerraformOptions, "cloud_iam_db_private_subregion")
    actualDatabaseSubRegion := terraform.Output(t, commonTerraformOptions, "cloud_iam_db_database_subregion")

    // website::tag::3::Check the output against expected values.
    // Verify we're getting back the outputs we expect
    assert.Equal(t, expectedPublicSubRegion, actualPublicSubRegion)
    assert.Equal(t, expectedPrivateSubRegion, actualPrivateSubRegion)
    assert.Equal(t, expectedDatabaseSubRegion, actualDatabaseSubRegion)
}

I configure and install the dependencies as described in the documentation:

go mod init github.com/gruntwork-io/terratest/modules/terraform
go: creating new go.mod: module github.com/gruntwork-io/terratest/modules/terraform
go: to add module requirements and sums:
        go mod tidy
go mod tidy
go: finding module for package github.com/stretchr/testify/assert
go: found github.com/stretchr/testify/assert in github.com/stretchr/testify v1.8.4

My go version: 1.21.5

My go.mod file:

module github.com/gruntwork-io/terratest/modules/terraform

go 1.21.5

require github.com/stretchr/testify v1.8.4

require (
    github.com/davecgh/go-spew v1.1.1 // indirect
    github.com/pmezard/go-difflib v1.0.0 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)

My go.sum file:

github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

Thx for your help

CarterSheehan commented 2 months ago

@BenjaminCloudIam What was the fix