diff --git a/pkg/acceptance/bettertestspoc/assert/commons.go b/pkg/acceptance/bettertestspoc/assert/commons.go index d4fd1554abf..3491f8e5cde 100644 --- a/pkg/acceptance/bettertestspoc/assert/commons.go +++ b/pkg/acceptance/bettertestspoc/assert/commons.go @@ -3,7 +3,7 @@ package assert import ( "errors" "fmt" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections" + "golang.org/x/exp/maps" "strconv" "strings" "testing" @@ -106,60 +106,84 @@ func AssertThatObject(t *testing.T, objectAssert InPlaceAssertionVerifier) { } // TODO: This function should iterate over items and look for list item in attributes that matches ALL items' entries AT ONCE (currently it's a pretty dumb assert running through all attributes) -func HasListItemsOrderIndependent(resourceKey string, attributePath string, items []map[string]string) resource.TestCheckFunc { +func HasListItemsOrderIndependent(resourceKey string, attributePath string, expectedItems []map[string]string) resource.TestCheckFunc { return func(state *terraform.State) error { + var actualItems []map[string]string + + // Allocate space for actualItems and assert length for key, value := range state.RootModule().Resources { if resourceKey == key { for attrKey, attrValue := range value.Primary.Attributes { if strings.HasPrefix(attrKey, attributePath) { attr := strings.TrimPrefix(attrKey, attributePath+".") - if strings.HasSuffix(attr, "%") { - continue - } - if attr == "#" { attrValueLen, err := strconv.Atoi(attrValue) if err != nil { return fmt.Errorf("failed to convert length of the attribute %s: %s", attrKey, err) } - if len(items) != attrValueLen { - return fmt.Errorf("expected to find %d items in %s, but found %d", len(items), attributePath, attrValueLen) + if len(expectedItems) != attrValueLen { + return fmt.Errorf("expected to find %d items in %s, but found %d", len(expectedItems), attributePath, attrValueLen) + } + + actualItems = make([]map[string]string, attrValueLen) + for i := range actualItems { + actualItems[i] = make(map[string]string) } } + } + } + } + } - attrParts := strings.Split(attr, ".") - _, indexErr := strconv.Atoi(attrParts[0]) + // Gather all actual items + for key, value := range state.RootModule().Resources { + if resourceKey == key { + for attrKey, attrValue := range value.Primary.Attributes { + if strings.HasPrefix(attrKey, attributePath) { + attr := strings.TrimPrefix(attrKey, attributePath+".") + + if strings.HasSuffix(attr, "%") || strings.HasSuffix(attr, "#") { + continue + } + + attrParts := strings.SplitN(attr, ".", 2) + index, indexErr := strconv.Atoi(attrParts[0]) isIndex := indexErr == nil if len(attrParts) > 1 && isIndex { itemKey := attrParts[1] - - found := false - valueEquals := false - - for _, item := range items { - if v, ok := item[itemKey]; ok { - found = true - - if v == attrValue { - valueEquals = true - } - } - } - - if !found { - return fmt.Errorf("%s found in attributes, but was not expected", attrKey) - } else if !valueEquals { - return fmt.Errorf("expected to find subpath %s that is equal to one of the values in %v", attrKey, collections.Map(items, func(item map[string]string) string { - return item[itemKey] - })) - } + actualItems[index][itemKey] = attrValue } } } } } - return nil + + errs := make([]error, 0) + for _, actualItem := range actualItems { + found := false + for _, expectedItem := range expectedItems { + if maps.Equal(actualItem, expectedItem) { + found = true + } + } + if !found { + errs = append(errs, fmt.Errorf("unexpected item found: %s", actualItem)) + } + } + + for _, expectedItem := range expectedItems { + found := false + for _, actualItem := range actualItems { + if maps.Equal(actualItem, expectedItem) { + found = true + } + } + if !found { + errs = append(errs, fmt.Errorf("expected item to be found, but it wasn't: %s", expectedItem)) + } + } + return errors.Join(errs...) } } diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index bbc2f2dd40a..fb8f462e547 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -3,6 +3,7 @@ package provider import ( "errors" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" "net" "net/url" "os" @@ -420,7 +421,7 @@ func Provider() *schema.Provider { } func getResources() map[string]*schema.Resource { - return map[string]*schema.Resource{ + resourceList := map[string]*schema.Resource{ "snowflake_account": resources.Account(), "snowflake_account_role": resources.AccountRole(), "snowflake_account_password_policy_attachment": resources.AccountPasswordPolicyAttachment(), @@ -500,9 +501,13 @@ func getResources() map[string]*schema.Resource { "snowflake_user_public_keys": resources.UserPublicKeys(), "snowflake_view": resources.View(), "snowflake_warehouse": resources.Warehouse(), - // TODO: Remove or comment - "snowflake_object_renaming": resources.ObjectRenamingListsAndSets(), } + + if os.Getenv(string(testenvs.EnableObjectRenamingTest)) != "" { + resourceList["snowflake_object_renaming"] = resources.ObjectRenamingListsAndSets() + } + + return resourceList } func getDataSources() map[string]*schema.Resource { diff --git a/pkg/resources/object_renaming_lists_and_sets_acceptance_test.go b/pkg/resources/object_renaming_lists_and_sets_acceptance_test.go index cb434ff6ff4..18c57defc44 100644 --- a/pkg/resources/object_renaming_lists_and_sets_acceptance_test.go +++ b/pkg/resources/object_renaming_lists_and_sets_acceptance_test.go @@ -14,6 +14,7 @@ import ( func TestAcc_BasicListFlow(t *testing.T) { _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + _ = testenvs.GetOrSkipTest(t, testenvs.EnableObjectRenamingTest) acc.TestAccPreCheck(t) resource.Test(t, resource.TestCase{ @@ -141,6 +142,7 @@ func TestAcc_BasicListFlow(t *testing.T) { // This test researches the possibility of performing update instead of remove + add item func TestAcc_ListNameUpdate(t *testing.T) { _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + _ = testenvs.GetOrSkipTest(t, testenvs.EnableObjectRenamingTest) acc.TestAccPreCheck(t) resource.Test(t, resource.TestCase{ @@ -193,6 +195,7 @@ func TestAcc_ListNameUpdate(t *testing.T) { func TestAcc_ListsWithDuplicatedItems(t *testing.T) { _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + _ = testenvs.GetOrSkipTest(t, testenvs.EnableObjectRenamingTest) acc.TestAccPreCheck(t) // Fails, because the SuppressDiffFunc works on the hash of individual items. @@ -277,6 +280,7 @@ resource "snowflake_object_renaming" "test" { func TestAcc_BasicManuallyOrderedListFlow(t *testing.T) { _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + _ = testenvs.GetOrSkipTest(t, testenvs.EnableObjectRenamingTest) acc.TestAccPreCheck(t) resource.Test(t, resource.TestCase{