Skip to main content
Knowledge Base

Verify the shape of terraform output

Answer

Hi Forum. I have pretty much hit a block on this. I am very much in the process of learning go and terratest :smiley_cat: I wanted to test terraform output, as it often the thing I find problematic when passing output to other loops though locals and failing on missing indexes/ attributes and so on. I have looked at [this example](https://github.com/gruntwork-io/terratest/blob/master/test/terraform_hello_world_example_test.go) which looked pretty straightforward but rather than test for a string or absolute value I was looking to generalise for a type and potentially with a view to move on to perhaps writing functions later to test the value, e.g. regex or integer ranges and so on. Struct looked highly suitable - arguably perfect for what I am trying to achieve in my first step - so much perhaps, this may have me in a rabbit hole: ```go type NsgEntry struct { access string destination_address_prefix string destination_port_range []string direction string name string priority int protocol string source_address_prefix string source_port_range string } ``` I was naively hoping the output from terraform (which in my case creates a list of logical objects) could be compared against this struct and verified to have the right 'shape'. For instance in the loop at the bottom here: ```go func TestTerraformNsgMap(t *testing.T) { t.Parallel() terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ TerraformDir: "../examples/terraform-nsg-map-example", VarFiles: []string{"nsg.tfvars"}, NoColor: false, }) defer terraform.Destroy(t, terraformOptions) terraform.InitAndApply(t, terraformOptions) actualTfOutput := terraform.OutputMap(t, terraformOptions, "nsg") for _, value := range actualTfOutput { assert.IsType(t, value, NsgEntry) } } ``` The assert is where I run into problems. Also the way that terratest is 'presenting the output' to go is confusing me as it is almost as it if requires unmarshalling from JSON, despite the OutputMap method being used. I have written more go using the struct directly (based on a book I am reading) and the behaviour of go is very different. In this instance I can access attributes of the object created from the struct like I would do in a python object, which is more familiar to me. I have searched for similar use cases, but I haven't really found anything suitable which is making wonder if my approach if fundamentally wrong. Would very much appreciate any guidance on the above if possible. Thanks in advance.

Hello @niksheridan! It seems like your approach to use a struct to represent the expected shape of Terraform output is reasonable. I believe the issue you are encountering is likely due to how Terraform output is returned as type `map[string]string` (via [OutputMap](https://github.com/gruntwork-io/terratest/blob/master/modules/terraform/output.go#L230-L254)). In your test function, you're using the `IsType` function to assert that the value retrieved from the map is of type `NsgEntry`. This will most likely always fail since the values in the actualTfOutput are of type `map[string]string` instead of `NsgEntry`. If you modify the logic so that it performs a type assertion to convert the values of actualTfOutput to `NsgEntry` structs, then you should be able to perform a proper comparison.