How to use go's TestMain with terratest?

Viewed 178

I would like to use go test and the terratest library for an integration test of a cluster with ~10 different components (pods, services, load balancers, links between components, etc). Tools used to build the infrastructure are terraform, kubernetes, and helm. Building the infrastructure takes approx. 10 minutes, so that I do not want to do it separately for every test. My solution suggests to use the pattern of setting up the test infrastructure in TestMain(*testing.M) and to group tests in test suits like TestAuth(*testing.T), TestMonitoring(*testing.T), etc. Now, I need to call terratest components such as terraform.InitAndApply(*testing.T, terraformOptions) outside a test suite that -- apparently to me-- is not possible.

I tried the following:

func TestMain(m *testing.M) {
    setupInfrastructure()
    rc = m.Run()
    teadDownInfrastructure()
    os.Exit(rc)
}

func setupInfrastructure() {
        terraformOptions := &terraform.Options{
            TerraformDir: testFolder,
            EnvVars: map[string]string{
                "TF_VAR_cluster_size":         3,
            },
        }
        terraform.InitAndApply(t, terraformOptions)   // <-- this is the problem
}

As this is the natural way of setting up a comprehensive test infrastructure, what do I miss?

I saw that all terratest samples (https://github.com/gruntwork-io/terratest/tree/master/test) use one test suite, stages and sub-tests, which I do not want to do as it gives up most of the features from go testing. Is this really the only way to do the job?

0 Answers
Related