Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: utilize slices and maps Golang packages for testing helper functions #322

Open
mschuchard opened this issue Apr 12, 2024 · 1 comment
Labels
enhancement New feature or request waiting-response Issues or pull requests waiting for an external response

Comments

@mschuchard
Copy link

mschuchard commented Apr 12, 2024

With the advent of the slices and maps packages promotion from experimental to core in Go 1.21, and generics in 1.17, we now have e.g. https://pkg.go.dev/slices#Equal. Could we have helper functions as part of the type https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/helper/resource#TestCheckFunc that e.g. ascertain list and set equality instead of using the cumbersome https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/helper/resource#example-TestCheckResourceAttr-TypeListAttribute?

There are for sure other examples, but I feel the above is demonstrative of what I am trying to communicate.

@mschuchard mschuchard added the enhancement New feature or request label Apr 12, 2024
@bendbennett
Copy link
Contributor

Hi @mschuchard 👋

One option that is available for verifying list and set equality of values in state is to use state checks with the knownvalue package introduced in v1.7.0.

For example, to perform the equivalent check to the list equality check that you linked to, you could use the following:

package provider

import (
	"testing"

	"github.com/hashicorp/terraform-plugin-testing/helper/resource"
	"github.com/hashicorp/terraform-plugin-testing/knownvalue"
	"github.com/hashicorp/terraform-plugin-testing/statecheck"
	"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

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

	resource.Test(t, resource.TestCase{
		// Provider definition omitted.
		Steps: []resource.TestStep{
			{
				// Example resource containing a computed list attribute named "list_attribute"
				Config: `resource "example_thing" "test" {}`,
				ConfigStateChecks: []statecheck.StateCheck{
					statecheck.ExpectKnownValue(
						"example_thing.test",
						tfjsonpath.New("list_attribute"),
						knownvalue.ListExact([]knownvalue.Check{
							knownvalue.StringExact("value1"),
							knownvalue.StringExact("value2"),
							knownvalue.StringExact("value3"),
						}),
					),
				},
			},
		},
	})
}

The acceptance testing documentation contains some information regarding state checks, and plan checks which can be used in conjunction with known value checks.

@bendbennett bendbennett added the waiting-response Issues or pull requests waiting for an external response label May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request waiting-response Issues or pull requests waiting for an external response
Projects
None yet
Development

No branches or pull requests

2 participants