1

I am new to Go and terratest. I have the following terratest

package main

import (
    "regexp"
    "testing"

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

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

    terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
        TerraformDir: "./unit-test",
    })

    defer terraform.Destroy(t, terraformOptions)

    terraform.InitAndApply(t, terraformOptions)

    bastionSecurityGroup := terraform.Output(t, terraformOptions, "bastion_security_group")

    assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup)
}

I initialised it as follows:

go mod init github.com/myorg/terraform-bastion-linux

When trying to run it with go test -v I get the error:

package github.com/myorg/terraform-bastion-linux: build constraints exclude all Go files in /Users/george/terraform-bastion-linux/test

My environment is as follows:

macOS Big Sur 11.6.4
CPU: Intel i9
terraform --version                                                                      
Terraform v1.2.3
on darwin_amd64
go version
go version go1.18.3 darwin/amd64

I have no env variables set that start with GO, for example, env|grep GO returns nothing as result.

As advised in:

I have tried adding the following on top of the file

//+build darwin,cgo linux
//go:build (darwin && cgo) || linux

And also exporting the GOOS and GOARCH env variables

export GOOS=darwin
export GOARCH=amd64

But I still get the same error.

How to troubleshoot this issue? What can I try in order to run the test successfully?

Georgios F.
  • 417
  • 5
  • 15
  • 1
    What is the name of the file? A [mre] would help diagnose the problem. – JimB Jun 16 '22 at 13:04
  • The name of the file is bastion_linux_test.go – Georgios F. Jun 16 '22 at 13:40
  • So you have given the file a `linux` [build constraint](https://pkg.go.dev/cmd/go#hdr-Build_constraints), it's not going to be read at all on darwin. – JimB Jun 16 '22 at 13:41
  • Omg the word linux in the file name means that it's meant to only run on linux? Thank you so much for spotting this, I dunno why my mind didn't even go there. I will try and let you know. – Georgios F. Jun 16 '22 at 13:43
  • Yes, that was it! Can't thank you enough! I am new to Go, but it appears to be terribly misleading with such error messages. – Georgios F. Jun 16 '22 at 13:47
  • The error message is actually quite precise here, but as with any sufficiently complex system, it takes some time to learn how to interpret such messages. The "build constraints" excluded the file for some reason, so in checking the docs for [build constraints](https://pkg.go.dev/cmd/go#hdr-Build_constraints), the filename formats matching `GOOS` and `GOARCH` combinations are listed which match the given file. – JimB Jun 16 '22 at 13:53

1 Answers1

0

As commented by JimB, the problem was that the filename included the word linux, which Go interpreted as a build constraint to only run on Linux and not Darwin. I am new to Go, and it appears to be terribly misleading with such errors. I didn't even include the file name in my original question as I thought it would be irrelevant.

Georgios F.
  • 417
  • 5
  • 15