Terraform framework: Access provider within tests

Hello,

I’m currently migrating terraform-lxd-provider to Terraform framework (from SDKv2).

We are using terraform-plugin-testing for testing. Within tests we retrieve a Client from globally defined Provider, and use it to verify whether Terraform actually made changes to the system and whether those changes are reflected correctly.

Previously, accessing the provider was easy, since it was defined as a global variable.

var testAccProviders map[string]*schema.Provider
var testAccProvider *schema.Provider

func init() {
	testAccProvider = Provider()
	testAccProviders = map[string]*schema.Provider{
		"lxd": testAccProvider,
	}
    ...
}

func TestAccInstance(t *testing.T) {
	resource.Test(t, resource.TestCase{
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				Check: resource.ComposeTestCheckFunc(
					testAccInstanceRunning(t, "lxd_instance.inst1"),
				),
			},
		},
		...
	})
}

func testAccInstanceRunning(t *testing.T, instName string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
        ...
		client, err := testAccProvider.Meta().(*lxdProvider).GetInstanceServer("")
		if err != nil {
			return err
		}

        // Use retrieved client to verify resources are created and
        // properly configured by Terraform. 
        ...
	}
}

However, I cannot figure out how can I access the provider within a test when using ProtoV6ProviderFactories (if possible at all)?

Thanks :slight_smile:

2 Likes

I’m faced with the same issue. Were you able to solve it? Currently, the best I can come up with is to have the provider store its provider data (an any) in a global variable by setting inside the provider’s Configure() method, but this is ugly. It seems like there ought to be a clean way to access the provider data from within an acceptance test.