terraform-plugin-sdk icon indicating copy to clipboard operation
terraform-plugin-sdk copied to clipboard

TestCheckOutput does not expose data for testing

Open nleiva opened this issue 2 years ago • 0 comments

SDK version

github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0

Relevant provider source code

helper/resource/testing.go

// TestCheckOutput checks an output in the Terraform configuration
func TestCheckOutput(name, value string) TestCheckFunc {
	return func(s *terraform.State) error {
		ms := s.RootModule()
		rs, ok := ms.Outputs[name]
		if !ok {
			return fmt.Errorf("Not found: %s", name)
		}

		if rs.Value != value {
			return fmt.Errorf(
				"Output '%s': expected %#v, got %#v",
				name,
				value,
				rs)
		}

		return nil
	}
}
...

Terraform Configuration Files

data "nautobot_manufacturers" "list" {}

variable "filter" {
	type    = string
	default = "Juniper"
}

output "vendor" {
	value = [
	  for manufacturer in data.nautobot_manufacturers.list.manufacturers :
	  manufacturer.slug
	  if manufacturer.name == var.filter
	]
}

Debug Output

I'm trying to run a test case, where I know a computed attribute (slug) should return a specific value.

func TestAccDataSourceManufacturers(t *testing.T) {
	resource.UnitTest(t, resource.TestCase{
		PreCheck:          func() { testAccPreCheck(t) },
		ProviderFactories: providerFactories,
		Steps: []resource.TestStep{
			{
				Config: testAccDataSourceManufacturer,
				Check: resource.ComposeTestCheckFunc( 
						resource.TestCheckOutput("vendor", "juniper"),
			),
			},
		},
	})
}

However, the value in the output is in a slice of empty interfaces ([]interface {}{"juniper"}), so it always fail and I can't find any resources in the package to get the concrete value of an element of this slice.

=== RUN   TestAccDataSourceManufacturers
    data_source_manufacturers_test.go:12: Step 1/1 error: Check failed: Check 1/1 error: Output 'vendor': expected "juniper", got &terraform.OutputState{Sensitive:false, Type:"list", Value:[]interface {}{"juniper"}, mu:sync.Mutex{state:0, sema:0x0}}

Expected Behavior

I might be missing something, but maybe the TestCheckOutput should do reflection to expose the string value?

Actual Behavior

The string I pass to the function "juniper" is compared against "[]interface {}{"juniper"}", so my test fails.

Steps to Reproduce

It's a test case.

References

I couldn't find any previous issues nor test cases for TestCheckOutput.

Thanks

nleiva avatar Apr 29 '22 03:04 nleiva