diff --git a/pkg/sdk/testint/databases_integration_test.go b/pkg/sdk/testint/databases_integration_test.go index 7b0c086f0c5..31ff69af3c7 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 e43c778c010..cb048695ac1 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 4021d76faff..d264b88a028 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 4508bfdbc99..190f5db2a22 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 4c29b517ab3..fe282b35fe1 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)