From 2d5b6948d5d241de471cb632aa0d99d914577a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Mon, 8 Jan 2024 11:38:39 +0100 Subject: [PATCH 1/4] Add secondary account and fix tests --- pkg/sdk/testint/helpers_test.go | 2 +- pkg/sdk/testint/shares_integration_test.go | 69 +++++++++++----------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/pkg/sdk/testint/helpers_test.go b/pkg/sdk/testint/helpers_test.go index 861108ef30..4021d76faf 100644 --- a/pkg/sdk/testint/helpers_test.go +++ b/pkg/sdk/testint/helpers_test.go @@ -91,7 +91,7 @@ func createDatabaseWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountO return database, func() { err := client.Databases.Drop(ctx, id, nil) require.NoError(t, err) - err = client.Sessions.UseSchema(ctx, testSchema(t).ID()) + err = testClient(t).Sessions.UseSchema(ctx, testSchema(t).ID()) require.NoError(t, err) } } diff --git a/pkg/sdk/testint/shares_integration_test.go b/pkg/sdk/testint/shares_integration_test.go index 1df89f1d1a..4c29b517ab 100644 --- a/pkg/sdk/testint/shares_integration_test.go +++ b/pkg/sdk/testint/shares_integration_test.go @@ -125,10 +125,11 @@ func TestInt_SharesDrop(t *testing.T) { func TestInt_SharesAlter(t *testing.T) { client := testClient(t) + secondaryClient, err := testClientFromProfile(t, secondaryAccountProfile) + require.NoError(t, err) ctx := testContext(t) t.Run("add and remove accounts", func(t *testing.T) { - t.Skipf("Snowflake secondary account is not configured. Must be set in ~./snowflake/config.yml with profile name: %s", secondaryAccountProfile) shareTest, shareCleanup := createShare(t, client) t.Cleanup(shareCleanup) err := client.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ @@ -141,7 +142,6 @@ func TestInt_SharesAlter(t *testing.T) { }, shareTest.ID()) }) require.NoError(t, err) - secondaryClient := testSecondaryClient(t) accountsToAdd := []sdk.AccountIdentifier{ getAccountIdentifier(t, secondaryClient), } @@ -184,32 +184,32 @@ func TestInt_SharesAlter(t *testing.T) { }) t.Run("set accounts", func(t *testing.T) { - t.Skipf("Snowflake secondary account is not configured. Must be set in ~./snowflake/config.yml with profile name: %s", secondaryAccountProfile) - shareTest, shareCleanup := createShare(t, client) + db, dbCleanup := createDatabase(t, secondaryClient) + t.Cleanup(dbCleanup) + shareTest, shareCleanup := createShare(t, secondaryClient) t.Cleanup(shareCleanup) - err := client.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ - Database: testDb(t).ID(), + err := secondaryClient.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ + Database: db.ID(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { - err = client.Grants.RevokePrivilegeFromShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.RevokePrivilegeFromShareOn{ - Database: testDb(t).ID(), + err = secondaryClient.Grants.RevokePrivilegeFromShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.RevokePrivilegeFromShareOn{ + Database: db.ID(), }, shareTest.ID()) }) require.NoError(t, err) - secondaryClient := testSecondaryClient(t) accountsToSet := []sdk.AccountIdentifier{ - getAccountIdentifier(t, secondaryClient), + getAccountIdentifier(t, client), } // first add the account. - err = client.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ + err = secondaryClient.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ IfExists: sdk.Bool(true), Set: &sdk.ShareSet{ Accounts: accountsToSet, }, }) require.NoError(t, err) - shares, err := client.Shares.Show(ctx, &sdk.ShowShareOptions{ + shares, err := secondaryClient.Shares.Show(ctx, &sdk.ShowShareOptions{ Like: &sdk.Like{ Pattern: sdk.String(shareTest.Name.Name()), }, @@ -346,39 +346,40 @@ func TestInt_ShareDescribeProvider(t *testing.T) { require.NoError(t, err) }) - t.Run("describe share by name", func(t *testing.T) { - shareDetails, err := client.Shares.DescribeProvider(ctx, shareTest.ID()) - require.NoError(t, err) - 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) - }) + shareDetails, err := client.Shares.DescribeProvider(ctx, shareTest.ID()) + require.NoError(t, err) + + 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) }) } func TestInt_ShareDescribeConsumer(t *testing.T) { - consumerClient := testSecondaryClient(t) ctx := testContext(t) - providerClient := testClient(t) + providerClient := testSecondaryClient(t) + consumerClient := testClient(t) t.Run("describe share", func(t *testing.T) { - t.Skipf("Snowflake secondary account is not configured. Must be set in ~./snowflake/config.yml with profile name: %s", secondaryAccountProfile) + db, dbCleanup := createDatabase(t, providerClient) + t.Cleanup(dbCleanup) + shareTest, shareCleanup := createShare(t, providerClient) t.Cleanup(shareCleanup) err := providerClient.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ - Database: testDb(t).ID(), + Database: db.ID(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { err = providerClient.Grants.RevokePrivilegeFromShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.RevokePrivilegeFromShareOn{ - Database: testDb(t).ID(), + Database: db.ID(), }, shareTest.ID()) require.NoError(t, err) }) - // add consumer account to share. + // add a consumer account to share. err = providerClient.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ Add: &sdk.ShareAdd{ Accounts: []sdk.AccountIdentifier{ @@ -387,13 +388,13 @@ func TestInt_ShareDescribeConsumer(t *testing.T) { }, }) require.NoError(t, err) - t.Run("describe consume share", func(t *testing.T) { - shareDetails, err := consumerClient.Shares.DescribeConsumer(ctx, shareTest.ExternalID()) - require.NoError(t, err) - assert.Equal(t, 1, len(shareDetails.SharedObjects)) - sharedObject := shareDetails.SharedObjects[0] - assert.Equal(t, sdk.ObjectTypeDatabase, sharedObject.Kind) - assert.Equal(t, sdk.NewAccountObjectIdentifier(""), sharedObject.Name) - }) + + shareDetails, err := consumerClient.Shares.DescribeConsumer(ctx, shareTest.ExternalID()) + require.NoError(t, err) + + assert.Equal(t, 1, len(shareDetails.SharedObjects)) + sharedObject := shareDetails.SharedObjects[0] + assert.Equal(t, sdk.ObjectTypeDatabase, sharedObject.Kind) + assert.Equal(t, sdk.NewAccountObjectIdentifier(""), sharedObject.Name) }) } From b60e81f3876c4fb699dd034e5c0f36bd5dd64299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Mon, 8 Jan 2024 16:16:43 +0100 Subject: [PATCH 2/4] changes after review --- pkg/sdk/testint/databases_integration_test.go | 62 ++++++++------- .../failover_groups_integration_test.go | 8 +- pkg/sdk/testint/helpers_test.go | 39 +-------- pkg/sdk/testint/setup_test.go | 79 ++++++++++++++++++- pkg/sdk/testint/shares_integration_test.go | 22 ++++-- 5 files changed, 138 insertions(+), 72 deletions(-) diff --git a/pkg/sdk/testint/databases_integration_test.go b/pkg/sdk/testint/databases_integration_test.go index 7b0c086f0c..31ff69af3c 100644 --- a/pkg/sdk/testint/databases_integration_test.go +++ b/pkg/sdk/testint/databases_integration_test.go @@ -109,29 +109,33 @@ func TestInt_DatabasesCreate(t *testing.T) { } func TestInt_CreateShared(t *testing.T) { - t.Skipf("Snowflake secondary account is not configured. Must be set in ~./snowflake/config.yml with profile name: %s", secondaryAccountProfile) client := testClient(t) + secondaryClient := testSecondaryClient(t) ctx := testContext(t) - databaseTest, databaseCleanup := createDatabase(t, client) + + databaseTest, databaseCleanup := createDatabase(t, secondaryClient) t.Cleanup(databaseCleanup) - shareTest, _ := createShare(t, client) - // t.Cleanup(shareCleanup) - err := client.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ + + shareTest, shareCleanup := createShare(t, secondaryClient) + t.Cleanup(shareCleanup) + + err := secondaryClient.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ Database: databaseTest.ID(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { - err = client.Grants.RevokePrivilegeFromShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.RevokePrivilegeFromShareOn{ + err := secondaryClient.Grants.RevokePrivilegeFromShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.RevokePrivilegeFromShareOn{ Database: databaseTest.ID(), }, shareTest.ID()) + require.NoError(t, err) }) - require.NoError(t, err) - secondaryClient := testSecondaryClient(t) + accountsToSet := []sdk.AccountIdentifier{ - getAccountIdentifier(t, secondaryClient), + getAccountIdentifier(t, client), } + // first add the account. - err = client.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ + err = secondaryClient.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ IfExists: sdk.Bool(true), Set: &sdk.ShareSet{ Accounts: accountsToSet, @@ -139,15 +143,17 @@ func TestInt_CreateShared(t *testing.T) { }) databaseID := sdk.RandomAccountObjectIdentifier() - err = secondaryClient.Databases.CreateShared(ctx, databaseID, shareTest.ExternalID(), nil) + err = client.Databases.CreateShared(ctx, databaseID, shareTest.ExternalID(), nil) require.NoError(t, err) - database, err := secondaryClient.Databases.ShowByID(ctx, databaseID) - require.NoError(t, err) - assert.Equal(t, databaseID.Name(), database.Name) t.Cleanup(func() { - err = secondaryClient.Databases.Drop(ctx, databaseID, nil) + err = client.Databases.Drop(ctx, databaseID, nil) require.NoError(t, err) }) + + database, err := client.Databases.ShowByID(ctx, databaseID) + require.NoError(t, err) + + assert.Equal(t, databaseID.Name(), database.Name) } func TestInt_DatabasesCreateSecondary(t *testing.T) { @@ -269,36 +275,38 @@ func TestInt_AlterReplication(t *testing.T) { func TestInt_AlterFailover(t *testing.T) { client := testClient(t) + secondaryClient := testSecondaryClient(t) ctx := testContext(t) - databaseTest, databaseCleanup := createDatabase(t, client) + + databaseTest, databaseCleanup := createDatabase(t, secondaryClient) t.Cleanup(databaseCleanup) - secondaryClient := testSecondaryClient(t) toAccounts := []sdk.AccountIdentifier{ - getAccountIdentifier(t, secondaryClient), + getAccountIdentifier(t, client), } + t.Run("enable and disable failover", func(t *testing.T) { - opts := &sdk.AlterDatabaseFailoverOptions{ + err := secondaryClient.Databases.AlterFailover(ctx, databaseTest.ID(), &sdk.AlterDatabaseFailoverOptions{ EnableFailover: &sdk.EnableFailover{ ToAccounts: toAccounts, }, - } - err := client.Databases.AlterFailover(ctx, databaseTest.ID(), opts) + }) + // TODO: has to be enabled by ORGADMIN (SNOW-1002025) if strings.Contains(err.Error(), "Accounts enabled for failover must also be enabled for replication. Enable replication to account") { t.Skip("Skipping test because secondary account not enabled for replication") } require.NoError(t, err) - opts = &sdk.AlterDatabaseFailoverOptions{ + + err = secondaryClient.Databases.AlterFailover(ctx, databaseTest.ID(), &sdk.AlterDatabaseFailoverOptions{ DisableFailover: &sdk.DisableFailover{ ToAccounts: toAccounts, }, - } - err = client.Databases.AlterFailover(ctx, databaseTest.ID(), opts) + }) require.NoError(t, err) - opts = &sdk.AlterDatabaseFailoverOptions{ + + err = secondaryClient.Databases.AlterFailover(ctx, databaseTest.ID(), &sdk.AlterDatabaseFailoverOptions{ Primary: sdk.Bool(true), - } - err = client.Databases.AlterFailover(ctx, databaseTest.ID(), opts) + }) require.NoError(t, err) }) } diff --git a/pkg/sdk/testint/failover_groups_integration_test.go b/pkg/sdk/testint/failover_groups_integration_test.go index e43c778c01..cb048695ac 100644 --- a/pkg/sdk/testint/failover_groups_integration_test.go +++ b/pkg/sdk/testint/failover_groups_integration_test.go @@ -29,7 +29,7 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { sdk.PluralObjectTypeDatabases, } allowedAccounts := []sdk.AccountIdentifier{ - getSecondaryAccountIdentifier(t), + getAccountIdentifier(t, testSecondaryClient(t)), } replicationSchedule := "10 MINUTE" err := client.FailoverGroups.Create(ctx, id, objectTypes, allowedAccounts, &sdk.CreateFailoverGroupOptions{ @@ -80,7 +80,7 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { sdk.PluralObjectTypeIntegrations, } allowedAccounts := []sdk.AccountIdentifier{ - getSecondaryAccountIdentifier(t), + getAccountIdentifier(t, testSecondaryClient(t)), } allowedIntegrationTypes := []sdk.IntegrationType{ sdk.IntegrationTypeAPIIntegrations, @@ -105,6 +105,7 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { } func TestInt_CreateSecondaryReplicationGroup(t *testing.T) { + // TODO: Business Critical Snowflake Edition (SNOW-1002023) if os.Getenv("SNOWFLAKE_TEST_BUSINESS_CRITICAL_FEATURES") != "1" { t.Skip("Skipping TestInt_FailoverGroupsCreate") } @@ -386,7 +387,7 @@ func TestInt_FailoverGroupsAlterSource(t *testing.T) { failoverGroup, cleanupFailoverGroup := createFailoverGroup(t, client) t.Cleanup(cleanupFailoverGroup) - secondaryAccountID := getSecondaryAccountIdentifier(t) + secondaryAccountID := getAccountIdentifier(t, testSecondaryClient(t)) // first add target account opts := &sdk.AlterSourceFailoverGroupOptions{ Add: &sdk.FailoverGroupAdd{ @@ -538,6 +539,7 @@ func TestInt_FailoverGroupsAlterSource(t *testing.T) { } func TestInt_FailoverGroupsAlterTarget(t *testing.T) { + // TODO: Business Critical Snowflake Edition (SNOW-1002023) if os.Getenv("SNOWFLAKE_TEST_BUSINESS_CRITICAL_FEATURES") != "1" { t.Skip("Skipping TestInt_FailoverGroupsCreate") } diff --git a/pkg/sdk/testint/helpers_test.go b/pkg/sdk/testint/helpers_test.go index 4021d76faf..d264b88a02 100644 --- a/pkg/sdk/testint/helpers_test.go +++ b/pkg/sdk/testint/helpers_test.go @@ -34,37 +34,6 @@ func getAccountIdentifier(t *testing.T, client *sdk.Client) sdk.AccountIdentifie return sdk.AccountIdentifier{} } -func getSecondaryAccountIdentifier(t *testing.T) sdk.AccountIdentifier { - t.Helper() - client := testSecondaryClient(t) - return getAccountIdentifier(t, client) -} - -const ( - secondaryAccountProfile = "secondary_test_account" -) - -// TODO: for now we leave it as is, later it would be nice to configure it also once in TestMain -func testSecondaryClient(t *testing.T) *sdk.Client { - t.Helper() - - client, err := testClientFromProfile(t, secondaryAccountProfile) - if err != nil { - t.Skipf("Snowflake secondary account not configured. Must be set in ~./snowflake/config.yml with profile name: %s", secondaryAccountProfile) - } - - return client -} - -func testClientFromProfile(t *testing.T, profile string) (*sdk.Client, error) { - t.Helper() - config, err := sdk.ProfileConfig(profile) - if err != nil { - return nil, err - } - return sdk.NewClient(config) -} - func useWarehouse(t *testing.T, client *sdk.Client, warehouseID sdk.AccountObjectIdentifier) func() { t.Helper() ctx := context.Background() @@ -78,20 +47,20 @@ func useWarehouse(t *testing.T, client *sdk.Client, warehouseID sdk.AccountObjec func createDatabase(t *testing.T, client *sdk.Client) (*sdk.Database, func()) { t.Helper() - return createDatabaseWithOptions(t, client, sdk.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{}) + return createDatabaseWithOptions(t, client, sdk.RandomAccountObjectIdentifier(), testSchema(t).ID(), &sdk.CreateDatabaseOptions{}) } -func createDatabaseWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountObjectIdentifier, _ *sdk.CreateDatabaseOptions) (*sdk.Database, func()) { +func createDatabaseWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountObjectIdentifier, useSchemaAfterDatabaseDrop sdk.DatabaseObjectIdentifier, opts *sdk.CreateDatabaseOptions) (*sdk.Database, func()) { t.Helper() ctx := context.Background() - err := client.Databases.Create(ctx, id, nil) + err := client.Databases.Create(ctx, id, opts) require.NoError(t, err) database, err := client.Databases.ShowByID(ctx, id) require.NoError(t, err) return database, func() { err := client.Databases.Drop(ctx, id, nil) require.NoError(t, err) - err = testClient(t).Sessions.UseSchema(ctx, testSchema(t).ID()) + err = client.Sessions.UseSchema(ctx, useSchemaAfterDatabaseDrop) require.NoError(t, err) } } diff --git a/pkg/sdk/testint/setup_test.go b/pkg/sdk/testint/setup_test.go index 4508bfdbc9..190f5db2a2 100644 --- a/pkg/sdk/testint/setup_test.go +++ b/pkg/sdk/testint/setup_test.go @@ -11,6 +11,10 @@ import ( "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" ) +const ( + secondaryAccountProfile = "secondary_test_account" +) + var itc integrationTestContext func TestMain(m *testing.M) { @@ -45,6 +49,12 @@ func cleanup() { if itc.schemaCleanup != nil { defer itc.schemaCleanup() } + if itc.secondaryDatabaseCleanup != nil { + defer itc.secondaryDatabaseCleanup() + } + if itc.secondarySchemaCleanup != nil { + defer itc.secondarySchemaCleanup() + } } type integrationTestContext struct { @@ -57,11 +67,21 @@ type integrationTestContext struct { schemaCleanup func() warehouse *sdk.Warehouse warehouseCleanup func() + + secondaryClient *sdk.Client + secondaryCtx context.Context + + secondaryDatabase *sdk.Database + secondaryDatabaseCleanup func() + secondarySchema *sdk.Schema + secondarySchemaCleanup func() + secondaryWarehouse *sdk.Warehouse + secondaryWarehouseCleanup func() } func (itc *integrationTestContext) initialize() error { log.Println("Initializing integration test context") - var err error + c, err := sdk.NewDefaultClient() if err != nil { return err @@ -90,6 +110,38 @@ func (itc *integrationTestContext) initialize() error { itc.warehouse = wh itc.warehouseCleanup = whCleanup + config, err := sdk.ProfileConfig(secondaryAccountProfile) + if err != nil { + return err + } + secondaryClient, err := sdk.NewClient(config) + if err != nil { + return err + } + itc.secondaryClient = secondaryClient + itc.secondaryCtx = context.Background() + + secondaryDb, secondaryDbCleanup, err := createDb(itc.secondaryClient, itc.secondaryCtx) + if err != nil { + return err + } + itc.secondaryDatabase = secondaryDb + itc.secondaryDatabaseCleanup = secondaryDbCleanup + + secondarySchema, secondarySchemaCleanup, err := createSc(itc.secondaryClient, itc.secondaryCtx, itc.database) + if err != nil { + return err + } + itc.secondarySchema = secondarySchema + itc.secondarySchemaCleanup = secondarySchemaCleanup + + secondaryWarehouse, secondaryWarehouseCleanup, err := createWh(itc.secondaryClient, itc.secondaryCtx) + if err != nil { + return err + } + itc.secondaryWarehouse = secondaryWarehouse + itc.secondaryWarehouseCleanup = secondaryWarehouseCleanup + return nil } @@ -167,3 +219,28 @@ func testWarehouse(t *testing.T) *sdk.Warehouse { t.Helper() return itc.warehouse } + +func testSecondaryClient(t *testing.T) *sdk.Client { + t.Helper() + return itc.secondaryClient +} + +func testSecondaryContext(t *testing.T) context.Context { + t.Helper() + 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 +} diff --git a/pkg/sdk/testint/shares_integration_test.go b/pkg/sdk/testint/shares_integration_test.go index 4c29b517ab..fe282b35fe 100644 --- a/pkg/sdk/testint/shares_integration_test.go +++ b/pkg/sdk/testint/shares_integration_test.go @@ -125,8 +125,7 @@ func TestInt_SharesDrop(t *testing.T) { func TestInt_SharesAlter(t *testing.T) { client := testClient(t) - secondaryClient, err := testClientFromProfile(t, secondaryAccountProfile) - require.NoError(t, err) + secondaryClient := testSecondaryClient(t) ctx := testContext(t) t.Run("add and remove accounts", func(t *testing.T) { @@ -184,23 +183,27 @@ func TestInt_SharesAlter(t *testing.T) { }) t.Run("set accounts", func(t *testing.T) { - db, dbCleanup := createDatabase(t, secondaryClient) + db, dbCleanup := createDatabaseWithOptions(t, secondaryClient, sdk.RandomAccountObjectIdentifier(), testSchema(t).ID(), nil) t.Cleanup(dbCleanup) + shareTest, shareCleanup := createShare(t, secondaryClient) t.Cleanup(shareCleanup) + err := secondaryClient.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ Database: db.ID(), }, shareTest.ID()) require.NoError(t, err) t.Cleanup(func() { - err = secondaryClient.Grants.RevokePrivilegeFromShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.RevokePrivilegeFromShareOn{ + err := secondaryClient.Grants.RevokePrivilegeFromShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.RevokePrivilegeFromShareOn{ Database: db.ID(), }, shareTest.ID()) + require.NoError(t, err) }) - require.NoError(t, err) + accountsToSet := []sdk.AccountIdentifier{ getAccountIdentifier(t, client), } + // first add the account. err = secondaryClient.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ IfExists: sdk.Bool(true), @@ -209,12 +212,14 @@ func TestInt_SharesAlter(t *testing.T) { }, }) require.NoError(t, err) + shares, err := secondaryClient.Shares.Show(ctx, &sdk.ShowShareOptions{ Like: &sdk.Like{ Pattern: sdk.String(shareTest.Name.Name()), }, }) require.NoError(t, err) + assert.Equal(t, 1, len(shares)) share := shares[0] assert.Equal(t, accountsToSet, share.To) @@ -223,6 +228,7 @@ func TestInt_SharesAlter(t *testing.T) { t.Run("set and unset comment", func(t *testing.T) { shareTest, shareCleanup := createShare(t, client) t.Cleanup(shareCleanup) + err := client.Grants.GrantPrivilegeToShare(ctx, sdk.ObjectPrivilegeUsage, &sdk.GrantPrivilegeToShareOn{ Database: testDb(t).ID(), }, shareTest.ID()) @@ -242,12 +248,14 @@ func TestInt_SharesAlter(t *testing.T) { }, }) require.NoError(t, err) + shares, err := client.Shares.Show(ctx, &sdk.ShowShareOptions{ Like: &sdk.Like{ Pattern: sdk.String(shareTest.Name.Name()), }, }) require.NoError(t, err) + assert.Equal(t, 1, len(shares)) share := shares[0] assert.Equal(t, comment, share.Comment) @@ -260,12 +268,14 @@ func TestInt_SharesAlter(t *testing.T) { }, }) require.NoError(t, err) + shares, err = client.Shares.Show(ctx, &sdk.ShowShareOptions{ Like: &sdk.Like{ Pattern: sdk.String(shareTest.Name.Name()), }, }) require.NoError(t, err) + assert.Equal(t, 1, len(shares)) share = shares[0] assert.Equal(t, "", share.Comment) @@ -362,7 +372,7 @@ func TestInt_ShareDescribeConsumer(t *testing.T) { consumerClient := testClient(t) t.Run("describe share", func(t *testing.T) { - db, dbCleanup := createDatabase(t, providerClient) + db, dbCleanup := createDatabaseWithOptions(t, providerClient, sdk.RandomAccountObjectIdentifier(), testSchema(t).ID(), nil) t.Cleanup(dbCleanup) shareTest, shareCleanup := createShare(t, providerClient) From 0e3d39e35e6c83349a7e6ed3490ce3148edd1872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Tue, 9 Jan 2024 11:16:24 +0100 Subject: [PATCH 3/4] changes after review --- pkg/sdk/testint/helpers_test.go | 6 +++--- pkg/sdk/testint/setup_test.go | 17 ++++++++++------- pkg/sdk/testint/shares_integration_test.go | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/sdk/testint/helpers_test.go b/pkg/sdk/testint/helpers_test.go index d264b88a02..8012ad0ed4 100644 --- a/pkg/sdk/testint/helpers_test.go +++ b/pkg/sdk/testint/helpers_test.go @@ -47,10 +47,10 @@ func useWarehouse(t *testing.T, client *sdk.Client, warehouseID sdk.AccountObjec func createDatabase(t *testing.T, client *sdk.Client) (*sdk.Database, func()) { t.Helper() - return createDatabaseWithOptions(t, client, sdk.RandomAccountObjectIdentifier(), testSchema(t).ID(), &sdk.CreateDatabaseOptions{}) + return createDatabaseWithOptions(t, client, sdk.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{}) } -func createDatabaseWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountObjectIdentifier, useSchemaAfterDatabaseDrop sdk.DatabaseObjectIdentifier, opts *sdk.CreateDatabaseOptions) (*sdk.Database, func()) { +func createDatabaseWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountObjectIdentifier, opts *sdk.CreateDatabaseOptions) (*sdk.Database, func()) { t.Helper() ctx := context.Background() err := client.Databases.Create(ctx, id, opts) @@ -60,7 +60,7 @@ func createDatabaseWithOptions(t *testing.T, client *sdk.Client, id sdk.AccountO return database, func() { err := client.Databases.Drop(ctx, id, nil) require.NoError(t, err) - err = client.Sessions.UseSchema(ctx, useSchemaAfterDatabaseDrop) + err = client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(TestDatabaseName, TestSchemaName)) require.NoError(t, err) } } diff --git a/pkg/sdk/testint/setup_test.go b/pkg/sdk/testint/setup_test.go index 190f5db2a2..2af6cbe572 100644 --- a/pkg/sdk/testint/setup_test.go +++ b/pkg/sdk/testint/setup_test.go @@ -15,6 +15,12 @@ const ( secondaryAccountProfile = "secondary_test_account" ) +var ( + TestWarehouseName = "int_test_wh_" + random.UUID() + TestDatabaseName = "int_test_db_" + random.UUID() + TestSchemaName = "int_test_sc_" + random.UUID() +) + var itc integrationTestContext func TestMain(m *testing.M) { @@ -146,8 +152,7 @@ func (itc *integrationTestContext) initialize() error { } func createDb(client *sdk.Client, ctx context.Context) (*sdk.Database, func(), error) { - name := "int_test_db_" + random.UUID() - id := sdk.NewAccountObjectIdentifier(name) + id := sdk.NewAccountObjectIdentifier(TestDatabaseName) err := client.Databases.Create(ctx, id, nil) if err != nil { return nil, nil, err @@ -159,21 +164,19 @@ func createDb(client *sdk.Client, ctx context.Context) (*sdk.Database, func(), e } func createSc(client *sdk.Client, ctx context.Context, db *sdk.Database) (*sdk.Schema, func(), error) { - name := "int_test_sc_" + random.UUID() - id := sdk.NewDatabaseObjectIdentifier(db.Name, name) + id := sdk.NewDatabaseObjectIdentifier(db.Name, TestSchemaName) err := client.Schemas.Create(ctx, id, nil) if err != nil { return nil, nil, err } - schema, err := client.Schemas.ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(db.Name, name)) + schema, err := client.Schemas.ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(db.Name, TestSchemaName)) return schema, func() { _ = client.Schemas.Drop(ctx, id, nil) }, err } func createWh(client *sdk.Client, ctx context.Context) (*sdk.Warehouse, func(), error) { - name := "int_test_wh_" + random.UUID() - id := sdk.NewAccountObjectIdentifier(name) + id := sdk.NewAccountObjectIdentifier(TestWarehouseName) err := client.Warehouses.Create(ctx, id, nil) if err != nil { return nil, nil, err diff --git a/pkg/sdk/testint/shares_integration_test.go b/pkg/sdk/testint/shares_integration_test.go index fe282b35fe..0050a1e43c 100644 --- a/pkg/sdk/testint/shares_integration_test.go +++ b/pkg/sdk/testint/shares_integration_test.go @@ -183,7 +183,7 @@ func TestInt_SharesAlter(t *testing.T) { }) t.Run("set accounts", func(t *testing.T) { - db, dbCleanup := createDatabaseWithOptions(t, secondaryClient, sdk.RandomAccountObjectIdentifier(), testSchema(t).ID(), nil) + db, dbCleanup := createDatabase(t, secondaryClient) t.Cleanup(dbCleanup) shareTest, shareCleanup := createShare(t, secondaryClient) @@ -372,7 +372,7 @@ func TestInt_ShareDescribeConsumer(t *testing.T) { consumerClient := testClient(t) t.Run("describe share", func(t *testing.T) { - db, dbCleanup := createDatabaseWithOptions(t, providerClient, sdk.RandomAccountObjectIdentifier(), testSchema(t).ID(), nil) + db, dbCleanup := createDatabase(t, providerClient) t.Cleanup(dbCleanup) shareTest, shareCleanup := createShare(t, providerClient) From f60d8deea71808b5324c26174f2f2e3fc8dd8cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Tue, 9 Jan 2024 12:01:48 +0100 Subject: [PATCH 4/4] lint fix --- pkg/sdk/testint/databases_integration_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/sdk/testint/databases_integration_test.go b/pkg/sdk/testint/databases_integration_test.go index 31ff69af3c..7441ff7040 100644 --- a/pkg/sdk/testint/databases_integration_test.go +++ b/pkg/sdk/testint/databases_integration_test.go @@ -141,6 +141,7 @@ func TestInt_CreateShared(t *testing.T) { Accounts: accountsToSet, }, }) + require.NoError(t, err) databaseID := sdk.RandomAccountObjectIdentifier() err = client.Databases.CreateShared(ctx, databaseID, shareTest.ExternalID(), nil)