From 812da9907f6fc6e537ac62338910dafbb07e6100 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 5 Sep 2024 15:28:42 +0200 Subject: [PATCH 01/21] Remove schema and db old integration test helpers --- pkg/sdk/testint/alerts_integration_test.go | 24 +++++----- ...ient_unsafe_extensions_integration_test.go | 6 +-- .../testint/database_role_integration_test.go | 10 ++--- .../external_tables_integration_test.go | 2 +- .../failover_groups_integration_test.go | 16 +++---- .../testint/file_format_integration_test.go | 6 +-- pkg/sdk/testint/grants_integration_test.go | 44 +++++++++---------- .../masking_policy_integration_test.go | 18 ++++---- ...materialized_views_gen_integration_test.go | 8 ++-- .../password_policy_integration_test.go | 4 +- pkg/sdk/testint/pipes_integration_test.go | 6 +-- .../testint/procedures_integration_test.go | 18 ++++---- pkg/sdk/testint/schemas_integration_test.go | 2 +- pkg/sdk/testint/sessions_integration_test.go | 6 +-- pkg/sdk/testint/setup_test.go | 25 ----------- pkg/sdk/testint/shares_integration_test.go | 18 ++++---- .../testint/streams_gen_integration_test.go | 42 +++++++++--------- pkg/sdk/testint/tables_integration_test.go | 8 ++-- pkg/sdk/testint/tasks_gen_integration_test.go | 12 ++--- pkg/sdk/testint/views_gen_integration_test.go | 8 ++-- 20 files changed, 129 insertions(+), 154 deletions(-) diff --git a/pkg/sdk/testint/alerts_integration_test.go b/pkg/sdk/testint/alerts_integration_test.go index 770e34cdb4..018dd080d8 100644 --- a/pkg/sdk/testint/alerts_integration_test.go +++ b/pkg/sdk/testint/alerts_integration_test.go @@ -30,7 +30,7 @@ func TestInt_AlertsShow(t *testing.T) { t.Run("with show options", func(t *testing.T) { showOptions := &sdk.ShowAlertOptions{ In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, } alerts, err := client.Alerts.Show(ctx, showOptions) @@ -46,7 +46,7 @@ func TestInt_AlertsShow(t *testing.T) { Pattern: sdk.String(alertTest.Name), }, In: &sdk.In{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, } alerts, err := client.Alerts.Show(ctx, showOptions) @@ -69,7 +69,7 @@ func TestInt_AlertsShow(t *testing.T) { t.Run("when limiting the number of results", func(t *testing.T) { showOptions := &sdk.ShowAlertOptions{ In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, Limit: sdk.Int(1), } @@ -110,7 +110,7 @@ func TestInt_AlertCreate(t *testing.T) { Pattern: sdk.String(name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -146,7 +146,7 @@ func TestInt_AlertCreate(t *testing.T) { Pattern: sdk.String(name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -176,7 +176,7 @@ func TestInt_AlertCreate(t *testing.T) { Pattern: sdk.String(name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -214,7 +214,7 @@ func TestInt_AlertCreate(t *testing.T) { Pattern: sdk.String(name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -265,7 +265,7 @@ func TestInt_AlertAlter(t *testing.T) { Pattern: sdk.String(alert.Name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -289,7 +289,7 @@ func TestInt_AlertAlter(t *testing.T) { Pattern: sdk.String(alert.Name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -309,7 +309,7 @@ func TestInt_AlertAlter(t *testing.T) { Pattern: sdk.String(alert.Name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -332,7 +332,7 @@ func TestInt_AlertAlter(t *testing.T) { Pattern: sdk.String(alert.Name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -350,7 +350,7 @@ func TestInt_AlertAlter(t *testing.T) { Pattern: sdk.String(alert.Name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) diff --git a/pkg/sdk/testint/client_unsafe_extensions_integration_test.go b/pkg/sdk/testint/client_unsafe_extensions_integration_test.go index f50cfc6ce6..b9acbb83bb 100644 --- a/pkg/sdk/testint/client_unsafe_extensions_integration_test.go +++ b/pkg/sdk/testint/client_unsafe_extensions_integration_test.go @@ -13,13 +13,13 @@ func TestInt_Client_UnsafeQuery(t *testing.T) { ctx := testContext(t) t.Run("test show databases", func(t *testing.T) { - sql := fmt.Sprintf("SHOW DATABASES LIKE '%%%s%%'", testDb(t).Name) + sql := fmt.Sprintf("SHOW DATABASES LIKE '%%%s%%'", testClientHelper().Ids.DatabaseId().Name()) results, err := client.QueryUnsafe(ctx, sql) require.NoError(t, err) assert.Len(t, results, 1) row := results[0] - assert.Equal(t, testDb(t).Name, *row["name"]) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), *row["name"]) assert.NotEmpty(t, *row["created_on"]) assert.Equal(t, "STANDARD", *row["kind"]) assert.Equal(t, "ACCOUNTADMIN", *row["owner"]) @@ -46,7 +46,7 @@ func TestInt_Client_UnsafeQuery(t *testing.T) { for i, r := range results { names[i] = *r["name"] } - assert.Contains(t, names, testDb(t).Name) + assert.Contains(t, names, testClientHelper().Ids.DatabaseId().Name()) assert.Contains(t, names, db1.Name) assert.Contains(t, names, db2.Name) assert.Contains(t, names, db3.Name) diff --git a/pkg/sdk/testint/database_role_integration_test.go b/pkg/sdk/testint/database_role_integration_test.go index 7ca8a9707b..aa0f716150 100644 --- a/pkg/sdk/testint/database_role_integration_test.go +++ b/pkg/sdk/testint/database_role_integration_test.go @@ -200,7 +200,7 @@ func TestInt_DatabaseRoles(t *testing.T) { role1 := createDatabaseRole(t) role2 := createDatabaseRole(t) - showRequest := sdk.NewShowDatabaseRoleRequest(testDb(t).ID()) + showRequest := sdk.NewShowDatabaseRoleRequest(testClientHelper().Ids.DatabaseId()) returnedDatabaseRoles, err := client.DatabaseRoles.Show(ctx, showRequest) require.NoError(t, err) @@ -213,7 +213,7 @@ func TestInt_DatabaseRoles(t *testing.T) { role1 := createDatabaseRole(t) role2 := createDatabaseRole(t) - showRequest := sdk.NewShowDatabaseRoleRequest(testDb(t).ID()).WithLike(sdk.Like{Pattern: &role1.Name}) + showRequest := sdk.NewShowDatabaseRoleRequest(testClientHelper().Ids.DatabaseId()).WithLike(sdk.Like{Pattern: &role1.Name}) returnedDatabaseRoles, err := client.DatabaseRoles.Show(ctx, showRequest) require.NoError(t, err) @@ -233,7 +233,7 @@ func TestInt_DatabaseRoles(t *testing.T) { role2, cleanupRole2 := testClientHelper().DatabaseRole.CreateDatabaseRoleWithName(t, roleId2) t.Cleanup(cleanupRole2) - showRequest := sdk.NewShowDatabaseRoleRequest(testDb(t).ID()). + showRequest := sdk.NewShowDatabaseRoleRequest(testClientHelper().Ids.DatabaseId()). WithLike(sdk.Like{ Pattern: sdk.Pointer(prefix + "%"), }). @@ -250,7 +250,7 @@ func TestInt_DatabaseRoles(t *testing.T) { }) t.Run("show database_role: no matches", func(t *testing.T) { - showRequest := sdk.NewShowDatabaseRoleRequest(testDb(t).ID()).WithLike(sdk.Like{Pattern: sdk.Pointer("non-existent")}) + showRequest := sdk.NewShowDatabaseRoleRequest(testClientHelper().Ids.DatabaseId()).WithLike(sdk.Like{Pattern: sdk.Pointer("non-existent")}) returnedDatabaseRoles, err := client.DatabaseRoles.Show(ctx, showRequest) require.NoError(t, err) @@ -313,7 +313,7 @@ func TestInt_DatabaseRoles(t *testing.T) { share, shareCleanup := testClientHelper().Share.CreateShare(t) t.Cleanup(shareCleanup) - err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{Database: testDb(t).ID()}, share.ID()) + err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{Database: testClientHelper().Ids.DatabaseId()}, share.ID()) require.NoError(t, err) grantRequest := sdk.NewGrantDatabaseRoleToShareRequest(roleId, share.ID()) diff --git a/pkg/sdk/testint/external_tables_integration_test.go b/pkg/sdk/testint/external_tables_integration_test.go index 81ecb9bbc9..1a170eafbd 100644 --- a/pkg/sdk/testint/external_tables_integration_test.go +++ b/pkg/sdk/testint/external_tables_integration_test.go @@ -351,7 +351,7 @@ func TestInt_ExternalTables(t *testing.T) { sdk.NewShowExternalTableRequest(). WithTerse(true). WithLike(name). - WithIn(*sdk.NewShowExternalTableInRequest().WithDatabase(testDb(t).ID())). + WithIn(*sdk.NewShowExternalTableInRequest().WithDatabase(testClientHelper().Ids.DatabaseId())). WithStartsWith(name). WithLimitFrom(*sdk.NewLimitFromRequest().WithRows(sdk.Int(1))), ) diff --git a/pkg/sdk/testint/failover_groups_integration_test.go b/pkg/sdk/testint/failover_groups_integration_test.go index 521ddec23b..554a6c73fc 100644 --- a/pkg/sdk/testint/failover_groups_integration_test.go +++ b/pkg/sdk/testint/failover_groups_integration_test.go @@ -38,7 +38,7 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { err := client.FailoverGroups.Create(ctx, id, objectTypes, allowedAccounts, &sdk.CreateFailoverGroupOptions{ IfNotExists: sdk.Bool(true), AllowedDatabases: []sdk.AccountObjectIdentifier{ - testDb(t).ID(), + testClientHelper().Ids.DatabaseId(), }, AllowedShares: []sdk.AccountObjectIdentifier{ shareTest.ID(), @@ -69,7 +69,7 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { fgDBS, err := client.FailoverGroups.ShowDatabases(ctx, id) require.NoError(t, err) assert.Equal(t, 1, len(fgDBS)) - assert.Equal(t, testDb(t).ID().Name(), fgDBS[0].Name()) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), fgDBS[0].Name()) fgShares, err := client.FailoverGroups.ShowShares(ctx, id) require.NoError(t, err) @@ -169,7 +169,7 @@ func TestInt_Issue2544(t *testing.T) { replicationSchedule := "10 MINUTE" err := client.FailoverGroups.Create(ctx, id, objectTypes, allowedAccounts, &sdk.CreateFailoverGroupOptions{ AllowedDatabases: []sdk.AccountObjectIdentifier{ - testDb(t).ID(), + testClientHelper().Ids.DatabaseId(), }, AllowedIntegrationTypes: allowedIntegrationTypes, ReplicationSchedule: sdk.String(replicationSchedule), @@ -390,7 +390,7 @@ func TestInt_FailoverGroupsAlterSource(t *testing.T) { opts = &sdk.AlterSourceFailoverGroupOptions{ Add: &sdk.FailoverGroupAdd{ AllowedDatabases: []sdk.AccountObjectIdentifier{ - testDb(t).ID(), + testClientHelper().Ids.DatabaseId(), }, }, } @@ -399,13 +399,13 @@ func TestInt_FailoverGroupsAlterSource(t *testing.T) { allowedDBs, err := client.FailoverGroups.ShowDatabases(ctx, failoverGroup.ID()) require.NoError(t, err) assert.Equal(t, 1, len(allowedDBs)) - assert.Equal(t, testDb(t).ID().Name(), allowedDBs[0].Name()) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), allowedDBs[0].Name()) // now remove database from allowed databases opts = &sdk.AlterSourceFailoverGroupOptions{ Remove: &sdk.FailoverGroupRemove{ AllowedDatabases: []sdk.AccountObjectIdentifier{ - testDb(t).ID(), + testClientHelper().Ids.DatabaseId(), }, }, } @@ -867,7 +867,7 @@ func TestInt_FailoverGroupsShowDatabases(t *testing.T) { opts = &sdk.AlterSourceFailoverGroupOptions{ Add: &sdk.FailoverGroupAdd{ AllowedDatabases: []sdk.AccountObjectIdentifier{ - testDb(t).ID(), + testClientHelper().Ids.DatabaseId(), }, }, } @@ -876,7 +876,7 @@ func TestInt_FailoverGroupsShowDatabases(t *testing.T) { databases, err := client.FailoverGroups.ShowDatabases(ctx, failoverGroupTest.ID()) require.NoError(t, err) assert.Equal(t, 1, len(databases)) - assert.Equal(t, testDb(t).ID(), databases[0]) + assert.Equal(t, testClientHelper().Ids.DatabaseId(), databases[0]) } func TestInt_FailoverGroupsShowShares(t *testing.T) { diff --git a/pkg/sdk/testint/file_format_integration_test.go b/pkg/sdk/testint/file_format_integration_test.go index 8044c4cabf..8aa2d7220f 100644 --- a/pkg/sdk/testint/file_format_integration_test.go +++ b/pkg/sdk/testint/file_format_integration_test.go @@ -489,7 +489,7 @@ func TestInt_FileFormatsShow(t *testing.T) { t.Run("IN", func(t *testing.T) { fileFormats, err := client.FileFormats.Show(ctx, &sdk.ShowFileFormatsOptions{ In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -520,8 +520,8 @@ func TestInt_FileFormatsShowById(t *testing.T) { fileFormat, err := client.FileFormats.ShowByID(ctx, fileFormatTest.ID()) require.NoError(t, err) - assert.Equal(t, testDb(t).Name, fileFormat.Name.DatabaseName()) - assert.Equal(t, testSchema(t).Name, fileFormat.Name.SchemaName()) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), fileFormat.Name.DatabaseName()) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), fileFormat.Name.SchemaName()) assert.Equal(t, fileFormatTest.Name.Name(), fileFormat.Name.Name()) }) } diff --git a/pkg/sdk/testint/grants_integration_test.go b/pkg/sdk/testint/grants_integration_test.go index 429e8cfc63..e0fe30a433 100644 --- a/pkg/sdk/testint/grants_integration_test.go +++ b/pkg/sdk/testint/grants_integration_test.go @@ -129,7 +129,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { } on := &sdk.AccountRoleGrantOn{ Schema: &sdk.GrantOnSchema{ - Schema: sdk.Pointer(testSchema(t).ID()), + Schema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, } err := client.Grants.GrantPrivilegesToAccountRole(ctx, privileges, on, roleTest.ID(), nil) @@ -167,7 +167,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { SchemaObject: &sdk.GrantOnSchemaObject{ All: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeTables, - InSchema: sdk.Pointer(testSchema(t).ID()), + InSchema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, }, } @@ -253,7 +253,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { SchemaObject: &sdk.GrantOnSchemaObject{ All: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeCortexSearchServices, - InSchema: sdk.Pointer(testSchema(t).ID()), + InSchema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, }, } @@ -295,7 +295,7 @@ func TestInt_GrantAndRevokePrivilegesToAccountRole(t *testing.T) { SchemaObject: &sdk.GrantOnSchemaObject{ Future: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeExternalTables, - InDatabase: sdk.Pointer(testDb(t).ID()), + InDatabase: sdk.Pointer(testClientHelper().Ids.DatabaseId()), }, }, } @@ -480,7 +480,7 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { DatabasePrivileges: []sdk.AccountObjectPrivilege{sdk.AccountObjectPrivilegeCreateSchema}, } on := &sdk.DatabaseRoleGrantOn{ - Database: sdk.Pointer(testDb(t).ID()), + Database: sdk.Pointer(testClientHelper().Ids.DatabaseId()), } err := client.Grants.GrantPrivilegesToDatabaseRole(ctx, privileges, on, databaseRoleId, nil) @@ -529,7 +529,7 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { } on := &sdk.DatabaseRoleGrantOn{ Schema: &sdk.GrantOnSchema{ - Schema: sdk.Pointer(testSchema(t).ID()), + Schema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, } @@ -582,7 +582,7 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { SchemaObject: &sdk.GrantOnSchemaObject{ All: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeTables, - InSchema: sdk.Pointer(testSchema(t).ID()), + InSchema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, }, } @@ -688,7 +688,7 @@ func TestInt_GrantAndRevokePrivilegesToDatabaseRole(t *testing.T) { SchemaObject: &sdk.GrantOnSchemaObject{ Future: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeExternalTables, - InDatabase: sdk.Pointer(testDb(t).ID()), + InDatabase: sdk.Pointer(testClientHelper().Ids.DatabaseId()), }, }, } @@ -860,13 +860,13 @@ func TestInt_GrantPrivilegeToShare(t *testing.T) { grantShareOnDatabase := func(t *testing.T, share *sdk.Share) { t.Helper() err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, share.ID()) require.NoError(t, err) t.Cleanup(func() { err := client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, share.ID()) assert.NoError(t, err) }) @@ -879,7 +879,7 @@ func TestInt_GrantPrivilegeToShare(t *testing.T) { err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeSelect}, &sdk.ShareGrantOn{ Table: &sdk.OnTable{ - AllInSchema: testSchema(t).ID(), + AllInSchema: testClientHelper().Ids.SchemaId(), }, }, shareTest.ID()) require.NoError(t, err) @@ -948,7 +948,7 @@ func TestInt_GrantPrivilegeToShare(t *testing.T) { err = client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeSelect}, &sdk.ShareGrantOn{ Table: &sdk.OnTable{ - AllInSchema: testSchema(t).ID(), + AllInSchema: testClientHelper().Ids.SchemaId(), }, }, shareTest.ID()) require.NoError(t, err) @@ -963,7 +963,7 @@ func TestInt_GrantPrivilegeToShare(t *testing.T) { err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeSelect}, &sdk.ShareGrantOn{ Table: &sdk.OnTable{ - AllInSchema: testSchema(t).ID(), + AllInSchema: testClientHelper().Ids.SchemaId(), }, }, shareTest.ID()) require.NoError(t, err) @@ -996,7 +996,7 @@ func TestInt_RevokePrivilegeToShare(t *testing.T) { shareTest, shareCleanup := testClientHelper().Share.CreateShare(t) t.Cleanup(shareCleanup) err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) t.Run("without options", func(t *testing.T) { @@ -1005,7 +1005,7 @@ func TestInt_RevokePrivilegeToShare(t *testing.T) { }) t.Run("with options", func(t *testing.T) { err = client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) }) @@ -1264,7 +1264,7 @@ func TestInt_GrantOwnership(t *testing.T) { on := sdk.OwnershipGrantOn{ Future: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeExternalTables, - InDatabase: sdk.Pointer(testDb(t).ID()), + InDatabase: sdk.Pointer(testClientHelper().Ids.DatabaseId()), }, } to := sdk.OwnershipGrantTo{ @@ -1623,7 +1623,7 @@ func TestInt_GrantOwnership(t *testing.T) { onAllPipesInSchema := sdk.OwnershipGrantOn{ All: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypePipes, - InSchema: sdk.Pointer(testSchema(t).ID()), + InSchema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, } err = client.Grants.GrantOwnership( @@ -1827,7 +1827,7 @@ func TestInt_GrantOwnership(t *testing.T) { onAllTasks := sdk.OwnershipGrantOn{ All: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeTasks, - InSchema: sdk.Pointer(testSchema(t).ID()), + InSchema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, } err = client.Grants.GrantOwnership( @@ -1944,7 +1944,7 @@ func TestInt_GrantOwnership(t *testing.T) { onAllTasks := sdk.OwnershipGrantOn{ All: &sdk.GrantOnSchemaObjectIn{ PluralObjectType: sdk.PluralObjectTypeTasks, - InSchema: sdk.Pointer(testSchema(t).ID()), + InSchema: sdk.Pointer(testClientHelper().Ids.SchemaId()), }, } err = client.Grants.GrantOwnership( @@ -1984,12 +1984,12 @@ func TestInt_ShowGrants(t *testing.T) { t.Cleanup(shareCleanup) err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { err = client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) }) @@ -2004,7 +2004,7 @@ func TestInt_ShowGrants(t *testing.T) { On: &sdk.ShowGrantsOn{ Object: &sdk.Object{ ObjectType: sdk.ObjectTypeDatabase, - Name: testDb(t).ID(), + Name: testClientHelper().Ids.DatabaseId(), }, }, }) diff --git a/pkg/sdk/testint/masking_policy_integration_test.go b/pkg/sdk/testint/masking_policy_integration_test.go index 75c9d705ee..85aa2b6505 100644 --- a/pkg/sdk/testint/masking_policy_integration_test.go +++ b/pkg/sdk/testint/masking_policy_integration_test.go @@ -30,7 +30,7 @@ func TestInt_MaskingPoliciesShow(t *testing.T) { t.Run("with show options", func(t *testing.T) { showOptions := &sdk.ShowMaskingPolicyOptions{ In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, } maskingPolicies, err := client.MaskingPolicies.Show(ctx, showOptions) @@ -46,7 +46,7 @@ func TestInt_MaskingPoliciesShow(t *testing.T) { Pattern: sdk.String(maskingPolicyTest.Name), }, In: &sdk.In{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, } maskingPolicies, err := client.MaskingPolicies.Show(ctx, showOptions) @@ -121,7 +121,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { Pattern: sdk.String(name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -165,7 +165,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { Pattern: sdk.String(name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -199,7 +199,7 @@ func TestInt_MaskingPolicyCreate(t *testing.T) { Pattern: sdk.String(name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -278,7 +278,7 @@ func TestInt_MaskingPolicyAlter(t *testing.T) { Pattern: sdk.String(maskingPolicy.Name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -299,7 +299,7 @@ func TestInt_MaskingPolicyAlter(t *testing.T) { Pattern: sdk.String(maskingPolicy.Name), }, In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, }) require.NoError(t, err) @@ -418,8 +418,8 @@ func TestInt_MaskingPoliciesShowByID(t *testing.T) { assert.Equal(t, id, mp.ID()) assert.NotEmpty(t, mp.CreatedOn) assert.Equal(t, id.Name(), mp.Name) - assert.Equal(t, testDb(t).Name, mp.DatabaseName) - assert.Equal(t, testSchema(t).Name, mp.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), mp.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), mp.SchemaName) assert.Equal(t, "MASKING_POLICY", mp.Kind) assert.Equal(t, "ACCOUNTADMIN", mp.Owner) assert.Equal(t, "", mp.Comment) diff --git a/pkg/sdk/testint/materialized_views_gen_integration_test.go b/pkg/sdk/testint/materialized_views_gen_integration_test.go index 6f17a07cd2..05df49de67 100644 --- a/pkg/sdk/testint/materialized_views_gen_integration_test.go +++ b/pkg/sdk/testint/materialized_views_gen_integration_test.go @@ -26,13 +26,13 @@ func TestInt_MaterializedViews(t *testing.T) { assert.NotEmpty(t, view.CreatedOn) assert.Equal(t, id.Name(), view.Name) assert.Empty(t, view.Reserved) - assert.Equal(t, testDb(t).Name, view.DatabaseName) - assert.Equal(t, testSchema(t).Name, view.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), view.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), view.SchemaName) assert.Equal(t, clusterBy, view.ClusterBy) assert.Equal(t, 0, view.Rows) assert.Equal(t, 0, view.Bytes) - assert.Equal(t, testDb(t).Name, view.SourceDatabaseName) - assert.Equal(t, testSchema(t).Name, view.SourceSchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), view.SourceDatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), view.SourceSchemaName) assert.Equal(t, table.Name, view.SourceTableName) assert.NotEmpty(t, view.RefreshedOn) assert.NotEmpty(t, view.CompactedOn) diff --git a/pkg/sdk/testint/password_policy_integration_test.go b/pkg/sdk/testint/password_policy_integration_test.go index eb21eb8617..62f3ddcd4c 100644 --- a/pkg/sdk/testint/password_policy_integration_test.go +++ b/pkg/sdk/testint/password_policy_integration_test.go @@ -28,7 +28,7 @@ func TestInt_PasswordPoliciesShow(t *testing.T) { t.Run("with show options", func(t *testing.T) { showOptions := &sdk.ShowPasswordPolicyOptions{ In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, } passwordPolicies, err := client.PasswordPolicies.Show(ctx, showOptions) @@ -44,7 +44,7 @@ func TestInt_PasswordPoliciesShow(t *testing.T) { Pattern: sdk.String(passwordPolicyTest.Name), }, In: &sdk.In{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, } passwordPolicies, err := client.PasswordPolicies.Show(ctx, showOptions) diff --git a/pkg/sdk/testint/pipes_integration_test.go b/pkg/sdk/testint/pipes_integration_test.go index 145ff5e099..8ccfed7d1d 100644 --- a/pkg/sdk/testint/pipes_integration_test.go +++ b/pkg/sdk/testint/pipes_integration_test.go @@ -95,7 +95,7 @@ func TestInt_PipesShowAndDescribe(t *testing.T) { t.Run("show: in schema", func(t *testing.T) { showOptions := &sdk.ShowPipeOptions{ In: &sdk.In{ - Schema: testSchema(t).ID(), + Schema: testClientHelper().Ids.SchemaId(), }, } pipes, err := itc.client.Pipes.Show(itc.ctx, showOptions) @@ -157,8 +157,8 @@ func TestInt_PipeCreate(t *testing.T) { t.Helper() assert.NotEmpty(t, pipeDetails.CreatedOn) assert.Equal(t, expectedName, pipeDetails.Name) - assert.Equal(t, testDb(t).Name, pipeDetails.DatabaseName) - assert.Equal(t, testSchema(t).Name, pipeDetails.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), pipeDetails.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), pipeDetails.SchemaName) assert.Equal(t, copyStatement, pipeDetails.Definition) assert.Equal(t, "ACCOUNTADMIN", pipeDetails.Owner) assert.Empty(t, pipeDetails.NotificationChannel) diff --git a/pkg/sdk/testint/procedures_integration_test.go b/pkg/sdk/testint/procedures_integration_test.go index bd98da65a7..ad5bab732e 100644 --- a/pkg/sdk/testint/procedures_integration_test.go +++ b/pkg/sdk/testint/procedures_integration_test.go @@ -564,7 +564,7 @@ func TestInt_CallProcedure(t *testing.T) { client := testClient(t) ctx := testContext(t) - databaseTest, schemaTest := testDb(t), testSchema(t) + databaseId, schemaId := testClientHelper().Ids.DatabaseId(), testClientHelper().Ids.SchemaId() cleanupProcedureHandle := func(id sdk.SchemaObjectIdentifierWithArguments) func() { return func() { err := client.Procedures.Drop(ctx, sdk.NewDropProcedureRequest(id)) @@ -589,7 +589,7 @@ func TestInt_CallProcedure(t *testing.T) { } // create a employees table - tid := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, "employees") + tid := sdk.NewSchemaObjectIdentifier(databaseId.Name(), schemaId.Name(), "employees") createTableHandle(t, tid) createProcedureForSQLHandle := func(t *testing.T, cleanup bool) *sdk.Procedure { @@ -633,7 +633,7 @@ func TestInt_CallProcedure(t *testing.T) { t.Run("call procedure for Java: returns table", func(t *testing.T) { // https://docs.snowflake.com/en/developer-guide/stored-procedure/stored-procedures-java#omitting-return-column-names-and-types name := "filter_by_role" - id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, name, sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseId.Name(), schemaId.Name(), name, sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR) definition := ` import com.snowflake.snowpark_java.*; @@ -668,7 +668,7 @@ func TestInt_CallProcedure(t *testing.T) { t.Run("call procedure for Scala: returns table", func(t *testing.T) { // https://docs.snowflake.com/en/developer-guide/stored-procedure/stored-procedures-scala#omitting-return-column-names-and-types name := "filter_by_role" - id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, name, sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseId.Name(), schemaId.Name(), name, sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR) definition := ` import com.snowflake.snowpark.functions._ @@ -702,7 +702,7 @@ func TestInt_CallProcedure(t *testing.T) { t.Run("call procedure for Javascript", func(t *testing.T) { // https://docs.snowflake.com/en/developer-guide/stored-procedure/stored-procedures-javascript#basic-examples name := "stproc1" - id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, name, sdk.DataTypeFloat) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseId.Name(), schemaId.Name(), name, sdk.DataTypeFloat) definition := ` var sql_command = "INSERT INTO stproc_test_table1 (num_col1) VALUES (" + FLOAT_PARAM1 + ")"; @@ -732,7 +732,7 @@ func TestInt_CallProcedure(t *testing.T) { t.Run("call procedure for Javascript: no arguments", func(t *testing.T) { // https://docs.snowflake.com/en/developer-guide/stored-procedure/stored-procedures-javascript#basic-examples name := "sp_pi" - id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, name) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseId.Name(), schemaId.Name(), name) definition := `return 3.1415926;` request := sdk.NewCreateForJavaScriptProcedureRequest(id.SchemaObjectId(), sdk.DataTypeFloat, definition).WithNotNull(true).WithOrReplace(true) @@ -746,7 +746,7 @@ func TestInt_CallProcedure(t *testing.T) { t.Run("call procedure for Python: returns table", func(t *testing.T) { // https://docs.snowflake.com/en/developer-guide/stored-procedure/stored-procedures-python#omitting-return-column-names-and-types - id := sdk.NewSchemaObjectIdentifierWithArguments(databaseTest.Name, schemaTest.Name, "filterByRole", sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR) + id := sdk.NewSchemaObjectIdentifierWithArguments(databaseId.Name(), schemaId.Name(), "filterByRole", sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR) definition := ` from snowflake.snowpark.functions import col @@ -776,7 +776,7 @@ func TestInt_CreateAndCallProcedures(t *testing.T) { client := testClient(t) ctx := testContext(t) - databaseTest, schemaTest := testDb(t), testSchema(t) + databaseId, schemaId := testClientHelper().Ids.DatabaseId(), testClientHelper().Ids.SchemaId() createTableHandle := func(t *testing.T, table sdk.SchemaObjectIdentifier) { t.Helper() @@ -791,7 +791,7 @@ func TestInt_CreateAndCallProcedures(t *testing.T) { } // create a employees table - tid := sdk.NewSchemaObjectIdentifier(databaseTest.Name, schemaTest.Name, "employees") + tid := sdk.NewSchemaObjectIdentifier(databaseId.Name(), schemaId.Name(), "employees") createTableHandle(t, tid) t.Run("create and call procedure for Java: returns table", func(t *testing.T) { diff --git a/pkg/sdk/testint/schemas_integration_test.go b/pkg/sdk/testint/schemas_integration_test.go index 7d3116b138..151088c943 100644 --- a/pkg/sdk/testint/schemas_integration_test.go +++ b/pkg/sdk/testint/schemas_integration_test.go @@ -256,7 +256,7 @@ func TestInt_Schemas(t *testing.T) { // new schema created on purpose schema, _ := testClientHelper().Schema.CreateSchema(t) t.Cleanup(func() { - err := client.Sessions.UseSchema(ctx, testSchema(t).ID()) + err := client.Sessions.UseSchema(ctx, testClientHelper().Ids.SchemaId()) require.NoError(t, err) }) newID := testClientHelper().Ids.RandomDatabaseObjectIdentifier() diff --git a/pkg/sdk/testint/sessions_integration_test.go b/pkg/sdk/testint/sessions_integration_test.go index 8675a3bc40..da4efc3f43 100644 --- a/pkg/sdk/testint/sessions_integration_test.go +++ b/pkg/sdk/testint/sessions_integration_test.go @@ -80,7 +80,7 @@ func TestInt_ShowSessionParameter(t *testing.T) { func TestInt_ShowObjectParameter(t *testing.T) { client := testClient(t) ctx := testContext(t) - parameter, err := client.Parameters.ShowObjectParameter(ctx, sdk.ObjectParameterDataRetentionTimeInDays, sdk.Object{ObjectType: testDb(t).ObjectType(), Name: testDb(t).ID()}) + parameter, err := client.Parameters.ShowObjectParameter(ctx, sdk.ObjectParameterDataRetentionTimeInDays, sdk.Object{ObjectType: sdk.ObjectTypeDatabase, Name: testClientHelper().Ids.DatabaseId()}) require.NoError(t, err) assert.NotEmpty(t, parameter) } @@ -118,7 +118,7 @@ func TestInt_UseDatabase(t *testing.T) { ctx := testContext(t) t.Cleanup(func() { - err := client.Sessions.UseSchema(ctx, testSchema(t).ID()) + err := client.Sessions.UseSchema(ctx, testClientHelper().Ids.SchemaId()) require.NoError(t, err) }) // new database created on purpose @@ -137,7 +137,7 @@ func TestInt_UseSchema(t *testing.T) { ctx := testContext(t) t.Cleanup(func() { - err := client.Sessions.UseSchema(ctx, testSchema(t).ID()) + err := client.Sessions.UseSchema(ctx, testClientHelper().Ids.SchemaId()) require.NoError(t, err) }) // new database and schema created on purpose diff --git a/pkg/sdk/testint/setup_test.go b/pkg/sdk/testint/setup_test.go index 9cfa0abc7b..8a1caa2085 100644 --- a/pkg/sdk/testint/setup_test.go +++ b/pkg/sdk/testint/setup_test.go @@ -270,16 +270,6 @@ func testContext(t *testing.T) context.Context { return itc.ctx } -func testDb(t *testing.T) *sdk.Database { - t.Helper() - return itc.database -} - -func testSchema(t *testing.T) *sdk.Schema { - t.Helper() - return itc.schema -} - func testWarehouse(t *testing.T) *sdk.Warehouse { t.Helper() return itc.warehouse @@ -295,21 +285,6 @@ func testSecondaryContext(t *testing.T) context.Context { return itc.secondaryCtx } -func testSecondaryDb(t *testing.T) *sdk.Database { - t.Helper() - return itc.secondaryDatabase -} - -func testSecondarySchema(t *testing.T) *sdk.Schema { - t.Helper() - return itc.secondarySchema -} - -func testSecondaryWarehouse(t *testing.T) *sdk.Warehouse { - t.Helper() - return itc.secondaryWarehouse -} - func testConfig(t *testing.T) *gosnowflake.Config { t.Helper() return itc.config diff --git a/pkg/sdk/testint/shares_integration_test.go b/pkg/sdk/testint/shares_integration_test.go index 4b4f282f68..ad54a6102a 100644 --- a/pkg/sdk/testint/shares_integration_test.go +++ b/pkg/sdk/testint/shares_integration_test.go @@ -128,12 +128,12 @@ func TestInt_SharesAlter(t *testing.T) { shareTest, shareCleanup := testClientHelper().Share.CreateShare(t) t.Cleanup(shareCleanup) err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { err = client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) }) require.NoError(t, err) @@ -226,12 +226,12 @@ func TestInt_SharesAlter(t *testing.T) { t.Cleanup(shareCleanup) err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { err = client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) }) @@ -281,12 +281,12 @@ func TestInt_SharesAlter(t *testing.T) { shareTest, shareCleanup := testClientHelper().Share.CreateShare(t) t.Cleanup(shareCleanup) err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { err = client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) }) @@ -342,12 +342,12 @@ func TestInt_ShareDescribeProvider(t *testing.T) { t.Cleanup(shareCleanup) err := client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { err = client.Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: testDb(t).ID(), + Database: testClientHelper().Ids.DatabaseId(), }, shareTest.ID()) require.NoError(t, err) }) @@ -358,7 +358,7 @@ func TestInt_ShareDescribeProvider(t *testing.T) { assert.Equal(t, 1, len(shareDetails.SharedObjects)) sharedObject := shareDetails.SharedObjects[0] assert.Equal(t, sdk.ObjectTypeDatabase, sharedObject.Kind) - assert.Equal(t, testDb(t).ID(), sharedObject.Name) + assert.Equal(t, testClientHelper().Ids.DatabaseId(), sharedObject.Name) }) } diff --git a/pkg/sdk/testint/streams_gen_integration_test.go b/pkg/sdk/testint/streams_gen_integration_test.go index cf6c4d9705..29fa5fd243 100644 --- a/pkg/sdk/testint/streams_gen_integration_test.go +++ b/pkg/sdk/testint/streams_gen_integration_test.go @@ -15,23 +15,23 @@ func TestInt_Streams(t *testing.T) { client := testClient(t) ctx := testContext(t) - db := testDb(t) - schema := testSchema(t) + databaseId := testClientHelper().Ids.DatabaseId() + schemaId := testClientHelper().Ids.SchemaId() assertStream := func(t *testing.T, s *sdk.Stream, id sdk.SchemaObjectIdentifier, sourceType string, mode string) { t.Helper() assert.NotNil(t, s) assert.Nil(t, s.TableOn) assert.Equal(t, id.Name(), s.Name) - assert.Equal(t, db.Name, s.DatabaseName) - assert.Equal(t, schema.Name, s.SchemaName) + assert.Equal(t, databaseId.Name(), s.DatabaseName) + assert.Equal(t, schemaId.Name(), s.SchemaName) assert.Equal(t, "some comment", *s.Comment) assert.Equal(t, sourceType, *s.SourceType) assert.Equal(t, mode, *s.Mode) } t.Run("CreateOnTable", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -102,7 +102,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("CreateOnView", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) view, cleanupView := testClientHelper().View.CreateView(t, fmt.Sprintf("SELECT id FROM %s", table.ID().FullyQualifiedName())) @@ -124,7 +124,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Clone", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -153,7 +153,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Alter tags", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -194,7 +194,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Alter comment", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -229,7 +229,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Drop", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -248,7 +248,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show terse", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -268,8 +268,8 @@ func TestInt_Streams(t *testing.T) { assert.NotNil(t, stream) assert.Equal(t, id.Name(), stream.Name) - assert.Equal(t, db.Name, stream.DatabaseName) - assert.Equal(t, schema.Name, stream.SchemaName) + assert.Equal(t, databaseId.Name(), stream.DatabaseName) + assert.Equal(t, schemaId.Name(), stream.SchemaName) assert.Equal(t, table.Name, *stream.TableOn) assert.Nil(t, stream.Comment) assert.Nil(t, stream.SourceType) @@ -277,7 +277,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show single with options", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -292,7 +292,7 @@ func TestInt_Streams(t *testing.T) { s, err := client.Streams.Show(ctx, sdk.NewShowStreamRequest(). WithTerse(sdk.Bool(false)). WithIn(&sdk.In{ - Schema: schema.ID(), + Schema: schemaId, }). WithLike(&sdk.Like{ Pattern: sdk.String(id.Name()), @@ -311,7 +311,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show multiple", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -347,7 +347,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Show multiple with options", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) idPrefix := "stream_show_" @@ -373,7 +373,7 @@ func TestInt_Streams(t *testing.T) { s, err := client.Streams.Show(ctx, sdk.NewShowStreamRequest(). WithTerse(sdk.Bool(false)). WithIn(&sdk.In{ - Schema: schema.ID(), + Schema: schemaId, }). WithStartsWith(sdk.String(idPrefix)). WithLimit(&sdk.LimitFrom{ @@ -393,7 +393,7 @@ func TestInt_Streams(t *testing.T) { }) t.Run("Describe", func(t *testing.T) { - table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schema.ID()) + table, cleanupTable := testClientHelper().Table.CreateTableInSchema(t, schemaId) t.Cleanup(cleanupTable) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() @@ -410,8 +410,8 @@ func TestInt_Streams(t *testing.T) { assert.NotNil(t, s) assert.Equal(t, id.Name(), s.Name) - assert.Equal(t, db.Name, s.DatabaseName) - assert.Equal(t, schema.Name, s.SchemaName) + assert.Equal(t, databaseId.Name(), s.DatabaseName) + assert.Equal(t, schemaId.Name(), s.SchemaName) assert.Nil(t, s.TableOn) assert.Equal(t, "some comment", *s.Comment) // TODO [SNOW-1348112]: make nicer during the stream rework diff --git a/pkg/sdk/testint/tables_integration_test.go b/pkg/sdk/testint/tables_integration_test.go index 8c6898dda4..aaedb0a307 100644 --- a/pkg/sdk/testint/tables_integration_test.go +++ b/pkg/sdk/testint/tables_integration_test.go @@ -54,8 +54,8 @@ func TestInt_Table(t *testing.T) { assert.Equal(t, id, table.ID()) assert.NotEmpty(t, table.CreatedOn) assert.Equal(t, id.Name(), table.Name) - assert.Equal(t, testDb(t).Name, table.DatabaseName) - assert.Equal(t, testSchema(t).Name, table.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), table.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), table.SchemaName) assert.Equal(t, "TABLE", table.Kind) assert.Equal(t, 0, table.Rows) assert.Equal(t, "ACCOUNTADMIN", table.Owner) @@ -67,8 +67,8 @@ func TestInt_Table(t *testing.T) { assert.Equal(t, id, table.ID()) assert.NotEmpty(t, table.CreatedOn) assert.Equal(t, id.Name(), table.Name) - assert.Equal(t, testDb(t).Name, table.DatabaseName) - assert.Equal(t, testSchema(t).Name, table.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), table.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), table.SchemaName) assert.Equal(t, "TABLE", table.Kind) assert.Empty(t, table.Rows) assert.Empty(t, table.Owner) diff --git a/pkg/sdk/testint/tasks_gen_integration_test.go b/pkg/sdk/testint/tasks_gen_integration_test.go index bb336de0cf..0a470e0c66 100644 --- a/pkg/sdk/testint/tasks_gen_integration_test.go +++ b/pkg/sdk/testint/tasks_gen_integration_test.go @@ -21,8 +21,8 @@ func TestInt_Tasks(t *testing.T) { assert.NotEmpty(t, task.CreatedOn) assert.Equal(t, id.Name(), task.Name) assert.NotEmpty(t, task.Id) - assert.Equal(t, testDb(t).Name, task.DatabaseName) - assert.Equal(t, testSchema(t).Name, task.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), task.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), task.SchemaName) assert.Equal(t, "ACCOUNTADMIN", task.Owner) assert.Equal(t, "", task.Comment) assert.Equal(t, "", task.Warehouse) @@ -46,8 +46,8 @@ func TestInt_Tasks(t *testing.T) { assert.NotEmpty(t, task.CreatedOn) assert.Equal(t, id.Name(), task.Name) assert.NotEmpty(t, task.Id) - assert.Equal(t, testDb(t).Name, task.DatabaseName) - assert.Equal(t, testSchema(t).Name, task.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), task.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), task.SchemaName) assert.Equal(t, "ACCOUNTADMIN", task.Owner) assert.Equal(t, comment, task.Comment) assert.Equal(t, warehouse, task.Warehouse) @@ -75,8 +75,8 @@ func TestInt_Tasks(t *testing.T) { assert.Equal(t, id, task.ID()) assert.NotEmpty(t, task.CreatedOn) assert.Equal(t, id.Name(), task.Name) - assert.Equal(t, testDb(t).Name, task.DatabaseName) - assert.Equal(t, testSchema(t).Name, task.SchemaName) + assert.Equal(t, testClientHelper().Ids.DatabaseId().Name(), task.DatabaseName) + assert.Equal(t, testClientHelper().Ids.SchemaId().Name(), task.SchemaName) assert.Equal(t, schedule, task.Schedule) // all below are not contained in the terse response, that's why all of them we expect to be empty diff --git a/pkg/sdk/testint/views_gen_integration_test.go b/pkg/sdk/testint/views_gen_integration_test.go index ff23d78ce1..eee3623a07 100644 --- a/pkg/sdk/testint/views_gen_integration_test.go +++ b/pkg/sdk/testint/views_gen_integration_test.go @@ -34,8 +34,8 @@ func TestInt_Views(t *testing.T) { HasName(id.Name()). HasKind(""). HasReserved(""). - HasDatabaseName(testDb(t).Name). - HasSchemaName(testSchema(t).Name). + HasDatabaseName(testClientHelper().Ids.DatabaseId().Name()). + HasSchemaName(testClientHelper().Ids.SchemaId().Name()). HasOwner(snowflakeroles.Accountadmin.Name()). HasComment(comment). HasNonEmptyText(). @@ -56,8 +56,8 @@ func TestInt_Views(t *testing.T) { HasCreatedOnNotEmpty(). HasName(id.Name()). HasKind("VIEW"). - HasDatabaseName(testDb(t).Name). - HasSchemaName(testSchema(t).Name). + HasDatabaseName(testClientHelper().Ids.DatabaseId().Name()). + HasSchemaName(testClientHelper().Ids.SchemaId().Name()). // all below are not contained in the terse response, that's why all of them we expect to be empty HasReserved(""). HasOwner(""). From 90191b789a89c110fcf5fa6a6a9ff2c7b540fa3e Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 5 Sep 2024 15:31:44 +0200 Subject: [PATCH 02/21] Remove warehouse old integration test helpers --- pkg/sdk/testint/alerts_integration_test.go | 20 +++++++++---------- pkg/sdk/testint/comments_integration_test.go | 4 ++-- ...cortex_search_services_integration_test.go | 6 +++--- .../testint/dynamic_table_integration_test.go | 18 ++++++++--------- .../external_tables_integration_test.go | 2 +- pkg/sdk/testint/grants_integration_test.go | 4 ++-- pkg/sdk/testint/sessions_integration_test.go | 2 +- pkg/sdk/testint/setup_test.go | 5 ----- pkg/sdk/testint/tasks_gen_integration_test.go | 4 ++-- 9 files changed, 30 insertions(+), 35 deletions(-) diff --git a/pkg/sdk/testint/alerts_integration_test.go b/pkg/sdk/testint/alerts_integration_test.go index 018dd080d8..06a1052a8a 100644 --- a/pkg/sdk/testint/alerts_integration_test.go +++ b/pkg/sdk/testint/alerts_integration_test.go @@ -90,7 +90,7 @@ func TestInt_AlertCreate(t *testing.T) { condition := "SELECT 1" action := "SELECT 1" comment := random.Comment() - err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, &sdk.CreateAlertOptions{ + err := client.Alerts.Create(ctx, id, testClientHelper().Ids.WarehouseId(), schedule, condition, action, &sdk.CreateAlertOptions{ OrReplace: sdk.Bool(true), IfNotExists: sdk.Bool(false), Comment: sdk.String(comment), @@ -99,7 +99,7 @@ func TestInt_AlertCreate(t *testing.T) { alertDetails, err := client.Alerts.Describe(ctx, id) require.NoError(t, err) assert.Equal(t, name, alertDetails.Name) - assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse) + assert.Equal(t, testClientHelper().Ids.WarehouseId().Name(), alertDetails.Warehouse) assert.Equal(t, schedule, alertDetails.Schedule) assert.Equal(t, comment, *alertDetails.Comment) assert.Equal(t, condition, alertDetails.Condition) @@ -126,7 +126,7 @@ func TestInt_AlertCreate(t *testing.T) { condition := "SELECT 1" action := "SELECT 1" comment := random.Comment() - err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, &sdk.CreateAlertOptions{ + err := client.Alerts.Create(ctx, id, testClientHelper().Ids.WarehouseId(), schedule, condition, action, &sdk.CreateAlertOptions{ OrReplace: sdk.Bool(false), IfNotExists: sdk.Bool(true), Comment: sdk.String(comment), @@ -135,7 +135,7 @@ func TestInt_AlertCreate(t *testing.T) { alertDetails, err := client.Alerts.Describe(ctx, id) require.NoError(t, err) assert.Equal(t, name, alertDetails.Name) - assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse) + assert.Equal(t, testClientHelper().Ids.WarehouseId().Name(), alertDetails.Warehouse) assert.Equal(t, schedule, alertDetails.Schedule) assert.Equal(t, comment, *alertDetails.Comment) assert.Equal(t, condition, alertDetails.Condition) @@ -161,12 +161,12 @@ func TestInt_AlertCreate(t *testing.T) { schedule := "USING CRON * * * * TUE,THU UTC" condition := "SELECT 1" action := "SELECT 1" - err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, nil) + err := client.Alerts.Create(ctx, id, testClientHelper().Ids.WarehouseId(), schedule, condition, action, nil) require.NoError(t, err) alertDetails, err := client.Alerts.Describe(ctx, id) require.NoError(t, err) assert.Equal(t, name, alertDetails.Name) - assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse) + assert.Equal(t, testClientHelper().Ids.WarehouseId().Name(), alertDetails.Warehouse) assert.Equal(t, schedule, alertDetails.Schedule) assert.Equal(t, condition, alertDetails.Condition) assert.Equal(t, action, alertDetails.Action) @@ -199,12 +199,12 @@ func TestInt_AlertCreate(t *testing.T) { 2 end ` - err := client.Alerts.Create(ctx, id, testWarehouse(t).ID(), schedule, condition, action, nil) + err := client.Alerts.Create(ctx, id, testClientHelper().Ids.WarehouseId(), schedule, condition, action, nil) require.NoError(t, err) alertDetails, err := client.Alerts.Describe(ctx, id) require.NoError(t, err) assert.Equal(t, name, alertDetails.Name) - assert.Equal(t, testWarehouse(t).Name, alertDetails.Warehouse) + assert.Equal(t, testClientHelper().Ids.WarehouseId().Name(), alertDetails.Warehouse) assert.Equal(t, schedule, alertDetails.Schedule) assert.Equal(t, condition, alertDetails.Condition) assert.Equal(t, strings.TrimSpace(action), alertDetails.Action) @@ -382,7 +382,7 @@ func TestInt_AlertsShowByID(t *testing.T) { client := testClient(t) ctx := testContext(t) - warehouseTest := testWarehouse(t) + warehouseId := testClientHelper().Ids.WarehouseId() cleanupAlertHandle := func(t *testing.T, id sdk.SchemaObjectIdentifier) func() { t.Helper() return func() { @@ -398,7 +398,7 @@ func TestInt_AlertsShowByID(t *testing.T) { t.Helper() schedule, condition, action := "USING CRON * * * * * UTC", "SELECT 1", "SELECT 1" - err := client.Alerts.Create(ctx, id, warehouseTest.ID(), schedule, condition, action, &sdk.CreateAlertOptions{}) + err := client.Alerts.Create(ctx, id, warehouseId, schedule, condition, action, &sdk.CreateAlertOptions{}) require.NoError(t, err) t.Cleanup(cleanupAlertHandle(t, id)) } diff --git a/pkg/sdk/testint/comments_integration_test.go b/pkg/sdk/testint/comments_integration_test.go index ab950bb327..3c90ae3e5a 100644 --- a/pkg/sdk/testint/comments_integration_test.go +++ b/pkg/sdk/testint/comments_integration_test.go @@ -18,11 +18,11 @@ func TestInt_Comment(t *testing.T) { comment := random.Comment() err := client.Comments.Set(ctx, &sdk.SetCommentOptions{ ObjectType: sdk.ObjectTypeWarehouse, - ObjectName: testWarehouse(t).ID(), + ObjectName: testClientHelper().Ids.WarehouseId(), Value: sdk.String(comment), }) require.NoError(t, err) - wh, err := client.Warehouses.ShowByID(ctx, testWarehouse(t).ID()) + wh, err := client.Warehouses.ShowByID(ctx, testClientHelper().Ids.WarehouseId()) require.NoError(t, err) assert.Equal(t, comment, wh.Comment) }) diff --git a/pkg/sdk/testint/cortex_search_services_integration_test.go b/pkg/sdk/testint/cortex_search_services_integration_test.go index 52910aaf08..4f8368f1c3 100644 --- a/pkg/sdk/testint/cortex_search_services_integration_test.go +++ b/pkg/sdk/testint/cortex_search_services_integration_test.go @@ -16,7 +16,7 @@ func TestInt_CortexSearchServices(t *testing.T) { client := testClient(t) ctx := context.Background() - warehouse := testWarehouse(t) + warehouseId := testClientHelper().Ids.WarehouseId() on := "some_text_column" targetLag := "2 minutes" @@ -31,7 +31,7 @@ func TestInt_CortexSearchServices(t *testing.T) { table, tableCleanup := testClientHelper().Table.CreateTableWithPredefinedColumns(t) t.Cleanup(tableCleanup) - err := client.CortexSearchServices.Create(ctx, sdk.NewCreateCortexSearchServiceRequest(id, on, warehouse.ID(), targetLag, buildQuery(table.ID()))) + err := client.CortexSearchServices.Create(ctx, sdk.NewCreateCortexSearchServiceRequest(id, on, warehouseId, targetLag, buildQuery(table.ID()))) require.NoError(t, err) t.Cleanup(testClientHelper().CortexSearchService.DropCortexSearchServiceFunc(t, id)) @@ -47,7 +47,7 @@ func TestInt_CortexSearchServices(t *testing.T) { name := testClientHelper().Ids.RandomSchemaObjectIdentifier() comment := random.Comment() - err := client.CortexSearchServices.Create(ctx, sdk.NewCreateCortexSearchServiceRequest(name, on, testWarehouse(t).ID(), targetLag, buildQuery(table.ID())).WithOrReplace(true).WithComment(comment)) + err := client.CortexSearchServices.Create(ctx, sdk.NewCreateCortexSearchServiceRequest(name, on, testClientHelper().Ids.WarehouseId(), targetLag, buildQuery(table.ID())).WithOrReplace(true).WithComment(comment)) require.NoError(t, err) t.Cleanup(func() { err = client.CortexSearchServices.Drop(ctx, sdk.NewDropCortexSearchServiceRequest(name)) diff --git a/pkg/sdk/testint/dynamic_table_integration_test.go b/pkg/sdk/testint/dynamic_table_integration_test.go index 163133ab59..b72148210d 100644 --- a/pkg/sdk/testint/dynamic_table_integration_test.go +++ b/pkg/sdk/testint/dynamic_table_integration_test.go @@ -25,7 +25,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { } query := "select id from " + tableTest.ID().FullyQualifiedName() comment := random.Comment() - err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, testWarehouse(t).ID(), targetLag, query).WithOrReplace(true).WithComment(&comment)) + err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(name, testClientHelper().Ids.WarehouseId(), targetLag, query).WithOrReplace(true).WithComment(&comment)) require.NoError(t, err) t.Cleanup(func() { err = client.DynamicTables.Drop(ctx, sdk.NewDropDynamicTableRequest(name)) @@ -37,14 +37,14 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { entity := entities[0] require.Equal(t, name.Name(), entity.Name) - require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse) + require.Equal(t, testClientHelper().Ids.WarehouseId().Name(), entity.Warehouse) require.Equal(t, *targetLag.MaximumDuration, entity.TargetLag) dynamicTableById, err := client.DynamicTables.ShowByID(ctx, name) require.NoError(t, err) require.NotNil(t, dynamicTableById) require.Equal(t, name.Name(), dynamicTableById.Name) - require.Equal(t, testWarehouse(t).ID().Name(), dynamicTableById.Warehouse) + require.Equal(t, testClientHelper().Ids.WarehouseId().Name(), dynamicTableById.Warehouse) require.Equal(t, *targetLag.MaximumDuration, dynamicTableById.TargetLag) assert.Equal(t, "ROLE", dynamicTableById.OwnerRoleType) }) @@ -56,7 +56,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { } query := "select id from " + tableTest.ID().FullyQualifiedName() comment := random.Comment() - err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(id, testWarehouse(t).ID(), targetLag, query).WithOrReplace(true).WithComment(&comment)) + err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(id, testClientHelper().Ids.WarehouseId(), targetLag, query).WithOrReplace(true).WithComment(&comment)) require.NoError(t, err) t.Cleanup(func() { err = client.DynamicTables.Drop(ctx, sdk.NewDropDynamicTableRequest(id)) @@ -68,7 +68,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { entity := entities[0] require.Equal(t, id.Name(), entity.Name) - require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse) + require.Equal(t, testClientHelper().Ids.WarehouseId().Name(), entity.Warehouse) require.Equal(t, "DOWNSTREAM", entity.TargetLag) require.Equal(t, sdk.DynamicTableRefreshModeIncremental, entity.RefreshMode) require.Contains(t, entity.Text, "initialize = 'ON_CREATE'") @@ -84,7 +84,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { comment := random.Comment() refreshMode := sdk.DynamicTableRefreshModeFull initialize := sdk.DynamicTableInitializeOnSchedule - err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(id, testWarehouse(t).ID(), targetLag, query).WithOrReplace(true).WithInitialize(initialize).WithRefreshMode(refreshMode).WithComment(&comment)) + err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(id, testClientHelper().Ids.WarehouseId(), targetLag, query).WithOrReplace(true).WithInitialize(initialize).WithRefreshMode(refreshMode).WithComment(&comment)) require.NoError(t, err) t.Cleanup(func() { err = client.DynamicTables.Drop(ctx, sdk.NewDropDynamicTableRequest(id)) @@ -96,7 +96,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { entity := entities[0] require.Equal(t, id.Name(), entity.Name) - require.Equal(t, testWarehouse(t).ID().Name(), entity.Warehouse) + require.Equal(t, testClientHelper().Ids.WarehouseId().Name(), entity.Warehouse) require.Equal(t, *targetLag.MaximumDuration, entity.TargetLag) require.Equal(t, sdk.DynamicTableRefreshModeFull, entity.RefreshMode) require.Contains(t, entity.Text, "initialize = 'ON_SCHEDULE'") @@ -199,7 +199,7 @@ func TestInt_DynamicTablesShowByID(t *testing.T) { client := testClient(t) ctx := context.Background() - warehouseTest := testWarehouse(t) + warehouseId := testClientHelper().Ids.WarehouseId() cleanupDynamicTableHandle := func(t *testing.T, id sdk.SchemaObjectIdentifier) func() { t.Helper() @@ -221,7 +221,7 @@ func TestInt_DynamicTablesShowByID(t *testing.T) { MaximumDuration: sdk.String("2 minutes"), } query := "select id from " + tableTest.ID().FullyQualifiedName() - err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(id, warehouseTest.ID(), targetLag, query).WithOrReplace(true)) + err := client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(id, warehouseId, targetLag, query).WithOrReplace(true)) require.NoError(t, err) t.Cleanup(cleanupDynamicTableHandle(t, id)) } diff --git a/pkg/sdk/testint/external_tables_integration_test.go b/pkg/sdk/testint/external_tables_integration_test.go index 1a170eafbd..802ee41980 100644 --- a/pkg/sdk/testint/external_tables_integration_test.go +++ b/pkg/sdk/testint/external_tables_integration_test.go @@ -110,7 +110,7 @@ func TestInt_ExternalTables(t *testing.T) { fileFormat, fileFormatCleanup := testClientHelper().FileFormat.CreateFileFormat(t) t.Cleanup(fileFormatCleanup) - err := client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID()) + err := client.Sessions.UseWarehouse(ctx, testClientHelper().Ids.WarehouseId()) require.NoError(t, err) id := testClientHelper().Ids.RandomSchemaObjectIdentifier() diff --git a/pkg/sdk/testint/grants_integration_test.go b/pkg/sdk/testint/grants_integration_test.go index e0fe30a433..f4acd68643 100644 --- a/pkg/sdk/testint/grants_integration_test.go +++ b/pkg/sdk/testint/grants_integration_test.go @@ -1151,7 +1151,7 @@ func TestInt_GrantOwnership(t *testing.T) { }, &sdk.AccountRoleGrantOn{ AccountObject: &sdk.GrantOnAccountObject{ - Warehouse: sdk.Pointer(testWarehouse(t).ID()), + Warehouse: sdk.Pointer(testClientHelper().Ids.WarehouseId()), }, }, roleId, @@ -1300,7 +1300,7 @@ func TestInt_GrantOwnership(t *testing.T) { on := sdk.OwnershipGrantOn{ Object: &sdk.Object{ ObjectType: sdk.ObjectTypeWarehouse, - Name: testWarehouse(t).ID(), + Name: testClientHelper().Ids.WarehouseId(), }, } to := sdk.OwnershipGrantTo{ diff --git a/pkg/sdk/testint/sessions_integration_test.go b/pkg/sdk/testint/sessions_integration_test.go index da4efc3f43..b999f7adc2 100644 --- a/pkg/sdk/testint/sessions_integration_test.go +++ b/pkg/sdk/testint/sessions_integration_test.go @@ -99,7 +99,7 @@ func TestInt_UseWarehouse(t *testing.T) { ctx := testContext(t) t.Cleanup(func() { - err := client.Sessions.UseWarehouse(ctx, testWarehouse(t).ID()) + err := client.Sessions.UseWarehouse(ctx, testClientHelper().Ids.WarehouseId()) require.NoError(t, err) }) // new warehouse created on purpose diff --git a/pkg/sdk/testint/setup_test.go b/pkg/sdk/testint/setup_test.go index 8a1caa2085..345971f70f 100644 --- a/pkg/sdk/testint/setup_test.go +++ b/pkg/sdk/testint/setup_test.go @@ -270,11 +270,6 @@ func testContext(t *testing.T) context.Context { return itc.ctx } -func testWarehouse(t *testing.T) *sdk.Warehouse { - t.Helper() - return itc.warehouse -} - func testSecondaryClient(t *testing.T) *sdk.Client { t.Helper() return itc.secondaryClient diff --git a/pkg/sdk/testint/tasks_gen_integration_test.go b/pkg/sdk/testint/tasks_gen_integration_test.go index 0a470e0c66..984f1f80ea 100644 --- a/pkg/sdk/testint/tasks_gen_integration_test.go +++ b/pkg/sdk/testint/tasks_gen_integration_test.go @@ -150,7 +150,7 @@ func TestInt_Tasks(t *testing.T) { t.Run("create task: almost complete case", func(t *testing.T) { request := createTaskBasicRequest(t). WithOrReplace(sdk.Bool(true)). - WithWarehouse(sdk.NewCreateTaskWarehouseRequest().WithWarehouse(sdk.Pointer(testWarehouse(t).ID()))). + WithWarehouse(sdk.NewCreateTaskWarehouseRequest().WithWarehouse(sdk.Pointer(testClientHelper().Ids.WarehouseId()))). WithSchedule(sdk.String("10 MINUTE")). WithConfig(sdk.String(`$${"output_dir": "/temp/test_directory/", "learning_rate": 0.1}$$`)). WithAllowOverlappingExecution(sdk.Bool(true)). @@ -165,7 +165,7 @@ func TestInt_Tasks(t *testing.T) { task := createTaskWithRequest(t, request) - assertTaskWithOptions(t, task, id, "some comment", testWarehouse(t).Name, "10 MINUTE", `SYSTEM$STREAM_HAS_DATA('MYSTREAM')`, true, `{"output_dir": "/temp/test_directory/", "learning_rate": 0.1}`, nil) + assertTaskWithOptions(t, task, id, "some comment", testClientHelper().Ids.WarehouseId().Name(), "10 MINUTE", `SYSTEM$STREAM_HAS_DATA('MYSTREAM')`, true, `{"output_dir": "/temp/test_directory/", "learning_rate": 0.1}`, nil) }) t.Run("create task: with after", func(t *testing.T) { From 27bd54bc110fcabd3f115e50f4cfd2a94cfe0f70 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 5 Sep 2024 18:00:17 +0200 Subject: [PATCH 03/21] Cleanup Client in acceptance tests (WIP) --- pkg/acceptance/helpers/database_client.go | 24 +++++++ pkg/acceptance/helpers/test_client.go | 4 ++ pkg/resources/account_role_acceptance_test.go | 16 ++--- pkg/resources/custom_diffs_test.go | 1 + .../external_table_acceptance_test.go | 1 + .../secondary_database_acceptance_test.go | 12 ++-- .../shared_database_acceptance_test.go | 65 ++++--------------- 7 files changed, 53 insertions(+), 70 deletions(-) diff --git a/pkg/acceptance/helpers/database_client.go b/pkg/acceptance/helpers/database_client.go index 1011336f7a..3a3b945cad 100644 --- a/pkg/acceptance/helpers/database_client.go +++ b/pkg/acceptance/helpers/database_client.go @@ -153,3 +153,27 @@ 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 "" 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) +} diff --git a/pkg/acceptance/helpers/test_client.go b/pkg/acceptance/helpers/test_client.go index 21bfd5d015..77b0d44b6c 100644 --- a/pkg/acceptance/helpers/test_client.go +++ b/pkg/acceptance/helpers/test_client.go @@ -129,3 +129,7 @@ type TestClientContext struct { warehouse string testObjectSuffix string } + +func (c *TestClient) GetAccountLocator() string { + return c.context.client.GetAccountLocator() +} diff --git a/pkg/resources/account_role_acceptance_test.go b/pkg/resources/account_role_acceptance_test.go index 698522805d..cda06bec4d 100644 --- a/pkg/resources/account_role_acceptance_test.go +++ b/pkg/resources/account_role_acceptance_test.go @@ -1,21 +1,17 @@ package resources_test import ( - "context" "fmt" "testing" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" - - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/importchecks" - "github.com/hashicorp/terraform-plugin-testing/plancheck" - "github.com/stretchr/testify/require" - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/importchecks" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -23,8 +19,7 @@ func TestAcc_AccountRole_Basic(t *testing.T) { id := acc.TestClient().Ids.RandomAccountObjectIdentifier() comment := random.Comment() - currentRole, err := acc.Client(t).ContextFunctions.CurrentRole(context.Background()) - require.NoError(t, err) + currentRole := acc.TestClient().Context.CurrentRole(t) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -135,8 +130,7 @@ func TestAcc_AccountRole_Complete(t *testing.T) { newId := acc.TestClient().Ids.RandomAccountObjectIdentifier() newComment := random.Comment() - currentRole, err := acc.Client(t).ContextFunctions.CurrentRole(context.Background()) - require.NoError(t, err) + currentRole := acc.TestClient().Context.CurrentRole(t) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, diff --git a/pkg/resources/custom_diffs_test.go b/pkg/resources/custom_diffs_test.go index acecdfee11..925542c314 100644 --- a/pkg/resources/custom_diffs_test.go +++ b/pkg/resources/custom_diffs_test.go @@ -138,6 +138,7 @@ func calculateDiff(t *testing.T, providerConfig *schema.Provider, rawConfigValue &terraform.ResourceConfig{ Config: stateValue, }, + // TODO [helper-cleanup]: do we need this client here? &provider.Context{Client: acc.Client(t)}, ) require.NoError(t, err) diff --git a/pkg/resources/external_table_acceptance_test.go b/pkg/resources/external_table_acceptance_test.go index 0541a23059..266b128dda 100644 --- a/pkg/resources/external_table_acceptance_test.go +++ b/pkg/resources/external_table_acceptance_test.go @@ -348,6 +348,7 @@ func externalTableContainsData(id sdk.SchemaObjectIdentifier, contains func(rows } } +// TODO [helper-cleanup]: to helper func publishExternalTablesTestData(t *testing.T, stageName sdk.SchemaObjectIdentifier, data []byte) { t.Helper() client := acc.Client(t) diff --git a/pkg/resources/secondary_database_acceptance_test.go b/pkg/resources/secondary_database_acceptance_test.go index 6c8d9fe69c..e499a08317 100644 --- a/pkg/resources/secondary_database_acceptance_test.go +++ b/pkg/resources/secondary_database_acceptance_test.go @@ -1,7 +1,6 @@ package resources_test import ( - "context" "fmt" "strconv" "testing" @@ -162,7 +161,7 @@ func TestAcc_CreateSecondaryDatabase_complete(t *testing.T) { comment := random.Comment() primaryDatabase, externalPrimaryId, _ := acc.SecondaryTestClient().Database.CreatePrimaryDatabase(t, []sdk.AccountIdentifier{ - sdk.NewAccountIdentifierFromAccountLocator(acc.Client(t).GetAccountLocator()), + sdk.NewAccountIdentifierFromAccountLocator(acc.TestClient().GetAccountLocator()), }) t.Cleanup(func() { // TODO(SNOW-1562172): Create a better solution for this type of situations @@ -405,15 +404,14 @@ func TestAcc_CreateSecondaryDatabase_DataRetentionTimeInDays(t *testing.T) { id := acc.TestClient().Ids.RandomAccountObjectIdentifier() primaryDatabase, externalPrimaryId, _ := acc.SecondaryTestClient().Database.CreatePrimaryDatabase(t, []sdk.AccountIdentifier{ - sdk.NewAccountIdentifierFromAccountLocator(acc.Client(t).GetAccountLocator()), + sdk.NewAccountIdentifierFromAccountLocator(acc.TestClient().GetAccountLocator()), }) t.Cleanup(func() { // TODO(SNOW-1562172): Create a better solution for this type of situations require.Eventually(t, func() bool { return acc.SecondaryTestClient().Database.DropDatabase(t, primaryDatabase.ID()) == nil }, time.Second*5, time.Second) }) - accountDataRetentionTimeInDays, err := acc.Client(t).Parameters.ShowAccountParameter(context.Background(), sdk.AccountParameterDataRetentionTimeInDays) - require.NoError(t, err) + accountDataRetentionTimeInDays := acc.TestClient().Parameter.ShowAccountParameter(t, sdk.AccountParameterDataRetentionTimeInDays) externalVolumeId, externalVolumeCleanup := acc.TestClient().ExternalVolume.Create(t) t.Cleanup(externalVolumeCleanup) @@ -528,7 +526,7 @@ func TestAcc_SecondaryDatabase_migrateFromV0941_ensureSmoothUpgradeWithNewResour id := acc.TestClient().Ids.RandomAccountObjectIdentifier() primaryDatabase, externalPrimaryId, _ := acc.SecondaryTestClient().Database.CreatePrimaryDatabase(t, []sdk.AccountIdentifier{ - sdk.NewAccountIdentifierFromAccountLocator(acc.Client(t).GetAccountLocator()), + sdk.NewAccountIdentifierFromAccountLocator(acc.TestClient().GetAccountLocator()), }) t.Cleanup(func() { // TODO(SNOW-1562172): Create a better solution for this type of situations @@ -577,7 +575,7 @@ func TestAcc_SecondaryDatabase_IdentifierQuotingDiffSuppression(t *testing.T) { quotedId := fmt.Sprintf(`\"%s\"`, id.Name()) primaryDatabase, externalPrimaryId, _ := acc.SecondaryTestClient().Database.CreatePrimaryDatabase(t, []sdk.AccountIdentifier{ - sdk.NewAccountIdentifierFromAccountLocator(acc.Client(t).GetAccountLocator()), + sdk.NewAccountIdentifierFromAccountLocator(acc.TestClient().GetAccountLocator()), }) unquotedExternalPrimaryId := fmt.Sprintf("%s.%s.%s", externalPrimaryId.AccountIdentifier().OrganizationName(), externalPrimaryId.AccountIdentifier().AccountName(), externalPrimaryId.Name()) t.Cleanup(func() { diff --git a/pkg/resources/shared_database_acceptance_test.go b/pkg/resources/shared_database_acceptance_test.go index 26fa13a7af..f3dfeec96e 100644 --- a/pkg/resources/shared_database_acceptance_test.go +++ b/pkg/resources/shared_database_acceptance_test.go @@ -6,11 +6,12 @@ import ( "regexp" "strconv" "testing" - "time" acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" @@ -147,6 +148,9 @@ func TestAcc_CreateSharedDatabase_Basic(t *testing.T) { } func TestAcc_CreateSharedDatabase_complete(t *testing.T) { + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + acc.TestAccPreCheck(t) + id := acc.TestClient().Ids.RandomAccountObjectIdentifier() comment := random.Comment() externalShareId := createShareableDatabase(t) @@ -178,23 +182,7 @@ func TestAcc_CreateSharedDatabase_complete(t *testing.T) { "enable_console_output": config.BoolVariable(true), } - // 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). - testId := acc.TestClient().Ids.RandomAccountObjectIdentifier() - err := acc.Client(t).Databases.CreateShared(context.Background(), testId, externalShareId, new(sdk.CreateSharedDatabaseOptions)) - require.NoError(t, err) - - require.Eventually(t, func() bool { - database, err := acc.TestClient().Database.Show(t, testId) - if err != nil { - return false - } - // Origin is returned as "" 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) - acc.TestClient().Database.DropDatabaseFunc(t, testId)() + acc.TestClient().Database.CreateDatabaseFromShareTemporarily(t, externalShareId) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -322,27 +310,13 @@ func createShareableDatabase(t *testing.T) sdk.ExternalObjectIdentifier { } func TestAcc_SharedDatabase_migrateFromV0941_ensureSmoothUpgradeWithNewResourceId(t *testing.T) { + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) acc.TestAccPreCheck(t) + id := acc.TestClient().Ids.RandomAccountObjectIdentifier() externalShareId := createShareableDatabase(t) - // 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). - testId := acc.TestClient().Ids.RandomAccountObjectIdentifier() - err := acc.Client(t).Databases.CreateShared(context.Background(), testId, externalShareId, new(sdk.CreateSharedDatabaseOptions)) - require.NoError(t, err) - - require.Eventually(t, func() bool { - database, err := acc.TestClient().Database.Show(t, testId) - if err != nil { - return false - } - // Origin is returned as "" 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) - acc.TestClient().Database.DropDatabaseFunc(t, testId)() + acc.TestClient().Database.CreateDatabaseFromShareTemporarily(t, externalShareId) resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, @@ -382,29 +356,16 @@ func sharedDatabaseConfigBasic(name, externalShareId string) string { } func TestAcc_SharedDatabase_IdentifierQuotingDiffSuppression(t *testing.T) { + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + acc.TestAccPreCheck(t) + id := acc.TestClient().Ids.RandomAccountObjectIdentifier() quotedId := fmt.Sprintf(`\"%s\"`, id.Name()) externalShareId := createShareableDatabase(t) unquotedExternalShareId := fmt.Sprintf("%s.%s.%s", externalShareId.AccountIdentifier().OrganizationName(), externalShareId.AccountIdentifier().AccountName(), externalShareId.Name()) - // 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). - testId := acc.TestClient().Ids.RandomAccountObjectIdentifier() - err := acc.Client(t).Databases.CreateShared(context.Background(), testId, externalShareId, new(sdk.CreateSharedDatabaseOptions)) - require.NoError(t, err) - - require.Eventually(t, func() bool { - database, err := acc.TestClient().Database.Show(t, testId) - if err != nil { - return false - } - // Origin is returned as "" 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) - acc.TestClient().Database.DropDatabaseFunc(t, testId)() + acc.TestClient().Database.CreateDatabaseFromShareTemporarily(t, externalShareId) resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, From 884d0b5b3a60e7811aa9a091927c99b274ca4250 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 08:51:57 +0200 Subject: [PATCH 04/21] Fix setup in two unsafe execute tests --- .../unsafe_execute_acceptance_test.go | 80 ++++++++----------- 1 file changed, 35 insertions(+), 45 deletions(-) diff --git a/pkg/resources/unsafe_execute_acceptance_test.go b/pkg/resources/unsafe_execute_acceptance_test.go index 735874cb9d..b98b069aed 100644 --- a/pkg/resources/unsafe_execute_acceptance_test.go +++ b/pkg/resources/unsafe_execute_acceptance_test.go @@ -1,7 +1,6 @@ package resources_test import ( - "context" "errors" "fmt" "regexp" @@ -10,6 +9,7 @@ import ( acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -512,11 +512,20 @@ func TestAcc_UnsafeExecute_executeUpdated(t *testing.T) { } func TestAcc_UnsafeExecute_grants(t *testing.T) { - id := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix("UNSAFE_EXECUTE_TEST_DATABASE_") - roleId := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix("UNSAFE_EXECUTE_TEST_ROLE_") + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + acc.TestAccPreCheck(t) + + client := acc.TestClient() + + database, databaseCleanup := client.Database.CreateDatabase(t) + t.Cleanup(databaseCleanup) + + role, roleCleanup := client.Role.CreateRole(t) + t.Cleanup(roleCleanup) + privilege := sdk.AccountObjectPrivilegeCreateSchema - execute := fmt.Sprintf("GRANT %s ON DATABASE %s TO ROLE %s", privilege, id.Name(), roleId.Name()) - revert := fmt.Sprintf("REVOKE %s ON DATABASE %s FROM ROLE %s", privilege, id.Name(), roleId.Name()) + execute := fmt.Sprintf("GRANT %s ON DATABASE %s TO ROLE %s", privilege, database.ID().FullyQualifiedName(), role.ID().FullyQualifiedName()) + revert := fmt.Sprintf("REVOKE %s ON DATABASE %s FROM ROLE %s", privilege, database.ID().FullyQualifiedName(), role.ID().FullyQualifiedName()) resourceName := "snowflake_unsafe_execute.test" createConfigVariables := func(execute string, revert string) map[string]config.Variable { @@ -533,13 +542,12 @@ func TestAcc_UnsafeExecute_grants(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, CheckDestroy: func(state *terraform.State) error { - err := verifyGrantExists(t, roleId, privilege, false)(state) - dropResourcesForUnsafeExecuteTestCaseForGrants(t, id, roleId) + err := verifyGrantExists(t, role.ID(), privilege, false)(state) return err }, Steps: []resource.TestStep{ { - PreConfig: func() { createResourcesForExecuteUnsafeTestCaseForGrants(t, id, roleId) }, + PreConfig: func() {}, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_UnsafeExecute_commonSetup"), ConfigVariables: createConfigVariables(execute, revert), ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -549,7 +557,7 @@ func TestAcc_UnsafeExecute_grants(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "execute", execute), resource.TestCheckResourceAttr(resourceName, "revert", revert), resource.TestCheckResourceAttrSet(resourceName, "id"), - verifyGrantExists(t, roleId, privilege, true), + verifyGrantExists(t, role.ID(), privilege, true), ), }, }, @@ -570,10 +578,24 @@ func TestAcc_UnsafeExecute_grants(t *testing.T) { func TestAcc_UnsafeExecute_grantsComplex(t *testing.T) { t.Skip("Skipping TestAcc_UnsafeExecute_grantsComplex because of https://github.com/hashicorp/terraform-plugin-sdk/issues/536 issue") - dbId1 := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix("UNSAFE_EXECUTE_TEST_DATABASE_") - dbId2 := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix("UNSAFE_EXECUTE_TEST_DATABASE_") - roleId1 := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix("UNSAFE_EXECUTE_TEST_ROLE_") - roleId2 := acc.TestClient().Ids.RandomAccountObjectIdentifierWithPrefix("UNSAFE_EXECUTE_TEST_ROLE_") + client := acc.TestClient() + + database1, database1Cleanup := client.Database.CreateDatabase(t) + t.Cleanup(database1Cleanup) + + database2, database2Cleanup := client.Database.CreateDatabase(t) + t.Cleanup(database2Cleanup) + + role1, role1Cleanup := client.Role.CreateRole(t) + t.Cleanup(role1Cleanup) + + role2, role2Cleanup := client.Role.CreateRole(t) + t.Cleanup(role2Cleanup) + + dbId1 := database1.ID() + dbId2 := database2.ID() + roleId1 := role1.ID() + roleId2 := role2.ID() privilege1 := sdk.AccountObjectPrivilegeCreateSchema privilege2 := sdk.AccountObjectPrivilegeModify privilege3 := sdk.AccountObjectPrivilegeUsage @@ -625,16 +647,10 @@ func TestAcc_UnsafeExecute_grantsComplex(t *testing.T) { if err != nil { return err } - dropResourcesForUnsafeExecuteTestCaseForGrants(t, dbId1, roleId1) - dropResourcesForUnsafeExecuteTestCaseForGrants(t, dbId2, roleId2) return err }, Steps: []resource.TestStep{ { - PreConfig: func() { - createResourcesForExecuteUnsafeTestCaseForGrants(t, dbId1, roleId1) - createResourcesForExecuteUnsafeTestCaseForGrants(t, dbId2, roleId2) - }, ConfigDirectory: config.TestNameDirectory(), ConfigVariables: createConfigVariables(), ConfigPlanChecks: resource.ConfigPlanChecks{ @@ -703,32 +719,6 @@ output "unsafe" { `, queryNumber) } -func createResourcesForExecuteUnsafeTestCaseForGrants(t *testing.T, dbId sdk.AccountObjectIdentifier, roleId sdk.AccountObjectIdentifier) { - t.Helper() - - client := acc.Client(t) - ctx := context.Background() - - err := client.Databases.Create(ctx, dbId, &sdk.CreateDatabaseOptions{}) - require.NoError(t, err) - - err = client.Roles.Create(ctx, sdk.NewCreateRoleRequest(roleId)) - require.NoError(t, err) -} - -func dropResourcesForUnsafeExecuteTestCaseForGrants(t *testing.T, dbId sdk.AccountObjectIdentifier, roleId sdk.AccountObjectIdentifier) { - t.Helper() - - client := acc.Client(t) - ctx := context.Background() - - err := client.Databases.Drop(ctx, dbId, &sdk.DropDatabaseOptions{}) - assert.NoError(t, err) - - err = client.Roles.Drop(ctx, sdk.NewDropRoleRequest(roleId)) - assert.NoError(t, err) -} - func verifyGrantExists(t *testing.T, roleId sdk.AccountObjectIdentifier, privilege sdk.AccountObjectPrivilege, shouldExist bool) func(state *terraform.State) error { t.Helper() return func(state *terraform.State) error { From 46fe9cbaef9ac8136e3d83f1857a40c51283b870 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 08:54:34 +0200 Subject: [PATCH 05/21] Removed dependency on acceptance in the unit test for custom diffs --- pkg/resources/custom_diffs_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/resources/custom_diffs_test.go b/pkg/resources/custom_diffs_test.go index 925542c314..14fcb2c848 100644 --- a/pkg/resources/custom_diffs_test.go +++ b/pkg/resources/custom_diffs_test.go @@ -5,7 +5,6 @@ import ( "strings" "testing" - acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -138,8 +137,7 @@ func calculateDiff(t *testing.T, providerConfig *schema.Provider, rawConfigValue &terraform.ResourceConfig{ Config: stateValue, }, - // TODO [helper-cleanup]: do we need this client here? - &provider.Context{Client: acc.Client(t)}, + &provider.Context{Client: &sdk.Client{}}, ) require.NoError(t, err) return diff @@ -155,7 +153,7 @@ func calculateDiffFromAttributes(t *testing.T, providerConfig *schema.Provider, &terraform.ResourceConfig{ Config: newValue, }, - &provider.Context{Client: acc.Client(t)}, + &provider.Context{Client: &sdk.Client{}}, ) require.NoError(t, err) return diff From fe2daaa09111df31297495dc2de14c880b952e73 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 10:56:53 +0200 Subject: [PATCH 06/21] Extract grant/revoke helpers --- pkg/acceptance/helpers/grant_client.go | 111 ++++++++++++++++++ ...vileges_to_account_role_acceptance_test.go | 45 +------ ...ileges_to_database_role_acceptance_test.go | 37 +----- 3 files changed, 118 insertions(+), 75 deletions(-) diff --git a/pkg/acceptance/helpers/grant_client.go b/pkg/acceptance/helpers/grant_client.go index fa7ba85ccc..1795f20f33 100644 --- a/pkg/acceptance/helpers/grant_client.go +++ b/pkg/acceptance/helpers/grant_client.go @@ -43,3 +43,114 @@ func (c *GrantClient) GrantOnSchemaToAccountRole(t *testing.T, schemaId sdk.Data ) require.NoError(t, err) } + +func (c *GrantClient) RevokePrivilegesOnSchemaObjectFromAccountRole( + t *testing.T, + accountRoleId sdk.AccountObjectIdentifier, + objectType sdk.ObjectType, + schemaObjectIdentifier sdk.SchemaObjectIdentifier, + privileges []sdk.SchemaObjectPrivilege, +) { + t.Helper() + ctx := context.Background() + + err := c.client().RevokePrivilegesFromAccountRole( + ctx, + &sdk.AccountRoleGrantPrivileges{ + SchemaObjectPrivileges: privileges, + }, + &sdk.AccountRoleGrantOn{ + SchemaObject: &sdk.GrantOnSchemaObject{ + SchemaObject: &sdk.Object{ + ObjectType: objectType, + Name: schemaObjectIdentifier, + }, + }, + }, + accountRoleId, + new(sdk.RevokePrivilegesFromAccountRoleOptions), + ) + + require.NoError(t, err) +} + +func (c *GrantClient) GrantPrivilegesOnSchemaObjectToAccountRole( + t *testing.T, + accountRoleId sdk.AccountObjectIdentifier, + objectType sdk.ObjectType, + schemaObjectIdentifier sdk.SchemaObjectIdentifier, + privileges []sdk.SchemaObjectPrivilege, + withGrantOption bool, +) { + t.Helper() + ctx := context.Background() + + err := c.client().GrantPrivilegesToAccountRole( + ctx, + &sdk.AccountRoleGrantPrivileges{ + SchemaObjectPrivileges: privileges, + }, + &sdk.AccountRoleGrantOn{ + SchemaObject: &sdk.GrantOnSchemaObject{ + SchemaObject: &sdk.Object{ + ObjectType: objectType, + Name: schemaObjectIdentifier, + }, + }, + }, + accountRoleId, + &sdk.GrantPrivilegesToAccountRoleOptions{ + WithGrantOption: sdk.Bool(withGrantOption), + }, + ) + require.NoError(t, err) +} + +func (c *GrantClient) RevokePrivilegesOnDatabaseFromDatabaseRole( + t *testing.T, + databaseRoleId sdk.DatabaseObjectIdentifier, + databaseId sdk.AccountObjectIdentifier, + privileges []sdk.AccountObjectPrivilege, +) { + t.Helper() + ctx := context.Background() + + err := c.client().RevokePrivilegesFromDatabaseRole( + ctx, + &sdk.DatabaseRoleGrantPrivileges{ + DatabasePrivileges: privileges, + }, + &sdk.DatabaseRoleGrantOn{ + Database: sdk.Pointer(databaseId), + }, + databaseRoleId, + new(sdk.RevokePrivilegesFromDatabaseRoleOptions), + ) + require.NoError(t, err) +} + +func (c *GrantClient) GrantPrivilegesOnDatabaseToDatabaseRole( + t *testing.T, + databaseRoleId sdk.DatabaseObjectIdentifier, + databaseId sdk.AccountObjectIdentifier, + privileges []sdk.AccountObjectPrivilege, + withGrantOption bool, +) { + t.Helper() + ctx := context.Background() + + err := c.client().GrantPrivilegesToDatabaseRole( + ctx, + &sdk.DatabaseRoleGrantPrivileges{ + DatabasePrivileges: privileges, + }, + &sdk.DatabaseRoleGrantOn{ + Database: sdk.Pointer(databaseId), + }, + databaseRoleId, + &sdk.GrantPrivilegesToDatabaseRoleOptions{ + WithGrantOption: sdk.Bool(withGrantOption), + }, + ) + require.NoError(t, err) +} diff --git a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go index 66dc2efd39..29b05973b8 100644 --- a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go @@ -1478,49 +1478,10 @@ func revokeAndGrantPrivilegesOnTableToAccountRole( withGrantOption bool, ) { t.Helper() - client := acc.Client(t) - ctx := context.Background() - err := client.Grants.RevokePrivilegesFromAccountRole( - ctx, - &sdk.AccountRoleGrantPrivileges{ - SchemaObjectPrivileges: privileges, - }, - &sdk.AccountRoleGrantOn{ - SchemaObject: &sdk.GrantOnSchemaObject{ - SchemaObject: &sdk.Object{ - ObjectType: sdk.ObjectTypeTable, - Name: tableName, - }, - }, - }, - accountRoleId, - new(sdk.RevokePrivilegesFromAccountRoleOptions), - ) - if err != nil { - t.Fatal(err) - } + client := acc.TestClient() - err = client.Grants.GrantPrivilegesToAccountRole( - ctx, - &sdk.AccountRoleGrantPrivileges{ - SchemaObjectPrivileges: privileges, - }, - &sdk.AccountRoleGrantOn{ - SchemaObject: &sdk.GrantOnSchemaObject{ - SchemaObject: &sdk.Object{ - ObjectType: sdk.ObjectTypeTable, - Name: tableName, - }, - }, - }, - accountRoleId, - &sdk.GrantPrivilegesToAccountRoleOptions{ - WithGrantOption: sdk.Bool(withGrantOption), - }, - ) - if err != nil { - t.Fatal(err) - } + client.Grant.RevokePrivilegesOnSchemaObjectFromAccountRole(t, accountRoleId, sdk.ObjectTypeTable, tableName, privileges) + client.Grant.GrantPrivilegesOnSchemaObjectToAccountRole(t, accountRoleId, sdk.ObjectTypeTable, tableName, privileges, withGrantOption) } // proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2621 doesn't apply to this resource diff --git a/pkg/resources/grant_privileges_to_database_role_acceptance_test.go b/pkg/resources/grant_privileges_to_database_role_acceptance_test.go index a272447b7e..47872e837e 100644 --- a/pkg/resources/grant_privileges_to_database_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_database_role_acceptance_test.go @@ -1358,45 +1358,16 @@ func queriedPrivilegesToDatabaseRoleContainAtLeast(databaseRoleName sdk.Database func revokeAndGrantPrivilegesOnDatabaseToDatabaseRole( t *testing.T, - databaseRoleName sdk.DatabaseObjectIdentifier, + databaseRoleId sdk.DatabaseObjectIdentifier, databaseId sdk.AccountObjectIdentifier, privileges []sdk.AccountObjectPrivilege, withGrantOption bool, ) { t.Helper() - client := acc.Client(t) - ctx := context.Background() - err := client.Grants.RevokePrivilegesFromDatabaseRole( - ctx, - &sdk.DatabaseRoleGrantPrivileges{ - DatabasePrivileges: privileges, - }, - &sdk.DatabaseRoleGrantOn{ - Database: sdk.Pointer(databaseId), - }, - databaseRoleName, - new(sdk.RevokePrivilegesFromDatabaseRoleOptions), - ) - if err != nil { - t.Fatal(err) - } + client := acc.TestClient() - err = client.Grants.GrantPrivilegesToDatabaseRole( - ctx, - &sdk.DatabaseRoleGrantPrivileges{ - DatabasePrivileges: privileges, - }, - &sdk.DatabaseRoleGrantOn{ - Database: sdk.Pointer(databaseId), - }, - databaseRoleName, - &sdk.GrantPrivilegesToDatabaseRoleOptions{ - WithGrantOption: sdk.Bool(withGrantOption), - }, - ) - if err != nil { - t.Fatal(err) - } + client.Grant.RevokePrivilegesOnDatabaseFromDatabaseRole(t, databaseRoleId, databaseId, privileges) + client.Grant.GrantPrivilegesOnDatabaseToDatabaseRole(t, databaseRoleId, databaseId, privileges, withGrantOption) } func TestAcc_GrantPrivilegesToDatabaseRole_migrateFromV0941_ensureSmoothUpgradeWithNewResourceId(t *testing.T) { From 67652d25a8745993dde2b51b381a18a6afaf673c Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:04:27 +0200 Subject: [PATCH 07/21] Use client external volume --- ...vileges_to_account_role_acceptance_test.go | 53 +++++-------------- 1 file changed, 14 insertions(+), 39 deletions(-) diff --git a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go index 29b05973b8..dbbf4d5fd4 100644 --- a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go @@ -7,15 +7,14 @@ import ( "testing" acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" - "github.com/stretchr/testify/require" ) func TestAcc_GrantPrivilegesToAccountRole_OnAccount(t *testing.T) { @@ -1250,12 +1249,17 @@ func TestAcc_GrantPrivilegesToAccountRole_MultiplePartsInRoleName(t *testing.T) // proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2533 is fixed func TestAcc_GrantPrivilegesToAccountRole_OnExternalVolume(t *testing.T) { - roleId := acc.TestClient().Ids.RandomAccountObjectIdentifier() - roleFullyQualifiedName := roleId.FullyQualifiedName() - externalVolumeName := acc.TestClient().Ids.Alpha() + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + acc.TestAccPreCheck(t) + + role, roleCleanup := acc.TestClient().Role.CreateRole(t) + t.Cleanup(roleCleanup) + externalVolumeId, cleanupExternalVolume := acc.TestClient().ExternalVolume.Create(t) + t.Cleanup(cleanupExternalVolume) + configVariables := config.Variables{ - "name": config.StringVariable(roleFullyQualifiedName), - "external_volume": config.StringVariable(externalVolumeName), + "name": config.StringVariable(role.ID().FullyQualifiedName()), + "external_volume": config.StringVariable(externalVolumeId.Name()), "privileges": config.ListVariable( config.StringVariable(string(sdk.AccountObjectPrivilegeUsage)), ), @@ -1272,23 +1276,17 @@ func TestAcc_GrantPrivilegesToAccountRole_OnExternalVolume(t *testing.T) { CheckDestroy: acc.CheckAccountRolePrivilegesRevoked(t), Steps: []resource.TestStep{ { - PreConfig: func() { - _, roleCleanup := acc.TestClient().Role.CreateRoleWithIdentifier(t, roleId) - t.Cleanup(roleCleanup) - cleanupExternalVolume := createExternalVolume(t, externalVolumeName) - t.Cleanup(cleanupExternalVolume) - }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToAccountRole/OnExternalVolume"), ConfigVariables: configVariables, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "account_role_name", roleFullyQualifiedName), + resource.TestCheckResourceAttr(resourceName, "account_role_name", role.ID().FullyQualifiedName()), resource.TestCheckResourceAttr(resourceName, "privileges.#", "1"), resource.TestCheckResourceAttr(resourceName, "privileges.0", string(sdk.AccountObjectPrivilegeUsage)), resource.TestCheckResourceAttr(resourceName, "with_grant_option", "true"), resource.TestCheckResourceAttr(resourceName, "on_account_object.#", "1"), resource.TestCheckResourceAttr(resourceName, "on_account_object.0.object_type", "EXTERNAL VOLUME"), - resource.TestCheckResourceAttr(resourceName, "on_account_object.0.object_name", externalVolumeName), - resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s|true|false|USAGE|OnAccountObject|EXTERNAL VOLUME|\"%s\"", roleFullyQualifiedName, externalVolumeName)), + resource.TestCheckResourceAttr(resourceName, "on_account_object.0.object_name", externalVolumeId.Name()), + resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s|true|false|USAGE|OnAccountObject|EXTERNAL VOLUME|%s", role.ID().FullyQualifiedName(), externalVolumeId.FullyQualifiedName())), ), }, }, @@ -1651,29 +1649,6 @@ func queriedAccountRolePrivilegesContainAtLeast(roleName sdk.AccountObjectIdenti }, roleName, privileges...) } -func createExternalVolume(t *testing.T, externalVolumeName string) func() { - t.Helper() - - client := acc.Client(t) - ctx := context.Background() - _, err := client.ExecForTests(ctx, fmt.Sprintf(`create external volume "%s" storage_locations = ( - ( - name = 'test' - storage_provider = 's3' - storage_base_url = 's3://my_example_bucket/' - storage_aws_role_arn = 'arn:aws:iam::123456789012:role/myrole' - encryption=(type='aws_sse_kms' kms_key_id='1234abcd-12ab-34cd-56ef-1234567890ab') - ) -) -`, externalVolumeName)) - require.NoError(t, err) - - return func() { - _, err = client.ExecForTests(ctx, fmt.Sprintf(`drop external volume "%s"`, externalVolumeName)) - require.NoError(t, err) - } -} - func TestAcc_GrantPrivilegesToAccountRole_migrateFromV0941_ensureSmoothUpgradeWithNewResourceId(t *testing.T) { accountRoleId := acc.TestClient().Ids.RandomAccountObjectIdentifier() schemaId := acc.TestClient().Ids.SchemaId() From 53d3a02748e9bb3f398438f3348a2bc90899db1a Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:10:09 +0200 Subject: [PATCH 08/21] Add granting ownership to helper --- pkg/acceptance/helpers/grant_client.go | 25 +++++++++++++++++++ pkg/resources/database_old_acceptance_test.go | 1 + .../grant_ownership_acceptance_test.go | 24 +----------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/pkg/acceptance/helpers/grant_client.go b/pkg/acceptance/helpers/grant_client.go index 1795f20f33..bc09453de0 100644 --- a/pkg/acceptance/helpers/grant_client.go +++ b/pkg/acceptance/helpers/grant_client.go @@ -154,3 +154,28 @@ func (c *GrantClient) GrantPrivilegesOnDatabaseToDatabaseRole( ) require.NoError(t, err) } + +func (c *GrantClient) GrantOwnershipToAccountRole( + t *testing.T, + accountRoleId sdk.AccountObjectIdentifier, + objectType sdk.ObjectType, + objectName sdk.ObjectIdentifier, +) { + t.Helper() + ctx := context.Background() + + err := c.client().GrantOwnership( + ctx, + sdk.OwnershipGrantOn{ + Object: &sdk.Object{ + ObjectType: objectType, + Name: objectName, + }, + }, + sdk.OwnershipGrantTo{ + AccountRoleName: &accountRoleId, + }, + new(sdk.GrantOwnershipOptions), + ) + require.NoError(t, err) +} diff --git a/pkg/resources/database_old_acceptance_test.go b/pkg/resources/database_old_acceptance_test.go index 2ac6014380..255aa8b25a 100644 --- a/pkg/resources/database_old_acceptance_test.go +++ b/pkg/resources/database_old_acceptance_test.go @@ -396,6 +396,7 @@ func testAccCheckIfDatabaseIsReplicated(t *testing.T, id string) func(state *ter client := acc.Client(t) ctx := context.Background() + // TODO [helper-cleanup]: to helper replicationDatabases, err := client.ReplicationFunctions.ShowReplicationDatabases(ctx, nil) if err != nil { return err diff --git a/pkg/resources/grant_ownership_acceptance_test.go b/pkg/resources/grant_ownership_acceptance_test.go index 63212e069f..88d113244f 100644 --- a/pkg/resources/grant_ownership_acceptance_test.go +++ b/pkg/resources/grant_ownership_acceptance_test.go @@ -16,7 +16,6 @@ import ( "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-plugin-testing/tfversion" - "github.com/stretchr/testify/assert" ) func TestAcc_GrantOwnership_OnObject_Database_ToAccountRole(t *testing.T) { @@ -945,7 +944,7 @@ func TestAcc_GrantOwnership_MoveOwnershipOutsideTerraform(t *testing.T) { }, { PreConfig: func() { - moveResourceOwnershipToAccountRole(t, sdk.ObjectTypeDatabase, databaseId, otherAccountRoleId) + acc.TestClient().Grant.GrantOwnershipToAccountRole(t, otherAccountRoleId, sdk.ObjectTypeDatabase, databaseId) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantOwnership/MoveResourceOwnershipOutsideTerraform"), ConfigVariables: configVariables, @@ -1319,27 +1318,6 @@ func TestAcc_GrantOwnership_OnDatabaseRole(t *testing.T) { }) } -func moveResourceOwnershipToAccountRole(t *testing.T, objectType sdk.ObjectType, objectName sdk.ObjectIdentifier, accountRoleName sdk.AccountObjectIdentifier) { - t.Helper() - - client := acc.Client(t) - ctx := context.Background() - err := client.Grants.GrantOwnership( - ctx, - sdk.OwnershipGrantOn{ - Object: &sdk.Object{ - ObjectType: objectType, - Name: objectName, - }, - }, - sdk.OwnershipGrantTo{ - AccountRoleName: &accountRoleName, - }, - new(sdk.GrantOwnershipOptions), - ) - assert.NoError(t, err) -} - func checkResourceOwnershipIsGranted(opts *sdk.ShowGrantOptions, grantOn sdk.ObjectType, roleName string, objectNames ...string) func(s *terraform.State) error { return func(s *terraform.State) error { client := acc.TestAccProvider.Meta().(*provider.Context).Client From 289c4db86d524a9f530c0bf0a52c42bc112a7ce4 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:16:14 +0200 Subject: [PATCH 09/21] Extract external table helper --- .../helpers/external_table_client.go | 33 +++++++++++++++++++ pkg/acceptance/helpers/test_client.go | 2 ++ .../external_table_acceptance_test.go | 14 +------- 3 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 pkg/acceptance/helpers/external_table_client.go diff --git a/pkg/acceptance/helpers/external_table_client.go b/pkg/acceptance/helpers/external_table_client.go new file mode 100644 index 0000000000..dcd1aadb91 --- /dev/null +++ b/pkg/acceptance/helpers/external_table_client.go @@ -0,0 +1,33 @@ +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) { + 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) +} diff --git a/pkg/acceptance/helpers/test_client.go b/pkg/acceptance/helpers/test_client.go index 77b0d44b6c..6efd1207fb 100644 --- a/pkg/acceptance/helpers/test_client.go +++ b/pkg/acceptance/helpers/test_client.go @@ -26,6 +26,7 @@ type TestClient struct { DynamicTable *DynamicTableClient ExternalAccessIntegration *ExternalAccessIntegrationClient ExternalFunction *ExternalFunctionClient + ExternalTable *ExternalTableClient ExternalVolume *ExternalVolumeClient FailoverGroup *FailoverGroupClient FileFormat *FileFormatClient @@ -89,6 +90,7 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri DynamicTable: NewDynamicTableClient(context, idsGenerator), ExternalAccessIntegration: NewExternalAccessIntegrationClient(context, idsGenerator), ExternalFunction: NewExternalFunctionClient(context, idsGenerator), + ExternalTable: NewExternalTableClient(context, idsGenerator), ExternalVolume: NewExternalVolumeClient(context, idsGenerator), FailoverGroup: NewFailoverGroupClient(context, idsGenerator), FileFormat: NewFileFormatClient(context, idsGenerator), diff --git a/pkg/resources/external_table_acceptance_test.go b/pkg/resources/external_table_acceptance_test.go index 266b128dda..292a2a1463 100644 --- a/pkg/resources/external_table_acceptance_test.go +++ b/pkg/resources/external_table_acceptance_test.go @@ -75,7 +75,7 @@ func TestAcc_ExternalTable_basic(t *testing.T) { }, { PreConfig: func() { - publishExternalTablesTestData(t, id, data) + acc.TestClient().ExternalTable.PublishDataToStage(t, id, data) }, ConfigDirectory: config.TestStepDirectory(), ConfigVariables: configVariables, @@ -348,18 +348,6 @@ func externalTableContainsData(id sdk.SchemaObjectIdentifier, contains func(rows } } -// TODO [helper-cleanup]: to helper -func publishExternalTablesTestData(t *testing.T, stageName sdk.SchemaObjectIdentifier, data []byte) { - t.Helper() - client := acc.Client(t) - ctx := context.Background() - - _, err := client.ExecForTests(ctx, fmt.Sprintf(`copy into @%s/external_tables_test_data/test_data from (select parse_json('%s')) overwrite = true`, stageName.FullyQualifiedName(), string(data))) - if err != nil { - log.Fatal(err) - } -} - func expectTableToHaveColumnDataTypes(id sdk.SchemaObjectIdentifier, expectedDataTypes []sdk.DataType) func(s *terraform.State) error { return func(s *terraform.State) error { client := acc.TestAccProvider.Meta().(*provider.Context).Client From df6be531313d544ffff774a37dad7f02a3932b5a Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:22:38 +0200 Subject: [PATCH 10/21] Move replication databases to the helper --- pkg/acceptance/helpers/database_client.go | 7 +++++++ pkg/acceptance/helpers/external_table_client.go | 1 + pkg/resources/database_old_acceptance_test.go | 6 +----- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/acceptance/helpers/database_client.go b/pkg/acceptance/helpers/database_client.go index 3a3b945cad..d5474476b3 100644 --- a/pkg/acceptance/helpers/database_client.go +++ b/pkg/acceptance/helpers/database_client.go @@ -177,3 +177,10 @@ func (c *DatabaseClient) CreateDatabaseFromShareTemporarily(t *testing.T, extern 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) +} diff --git a/pkg/acceptance/helpers/external_table_client.go b/pkg/acceptance/helpers/external_table_client.go index dcd1aadb91..950bd1e416 100644 --- a/pkg/acceptance/helpers/external_table_client.go +++ b/pkg/acceptance/helpers/external_table_client.go @@ -26,6 +26,7 @@ func (c *ExternalTableClient) client() sdk.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))) diff --git a/pkg/resources/database_old_acceptance_test.go b/pkg/resources/database_old_acceptance_test.go index 255aa8b25a..31d5e46702 100644 --- a/pkg/resources/database_old_acceptance_test.go +++ b/pkg/resources/database_old_acceptance_test.go @@ -393,11 +393,7 @@ func testAccCheckDatabaseExistence(t *testing.T, id sdk.AccountObjectIdentifier, func testAccCheckIfDatabaseIsReplicated(t *testing.T, id string) func(state *terraform.State) error { t.Helper() return func(state *terraform.State) error { - client := acc.Client(t) - - ctx := context.Background() - // TODO [helper-cleanup]: to helper - replicationDatabases, err := client.ReplicationFunctions.ShowReplicationDatabases(ctx, nil) + replicationDatabases, err := acc.TestClient().Database.ShowAllReplicationDatabases(t) if err != nil { return err } From 644371eb583256dd001b07e8bf172ebea8d4d80f Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:43:14 +0200 Subject: [PATCH 11/21] Remove old policy references from helpers --- .../assert/objectassert/view_snowflake_ext.go | 12 ++--- pkg/acceptance/check_destroy.go | 8 +--- pkg/acceptance/helpers/common.go | 18 ------- .../helpers/policy_references_client.go | 28 +++++------ .../testint/event_tables_integration_test.go | 10 ++-- ...materialized_views_gen_integration_test.go | 2 +- pkg/sdk/testint/views_gen_integration_test.go | 47 +++++++++---------- 7 files changed, 49 insertions(+), 76 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/assert/objectassert/view_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/objectassert/view_snowflake_ext.go index 494bdadc1a..b50fd52833 100644 --- a/pkg/acceptance/bettertestspoc/assert/objectassert/view_snowflake_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/objectassert/view_snowflake_ext.go @@ -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) @@ -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) diff --git a/pkg/acceptance/check_destroy.go b/pkg/acceptance/check_destroy.go index 617c90e5d2..9f2bc79a7c 100644 --- a/pkg/acceptance/check_destroy.go +++ b/pkg/acceptance/check_destroy.go @@ -421,18 +421,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 diff --git a/pkg/acceptance/helpers/common.go b/pkg/acceptance/helpers/common.go index 574f33a1fb..153b007e39 100644 --- a/pkg/acceptance/helpers/common.go +++ b/pkg/acceptance/helpers/common.go @@ -2,7 +2,6 @@ package helpers import ( "context" - "database/sql" "fmt" "log" "testing" @@ -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"` -} diff --git a/pkg/acceptance/helpers/policy_references_client.go b/pkg/acceptance/helpers/policy_references_client.go index 15b6d6b736..f87beba335 100644 --- a/pkg/acceptance/helpers/policy_references_client.go +++ b/pkg/acceptance/helpers/policy_references_client.go @@ -2,10 +2,10 @@ package helpers import ( "context" - "fmt" "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/stretchr/testify/require" ) type PolicyReferencesClient struct { @@ -18,30 +18,28 @@ func NewPolicyReferencesClient(context *TestClientContext) *PolicyReferencesClie } } -func (c *PolicyReferencesClient) client() sdk.RowAccessPolicies { - return c.context.client.RowAccessPolicies +func (c *PolicyReferencesClient) client() sdk.PolicyReferences { + return c.context.client.PolicyReferences } // GetPolicyReferences is based on https://docs.snowflake.com/en/sql-reference/functions/policy_references. -func (c *PolicyReferencesClient) GetPolicyReferences(t *testing.T, id sdk.SchemaObjectIdentifier, objectType sdk.ObjectType) ([]PolicyReference, error) { +func (c *PolicyReferencesClient) GetPolicyReferences(t *testing.T, objectId sdk.ObjectIdentifier, entity sdk.PolicyEntityDomain) ([]sdk.PolicyReference, error) { t.Helper() ctx := context.Background() - s := []PolicyReference{} - policyReferencesId := sdk.NewSchemaObjectIdentifier(id.DatabaseName(), "INFORMATION_SCHEMA", "POLICY_REFERENCES") - err := c.context.client.QueryForTests(ctx, &s, fmt.Sprintf(`SELECT * FROM TABLE(%s(REF_ENTITY_NAME => '%s', REF_ENTITY_DOMAIN => '%v'))`, policyReferencesId.FullyQualifiedName(), id.FullyQualifiedName(), objectType)) - - return s, err + return c.client().GetForEntity(ctx, sdk.NewGetForEntityPolicyReferenceRequest( + objectId, + entity, + )) } // GetPolicyReference is based on https://docs.snowflake.com/en/sql-reference/functions/policy_references. -func (c *PolicyReferencesClient) GetPolicyReference(t *testing.T, id sdk.SchemaObjectIdentifier, objectType sdk.ObjectType) (*PolicyReference, error) { +func (c *PolicyReferencesClient) GetPolicyReference(t *testing.T, id sdk.ObjectIdentifier, entity sdk.PolicyEntityDomain) (*sdk.PolicyReference, error) { t.Helper() - ctx := context.Background() - s := &PolicyReference{} - policyReferencesId := sdk.NewSchemaObjectIdentifier(id.DatabaseName(), "INFORMATION_SCHEMA", "POLICY_REFERENCES") - err := c.context.client.QueryOneForTests(ctx, s, fmt.Sprintf(`SELECT * FROM TABLE(%s(REF_ENTITY_NAME => '%s', REF_ENTITY_DOMAIN => '%v'))`, policyReferencesId.FullyQualifiedName(), id.FullyQualifiedName(), objectType)) + references, err := c.GetPolicyReferences(t, id, entity) + require.NoError(t, err) + require.Len(t, references, 1) - return s, err + return sdk.Pointer(references[0]), nil } diff --git a/pkg/sdk/testint/event_tables_integration_test.go b/pkg/sdk/testint/event_tables_integration_test.go index d18a4d31ba..8195039a47 100644 --- a/pkg/sdk/testint/event_tables_integration_test.go +++ b/pkg/sdk/testint/event_tables_integration_test.go @@ -224,7 +224,7 @@ func TestInt_EventTables(t *testing.T) { err := client.EventTables.Alter(ctx, alterRequest) require.NoError(t, err) - e, err := testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.ObjectTypeTable) + e, err := testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.PolicyEntityDomainTable) require.NoError(t, err) assert.Equal(t, rowAccessPolicy.ID().Name(), e.PolicyName) assert.Equal(t, "ROW_ACCESS_POLICY", e.PolicyKind) @@ -237,7 +237,7 @@ func TestInt_EventTables(t *testing.T) { err = client.EventTables.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.ObjectTypeTable) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.PolicyEntityDomainTable) require.Error(t, err, "no rows in result set") // add policy again @@ -245,7 +245,7 @@ func TestInt_EventTables(t *testing.T) { err = client.EventTables.Alter(ctx, alterRequest) require.NoError(t, err) - e, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.ObjectTypeTable) + e, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.PolicyEntityDomainTable) require.NoError(t, err) assert.Equal(t, rowAccessPolicy.ID().Name(), e.PolicyName) @@ -257,7 +257,7 @@ func TestInt_EventTables(t *testing.T) { err = client.EventTables.Alter(ctx, alterRequest) require.NoError(t, err) - e, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.ObjectTypeTable) + e, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.PolicyEntityDomainTable) require.NoError(t, err) assert.Equal(t, rowAccessPolicy2.ID().Name(), e.PolicyName) @@ -266,7 +266,7 @@ func TestInt_EventTables(t *testing.T) { err = client.EventTables.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.ObjectTypeView) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, table.ID(), sdk.PolicyEntityDomainView) require.Error(t, err, "no rows in result set") }) } diff --git a/pkg/sdk/testint/materialized_views_gen_integration_test.go b/pkg/sdk/testint/materialized_views_gen_integration_test.go index 05df49de67..5d93a001f0 100644 --- a/pkg/sdk/testint/materialized_views_gen_integration_test.go +++ b/pkg/sdk/testint/materialized_views_gen_integration_test.go @@ -136,7 +136,7 @@ func TestInt_MaterializedViews(t *testing.T) { view := createMaterializedViewWithRequest(t, request) assertMaterializedViewWithOptions(t, view, id, true, "comment", fmt.Sprintf(`LINEAR("%s")`, "COLUMN_WITH_COMMENT")) - rowAccessPolicyReference, err := testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReference, err := testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) assert.Equal(t, rowAccessPolicy.Name, rowAccessPolicyReference.PolicyName) assert.Equal(t, "ROW_ACCESS_POLICY", rowAccessPolicyReference.PolicyKind) diff --git a/pkg/sdk/testint/views_gen_integration_test.go b/pkg/sdk/testint/views_gen_integration_test.go index eee3623a07..a2597c8cef 100644 --- a/pkg/sdk/testint/views_gen_integration_test.go +++ b/pkg/sdk/testint/views_gen_integration_test.go @@ -10,7 +10,6 @@ import ( assertions "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/objectassert" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/snowflakeroles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -86,7 +85,7 @@ func TestInt_Views(t *testing.T) { }, *viewDetails) } - assertPolicyReference := func(t *testing.T, policyRef helpers.PolicyReference, + assertPolicyReference := func(t *testing.T, policyRef sdk.PolicyReference, policyId sdk.SchemaObjectIdentifier, policyType string, viewId sdk.SchemaObjectIdentifier, @@ -99,10 +98,10 @@ func TestInt_Views(t *testing.T) { assert.Equal(t, "VIEW", policyRef.RefEntityDomain) assert.Equal(t, "ACTIVE", policyRef.PolicyStatus) if refColumnName != nil { - assert.True(t, policyRef.RefColumnName.Valid) - assert.Equal(t, *refColumnName, policyRef.RefColumnName.String) + assert.NotNil(t, policyRef.RefColumnName) + assert.Equal(t, *refColumnName, *policyRef.RefColumnName) } else { - assert.False(t, policyRef.RefColumnName.Valid) + assert.Nil(t, policyRef.RefColumnName) } } @@ -198,18 +197,18 @@ func TestInt_Views(t *testing.T) { view := createViewWithRequest(t, request) assertViewWithOptions(t, view, id, true, "comment") - rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) assert.Len(t, rowAccessPolicyReferences, 2) - slices.SortFunc(rowAccessPolicyReferences, func(x, y helpers.PolicyReference) int { + slices.SortFunc(rowAccessPolicyReferences, func(x, y sdk.PolicyReference) int { return cmp.Compare(x.PolicyKind, y.PolicyKind) }) assertPolicyReference(t, rowAccessPolicyReferences[0], aggregationPolicy, "AGGREGATION_POLICY", view.ID(), nil) assertPolicyReference(t, rowAccessPolicyReferences[1], rowAccessPolicy.ID(), "ROW_ACCESS_POLICY", view.ID(), nil) - assert.True(t, rowAccessPolicyReferences[1].RefArgColumnNames.Valid) - refArgColumnNames := sdk.ParseCommaSeparatedStringArray(rowAccessPolicyReferences[1].RefArgColumnNames.String, true) + require.NotNil(t, rowAccessPolicyReferences[1].RefArgColumnNames) + refArgColumnNames := sdk.ParseCommaSeparatedStringArray(*rowAccessPolicyReferences[1].RefArgColumnNames, true) assert.Len(t, refArgColumnNames, 1) assert.Equal(t, "column_with_comment", refArgColumnNames[0]) }) @@ -238,10 +237,10 @@ func TestInt_Views(t *testing.T) { assertViewWithOptions(t, view, id, false, "") assert.Contains(t, view.Text, "RECURSIVE VIEW") - rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) assert.Len(t, rowAccessPolicyReferences, 2) - slices.SortFunc(rowAccessPolicyReferences, func(x, y helpers.PolicyReference) int { + slices.SortFunc(rowAccessPolicyReferences, func(x, y sdk.PolicyReference) int { return cmp.Compare(x.PolicyKind, y.PolicyKind) }) @@ -405,7 +404,7 @@ func TestInt_Views(t *testing.T) { err := client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - policyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.ObjectTypeView) + policyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) require.Len(t, policyReferences, 1) @@ -417,7 +416,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.Error(t, err, "no rows in result set") }) @@ -434,7 +433,7 @@ func TestInt_Views(t *testing.T) { err := client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) require.Len(t, rowAccessPolicyReferences, 1) @@ -446,7 +445,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.Error(t, err, "no rows in result set") }) @@ -504,7 +503,7 @@ func TestInt_Views(t *testing.T) { err := client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - rowAccessPolicyReference, err := testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReference, err := testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) assertPolicyReference(t, *rowAccessPolicyReference, rowAccessPolicy.ID(), "ROW_ACCESS_POLICY", view.ID(), nil) @@ -514,7 +513,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.Error(t, err, "no rows in result set") // add policy again @@ -522,7 +521,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - rowAccessPolicyReference, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReference, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) assert.Equal(t, rowAccessPolicy.ID().Name(), rowAccessPolicyReference.PolicyName) @@ -534,7 +533,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - rowAccessPolicyReference, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReference, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) assert.Equal(t, rowAccessPolicy2.ID().Name(), rowAccessPolicyReference.PolicyName) @@ -543,7 +542,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.Error(t, err, "no rows in result set") }) @@ -616,7 +615,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.Error(t, err, "no rows in result set") }) @@ -634,7 +633,7 @@ func TestInt_Views(t *testing.T) { err := client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReferences, err := testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) require.Len(t, rowAccessPolicyReferences, 1) @@ -647,7 +646,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - rowAccessPolicyReferences, err = testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.ObjectTypeView) + rowAccessPolicyReferences, err = testClientHelper().PolicyReferences.GetPolicyReferences(t, view.ID(), sdk.PolicyEntityDomainView) require.NoError(t, err) require.Len(t, rowAccessPolicyReferences, 1) @@ -658,7 +657,7 @@ func TestInt_Views(t *testing.T) { err = client.Views.Alter(ctx, alterRequest) require.NoError(t, err) - _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.ObjectTypeView) + _, err = testClientHelper().PolicyReferences.GetPolicyReference(t, view.ID(), sdk.PolicyEntityDomainView) require.Error(t, err, "no rows in result set") }) From 41e9130f335ac804aa26f44815ef9787d349ee41 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:49:22 +0200 Subject: [PATCH 12/21] Extract showing grants to share to helper --- pkg/acceptance/check_destroy.go | 10 +--------- pkg/acceptance/helpers/grant_client.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/acceptance/check_destroy.go b/pkg/acceptance/check_destroy.go index 9f2bc79a7c..fffbbbff38 100644 --- a/pkg/acceptance/check_destroy.go +++ b/pkg/acceptance/check_destroy.go @@ -386,23 +386,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 } diff --git a/pkg/acceptance/helpers/grant_client.go b/pkg/acceptance/helpers/grant_client.go index bc09453de0..d7637354c1 100644 --- a/pkg/acceptance/helpers/grant_client.go +++ b/pkg/acceptance/helpers/grant_client.go @@ -179,3 +179,16 @@ func (c *GrantClient) GrantOwnershipToAccountRole( ) require.NoError(t, err) } + +func (c *GrantClient) ShowGrantsToShare(t *testing.T, shareId sdk.AccountObjectIdentifier) ([]sdk.Grant, error) { + t.Helper() + ctx := context.Background() + + return c.client().Show(ctx, &sdk.ShowGrantOptions{ + To: &sdk.ShowGrantsTo{ + Share: &sdk.ShowGrantsToShare{ + Name: shareId, + }, + }, + }) +} From 140dba06dd21177edb492dced4a6c1ecfd273773 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:52:28 +0200 Subject: [PATCH 13/21] Extract showing grants of roles to helpers --- pkg/acceptance/check_destroy.go | 16 ++-------------- pkg/acceptance/helpers/grant_client.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pkg/acceptance/check_destroy.go b/pkg/acceptance/check_destroy.go index fffbbbff38..451c9c1bf8 100644 --- a/pkg/acceptance/check_destroy.go +++ b/pkg/acceptance/check_destroy.go @@ -236,24 +236,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 } @@ -278,24 +272,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 } diff --git a/pkg/acceptance/helpers/grant_client.go b/pkg/acceptance/helpers/grant_client.go index d7637354c1..4048631142 100644 --- a/pkg/acceptance/helpers/grant_client.go +++ b/pkg/acceptance/helpers/grant_client.go @@ -192,3 +192,25 @@ func (c *GrantClient) ShowGrantsToShare(t *testing.T, shareId sdk.AccountObjectI }, }) } + +func (c *GrantClient) ShowGrantsOfAccountRole(t *testing.T, accountRoleId sdk.AccountObjectIdentifier) ([]sdk.Grant, error) { + t.Helper() + ctx := context.Background() + + return c.client().Show(ctx, &sdk.ShowGrantOptions{ + Of: &sdk.ShowGrantsOf{ + Role: accountRoleId, + }, + }) +} + +func (c *GrantClient) ShowGrantsOfDatabaseRole(t *testing.T, databaseRoleId sdk.DatabaseObjectIdentifier) ([]sdk.Grant, error) { + t.Helper() + ctx := context.Background() + + return c.client().Show(ctx, &sdk.ShowGrantOptions{ + Of: &sdk.ShowGrantsOf{ + DatabaseRole: databaseRoleId, + }, + }) +} From 43a5665a92f259030f922cfbcdc9c4f87378a521 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:55:25 +0200 Subject: [PATCH 14/21] Extract showing grants to roles to helpers --- pkg/acceptance/check_destroy.go | 16 ++-------------- pkg/acceptance/helpers/grant_client.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pkg/acceptance/check_destroy.go b/pkg/acceptance/check_destroy.go index 451c9c1bf8..00752299a3 100644 --- a/pkg/acceptance/check_destroy.go +++ b/pkg/acceptance/check_destroy.go @@ -302,21 +302,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 @@ -338,21 +332,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 } diff --git a/pkg/acceptance/helpers/grant_client.go b/pkg/acceptance/helpers/grant_client.go index 4048631142..cd93e454e6 100644 --- a/pkg/acceptance/helpers/grant_client.go +++ b/pkg/acceptance/helpers/grant_client.go @@ -204,6 +204,17 @@ func (c *GrantClient) ShowGrantsOfAccountRole(t *testing.T, accountRoleId sdk.Ac }) } +func (c *GrantClient) ShowGrantsToAccountRole(t *testing.T, accountRoleId sdk.AccountObjectIdentifier) ([]sdk.Grant, error) { + t.Helper() + ctx := context.Background() + + return c.client().Show(ctx, &sdk.ShowGrantOptions{ + To: &sdk.ShowGrantsTo{ + Role: accountRoleId, + }, + }) +} + func (c *GrantClient) ShowGrantsOfDatabaseRole(t *testing.T, databaseRoleId sdk.DatabaseObjectIdentifier) ([]sdk.Grant, error) { t.Helper() ctx := context.Background() @@ -214,3 +225,14 @@ func (c *GrantClient) ShowGrantsOfDatabaseRole(t *testing.T, databaseRoleId sdk. }, }) } + +func (c *GrantClient) ShowGrantsToDatabaseRole(t *testing.T, databaseRoleId sdk.DatabaseObjectIdentifier) ([]sdk.Grant, error) { + t.Helper() + ctx := context.Background() + + return c.client().Show(ctx, &sdk.ShowGrantOptions{ + To: &sdk.ShowGrantsTo{ + DatabaseRole: databaseRoleId, + }, + }) +} From 973ce71bb248e29df6cfa3cdea0ebfba0feda030 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 11:56:43 +0200 Subject: [PATCH 15/21] Remove client method from acceptance tests --- pkg/acceptance/check_destroy.go | 3 ++- pkg/acceptance/testing.go | 5 ----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/acceptance/check_destroy.go b/pkg/acceptance/check_destroy.go index 00752299a3..62f0b32258 100644 --- a/pkg/acceptance/check_destroy.go +++ b/pkg/acceptance/check_destroy.go @@ -17,7 +17,8 @@ import ( func CheckDestroy(t *testing.T, resource resources.Resource) func(*terraform.State) error { t.Helper() - client := Client(t) + // TODO: use TestClient() here + client := atc.client t.Logf("running check destroy for resource %s", resource) return func(s *terraform.State) error { diff --git a/pkg/acceptance/testing.go b/pkg/acceptance/testing.go index 44ccdd24bf..a879a7c160 100644 --- a/pkg/acceptance/testing.go +++ b/pkg/acceptance/testing.go @@ -185,11 +185,6 @@ func ConfigurationDirectory(directory string) func(config.TestStepConfigRequest) } } -func Client(t *testing.T) *sdk.Client { - t.Helper() - return atc.client -} - func SecondaryClient(t *testing.T) *sdk.Client { t.Helper() return atc.secondaryClient From bb9f507611aa8b5fa5ec511beb3afa7e84c5bef9 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 12:07:36 +0200 Subject: [PATCH 16/21] Use only secondary test client --- pkg/acceptance/helpers/grant_client.go | 30 +++++++++++++++++++ pkg/acceptance/testing.go | 5 ---- pkg/resources/database_acceptance_test.go | 2 +- .../shared_database_acceptance_test.go | 26 ++-------------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/pkg/acceptance/helpers/grant_client.go b/pkg/acceptance/helpers/grant_client.go index cd93e454e6..d9740c3999 100644 --- a/pkg/acceptance/helpers/grant_client.go +++ b/pkg/acceptance/helpers/grant_client.go @@ -180,6 +180,36 @@ func (c *GrantClient) GrantOwnershipToAccountRole( require.NoError(t, err) } +func (c *GrantClient) GrantPrivilegeOnDatabaseToShare( + t *testing.T, + databaseId sdk.AccountObjectIdentifier, + shareId sdk.AccountObjectIdentifier, + privileges []sdk.ObjectPrivilege, +) func() { + t.Helper() + ctx := context.Background() + + err := c.client().GrantPrivilegeToShare(ctx, privileges, &sdk.ShareGrantOn{Database: databaseId}, shareId) + require.NoError(t, err) + + return func() { + c.RevokePrivilegeOnDatabaseFromShare(t, databaseId, shareId, privileges) + } +} + +func (c *GrantClient) RevokePrivilegeOnDatabaseFromShare( + t *testing.T, + databaseId sdk.AccountObjectIdentifier, + shareId sdk.AccountObjectIdentifier, + privileges []sdk.ObjectPrivilege, +) { + t.Helper() + ctx := context.Background() + + err := c.client().RevokePrivilegeFromShare(ctx, privileges, &sdk.ShareGrantOn{Database: databaseId}, shareId) + require.NoError(t, err) +} + func (c *GrantClient) ShowGrantsToShare(t *testing.T, shareId sdk.AccountObjectIdentifier) ([]sdk.Grant, error) { t.Helper() ctx := context.Background() diff --git a/pkg/acceptance/testing.go b/pkg/acceptance/testing.go index a879a7c160..79d52bd35f 100644 --- a/pkg/acceptance/testing.go +++ b/pkg/acceptance/testing.go @@ -185,11 +185,6 @@ func ConfigurationDirectory(directory string) func(config.TestStepConfigRequest) } } -func SecondaryClient(t *testing.T) *sdk.Client { - t.Helper() - return atc.secondaryClient -} - func DefaultConfig(t *testing.T) *gosnowflake.Config { t.Helper() return atc.config diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index ee0efcf51e..a1df0a1266 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -1097,7 +1097,7 @@ resource "snowflake_database" "test" { func TestAcc_Database_WithReplication(t *testing.T) { id := acc.TestClient().Ids.RandomAccountObjectIdentifier() - secondaryAccountLocator := acc.SecondaryClient(t).GetAccountLocator() + secondaryAccountLocator := acc.SecondaryTestClient().GetAccountLocator() secondaryAccountIdentifier := acc.SecondaryTestClient().Account.GetAccountIdentifier(t).FullyQualifiedName() resource.Test(t, resource.TestCase{ diff --git a/pkg/resources/shared_database_acceptance_test.go b/pkg/resources/shared_database_acceptance_test.go index f3dfeec96e..c31314899e 100644 --- a/pkg/resources/shared_database_acceptance_test.go +++ b/pkg/resources/shared_database_acceptance_test.go @@ -1,7 +1,6 @@ package resources_test import ( - "context" "fmt" "regexp" "strconv" @@ -18,7 +17,6 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/tfversion" - "github.com/stretchr/testify/require" ) func TestAcc_CreateSharedDatabase_Basic(t *testing.T) { @@ -277,34 +275,16 @@ func TestAcc_CreateSharedDatabase_InvalidValues(t *testing.T) { func createShareableDatabase(t *testing.T) sdk.ExternalObjectIdentifier { t.Helper() - ctx := context.Background() - share, shareCleanup := acc.SecondaryTestClient().Share.CreateShare(t) t.Cleanup(shareCleanup) sharedDatabase, sharedDatabaseCleanup := acc.SecondaryTestClient().Database.CreateDatabase(t) t.Cleanup(sharedDatabaseCleanup) - err := acc.SecondaryClient(t).Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: sharedDatabase.ID(), - }, share.ID()) - require.NoError(t, err) - t.Cleanup(func() { - err := acc.SecondaryClient(t).Grants.RevokePrivilegeFromShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{ - Database: sharedDatabase.ID(), - }, share.ID()) - require.NoError(t, err) - }) + revoke := acc.SecondaryTestClient().Grant.GrantPrivilegeOnDatabaseToShare(t, sharedDatabase.ID(), share.ID(), []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}) + t.Cleanup(revoke) - err = acc.SecondaryClient(t).Shares.Alter(ctx, share.ID(), &sdk.AlterShareOptions{ - IfExists: sdk.Bool(true), - Set: &sdk.ShareSet{ - Accounts: []sdk.AccountIdentifier{ - acc.TestClient().Account.GetAccountIdentifier(t), - }, - }, - }) - require.NoError(t, err) + acc.SecondaryTestClient().Share.SetAccountOnShare(t, acc.TestClient().Account.GetAccountIdentifier(t), share.ID()) return sdk.NewExternalObjectIdentifier(acc.SecondaryTestClient().Account.GetAccountIdentifier(t), share.ID()) } From b6b21abad1908c179974081e4683fc40ce0b10a5 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 12:09:34 +0200 Subject: [PATCH 17/21] Add TODO --- pkg/sdk/testint/setup_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/sdk/testint/setup_test.go b/pkg/sdk/testint/setup_test.go index 345971f70f..72ae3c04cc 100644 --- a/pkg/sdk/testint/setup_test.go +++ b/pkg/sdk/testint/setup_test.go @@ -260,6 +260,7 @@ func timer(name string) func() { } } +// TODO: distinguish between testClient and testClientHelper in tests (one is tested, the second helps with the tests, they should be the different ones) func testClient(t *testing.T) *sdk.Client { t.Helper() return itc.client From ad4b88e9db106b1be4e14950b67215fe9c067660 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 12:17:01 +0200 Subject: [PATCH 18/21] Assign issue number to the TODOs --- pkg/acceptance/check_destroy.go | 2 +- pkg/sdk/testint/setup_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/acceptance/check_destroy.go b/pkg/acceptance/check_destroy.go index 62f0b32258..0001f75795 100644 --- a/pkg/acceptance/check_destroy.go +++ b/pkg/acceptance/check_destroy.go @@ -17,7 +17,7 @@ import ( func CheckDestroy(t *testing.T, resource resources.Resource) func(*terraform.State) error { t.Helper() - // TODO: use TestClient() here + // TODO [SNOW-1653619]: use TestClient() here client := atc.client t.Logf("running check destroy for resource %s", resource) diff --git a/pkg/sdk/testint/setup_test.go b/pkg/sdk/testint/setup_test.go index 72ae3c04cc..83f696faca 100644 --- a/pkg/sdk/testint/setup_test.go +++ b/pkg/sdk/testint/setup_test.go @@ -260,7 +260,7 @@ func timer(name string) func() { } } -// TODO: distinguish between testClient and testClientHelper in tests (one is tested, the second helps with the tests, they should be the different ones) +// TODO [SNOW-1653619]: distinguish between testClient and testClientHelper in tests (one is tested, the second helps with the tests, they should be the different ones) func testClient(t *testing.T) *sdk.Client { t.Helper() return itc.client From 84a2fbe39356f377b60669a3ca3d8201354d5e22 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 12:20:10 +0200 Subject: [PATCH 19/21] Move helpers to a proper client --- pkg/acceptance/helpers/role_client.go | 24 ------------------- ...vileges_to_account_role_acceptance_test.go | 2 +- .../unsafe_execute_acceptance_test.go | 5 +++- 3 files changed, 5 insertions(+), 26 deletions(-) diff --git a/pkg/acceptance/helpers/role_client.go b/pkg/acceptance/helpers/role_client.go index af89bbf157..3d654a006f 100644 --- a/pkg/acceptance/helpers/role_client.go +++ b/pkg/acceptance/helpers/role_client.go @@ -152,27 +152,3 @@ func (c *RoleClient) GrantOwnershipOnSchemaObject(t *testing.T, roleId sdk.Accou ) require.NoError(t, err) } - -// TODO: move later to grants client -func (c *RoleClient) GrantPrivilegeOnDatabaseToShare(t *testing.T, databaseId sdk.AccountObjectIdentifier, shareId sdk.AccountObjectIdentifier) { - t.Helper() - ctx := context.Background() - - err := c.context.client.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeReferenceUsage}, &sdk.ShareGrantOn{Database: databaseId}, shareId) - require.NoError(t, err) -} - -// TODO: move later to grants client -func (c *RoleClient) ShowGrantsTo(t *testing.T, roleId sdk.AccountObjectIdentifier) []sdk.Grant { - t.Helper() - ctx := context.Background() - - grants, err := c.context.client.Grants.Show(ctx, &sdk.ShowGrantOptions{ - To: &sdk.ShowGrantsTo{ - Role: roleId, - }, - }) - require.NoError(t, err) - - return grants -} diff --git a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go index dbbf4d5fd4..0b70cc4375 100644 --- a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go @@ -1622,7 +1622,7 @@ func createSharedDatabaseOnSecondaryAccount(t *testing.T, databaseId sdk.Account share, shareCleanup := acc.SecondaryTestClient().Share.CreateShareWithIdentifier(t, shareId) t.Cleanup(shareCleanup) - acc.SecondaryTestClient().Role.GrantPrivilegeOnDatabaseToShare(t, database.ID(), share.ID()) + _ = acc.SecondaryTestClient().Grant.GrantPrivilegeOnDatabaseToShare(t, database.ID(), share.ID(), []sdk.ObjectPrivilege{sdk.ObjectPrivilegeReferenceUsage}) accountName := acc.TestClient().Context.CurrentAccount(t) accountId := sdk.NewAccountIdentifierFromAccountLocator(accountName) diff --git a/pkg/resources/unsafe_execute_acceptance_test.go b/pkg/resources/unsafe_execute_acceptance_test.go index b98b069aed..f778ba41c5 100644 --- a/pkg/resources/unsafe_execute_acceptance_test.go +++ b/pkg/resources/unsafe_execute_acceptance_test.go @@ -722,7 +722,10 @@ output "unsafe" { func verifyGrantExists(t *testing.T, roleId sdk.AccountObjectIdentifier, privilege sdk.AccountObjectPrivilege, shouldExist bool) func(state *terraform.State) error { t.Helper() return func(state *terraform.State) error { - grants := acc.TestClient().Role.ShowGrantsTo(t, roleId) + grants, err := acc.TestClient().Grant.ShowGrantsToAccountRole(t, roleId) + if err != nil { + return err + } if shouldExist { require.Equal(t, 1, len(grants)) From 84e8bec70e4970c66f0c8d0eb1d222b69596ed3a Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 6 Sep 2024 12:24:22 +0200 Subject: [PATCH 20/21] Move helpers to a proper client --- pkg/acceptance/helpers/grant_client.go | 30 ++++++++++++ pkg/acceptance/helpers/role_client.go | 46 ------------------- .../grant_ownership_acceptance_test.go | 4 +- pkg/resources/view_acceptance_test.go | 4 +- 4 files changed, 34 insertions(+), 50 deletions(-) diff --git a/pkg/acceptance/helpers/grant_client.go b/pkg/acceptance/helpers/grant_client.go index d9740c3999..4fd7791bfb 100644 --- a/pkg/acceptance/helpers/grant_client.go +++ b/pkg/acceptance/helpers/grant_client.go @@ -180,6 +180,36 @@ func (c *GrantClient) GrantOwnershipToAccountRole( require.NoError(t, err) } +func (c *GrantClient) GrantOwnershipOnSchemaObjectToAccountRole( + t *testing.T, + accountRoleId sdk.AccountObjectIdentifier, + objectType sdk.ObjectType, + objectId sdk.SchemaObjectIdentifier, + outboundPrivileges sdk.OwnershipCurrentGrantsOutboundPrivileges, +) { + t.Helper() + ctx := context.Background() + + err := c.client().GrantOwnership( + ctx, + sdk.OwnershipGrantOn{ + Object: &sdk.Object{ + ObjectType: objectType, + Name: objectId, + }, + }, + sdk.OwnershipGrantTo{ + AccountRoleName: sdk.Pointer(accountRoleId), + }, + &sdk.GrantOwnershipOptions{ + CurrentGrants: &sdk.OwnershipCurrentGrants{ + OutboundPrivileges: outboundPrivileges, + }, + }, + ) + require.NoError(t, err) +} + func (c *GrantClient) GrantPrivilegeOnDatabaseToShare( t *testing.T, databaseId sdk.AccountObjectIdentifier, diff --git a/pkg/acceptance/helpers/role_client.go b/pkg/acceptance/helpers/role_client.go index 3d654a006f..011af6046b 100644 --- a/pkg/acceptance/helpers/role_client.go +++ b/pkg/acceptance/helpers/role_client.go @@ -106,49 +106,3 @@ func (c *RoleClient) GrantRoleToCurrentRole(t *testing.T, id sdk.AccountObjectId })) require.NoError(t, err) } - -// TODO: move later to grants client -func (c *RoleClient) GrantOwnershipOnAccountObject(t *testing.T, roleId sdk.AccountObjectIdentifier, objectId sdk.AccountObjectIdentifier, objectType sdk.ObjectType) { - t.Helper() - ctx := context.Background() - - err := c.context.client.Grants.GrantOwnership( - ctx, - sdk.OwnershipGrantOn{ - Object: &sdk.Object{ - ObjectType: objectType, - Name: objectId, - }, - }, - sdk.OwnershipGrantTo{ - AccountRoleName: sdk.Pointer(roleId), - }, - new(sdk.GrantOwnershipOptions), - ) - require.NoError(t, err) -} - -// TODO: move later to grants client -func (c *RoleClient) GrantOwnershipOnSchemaObject(t *testing.T, roleId sdk.AccountObjectIdentifier, objectId sdk.SchemaObjectIdentifier, objectType sdk.ObjectType, outboundPrivileges sdk.OwnershipCurrentGrantsOutboundPrivileges) { - t.Helper() - ctx := context.Background() - - err := c.context.client.Grants.GrantOwnership( - ctx, - sdk.OwnershipGrantOn{ - Object: &sdk.Object{ - ObjectType: objectType, - Name: objectId, - }, - }, - sdk.OwnershipGrantTo{ - AccountRoleName: sdk.Pointer(roleId), - }, - &sdk.GrantOwnershipOptions{ - CurrentGrants: &sdk.OwnershipCurrentGrants{ - OutboundPrivileges: outboundPrivileges, - }, - }, - ) - require.NoError(t, err) -} diff --git a/pkg/resources/grant_ownership_acceptance_test.go b/pkg/resources/grant_ownership_acceptance_test.go index 88d113244f..4980f1bf37 100644 --- a/pkg/resources/grant_ownership_acceptance_test.go +++ b/pkg/resources/grant_ownership_acceptance_test.go @@ -707,7 +707,7 @@ func TestAcc_GrantOwnership_TargetObjectRemovedOutsideTerraform(t *testing.T) { { PreConfig: func() { currentRole := acc.TestClient().Context.CurrentRole(t) - acc.TestClient().Role.GrantOwnershipOnAccountObject(t, currentRole, databaseId, sdk.ObjectTypeDatabase) + acc.TestClient().Grant.GrantOwnershipToAccountRole(t, currentRole, sdk.ObjectTypeDatabase, databaseId) cleanupDatabase() }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantOwnership/OnObject_Database_ToAccountRole_NoDatabaseResource"), @@ -997,7 +997,7 @@ func TestAcc_GrantOwnership_ForceOwnershipTransferOnCreate(t *testing.T) { t.Cleanup(newRoleCleanup) database, databaseCleanup := acc.TestClient().Database.CreateDatabaseWithIdentifier(t, databaseId) t.Cleanup(databaseCleanup) - acc.TestClient().Role.GrantOwnershipOnAccountObject(t, role.ID(), database.ID(), sdk.ObjectTypeDatabase) + acc.TestClient().Grant.GrantOwnershipToAccountRole(t, role.ID(), sdk.ObjectTypeDatabase, database.ID()) }, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantOwnership/ForceOwnershipTransferOnCreate"), ConfigVariables: configVariables, diff --git a/pkg/resources/view_acceptance_test.go b/pkg/resources/view_acceptance_test.go index 50681a1859..cf29e6b78f 100644 --- a/pkg/resources/view_acceptance_test.go +++ b/pkg/resources/view_acceptance_test.go @@ -1061,7 +1061,7 @@ func TestAcc_View_Issue2640(t *testing.T) { PreConfig: func() { role, roleCleanup := acc.TestClient().Role.CreateRoleWithIdentifier(t, roleId) t.Cleanup(roleCleanup) - acc.TestClient().Role.GrantOwnershipOnSchemaObject(t, role.ID(), id, sdk.ObjectTypeView, sdk.Revoke) + acc.TestClient().Grant.GrantOwnershipOnSchemaObjectToAccountRole(t, role.ID(), sdk.ObjectTypeView, id, sdk.Revoke) }, ResourceName: "snowflake_view.test", ImportState: true, @@ -1070,7 +1070,7 @@ func TestAcc_View_Issue2640(t *testing.T) { // import with the proper role { PreConfig: func() { - acc.TestClient().Role.GrantOwnershipOnSchemaObject(t, snowflakeroles.Accountadmin, id, sdk.ObjectTypeView, sdk.Revoke) + acc.TestClient().Grant.GrantOwnershipOnSchemaObjectToAccountRole(t, snowflakeroles.Accountadmin, sdk.ObjectTypeView, id, sdk.Revoke) }, ResourceName: "snowflake_view.test", ImportState: true, From 49c4e27a96f37ecdf677a3c51d39ece3e747d68d Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 9 Sep 2024 21:49:06 +0200 Subject: [PATCH 21/21] Fix after review --- pkg/resources/unsafe_execute_acceptance_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/resources/unsafe_execute_acceptance_test.go b/pkg/resources/unsafe_execute_acceptance_test.go index f778ba41c5..33a78be8b0 100644 --- a/pkg/resources/unsafe_execute_acceptance_test.go +++ b/pkg/resources/unsafe_execute_acceptance_test.go @@ -547,7 +547,6 @@ func TestAcc_UnsafeExecute_grants(t *testing.T) { }, Steps: []resource.TestStep{ { - PreConfig: func() {}, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_UnsafeExecute_commonSetup"), ConfigVariables: createConfigVariables(execute, revert), ConfigPlanChecks: resource.ConfigPlanChecks{