From a90f474d069d7260755bab2311a129a2d64ca240 Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Tue, 1 Aug 2023 11:36:58 +0200 Subject: [PATCH] Add test --- pkg/tfbridge/provider_test.go | 83 ++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/pkg/tfbridge/provider_test.go b/pkg/tfbridge/provider_test.go index f110feba5..78a9d1ee3 100644 --- a/pkg/tfbridge/provider_test.go +++ b/pkg/tfbridge/provider_test.go @@ -495,25 +495,10 @@ func testCheckFailures(t *testing.T, provider *Provider, typeName tokens.Type) [ News: pulumiIns, }) assert.NoError(t, err) - assert.Len(t, checkResp.Failures, 3) return checkResp.Failures } -func TestProviderCheck(t *testing.T) { - provider := &Provider{ - tf: shimv1.NewProvider(testTFProvider), - config: shimv1.NewSchemaMap(testTFProvider.Schema), - } - provider.resources = map[tokens.Type]Resource{ - "SecondResource": { - TF: shimv1.NewResource(testTFProvider.ResourcesMap["second_resource"]), - TFName: "second_resource", - Schema: &ResourceInfo{Tok: "SecondResource"}, - }, - } - - failures := testCheckFailures(t, provider, "SecondResource") - sort.SliceStable(failures, func(i, j int) bool { return failures[i].Reason < failures[j].Reason }) +func testCheckFailuresV1(t *testing.T, failures []*pulumirpc.CheckFailure) { assert.Equal(t, "\"conflicting_property\": conflicts with conflicting_property2."+ " Examine values at 'name.conflictingProperty'.", failures[0].Reason) assert.Equal(t, "", failures[0].Property) @@ -524,21 +509,7 @@ func TestProviderCheck(t *testing.T) { assert.Equal(t, "", failures[2].Property) } -func TestProviderCheckV2(t *testing.T) { - provider := &Provider{ - tf: shimv2.NewProvider(testTFProviderV2), - config: shimv2.NewSchemaMap(testTFProviderV2.Schema), - } - provider.resources = map[tokens.Type]Resource{ - "SecondResource": { - TF: shimv2.NewResource(testTFProviderV2.ResourcesMap["second_resource"]), - TFName: "second_resource", - Schema: &ResourceInfo{Tok: "SecondResource"}, - }, - } - - failures := testCheckFailures(t, provider, "SecondResource") - sort.SliceStable(failures, func(i, j int) bool { return failures[i].Reason < failures[j].Reason }) +func testCheckFailuresV2(t *testing.T, failures []*pulumirpc.CheckFailure) { assert.Equal(t, "Conflicting configuration arguments: \"conflicting_property\": conflicts with "+ "conflicting_property2. Examine values at 'name.conflictingProperty'.", failures[0].Reason) assert.Equal(t, "", failures[0].Property) @@ -550,6 +521,56 @@ func TestProviderCheckV2(t *testing.T) { assert.Equal(t, "", failures[2].Property) } +func TestProviderCheck(t *testing.T) { + testFailures := map[string]func(*testing.T, []*pulumirpc.CheckFailure){ + "v1": testCheckFailuresV1, + "v2": testCheckFailuresV2, + } + + testCheck := func(t *testing.T, provider *Provider) { + callback := func(_ context.Context, config, _ resource.PropertyMap) (resource.PropertyMap, error) { + delete(config, "conflictingProperty2") + config["arrayPropertyValues"] = resource.NewArrayProperty([]resource.PropertyValue{ + resource.NewStringProperty("v1"), + resource.NewStringProperty("v2"), + }) + return config, nil + } + provider.resources["SecondResource"].Schema.PreCheckCallback = callback + + failures := testCheckFailures(t, provider, "SecondResource") + assert.Len(t, failures, 0) + } + + for _, f := range factories { + if f.SDKVersion() == "v2" { + continue + } + provider := &Provider{ + tf: f.NewTestProvider(), + config: f.NewTestProvider().Schema(), + } + + provider.resources = map[tokens.Type]Resource{ + "SecondResource": { + TF: provider.tf.ResourcesMap().Get("second_resource"), + TFName: "second_resource", + Schema: &ResourceInfo{Tok: "SecondResource"}, + }, + } + + t.Run(f.SDKVersion(), func(t *testing.T) { + t.Run("failures", func(t *testing.T) { + failures := testCheckFailures(t, provider, "SecondResource") + assert.Len(t, failures, 3) + sort.SliceStable(failures, func(i, j int) bool { return failures[i].Reason < failures[j].Reason }) + testFailures[f.SDKVersion()](t, failures) + }) + t.Run("preCheckCallback", func(t *testing.T) { testCheck(t, provider) }) + }) + } +} + func testProviderRead(t *testing.T, provider *Provider, typeName tokens.Type) { urn := resource.NewURN("stack", "project", "", typeName, "name") readResp, err := provider.Read(context.Background(), &pulumirpc.ReadRequest{