Skip to content

Commit

Permalink
Cross tests: ensure providers pass validation in input tests (#2023)
Browse files Browse the repository at this point in the history
As title, we shouldn't be testing invalid schemas.
  • Loading branch information
VenelinMartinov authored May 23, 2024
1 parent 872a08e commit 341014a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
15 changes: 14 additions & 1 deletion pkg/tests/cross-tests/input_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ func assertValEqual(t T, name string, tfVal, pulVal any) {
}
}

func ensureProviderValid(t T, tfp *schema.Provider) {
for _, r := range tfp.ResourcesMap {
//nolint:staticcheck
if r.Read == nil && r.ReadContext == nil {
r.ReadContext = func(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}
}
}
require.NoError(t, tfp.InternalValidate())
}

// Adapted from diff_check.go
func runCreateInputCheck(t T, tc inputTestCase) {
//nolint:staticcheck
Expand Down Expand Up @@ -99,6 +111,7 @@ func runCreateInputCheck(t T, tc inputTestCase) {
rtype: tc.Resource,
},
}
ensureProviderValid(t, tfp)

shimProvider := shimv2.NewProvider(tfp, shimv2.WithPlanResourceChange(
func(tfResourceType string) bool { return true },
Expand All @@ -110,7 +123,7 @@ func runCreateInputCheck(t T, tc inputTestCase) {
shimProvider: shimProvider,
pulumiResourceToken: rtoken,
tfResourceName: rtype,
objectType: nil,
objectType: tc.ObjectType,
}

puwd := t.TempDir()
Expand Down
32 changes: 8 additions & 24 deletions pkg/tests/cross-tests/input_cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,6 @@ func TestInputsEqualObjectBasic(t *testing.T) {
}
}

// Isolated from rapid-generated tests
func TestInputsEmptySchema(t *testing.T) {
skipUnlessLinux(t)
runCreateInputCheck(
t, inputTestCase{
Resource: &schema.Resource{
Schema: map[string]*schema.Schema{},
},
Config: tftypes.NewValue(tftypes.Object{}, map[string]tftypes.Value{}),
},
)
}

func TestInputsEqualEmptyList(t *testing.T) {
skipUnlessLinux(t)
for _, maxItems := range []int{0, 1} {
Expand Down Expand Up @@ -257,13 +244,9 @@ func TestExplicitNilList(t *testing.T) {
Optional: true,
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Computed: true,
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Sensitive: true,
Type: schema.TypeInt,
},
},
},
Expand Down Expand Up @@ -301,18 +284,19 @@ func TestInputsEmptyCollections(t *testing.T) {
}{
{"list block", 0, schema.TypeList, resourceElem, schema.SchemaConfigModeAuto},
{"set block", 0, schema.TypeSet, resourceElem, schema.SchemaConfigModeAuto},
// This isn't quite valid but should work
{"map block", 0, schema.TypeMap, resourceElem, schema.SchemaConfigModeAuto},
// TypeMap with Elem *Resource not supported
// {"map block", 0, schema.TypeMap, resourceElem, schema.SchemaConfigModeAuto},
{"list max items one block", 1, schema.TypeList, resourceElem, schema.SchemaConfigModeAuto},
{"set max items one block", 1, schema.TypeSet, resourceElem, schema.SchemaConfigModeAuto},
// This isn't quite valid but should work
{"map max items one block", 1, schema.TypeMap, resourceElem, schema.SchemaConfigModeAuto},
// MaxItems is only valid on lists and sets
// {"map max items one block", 1, schema.TypeMap, resourceElem, schema.SchemaConfigModeAuto},
{"list attr", 0, schema.TypeList, schemaElem, schema.SchemaConfigModeAuto},
{"set attr", 0, schema.TypeSet, schemaElem, schema.SchemaConfigModeAuto},
{"map attr", 0, schema.TypeMap, schemaElem, schema.SchemaConfigModeAuto},
{"list max items one attr", 1, schema.TypeList, schemaElem, schema.SchemaConfigModeAuto},
{"set max items one attr", 1, schema.TypeSet, schemaElem, schema.SchemaConfigModeAuto},
{"map max items one attr", 1, schema.TypeMap, schemaElem, schema.SchemaConfigModeAuto},
// MaxItems is only valid on lists and sets
// {"map max items one attr", 1, schema.TypeMap, schemaElem, schema.SchemaConfigModeAuto},
{"list config mode attr", 0, schema.TypeList, resourceElem, schema.SchemaConfigModeAttr},
{"set config mode attr", 0, schema.TypeSet, resourceElem, schema.SchemaConfigModeAttr},
} {
Expand Down

0 comments on commit 341014a

Please sign in to comment.