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

Migrate Testing Provider Code to terraform-plugin-go #151

Closed
bflad opened this issue Jul 13, 2023 · 1 comment · Fixed by #170
Closed

Migrate Testing Provider Code to terraform-plugin-go #151

bflad opened this issue Jul 13, 2023 · 1 comment · Fixed by #170
Assignees
Milestone

Comments

@bflad
Copy link
Contributor

bflad commented Jul 13, 2023

Description

In order to appropriately (unit) test the functionality offered to provider developers with helper/resource.TestCase and friends, it generally requires the need for real provider implementations. Currently, some of the tests utilize the lingering direct dependency on terraform-plugin-sdk. For example:

{
	Config: `resource "null_resource" "test" {}`,
	ProviderFactories: map[string]func() (*schema.Provider, error){
		"null": func() (*schema.Provider, error) { //nolint:unparam // required signature
			return &schema.Provider{
				ResourcesMap: map[string]*schema.Resource{
					"null_resource": {
						CreateContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
							d.SetId("test")
							return nil
						},
						DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
							return nil
						},
						ReadContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
							return nil
						},
						Schema: map[string]*schema.Schema{
							"triggers": {
								Elem:     &schema.Schema{Type: schema.TypeString},
								ForceNew: true,
								Optional: true,
								Type:     schema.TypeMap,
							},
						},
					},
				},
			}, nil
		},
	},
},

A long term goal of this Go module would be to remove that direct dependency, which is only based on supporting the legacy Providers and ProviderFactories fields which work with the terraform-plugin-sdk/helper/schema.Provider type.

Other tests rely on downloading real providers, such as hashicorp/random that happen to include the desired functionality for testing.

Proposal

The summary of this proposal is:

  • Introduce a relatively straightforward terraform-plugin-go based "provider SDK" for creating testing providers in this codebase
  • Remove any testing provider code that is directly terraform-plugin-sdk based by migrating to the terraform-plugin-go equivalent.
  • Real, externally hosted providers, such as hashicorp/random etc., can and should still be used for testing ExternalProviders field functionality, but not in other cases. Migrate those other cases to the terraform-plugin-go equivalent.

Some additional notes/assumptions:

  • Provider developers should always want to test in-development provider Go code, rather than being forced to host/publish them elsewhere first.
  • The terraform-plugin-go dependency should always exist in this Go module as long as that Go module is the one that contains the provider protocol implementation in Go or at least the lowest-level provider server implementation.
  • The goal of creating a terraform-plugin-go "provider SDK" is to enable consistency across testing, since correctly implementing the full lifecycle of a provider server is non-trivial.
  • The terraform-plugin-go "provider SDK" can be fairly lightweight, but it should abstract away all the lowest level details of directly handling the protocol RPCs and instead can present more familiar Create, Read, etc. methods.
  • The terraform-plugin-go "provider SDK" can/should use the tftypes type system to reduce complexity in that area.
@bflad bflad self-assigned this Aug 22, 2023
bflad added a commit that referenced this issue Aug 22, 2023
Reference: #151

Creates an internal terraform-plugin-go based SDK and declarative provider for unit testing for within this Go module. The new `helper/resource` tests should be equivalent to the existing terraform-plugin-sdk based `TestTest_TestStep_ExternalProviders_To_ProviderFactories` and `TestTest_TestStep_ProviderFactories_To_ExternalProviders` tests.
bflad added a commit that referenced this issue Aug 25, 2023
Reference: #151

Creates an internal terraform-plugin-go based SDK and declarative provider for unit testing for within this Go module. The new `helper/resource` tests should be equivalent to the existing terraform-plugin-sdk based `TestTest_TestStep_ExternalProviders_To_ProviderFactories` and `TestTest_TestStep_ProviderFactories_To_ExternalProviders` tests.
bflad added a commit that referenced this issue Aug 28, 2023
Reference: #151

This will reduce CI time and dependency on networking/registry calls.
bflad added a commit that referenced this issue Aug 28, 2023
Reference: #151

This will reduce CI time and dependency on networking/registry calls.
@bflad bflad added this to the v1.5.0 milestone Aug 28, 2023
bflad added a commit that referenced this issue Aug 28, 2023
#170)

Reference: #151

This will reduce CI time and dependency on networking/registry calls.
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
1 participant