Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Clean up old test object helpers #3049

Merged
merged 22 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
812da99
Remove schema and db old integration test helpers
sfc-gh-asawicki Sep 5, 2024
90191b7
Remove warehouse old integration test helpers
sfc-gh-asawicki Sep 5, 2024
27bd54b
Cleanup Client in acceptance tests (WIP)
sfc-gh-asawicki Sep 5, 2024
884d0b5
Fix setup in two unsafe execute tests
sfc-gh-asawicki Sep 6, 2024
46fe9cb
Removed dependency on acceptance in the unit test for custom diffs
sfc-gh-asawicki Sep 6, 2024
fe2daaa
Extract grant/revoke helpers
sfc-gh-asawicki Sep 6, 2024
67652d2
Use client external volume
sfc-gh-asawicki Sep 6, 2024
53d3a02
Add granting ownership to helper
sfc-gh-asawicki Sep 6, 2024
289c4db
Extract external table helper
sfc-gh-asawicki Sep 6, 2024
df6be53
Move replication databases to the helper
sfc-gh-asawicki Sep 6, 2024
644371e
Remove old policy references from helpers
sfc-gh-asawicki Sep 6, 2024
41e9130
Extract showing grants to share to helper
sfc-gh-asawicki Sep 6, 2024
140dba0
Extract showing grants of roles to helpers
sfc-gh-asawicki Sep 6, 2024
43a5665
Extract showing grants to roles to helpers
sfc-gh-asawicki Sep 6, 2024
973ce71
Remove client method from acceptance tests
sfc-gh-asawicki Sep 6, 2024
bb9f507
Use only secondary test client
sfc-gh-asawicki Sep 6, 2024
b6b21ab
Add TODO
sfc-gh-asawicki Sep 6, 2024
ad4b88e
Assign issue number to the TODOs
sfc-gh-asawicki Sep 6, 2024
84a2fbe
Move helpers to a proper client
sfc-gh-asawicki Sep 6, 2024
84e8bec
Move helpers to a proper client
sfc-gh-asawicki Sep 6, 2024
78c14e4
Merge branch 'main' into clean-up-old-test-object-helpers
sfc-gh-asawicki Sep 9, 2024
49c4e27
Fix after review
sfc-gh-asawicki Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func (v *ViewAssert) HasNoProjectionPolicyReferences(client *helpers.TestClient)
func (v *ViewAssert) hasNoPolicyReference(client *helpers.TestClient, kind sdk.PolicyKind) *ViewAssert {
v.AddAssertion(func(t *testing.T, o *sdk.View) error {
t.Helper()
refs, err := client.PolicyReferences.GetPolicyReferences(t, o.ID(), sdk.ObjectTypeView)
refs, err := client.PolicyReferences.GetPolicyReferences(t, o.ID(), sdk.PolicyEntityDomainView)
if err != nil {
return err
}
refs = slices.DeleteFunc(refs, func(reference helpers.PolicyReference) bool {
return reference.PolicyKind != string(kind)
refs = slices.DeleteFunc(refs, func(reference sdk.PolicyReference) bool {
return reference.PolicyKind != kind
})
if len(refs) > 0 {
return fmt.Errorf("expected no %s policy references; got: %v", kind, refs)
Expand Down Expand Up @@ -85,12 +85,12 @@ func (v *ViewAssert) HasProjectionPolicyReferences(client *helpers.TestClient, n
func (v *ViewAssert) hasPolicyReference(client *helpers.TestClient, kind sdk.PolicyKind, n int) *ViewAssert {
v.AddAssertion(func(t *testing.T, o *sdk.View) error {
t.Helper()
refs, err := client.PolicyReferences.GetPolicyReferences(t, o.ID(), sdk.ObjectTypeView)
refs, err := client.PolicyReferences.GetPolicyReferences(t, o.ID(), sdk.PolicyEntityDomainView)
if err != nil {
return err
}
refs = slices.DeleteFunc(refs, func(reference helpers.PolicyReference) bool {
return reference.PolicyKind != string(kind)
refs = slices.DeleteFunc(refs, func(reference sdk.PolicyReference) bool {
return reference.PolicyKind != kind
})
if len(refs) != n {
return fmt.Errorf("expected %d %s policy references; got: %d, %v", n, kind, len(refs), refs)
Expand Down
53 changes: 8 additions & 45 deletions pkg/acceptance/check_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (

func CheckDestroy(t *testing.T, resource resources.Resource) func(*terraform.State) error {
t.Helper()
client := Client(t)
// TODO [SNOW-1653619]: use TestClient() here
client := atc.client
t.Logf("running check destroy for resource %s", resource)

return func(s *terraform.State) error {
Expand Down Expand Up @@ -236,24 +237,18 @@ func asId[T sdk.AccountObjectIdentifier | sdk.DatabaseObjectIdentifier | sdk.Sch
// CheckGrantAccountRoleDestroy is a custom checks that should be later incorporated into generic CheckDestroy
func CheckGrantAccountRoleDestroy(t *testing.T) func(*terraform.State) error {
t.Helper()
client := Client(t)

return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "snowflake_grant_account_role" {
continue
}
ctx := context.Background()
parts := strings.Split(rs.Primary.ID, "|")
roleName := parts[0]
roleIdentifier := sdk.NewAccountObjectIdentifierFromFullyQualifiedName(roleName)
objectType := parts[1]
targetIdentifier := parts[2]
grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{
Of: &sdk.ShowGrantsOf{
Role: roleIdentifier,
},
})
grants, err := TestClient().Grant.ShowGrantsOfAccountRole(t, roleIdentifier)
if err != nil {
return nil
}
Expand All @@ -278,24 +273,18 @@ func CheckGrantAccountRoleDestroy(t *testing.T) func(*terraform.State) error {
// CheckGrantDatabaseRoleDestroy is a custom checks that should be later incorporated into generic CheckDestroy
func CheckGrantDatabaseRoleDestroy(t *testing.T) func(*terraform.State) error {
t.Helper()
client := Client(t)

return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "snowflake_grant_database_role" {
continue
}
ctx := context.Background()
id := rs.Primary.ID
ids := strings.Split(id, "|")
databaseRoleName := ids[0]
objectType := ids[1]
parentRoleName := ids[2]
grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{
Of: &sdk.ShowGrantsOf{
DatabaseRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(databaseRoleName),
},
})
grants, err := TestClient().Grant.ShowGrantsOfDatabaseRole(t, sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(databaseRoleName))
if err != nil {
continue
}
Expand All @@ -314,21 +303,15 @@ func CheckGrantDatabaseRoleDestroy(t *testing.T) func(*terraform.State) error {
// CheckAccountRolePrivilegesRevoked is a custom checks that should be later incorporated into generic CheckDestroy
func CheckAccountRolePrivilegesRevoked(t *testing.T) func(*terraform.State) error {
t.Helper()
client := Client(t)

return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "snowflake_grant_privileges_to_account_role" {
continue
}
ctx := context.Background()

id := sdk.NewAccountObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["account_role_name"])
grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{
To: &sdk.ShowGrantsTo{
Role: id,
},
})
grants, err := TestClient().Grant.ShowGrantsToAccountRole(t, id)
if err != nil {
if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) {
continue
Expand All @@ -350,21 +333,15 @@ func CheckAccountRolePrivilegesRevoked(t *testing.T) func(*terraform.State) erro
// CheckDatabaseRolePrivilegesRevoked is a custom checks that should be later incorporated into generic CheckDestroy
func CheckDatabaseRolePrivilegesRevoked(t *testing.T) func(*terraform.State) error {
t.Helper()
client := Client(t)

return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "snowflake_grant_privileges_to_database_role" {
continue
}
ctx := context.Background()

id := sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["database_role_name"])
grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{
To: &sdk.ShowGrantsTo{
DatabaseRole: id,
},
})
grants, err := TestClient().Grant.ShowGrantsToDatabaseRole(t, id)
if err != nil {
return err
}
Expand All @@ -386,23 +363,15 @@ func CheckDatabaseRolePrivilegesRevoked(t *testing.T) func(*terraform.State) err
// CheckSharePrivilegesRevoked is a custom checks that should be later incorporated into generic CheckDestroy
func CheckSharePrivilegesRevoked(t *testing.T) func(*terraform.State) error {
t.Helper()
client := Client(t)

return func(state *terraform.State) error {
for _, rs := range state.RootModule().Resources {
if rs.Type != "snowflake_grant_privileges_to_share" {
continue
}
ctx := context.Background()

id := sdk.NewExternalObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["to_share"])
grants, err := client.Grants.Show(ctx, &sdk.ShowGrantOptions{
To: &sdk.ShowGrantsTo{
Share: &sdk.ShowGrantsToShare{
Name: sdk.NewAccountObjectIdentifier(id.Name()),
},
},
})
grants, err := TestClient().Grant.ShowGrantsToShare(t, sdk.NewAccountObjectIdentifier(id.Name()))
if err != nil {
return err
}
Expand All @@ -421,18 +390,12 @@ func CheckSharePrivilegesRevoked(t *testing.T) func(*terraform.State) error {
// CheckUserPasswordPolicyAttachmentDestroy is a custom checks that should be later incorporated into generic CheckDestroy
func CheckUserPasswordPolicyAttachmentDestroy(t *testing.T) func(*terraform.State) error {
t.Helper()
client := Client(t)

return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "snowflake_user_password_policy_attachment" {
continue
}
ctx := context.Background()
policyReferences, err := client.PolicyReferences.GetForEntity(ctx, sdk.NewGetForEntityPolicyReferenceRequest(
sdk.NewAccountObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["user_name"]),
sdk.PolicyEntityDomainUser,
))
policyReferences, err := TestClient().PolicyReferences.GetPolicyReferences(t, sdk.NewAccountObjectIdentifierFromFullyQualifiedName(rs.Primary.Attributes["user_name"]), sdk.PolicyEntityDomainUser)
if err != nil {
if strings.Contains(err.Error(), "does not exist or not authorized") {
// Note: this can happen if the Policy Reference or the User has been deleted as well; in this case, ignore the error
Expand Down
18 changes: 0 additions & 18 deletions pkg/acceptance/helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package helpers

import (
"context"
"database/sql"
"fmt"
"log"
"testing"
Expand Down Expand Up @@ -71,20 +70,3 @@ func AssertErrorContainsPartsFunc(t *testing.T, parts []string) resource.ErrorCh
return nil
}
}

type PolicyReference struct {
PolicyDb string `db:"POLICY_DB"`
PolicySchema string `db:"POLICY_SCHEMA"`
PolicyName string `db:"POLICY_NAME"`
PolicyKind string `db:"POLICY_KIND"`
RefDatabaseName string `db:"REF_DATABASE_NAME"`
RefSchemaName string `db:"REF_SCHEMA_NAME"`
RefEntityName string `db:"REF_ENTITY_NAME"`
RefEntityDomain string `db:"REF_ENTITY_DOMAIN"`
RefColumnName sql.NullString `db:"REF_COLUMN_NAME"`
RefArgColumnNames sql.NullString `db:"REF_ARG_COLUMN_NAMES"`
TagDatabase sql.NullString `db:"TAG_DATABASE"`
TagSchema sql.NullString `db:"TAG_SCHEMA"`
TagName sql.NullString `db:"TAG_NAME"`
PolicyStatus string `db:"POLICY_STATUS"`
}
31 changes: 31 additions & 0 deletions pkg/acceptance/helpers/database_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,34 @@ func (c *DatabaseClient) Describe(t *testing.T, id sdk.AccountObjectIdentifier)

return c.client().Describe(ctx, id)
}

// TODO [SNOW-1562172]: Create a better solution for this type of situations
// We have to create test database from share before the actual test to check if the newly created share is ready
// after previous test (there's some kind of issue or delay between cleaning up a share and creating a new one right after).
func (c *DatabaseClient) CreateDatabaseFromShareTemporarily(t *testing.T, externalShareId sdk.ExternalObjectIdentifier) {
t.Helper()

databaseId := c.ids.RandomAccountObjectIdentifier()
err := c.client().CreateShared(context.Background(), databaseId, externalShareId, new(sdk.CreateSharedDatabaseOptions))
require.NoError(t, err)

require.Eventually(t, func() bool {
database, err := c.Show(t, databaseId)
if err != nil {
return false
}
// Origin is returned as "<revoked>" in those cases, because it's not valid sdk.ExternalObjectIdentifier parser sets it as nil.
// Once it turns into valid sdk.ExternalObjectIdentifier, we're ready to proceed with the actual test.
return database.Origin != nil
}, time.Minute, time.Second*6)

err = c.DropDatabase(t, databaseId)
require.NoError(t, err)
}

func (c *DatabaseClient) ShowAllReplicationDatabases(t *testing.T) ([]sdk.ReplicationDatabase, error) {
t.Helper()
ctx := context.Background()

return c.context.client.ReplicationFunctions.ShowReplicationDatabases(ctx, nil)
}
34 changes: 34 additions & 0 deletions pkg/acceptance/helpers/external_table_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package helpers

import (
"context"
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type ExternalTableClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewExternalTableClient(context *TestClientContext, idsGenerator *IdsGenerator) *ExternalTableClient {
return &ExternalTableClient{
context: context,
ids: idsGenerator,
}
}

func (c *ExternalTableClient) client() sdk.ExternalTables {
return c.context.client.ExternalTables
}

func (c *ExternalTableClient) PublishDataToStage(t *testing.T, stageId sdk.SchemaObjectIdentifier, data []byte) {
t.Helper()
ctx := context.Background()

_, err := c.context.client.ExecForTests(ctx, fmt.Sprintf(`copy into @%s/external_tables_test_data/test_data from (select parse_json('%s')) overwrite = true`, stageId.FullyQualifiedName(), string(data)))
require.NoError(t, err)
}
Loading
Loading