Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Aug 1, 2023
1 parent 1edb7d8 commit a90f474
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions pkg/tfbridge/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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{
Expand Down

0 comments on commit a90f474

Please sign in to comment.