From 341014ae60766657564d9f75e6967e7bc564f906 Mon Sep 17 00:00:00 2001 From: VenelinMartinov Date: Thu, 23 May 2024 16:24:03 +0300 Subject: [PATCH] Cross tests: ensure providers pass validation in input tests (#2023) As title, we shouldn't be testing invalid schemas. --- pkg/tests/cross-tests/input_check.go | 15 ++++++++++- pkg/tests/cross-tests/input_cross_test.go | 32 ++++++----------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/pkg/tests/cross-tests/input_check.go b/pkg/tests/cross-tests/input_check.go index eae358aa7..a34552fad 100644 --- a/pkg/tests/cross-tests/input_check.go +++ b/pkg/tests/cross-tests/input_check.go @@ -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 @@ -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 }, @@ -110,7 +123,7 @@ func runCreateInputCheck(t T, tc inputTestCase) { shimProvider: shimProvider, pulumiResourceToken: rtoken, tfResourceName: rtype, - objectType: nil, + objectType: tc.ObjectType, } puwd := t.TempDir() diff --git a/pkg/tests/cross-tests/input_cross_test.go b/pkg/tests/cross-tests/input_cross_test.go index 5dc517339..ea0cdd1cc 100644 --- a/pkg/tests/cross-tests/input_cross_test.go +++ b/pkg/tests/cross-tests/input_cross_test.go @@ -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} { @@ -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, }, }, }, @@ -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}, } {