Skip to content

Provider is not configured as part of the Test or UnitTest #535

@MovieStoreGuy

Description

@MovieStoreGuy

terraform-plugin-testing version

github.com/hashicorp/terraform-plugin-testing v1.13.2

Relevant provider source code

package main

import (
	"context"
	"sync/atomic"
	"testing"

	"github.com/hashicorp/terraform-plugin-framework/datasource"
	"github.com/hashicorp/terraform-plugin-framework/provider"
	"github.com/hashicorp/terraform-plugin-framework/providerserver"
	"github.com/hashicorp/terraform-plugin-framework/resource"
	"github.com/hashicorp/terraform-plugin-go/tfprotov5"
	testresource "github.com/hashicorp/terraform-plugin-testing/helper/resource"
	"github.com/stretchr/testify/assert"
)

type MockProvider struct {
	tb         testing.TB
	configured *atomic.Bool
}

var _ provider.Provider = (*MockProvider)(nil)

func (mp *MockProvider) Metadata(_ context.Context, _ provider.MetadataRequest, _ *provider.MetadataResponse) {
	// No-op for mock
	mp.tb.Log("Metadata requested")
}

func (mp *MockProvider) Schema(_ context.Context, _ provider.SchemaRequest, _ *provider.SchemaResponse) {
	mp.tb.Log("Schema requested")
}

func (mp *MockProvider) Configure(_ context.Context, _ provider.ConfigureRequest, _ *provider.ConfigureResponse) {
	mp.tb.Log("Provider configured")
	mp.configured.Store(true)
}

func (mp *MockProvider) DataSources(_ context.Context) []func() datasource.DataSource {
	mp.tb.Log("DataSources requested")
	return nil
}

func (mp *MockProvider) Resources(_ context.Context) []func() resource.Resource {
	mp.tb.Log("Resources requested")
	return nil
}

func TestProviderConfiguredExample(t *testing.T) {
	mock := &MockProvider{
		tb:         t,
		configured: &atomic.Bool{},
	}

	testresource.UnitTest(t, testresource.TestCase{
		Steps: []testresource.TestStep{
			{
				Config: `provider "mock" {}`,
				ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
					"mock": providerserver.NewProtocol5WithError(mock),
				},
			},
		},
	})

	assert.True(t, mock.configured.Load(), "Provider should be configured")
}

Terraform Configuration Files

# insert config here
provider "mock" {}

Expected Behavior

What I expect here is the mock provider configuration is called so that it can apply the data fields so the components can be applied using their optional configure method with using the framework.

Actual Behavior

What actually happens is provider.Configure is never called and it is not clear that it shoudl

Steps to Reproduce

I've simplified my issue in the above PoC, it should just require copying it locally to see what I am observing.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions