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.54k stars 1.33k forks source link

Terratest "module found, but does not contain package" #659

Open almezquita12 opened 4 years ago

almezquita12 commented 4 years ago

It happens to me with many modules and I cannot complete the test. I'm new to terratest, the "Import ()" section containing the git urls, can't find the package...

go: finding module for package github.com/stretchr/testify/tree/master/assert go: finding module for package github.com/gruntwork-io/terratest/tree/master/modules/random go: finding module for package github.com/stretchr/testify/tree/master/require secretsmanager_test3.go:6:2: module github.com/gruntwork-io/terratest@latest found (v0.30.3), but does not contain package github.com/gruntwork-io/terratest/tree/master/modules/random secretsmanager_test3.go:7:2: module github.com/stretchr/testify@latest found (v1.6.1), but does not contain package github.com/stretchr/testify/tree/master/assert secretsmanager_test3.go:8:2: module github.com/stretchr/testify@latest found (v1.6.1), but does not contain package github.com/stretchr/testify/tree/master/require

yorinasub17 commented 4 years ago

github.com/stretchr/testify/tree/master/require looks like a misconfiguration either in the code, or in go.mod. You should not have a reference to tree/master in the import URL. E.g., https://github.com/gruntwork-io/terratest/blob/master/test/terraform_aws_example_test.go#L10

Can you share your import block and go.mod file so we can see where that might be coming from?

almezquita12 commented 4 years ago

This is the "test" folder outside would be the "example" with the module. In the module I have not modified anything regarding Terratest

test.zip

Do I need to install DEP for these tests on AWS? Thanks for your help!

yorinasub17 commented 4 years ago

Thanks for providing the code and I see the problem. Update your imports to the following and try again:

import (
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/service/kms"
    "github.com/gruntwork-io/terratest/modules/testing"
)

That should resolve the issue.

almezquita12 commented 4 years ago

Yes, try changing the urls to get rid of the error it gives now.

I don't know where these variables should be defined or if they are missing any terraform initialization, this is the original error

command-line-arguments

.\kms.go:9:19: undefined: testing.T .\kms.go:10:23: undefined: terraform .\kms.go:14:8: undefined: terraform .\kms.go:16:2: undefined: terraform .\kms.go:60:15: undefined: NewAuthenticatedSession

Thanks again for your answer :)

yorinasub17 commented 4 years ago

These are additional imports you need to add. If you run the goimports tool, they should automatically be added for you: https://godoc.org/golang.org/x/tools/cmd/goimports

The one that it probably won't add is NewAuthenticatedSession, which needs to be aws.NewAuthenticatedSession and import the terratest aws package. Note that this will probably have a collision with the go SDK aws library, so you need to use a different name.

The final import list to resolve this specific list of errors will be:

import (
    "testing"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/service/kms"
    terraaws "github.com/gruntwork-io/terratest/modules/aws"
    "github.com/gruntwork-io/terratest/modules/terraform"
)

// update NewAuthenticatedSession to use terraaws.NewAuthenticatedSession
almezquita12 commented 4 years ago

I have run "$ go get golang.org/x/tools/cmd/goimports" shows the following

$ go get golang.org/x/tools/cmd/goimports
go: downloading golang.org/x/tools v0.0.0-20200928201943-a0ef9b62deab
go: found golang.org/x/tools/cmd/goimports in golang.org/x/tools v0.0.0-20200928201943-a0ef9b62deab
go: downloading golang.org/x/tools v0.0.0-20200113040837-eac381796e91
go: golang.org/x/tools upgrade => v0.0.0-20200928201943-a0ef9b62deab
go: downloading golang.org/x/mod v0.3.0
go: downloading golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1

Where can I download the aws.NewAuthenticatedSession and the terratest aws package from? I can't find it :(

yorinasub17 commented 4 years ago

Sorry, I added a comment in the code snippet to clarify this, which admittedly was easy to miss.

update NewAuthenticatedSession to use terraaws.NewAuthenticatedSession

Since you are using both the go aws SDK and the terratest aws library, there is a name collision so you need to use the alias (terraaws).

almezquita12 commented 4 years ago

Good! it works to remove the "undefined: NewAuthenticatedSession" but we stick with the terraform one.

$ go test -run kms.go
# kms.go
.\kms.go:25:18: undefined: "testing".TestingT
.\kms.go:35:19: undefined: "testing".TestingT
.\kms.go:53:21: undefined: "testing".TestingT
.\kms.go:62:22: undefined: "testing".TestingT

Thanks for yor help @yorinasub17

yorinasub17 commented 4 years ago

Can you share the updated kms.go file? I think the issue is that either:

almezquita12 commented 4 years ago

sure! thanks

test.zip

yorinasub17 commented 4 years ago

You need to change testing.TestingT to testing.T if your intention is to extend those functions. However, it looks like these are exactly the same as what we already have in terratest? If so, you can call those functions in your test with terraaws.GetCmkArn.

Actually, now that I am looking at this closely, it looks like you copied the kms.go file from our module. The intended usage is to import those functions into your project instead of copying them. I recommend reading through the quick start guide if you haven't already to build a mental model for how go testing and terratesting works.

Hopefully that exercise should clarify some things with the issues you are running into.

almezquita12 commented 4 years ago

Hi, I studied the example a lot before starting :) Yes, it is the same example I have modified the terraformOptions. The variables being those of kms I understand that they are valid for my kms module. Changing testing.TestingT to TestingT I have the same error:

# kms.go
.\kms.go: 25: 18: undefined: TestingT
.\kms.go: 35: 19: undefined: TestingT
.\kms.go: 53: 21: undefined: TestingT
.\kms.go: 62: 22: undefined: TestingT

Changing within the example function "\ kms.go: 26: 14" GetCmkArnE to terraaws.GetCmkArnE the error is the same

I have gone a different way and I have added the "github.com/gruntwork-io/terratest/modules/testing" and as we did with terraaws I have put test gives the following error, I don't know if it is an advance:

$ go test kms.go
? command-line-arguments [no test files]

test.zip

Thanks :)

almezquita12 commented 4 years ago

Hi @yorinasub17

I have created a repository and I have left the .tf inside I have managed to display the following:

$ go test
testing: warning: no tests to run
PASS
ok      github/almezquita12/TerratestKMS        1.869s

I'm confused by the "testing: warning: no tests to run"

yorinasub17 commented 4 years ago
almezquita12 commented 4 years ago

Hi @yorinasub17

I got this:

TestKMS 2020-10-05T13:30:10+02:00 logger.go:66: Destroy complete! Resources: 3 destroyed.
PASS
ok      kms_test.go 27.530s

With those variables and that ending I understand it works :)

test.zip

example.zip

almezquita12 commented 4 years ago

@yorinasub17 another case, Could you tell me how to change "terraform.Output" to get me "variables.tf"? I would like not to work with Output

region is variable actualTextExample := terraform.Output(t, terraformOptions, "region")

My trick is to reference the variables, but I don't like it ...

output "region" {
  value = var.region
}
brikis98 commented 4 years ago

@yorinasub17 Is out this week. I'm not sure I understand your question... What are you trying to achieve? What is your goal? What have you tried?

almezquita12 commented 4 years ago

Hi @brikis98 I am new to terratest and am trying to create a _test.go on my own.

I have created this one by looking at the basic example, it returns a pass after having created 3 objects. I understand that it works test.zip

My previous question, I meant on line 52 it says terraform.Output, does this mean it reads the output.tf file?

almezquita12 commented 4 years ago

Hi, how could I refer to my "../example" folder, inside NewS3Client , FindS3BucketWithTag function or similar? I have tried

terraformOptions: = & terraform.Options {
TerraformDir: "../example",
}

And with only TerraformDir: but does not read it

yorinasub17 commented 4 years ago

The mental model around terratest is to read out the outputs after an apply. There is no way to read in from the inputs right now, so if you want to do that, you will need to reflect it in the outputs.

If your goal is to write tests around the input vars, I recommend taking a look at using https://github.com/hashicorp/terraform-config-inspect as a library to load in the variable metadata.