-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use framework testprovider files for setup of test using ProtoV5Provi…
…derFactories (#16)
- Loading branch information
1 parent
4cac99e
commit 021c2e8
Showing
5 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package testprovider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/provider" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
var _ provider.Provider = &Provider{} | ||
|
||
// Provider is declarative provider.Provider for unit testing. | ||
type Provider struct { | ||
// Provider interface methods | ||
MetadataMethod func(context.Context, provider.MetadataRequest, *provider.MetadataResponse) | ||
ConfigureMethod func(context.Context, provider.ConfigureRequest, *provider.ConfigureResponse) | ||
SchemaMethod func(context.Context, provider.SchemaRequest, *provider.SchemaResponse) | ||
DataSourcesMethod func(context.Context) []func() datasource.DataSource | ||
ResourcesMethod func(context.Context) []func() resource.Resource | ||
} | ||
|
||
// Configure satisfies the provider.Provider interface. | ||
func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) { | ||
if p == nil || p.ConfigureMethod == nil { | ||
return | ||
} | ||
|
||
p.ConfigureMethod(ctx, req, resp) | ||
} | ||
|
||
// DataSources satisfies the provider.Provider interface. | ||
func (p *Provider) DataSources(ctx context.Context) []func() datasource.DataSource { | ||
if p == nil || p.DataSourcesMethod == nil { | ||
return nil | ||
} | ||
|
||
return p.DataSourcesMethod(ctx) | ||
} | ||
|
||
// Metadata satisfies the provider.Provider interface. | ||
func (p *Provider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { | ||
if p == nil || p.MetadataMethod == nil { | ||
return | ||
} | ||
|
||
p.MetadataMethod(ctx, req, resp) | ||
} | ||
|
||
// Schema satisfies the provider.Provider interface. | ||
func (p *Provider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { | ||
if p == nil || p.SchemaMethod == nil { | ||
return | ||
} | ||
|
||
p.SchemaMethod(ctx, req, resp) | ||
} | ||
|
||
// Resources satisfies the provider.Provider interface. | ||
func (p *Provider) Resources(ctx context.Context) []func() resource.Resource { | ||
if p == nil || p.ResourcesMethod == nil { | ||
return nil | ||
} | ||
|
||
return p.ResourcesMethod(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package testprovider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
var _ resource.Resource = &Resource{} | ||
|
||
// Resource is declarative resource.Resource for unit testing. | ||
type Resource struct { | ||
// Resource interface methods | ||
MetadataMethod func(context.Context, resource.MetadataRequest, *resource.MetadataResponse) | ||
SchemaMethod func(context.Context, resource.SchemaRequest, *resource.SchemaResponse) | ||
CreateMethod func(context.Context, resource.CreateRequest, *resource.CreateResponse) | ||
DeleteMethod func(context.Context, resource.DeleteRequest, *resource.DeleteResponse) | ||
ReadMethod func(context.Context, resource.ReadRequest, *resource.ReadResponse) | ||
UpdateMethod func(context.Context, resource.UpdateRequest, *resource.UpdateResponse) | ||
} | ||
|
||
// Metadata satisfies the resource.Resource interface. | ||
func (r *Resource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
if r.MetadataMethod == nil { | ||
return | ||
} | ||
|
||
r.MetadataMethod(ctx, req, resp) | ||
} | ||
|
||
// Schema satisfies the resource.Resource interface. | ||
func (r *Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
if r.SchemaMethod == nil { | ||
return | ||
} | ||
|
||
r.SchemaMethod(ctx, req, resp) | ||
} | ||
|
||
// Create satisfies the resource.Resource interface. | ||
func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
if r.CreateMethod == nil { | ||
return | ||
} | ||
|
||
r.CreateMethod(ctx, req, resp) | ||
} | ||
|
||
// Delete satisfies the resource.Resource interface. | ||
func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
if r.DeleteMethod == nil { | ||
return | ||
} | ||
|
||
r.DeleteMethod(ctx, req, resp) | ||
} | ||
|
||
// Read satisfies the resource.Resource interface. | ||
func (r *Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
if r.ReadMethod == nil { | ||
return | ||
} | ||
|
||
r.ReadMethod(ctx, req, resp) | ||
} | ||
|
||
// Update satisfies the resource.Resource interface. | ||
func (r *Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
if r.UpdateMethod == nil { | ||
return | ||
} | ||
|
||
r.UpdateMethod(ctx, req, resp) | ||
} |