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
Hi,
I'am trying to use terratest but i got the following error when I run my test:
This is my code:
I configure and install the dependencies as described in the documentation:
My go version: 1.21.5
My go.mod file:
My go.sum file:
Thx for your help