Skip to content

Commit

Permalink
changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Jan 8, 2024
1 parent bd19377 commit 699a230
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 72 deletions.
62 changes: 35 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,51 @@ 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{

Check failure on line 138 in pkg/sdk/testint/databases_integration_test.go

View workflow job for this annotation

GitHub Actions / reviewdog

[golangci] reported by reviewdog 🐶 ineffectual assignment to err (ineffassign) Raw Output: pkg/sdk/testint/databases_integration_test.go:138:2: ineffectual assignment to err (ineffassign) err = secondaryClient.Shares.Alter(ctx, shareTest.ID(), &sdk.AlterShareOptions{ ^
IfExists: sdk.Bool(true),
Set: &sdk.ShareSet{
Accounts: accountsToSet,
},
})

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) {
Expand Down Expand Up @@ -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)
})
}
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
39 changes: 4 additions & 35 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 @@ -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)
}
}
Expand Down
79 changes: 78 additions & 1 deletion pkg/sdk/testint/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 699a230

Please sign in to comment.