Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Oct 22, 2024
1 parent 8c65fc7 commit cb56b6c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
88 changes: 56 additions & 32 deletions pkg/acceptance/bettertestspoc/assert/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...)
}
}
11 changes: 8 additions & 3 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"errors"
"fmt"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit cb56b6c

Please sign in to comment.