Skip to content

Commit

Permalink
Fix nofork tests: Prevent using a global state in the Terraform plugi…
Browse files Browse the repository at this point in the history
…n SDK v2 & framework tests

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Feb 1, 2024
1 parent 6568a88 commit d717974
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
40 changes: 20 additions & 20 deletions pkg/controller/external_tfpluginfw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/crossplane/upjet/pkg/terraform"
)

func newBaseObject() *fake.Terraformed {
return &fake.Terraformed{
func newBaseObject() fake.Terraformed {
return fake.Terraformed{
Parameterizable: fake.Parameterizable{
Parameters: map[string]any{
"name": "example",
Expand Down Expand Up @@ -101,7 +101,7 @@ func newBaseUpjetConfig() *config.Resource {
type testConfiguration struct {
r resource.Resource
cfg *config.Resource
obj xpresource.Managed
obj fake.Terraformed
params map[string]any
currentStateMap map[string]any
plannedStateMap map[string]any
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestTPFConnect(t *testing.T) {
setupFn terraform.SetupFn
cfg *config.Resource
ots *OperationTrackerStore
obj xpresource.Managed
obj fake.Terraformed
}
type want struct {
err error
Expand All @@ -202,7 +202,7 @@ func TestTPFConnect(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
c := NewTerraformPluginFrameworkConnector(nil, tc.args.setupFn, tc.args.cfg, tc.args.ots, WithTerraformPluginFrameworkLogger(logTest))
_, err := c.Connect(context.TODO(), tc.args.obj)
_, err := c.Connect(context.TODO(), &tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestTPFObserve(t *testing.T) {
testConfiguration: testConfiguration{
r: newMockBaseTPFResource(),
cfg: newBaseUpjetConfig(),
obj: &fake.Terraformed{
obj: fake.Terraformed{
Parameterizable: fake.Parameterizable{
Parameters: map[string]any{
"name": "example",
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestTPFObserve(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tpfExternal := prepareTPFExternalWithTestConfig(tc.testConfiguration)
observation, err := tpfExternal.Observe(context.TODO(), tc.testConfiguration.obj)
observation, err := tpfExternal.Observe(context.TODO(), &tc.testConfiguration.obj)
if diff := cmp.Diff(tc.want.obs, observation); diff != "" {
t.Errorf("\n%s\nObserve(...): -want observation, +got observation:\n", diff)
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestTPFCreate(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tpfExternal := prepareTPFExternalWithTestConfig(tc.testConfiguration)
_, err := tpfExternal.Create(context.TODO(), tc.testConfiguration.obj)
_, err := tpfExternal.Create(context.TODO(), &tc.testConfiguration.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestTPFUpdate(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tpfExternal := prepareTPFExternalWithTestConfig(tc.testConfiguration)
_, err := tpfExternal.Update(context.TODO(), tc.testConfiguration.obj)
_, err := tpfExternal.Update(context.TODO(), &tc.testConfiguration.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand Down Expand Up @@ -502,7 +502,7 @@ func TestTPFDelete(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
tpfExternal := prepareTPFExternalWithTestConfig(tc.testConfiguration)
err := tpfExternal.Delete(context.TODO(), tc.testConfiguration.obj)
err := tpfExternal.Delete(context.TODO(), &tc.testConfiguration.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand Down Expand Up @@ -532,37 +532,37 @@ type mockTPFProviderServer struct {
ReadDataSourceFn func(ctx context.Context, request *tfprotov5.ReadDataSourceRequest) (*tfprotov5.ReadDataSourceResponse, error)
}

func (m *mockTPFProviderServer) GetMetadata(ctx context.Context, request *tfprotov5.GetMetadataRequest) (*tfprotov5.GetMetadataResponse, error) {
func (m *mockTPFProviderServer) GetMetadata(_ context.Context, _ *tfprotov5.GetMetadataRequest) (*tfprotov5.GetMetadataResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) GetProviderSchema(ctx context.Context, request *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
func (m *mockTPFProviderServer) GetProviderSchema(_ context.Context, _ *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) PrepareProviderConfig(ctx context.Context, request *tfprotov5.PrepareProviderConfigRequest) (*tfprotov5.PrepareProviderConfigResponse, error) {
func (m *mockTPFProviderServer) PrepareProviderConfig(_ context.Context, _ *tfprotov5.PrepareProviderConfigRequest) (*tfprotov5.PrepareProviderConfigResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) ConfigureProvider(ctx context.Context, request *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) {
func (m *mockTPFProviderServer) ConfigureProvider(_ context.Context, _ *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) StopProvider(ctx context.Context, request *tfprotov5.StopProviderRequest) (*tfprotov5.StopProviderResponse, error) {
func (m *mockTPFProviderServer) StopProvider(_ context.Context, _ *tfprotov5.StopProviderRequest) (*tfprotov5.StopProviderResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) ValidateResourceTypeConfig(ctx context.Context, request *tfprotov5.ValidateResourceTypeConfigRequest) (*tfprotov5.ValidateResourceTypeConfigResponse, error) {
func (m *mockTPFProviderServer) ValidateResourceTypeConfig(_ context.Context, _ *tfprotov5.ValidateResourceTypeConfigRequest) (*tfprotov5.ValidateResourceTypeConfigResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) UpgradeResourceState(ctx context.Context, request *tfprotov5.UpgradeResourceStateRequest) (*tfprotov5.UpgradeResourceStateResponse, error) {
func (m *mockTPFProviderServer) UpgradeResourceState(_ context.Context, _ *tfprotov5.UpgradeResourceStateRequest) (*tfprotov5.UpgradeResourceStateResponse, error) {
// TODO implement me
panic("implement me")
}
Expand All @@ -588,17 +588,17 @@ func (m *mockTPFProviderServer) ApplyResourceChange(ctx context.Context, request
return m.ApplyResourceChangeFn(ctx, request)
}

func (m *mockTPFProviderServer) ImportResourceState(ctx context.Context, request *tfprotov5.ImportResourceStateRequest) (*tfprotov5.ImportResourceStateResponse, error) {
func (m *mockTPFProviderServer) ImportResourceState(_ context.Context, _ *tfprotov5.ImportResourceStateRequest) (*tfprotov5.ImportResourceStateResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) ValidateDataSourceConfig(ctx context.Context, request *tfprotov5.ValidateDataSourceConfigRequest) (*tfprotov5.ValidateDataSourceConfigResponse, error) {
func (m *mockTPFProviderServer) ValidateDataSourceConfig(_ context.Context, _ *tfprotov5.ValidateDataSourceConfigRequest) (*tfprotov5.ValidateDataSourceConfigResponse, error) {
// TODO implement me
panic("implement me")
}

func (m *mockTPFProviderServer) ReadDataSource(ctx context.Context, request *tfprotov5.ReadDataSourceRequest) (*tfprotov5.ReadDataSourceResponse, error) {
func (m *mockTPFProviderServer) ReadDataSource(_ context.Context, _ *tfprotov5.ReadDataSourceRequest) (*tfprotov5.ReadDataSourceResponse, error) {
// TODO implement me
panic("implement me")
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/controller/external_tfpluginsdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var (
return nil, nil
}},
}
obj = &fake.Terraformed{
obj = fake.Terraformed{
Parameterizable: fake.Parameterizable{
Parameters: map[string]any{
"name": "example",
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestTerraformPluginSDKConnect(t *testing.T) {
setupFn terraform.SetupFn
cfg *config.Resource
ots *OperationTrackerStore
obj xpresource.Managed
obj fake.Terraformed
}
type want struct {
err error
Expand All @@ -146,7 +146,7 @@ func TestTerraformPluginSDKConnect(t *testing.T) {
return terraform.Setup{}, nil
},
cfg: cfg,
obj: &fake.Terraformed{
obj: fake.Terraformed{
Parameterizable: fake.Parameterizable{
Parameters: map[string]any{
"name": " ${jsonencode({\n type = \"object\"\n })}",
Expand All @@ -168,7 +168,7 @@ func TestTerraformPluginSDKConnect(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
c := NewTerraformPluginSDKConnector(nil, tc.args.setupFn, tc.args.cfg, tc.args.ots, WithTerraformPluginSDKLogger(logTest))
_, err := c.Connect(context.TODO(), tc.args.obj)
_, err := c.Connect(context.TODO(), &tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand All @@ -180,7 +180,7 @@ func TestTerraformPluginSDKObserve(t *testing.T) {
type args struct {
r Resource
cfg *config.Resource
obj xpresource.Managed
obj fake.Terraformed
}
type want struct {
obs managed.ExternalObservation
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestTerraformPluginSDKObserve(t *testing.T) {
},
},
cfg: cfg,
obj: &fake.Terraformed{
obj: fake.Terraformed{
Parameterizable: fake.Parameterizable{
Parameters: map[string]any{
"name": "example",
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestTerraformPluginSDKObserve(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
terraformPluginSDKExternal := prepareTerraformPluginSDKExternal(tc.args.r, tc.args.cfg)
observation, err := terraformPluginSDKExternal.Observe(context.TODO(), tc.args.obj)
observation, err := terraformPluginSDKExternal.Observe(context.TODO(), &tc.args.obj)
if diff := cmp.Diff(tc.want.obs, observation); diff != "" {
t.Errorf("\n%s\nObserve(...): -want observation, +got observation:\n", diff)
}
Expand All @@ -286,7 +286,7 @@ func TestTerraformPluginSDKCreate(t *testing.T) {
type args struct {
r Resource
cfg *config.Resource
obj xpresource.Managed
obj fake.Terraformed
}
type want struct {
err error
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestTerraformPluginSDKCreate(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
terraformPluginSDKExternal := prepareTerraformPluginSDKExternal(tc.args.r, tc.args.cfg)
_, err := terraformPluginSDKExternal.Create(context.TODO(), tc.args.obj)
_, err := terraformPluginSDKExternal.Create(context.TODO(), &tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand All @@ -336,7 +336,7 @@ func TestTerraformPluginSDKUpdate(t *testing.T) {
type args struct {
r Resource
cfg *config.Resource
obj xpresource.Managed
obj fake.Terraformed
}
type want struct {
err error
Expand All @@ -360,7 +360,7 @@ func TestTerraformPluginSDKUpdate(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
terraformPluginSDKExternal := prepareTerraformPluginSDKExternal(tc.args.r, tc.args.cfg)
_, err := terraformPluginSDKExternal.Update(context.TODO(), tc.args.obj)
_, err := terraformPluginSDKExternal.Update(context.TODO(), &tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand All @@ -372,7 +372,7 @@ func TestTerraformPluginSDKDelete(t *testing.T) {
type args struct {
r Resource
cfg *config.Resource
obj xpresource.Managed
obj fake.Terraformed
}
type want struct {
err error
Expand All @@ -396,7 +396,7 @@ func TestTerraformPluginSDKDelete(t *testing.T) {
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
terraformPluginSDKExternal := prepareTerraformPluginSDKExternal(tc.args.r, tc.args.cfg)
err := terraformPluginSDKExternal.Delete(context.TODO(), tc.args.obj)
err := terraformPluginSDKExternal.Delete(context.TODO(), &tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nConnect(...): -want error, +got error:\n", diff)
}
Expand Down

0 comments on commit d717974

Please sign in to comment.