This post provides a few resolutions to some Terratest errors encountered.


Problem:

You get a go: cannot find main module error as shown when running a go test:

ahmed@devhost:/u01/terraform/test> go test -v -timeout 1m -run TestVM

go: cannot find main module, but found .git/config in /u01
to create a module there, run:
cd ../.. && go mod init

Solution:

Run these commands to install and initialize go:

# Install the golang library if not already installed
sudo yum install -y go

# Go to top level Terraform folder
cd /u01/terraform

# This is optional
go mod tidy

# The last parameter is your module name
go mod init gitlab.com/AhmedAboulnaga/terraform/test

Problem:

You get a no required module provides package when running a go test:

ahmed@devhost:/u01/terraform/test> go test -v -timeout 1m -run TestVM

terratest_vm_test.go:7:2: no required module provides package github.com/gruntwork-io/terratest/modules/terraform; to add it:
go get github.com/gruntwork-io/terratest/modules/terraform

terratest_vm_test.go:8:9: no required module provides package github.com/stretchr/testify/assert; to add it:
go get github.com/stretchr/testify/assert

Solution:

Since the modules listed in the error were reference in the .go test scripts, download them:

# Navigate to top level Terraform folder
cd /u01/terraform

# Download the modules shown in the error
go get github.com/gruntwork-io/terratest/modules/terraform
go get github.com/stretchr/testify/assert

Problem:

You get no test files when running a go test:

ahmed@devhost:/u01/terraform/test> go test -v -timeout 1m -run TestVM

? gitlab.com/AhmedAboulnaga/terraform/test [no test files]

Solution:

In this particular error, the test file was not named correctly. Rename the file to include _test and the file extension must be .go:

mv terratest_vm.go terratest_vm_test.go

Problem:

You get warning: no tests to run when running a go test:

ahmed@devhost:/u01/terraform/test> go test -v -timeout 1m -run asdf

testing: warning: no tests to run
PASS
ok gitlab.com/AhmedAboulnaga/terraform/test 0.043s

Solution:

If you notice the command above it is running a test called "asdf". This test does not exist. The test must match the function name in your .go test file:

ahmed@devhost:/u01/terraform/test> go test -v -timeout 1m -run TestVM

Problem:

You get a found packages note when running a go test:

ahmed@devhost:/u01/terraform/test> go test -v -timeout 10m -run TestVM

found packages compute (terratest_vm_test.go) and database (terratest_db_test.go) in /u01/terraform/test

Solution:

The package name should be identical in all files in this directory.

  1. Edit all .go files in this folder.
  2. Ensure the first line (e.g., package unittests), should be identical in all files.