Skip to content

Commit

Permalink
fix: Add secondary account and fix tests (#2324)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak authored Jan 11, 2024
1 parent ae41691 commit da6ca73
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 107 deletions.
63 changes: 36 additions & 27 deletions pkg/sdk/testint/databases_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,45 +109,52 @@ 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,
},
})
require.NoError(t, err)

databaseID := sdk.RandomAccountObjectIdentifier()
err = secondaryClient.Databases.CreateShared(ctx, databaseID, shareTest.ExternalID(), nil)
require.NoError(t, err)
database, err := secondaryClient.Databases.ShowByID(ctx, databaseID)
err = client.Databases.CreateShared(ctx, databaseID, shareTest.ExternalID(), nil)
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) {
Expand Down Expand Up @@ -269,36 +276,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)
})
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/sdk/testint/failover_groups_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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")
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}
Expand Down
37 changes: 3 additions & 34 deletions pkg/sdk/testint/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -81,17 +50,17 @@ func createDatabase(t *testing.T, client *sdk.Client) (*sdk.Database, func()) {
return createDatabaseWithOptions(t, client, sdk.RandomAccountObjectIdentifier(), &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, 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 = client.Sessions.UseSchema(ctx, testSchema(t).ID())
err = client.Sessions.UseSchema(ctx, sdk.NewDatabaseObjectIdentifier(TestDatabaseName, TestSchemaName))
require.NoError(t, err)
}
}
Expand Down
96 changes: 88 additions & 8 deletions pkg/sdk/testint/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random"
)

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) {
Expand Down Expand Up @@ -45,6 +55,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 {
Expand All @@ -57,11 +73,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
Expand Down Expand Up @@ -90,12 +116,43 @@ 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
}

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
Expand All @@ -107,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
Expand Down Expand Up @@ -167,3 +222,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
}
Loading

0 comments on commit da6ca73

Please sign in to comment.