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

Draft for httptest implementation for mocks in tests #65

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/client/mock_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

import (
_ "embed"
"net/http"
"net/http/httptest"
)

type ClientMock struct {
}

func (c *ClientMock) Do(req *http.Request) (*http.Response, error) {
return &http.Response{}, nil
}

func NewMockClient(httpMock httptest.Server) *MockClient {

Check failure on line 16 in internal/client/mock_client.go

View workflow job for this annotation

GitHub Actions / lint

copylocks: NewMockClient passes lock by value: net/http/httptest.Server contains sync.WaitGroup contains sync.noCopy (govet)
return &MockClient{
httpClient: httpMock.Client(),
}
}

type MockClient = Client
126 changes: 126 additions & 0 deletions internal/provider/mock_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

tsClient "github.com/timescale/terraform-provider-timescale/internal/client"
)

// Ensure MockProvider satisfies various provider interfaces.
var _ provider.Provider = &MockProvider{}

// MockProvider defines the provider implementation.
type MockProvider struct {
// version is set to the provider version on release, "dev" when the
// provider is built and ran locally, and "test" when running acceptance
// testing.
version string
// terraformVersion is the caller's terraform version.
terraformVersion string
}

// MockProviderModel describes the provider data model.
type MockProviderModel struct {
ProjectID types.String `tfsdk:"project_id"`
AccessToken types.String `tfsdk:"access_token"`
AccessKey types.String `tfsdk:"access_key"`
SecretKey types.String `tfsdk:"secret_key"`
}

func (p *MockProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
tflog.Trace(ctx, "MockProvider.Metadata")
resp.Version = p.version
resp.TypeName = "timescale"
}

// Schema defines the provider-level schema for configuration data.
func (p *MockProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
tflog.Trace(ctx, "MockProvider.Schema")
resp.Schema = schema.Schema{
MarkdownDescription: "The Terraform provider for [Timescale Cloud](https://console.cloud.timescale.com/).",
Attributes: map[string]schema.Attribute{
"access_token": schema.StringAttribute{
MarkdownDescription: "Access Token",
Optional: true,
Sensitive: true,
},
"project_id": schema.StringAttribute{
MarkdownDescription: "Project ID",
Optional: true,
},
"access_key": schema.StringAttribute{
MarkdownDescription: "Access Key",
Optional: true,
},
"secret_key": schema.StringAttribute{
MarkdownDescription: "Secret Key",
Optional: true,
Sensitive: true,
},
},
}
}

func (p *MockProvider) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
return []resource.ConfigValidator{
resourcevalidator.Conflicting(
path.MatchRoot("access_token"),
path.MatchRoot("access_key"),
),
resourcevalidator.Conflicting(
path.MatchRoot("access_token"),
path.MatchRoot("secret_key"),
),
}
}

// Configure initializes a Timescale API client for data sources and resources.
func (p *MockProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
tflog.Trace(ctx, "MockProvider.Configure")
var data MockProviderModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

p.terraformVersion = req.TerraformVersion
client := tsClient.NewMockClient()

Check failure on line 98 in internal/provider/mock_provider.go

View workflow job for this annotation

GitHub Actions / generate

not enough arguments in call to tsClient.NewMockClient

Check failure on line 98 in internal/provider/mock_provider.go

View workflow job for this annotation

GitHub Actions / Build

not enough arguments in call to tsClient.NewMockClient

Check failure on line 98 in internal/provider/mock_provider.go

View workflow job for this annotation

GitHub Actions / generate

not enough arguments in call to tsClient.NewMockClient

Check failure on line 98 in internal/provider/mock_provider.go

View workflow job for this annotation

GitHub Actions / lint

not enough arguments in call to tsClient.NewMockClient

Check failure on line 98 in internal/provider/mock_provider.go

View workflow job for this annotation

GitHub Actions / Build

not enough arguments in call to tsClient.NewMockClient
resp.DataSourceData = client
resp.ResourceData = client
}

// Resources defines the resources implemented in the provider.
func (p *MockProvider) Resources(ctx context.Context) []func() resource.Resource {
tflog.Trace(ctx, "MockProvider.Resources")
return []func() resource.Resource{
NewServiceResource,
}
}

// DataSources defines the data sources implemented in the provider.
func (p *MockProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
tflog.Trace(ctx, "MockProvider.DataSources")
return []func() datasource.DataSource{
NewProductsDataSource,
NewServiceDataSource,
}
}

func NewMock(version string) func() provider.Provider {
return func() provider.Provider {
return &MockProvider{
version: version,
}
}
}
8 changes: 8 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServe
"timescale": providerserver.NewProtocol6WithError(New("test")()),
}

// testAccProtoV6ProviderFactories are used to instantiate a provider during
// acceptance testing. The factory function will be invoked for every Terraform
// CLI command executed to create a provider server to which the CLI can
// reattach.
var testMock = map[string]func() (tfprotov6.ProviderServer, error){
"timescale": providerserver.NewProtocol6WithError(New("test")()),
}

type Config struct {
Name string
Timeouts Timeouts
Expand Down