3

Hello I have the following test

func badTags(t *testing.T){
  terraformOptions := &terraform.Options{
        TerraformDir: "../bad_values",
    }
  tags := terraform.Output(t, terraformOptions, "test_required_tags")
  assert.Error(t, tags)

}

Note that the value of tag should throw an error but I keep getting the following error

string does not implement error (missing Error method)

If I remove the assertion , an error with a String message is throws as expected. How can I assert on the error?

user_mda
  • 18,148
  • 27
  • 82
  • 145

1 Answers1

0

assert.Error asserts that a function returned an error., it's just like :

if err == nil {
    t.Error("no error returned")
}

But here the given parameter is tags, and tags is a string, according to the terratest documentation that why your receive the following error :

string does not implement error (missing Error method)

Use OutputForKeys must solve your issue, please try this :

func badTags(t *testing.T){
  terraformOptions := &terraform.Options{
        TerraformDir: "../bad_values",
    }
  validTags := terraform.OutputForKeys(t, terraformOptions, []string{"test_required_tags"})
  assert.Contains(t, validTags, "test_required_tags")
}
Dany Sousa
  • 229
  • 2
  • 16
  • 1
    That gives me an error about 'tags' value not being used – user_mda Mar 31 '20 at 13:30
  • 1
    I added a print statement , just so the variable is used. But the test fails – user_mda Mar 31 '20 at 13:42
  • Mmmh, ok I read the `OutputE` code and the function return an error only if it fail to read stdout for exemple but it didn't check the terraform success. can you try this : `validTags := terraform.OutputForKeys(t, terraformOptions, []string{"test_required_tags"})` `assert.Contains(t, validTags, "test_required_tags")` – Dany Sousa Mar 31 '20 at 13:53
  • I've edited my comment with this other solution for lisibility – Dany Sousa Mar 31 '20 at 13:56
  • I want to check for failure, I want to check that the output throws an error , how is that captured here? Can you explain? I want to assert that terraform.Output throws an error – user_mda Mar 31 '20 at 15:37
  • Of course, `terraform.Output` didn't catch the `stdErr` and didn't parse the response. You have only 2 choices : compare the string to find part of the expected error or (the recommended way by terratest) use OutputForKeys and check if the tested field is in the result map (or if the value match with the given value for this field). https://godoc.org/github.com/gruntwork-io/terratest/modules/terraform#OutputForKeys – Dany Sousa Mar 31 '20 at 16:47