Skip to content

Commit

Permalink
chore: Replace id generation in integration and acceptance tests part1 (
Browse files Browse the repository at this point in the history
#2747)

In order to move to the unified ID generation (connected with the
changes in
#2737)
to stabilize parallel builds, the following changes were introduced
inside this PR:
- IdsGenerator was introduced in TestClient. It allows us to get the ids
of the default objects and build the new identifiers with the desired
suffix (indicating the current build). Most of the helpers were moved to
use this generator to get old and new ids. There are still TODOs left
there (there are helpers that get the name of the object from the caller
- we have to ensure that the names are generated the same way, or they
have a good reason to be different). Ids generator is exposed so that
both integration and acceptance tests can use it.
- Changed the generation method of resource/datasource names to the
exposed method described below in all the acceptance tests.
- Fixed the random.go file in the SDK (so that this one is used only in
the SDK unit tests - read the content of the file below, it had the long
comment describing the problems with this file):
  - renamed to random_test.go
  - unexported all the methods
  - switch to appropriate methods in integration/acceptance tests
- Next part of the change is to check all identifier creations
throughout helpers/acceptance tests/integration tests and switch to a
proper generator. This is not finished. The following methods were
attacked (the rest will be tackled in the next PR):
  - NewExternalObjectIdentifier
  - NewExternalObjectIdentifierFromFullyQualifiedName
  - NewAccountIdentifier
  - NewAccountIdentifierFromAccountLocator (moved to ids generator)
- NewAccountIdentifierFromFullyQualifiedName (2 usages left in business
critical skipped tests)
- NewAccountObjectIdentifier (testAccCheckDatabaseExistence left with
todo, grantOwnershipToTheCurrentRole,
testAccCheckSharePrivilegesRevoked,
sdk.NewAccountObjectIdentifier("ACCOUNTADMIN"),
sdk.NewAccountObjectIdentifier("ORGADMIN"),
sdk.NewAccountObjectIdentifier("NOT_EXISTING_ACCOUNT"),
sdk.NewAccountObjectIdentifier("does_not_exist"), current role/user,
sdk.NewAccountObjectIdentifier("<DB>"))
  - NewAccountObjectIdentifierFromFullyQualifiedName
- other smaller changes:
  - removed warehouse 2 in acceptance tests for now
  - fixed `TestAcc_TaskOwnershipGrant_onFuture` test

Left to be done regarding NewAccountObjectIdentifier:
- `alterMaterializedViewQueryExternally`
- `checkDatabaseAndSchemaDataRetentionTime`
- `checkDatabaseSchemaAndTableDataRetentionTime`
- `unsafe_execute_acceptance_test.go`
- `alterViewOwnershipExternally`
- check `app_role_1`
- check hardcoded `filter_by_role`, `stproc1`, `sp_pi`, `filterByRole`,
`records`
- `CreateDatabaseWithName` (and other similar helpers)
- check `setup_test.go`
- ask @sfc-gh-jcieslak about
`sdk.NewAccountObjectIdentifier("S3_STORAGE_INTEGRATION")`,
`sdk.NewAccountObjectIdentifier("GCP_STORAGE_INTEGRATION")`,
`sdk.NewAccountObjectIdentifier("AZURE_STORAGE_INTEGRATION")`
- extract common helpers for
`sdk.NewAccountObjectIdentifier("does_not_exist"` and similar
  • Loading branch information
sfc-gh-asawicki authored Apr 29, 2024
1 parent 424e393 commit cd4a914
Show file tree
Hide file tree
Showing 213 changed files with 1,598 additions and 1,708 deletions.
10 changes: 5 additions & 5 deletions pkg/acceptance/helpers/alert_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type AlertClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewAlertClient(context *TestClientContext) *AlertClient {
func NewAlertClient(context *TestClientContext, idsGenerator *IdsGenerator) *AlertClient {
return &AlertClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -35,10 +36,9 @@ func (c *AlertClient) CreateAlertWithOptions(t *testing.T, schedule string, cond
t.Helper()
ctx := context.Background()

name := random.String()
id := c.context.newSchemaObjectIdentifier(name)
id := c.ids.RandomSchemaObjectIdentifier()

err := c.client().Create(ctx, id, c.context.warehouseId(), schedule, condition, action, opts)
err := c.client().Create(ctx, id, c.ids.WarehouseId(), schedule, condition, action, opts)
require.NoError(t, err)

alert, err := c.client().ShowByID(ctx, id)
Expand Down
7 changes: 4 additions & 3 deletions pkg/acceptance/helpers/api_integration_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type ApiIntegrationClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewApiIntegrationClient(context *TestClientContext) *ApiIntegrationClient {
func NewApiIntegrationClient(context *TestClientContext, idsGenerator *IdsGenerator) *ApiIntegrationClient {
return &ApiIntegrationClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -27,7 +28,7 @@ func (c *ApiIntegrationClient) CreateApiIntegration(t *testing.T) (*sdk.ApiInteg
t.Helper()
ctx := context.Background()

id := sdk.NewAccountObjectIdentifier(random.AlphanumericN(12))
id := c.ids.RandomAccountObjectIdentifier()
apiAllowedPrefixes := []sdk.ApiIntegrationEndpointPrefix{{Path: "https://xyz.execute-api.us-west-2.amazonaws.com/production"}}
req := sdk.NewCreateApiIntegrationRequest(id, apiAllowedPrefixes, true)
req.WithAwsApiProviderParams(sdk.NewAwsApiParamsRequest(sdk.ApiIntegrationAwsApiGateway, "arn:aws:iam::123456789012:role/hello_cloud_account_role"))
Expand Down
7 changes: 4 additions & 3 deletions pkg/acceptance/helpers/application_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type ApplicationClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewApplicationClient(context *TestClientContext) *ApplicationClient {
func NewApplicationClient(context *TestClientContext, idsGenerator *IdsGenerator) *ApplicationClient {
return &ApplicationClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -27,7 +28,7 @@ func (c *ApplicationClient) CreateApplication(t *testing.T, packageId sdk.Accoun
t.Helper()
ctx := context.Background()

id := sdk.NewAccountObjectIdentifier(random.AlphaN(8))
id := c.ids.RandomAccountObjectIdentifier()
err := c.client().Create(ctx, sdk.NewCreateApplicationRequest(id, packageId).WithVersion(sdk.NewApplicationVersionRequest().WithVersionAndPatch(sdk.NewVersionAndPatchRequest(version, nil))))
require.NoError(t, err)

Expand Down
7 changes: 4 additions & 3 deletions pkg/acceptance/helpers/application_package_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import (
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type ApplicationPackageClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewApplicationPackageClient(context *TestClientContext) *ApplicationPackageClient {
func NewApplicationPackageClient(context *TestClientContext, idsGenerator *IdsGenerator) *ApplicationPackageClient {
return &ApplicationPackageClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -28,7 +29,7 @@ func (c *ApplicationPackageClient) CreateApplicationPackage(t *testing.T) (*sdk.
t.Helper()
ctx := context.Background()

id := sdk.NewAccountObjectIdentifier(random.AlphaN(8))
id := c.ids.RandomAccountObjectIdentifier()
err := c.client().Create(ctx, sdk.NewCreateApplicationPackageRequest(id))
require.NoError(t, err)

Expand Down
11 changes: 7 additions & 4 deletions pkg/acceptance/helpers/database_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (

type DatabaseClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewDatabaseClient(context *TestClientContext) *DatabaseClient {
func NewDatabaseClient(context *TestClientContext, idsGenerator *IdsGenerator) *DatabaseClient {
return &DatabaseClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -25,9 +27,10 @@ func (c *DatabaseClient) client() sdk.Databases {

func (c *DatabaseClient) CreateDatabase(t *testing.T) (*sdk.Database, func()) {
t.Helper()
return c.CreateDatabaseWithOptions(t, sdk.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{})
return c.CreateDatabaseWithOptions(t, c.ids.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{})
}

// TODO [SNOW-955520]: we have to control the name
func (c *DatabaseClient) CreateDatabaseWithName(t *testing.T, name string) (*sdk.Database, func()) {
t.Helper()
return c.CreateDatabaseWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateDatabaseOptions{})
Expand All @@ -53,7 +56,7 @@ func (c *DatabaseClient) DropDatabaseFunc(t *testing.T, id sdk.AccountObjectIden
return func() {
err := c.client().Drop(ctx, id, &sdk.DropDatabaseOptions{IfExists: sdk.Bool(true)})
require.NoError(t, err)
err = c.context.client.Sessions.UseSchema(ctx, c.context.schemaId())
err = c.context.client.Sessions.UseSchema(ctx, c.ids.SchemaId())
require.NoError(t, err)
}
}
Expand Down Expand Up @@ -82,7 +85,7 @@ func (c *DatabaseClient) CreateSecondaryDatabaseWithOptions(t *testing.T, id sdk
// TODO [926148]: make this wait better with tests stabilization
// waiting because sometimes dropping primary db right after dropping the secondary resulted in error
time.Sleep(1 * time.Second)
err = c.context.client.Sessions.UseSchema(ctx, c.context.schemaId())
err = c.context.client.Sessions.UseSchema(ctx, c.ids.SchemaId())
require.NoError(t, err)
}
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/acceptance/helpers/database_role_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (

type DatabaseRoleClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewDatabaseRoleClient(context *TestClientContext) *DatabaseRoleClient {
func NewDatabaseRoleClient(context *TestClientContext, idsGenerator *IdsGenerator) *DatabaseRoleClient {
return &DatabaseRoleClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -26,7 +28,7 @@ func (c *DatabaseRoleClient) client() sdk.DatabaseRoles {

func (c *DatabaseRoleClient) CreateDatabaseRole(t *testing.T) (*sdk.DatabaseRole, func()) {
t.Helper()
return c.CreateDatabaseRoleInDatabase(t, c.context.databaseId())
return c.CreateDatabaseRoleInDatabase(t, c.ids.DatabaseId())
}

func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabase(t *testing.T, databaseId sdk.AccountObjectIdentifier) (*sdk.DatabaseRole, func()) {
Expand All @@ -36,7 +38,7 @@ func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabase(t *testing.T, database

func (c *DatabaseRoleClient) CreateDatabaseRoleWithName(t *testing.T, name string) (*sdk.DatabaseRole, func()) {
t.Helper()
return c.CreateDatabaseRoleInDatabaseWithName(t, c.context.databaseId(), name)
return c.CreateDatabaseRoleInDatabaseWithName(t, c.ids.DatabaseId(), name)
}

func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabaseWithName(t *testing.T, databaseId sdk.AccountObjectIdentifier, name string) (*sdk.DatabaseRole, func()) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/acceptance/helpers/dynamic_table_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (

type DynamicTableClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewDynamicTableClient(context *TestClientContext) *DynamicTableClient {
func NewDynamicTableClient(context *TestClientContext, idsGenerator *IdsGenerator) *DynamicTableClient {
return &DynamicTableClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -26,7 +28,7 @@ func (c *DynamicTableClient) client() sdk.DynamicTables {

func (c *DynamicTableClient) CreateDynamicTable(t *testing.T, tableId sdk.SchemaObjectIdentifier) (*sdk.DynamicTable, func()) {
t.Helper()
return c.CreateDynamicTableWithOptions(t, c.context.schemaId(), random.AlphaN(12), c.context.warehouseId(), tableId)
return c.CreateDynamicTableWithOptions(t, c.ids.SchemaId(), random.AlphaN(12), c.ids.WarehouseId(), tableId)
}

func (c *DynamicTableClient) CreateDynamicTableWithOptions(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, name string, warehouseId sdk.AccountObjectIdentifier, tableId sdk.SchemaObjectIdentifier) (*sdk.DynamicTable, func()) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/acceptance/helpers/failover_group_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (

type FailoverGroupClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewFailoverGroupClient(context *TestClientContext) *FailoverGroupClient {
func NewFailoverGroupClient(context *TestClientContext, idsGenerator *IdsGenerator) *FailoverGroupClient {
return &FailoverGroupClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -25,9 +27,7 @@ func (c *FailoverGroupClient) client() sdk.FailoverGroups {
func (c *FailoverGroupClient) CreateFailoverGroup(t *testing.T) (*sdk.FailoverGroup, func()) {
t.Helper()
objectTypes := []sdk.PluralObjectType{sdk.PluralObjectTypeRoles}
currentAccount, err := c.context.client.ContextFunctions.CurrentAccount(context.Background())
require.NoError(t, err)
accountID := sdk.NewAccountIdentifierFromAccountLocator(currentAccount)
accountID := c.ids.AccountIdentifierWithLocator()
allowedAccounts := []sdk.AccountIdentifier{accountID}
return c.CreateFailoverGroupWithOptions(t, objectTypes, allowedAccounts, nil)
}
Expand All @@ -36,7 +36,7 @@ func (c *FailoverGroupClient) CreateFailoverGroupWithOptions(t *testing.T, objec
t.Helper()
ctx := context.Background()

id := sdk.RandomAlphanumericAccountObjectIdentifier()
id := c.ids.RandomAccountObjectIdentifier()

err := c.client().Create(ctx, id, objectTypes, allowedAccounts, opts)
require.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/acceptance/helpers/file_format_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type FileFormatClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewFileFormatClient(context *TestClientContext) *FileFormatClient {
func NewFileFormatClient(context *TestClientContext, idsGenerator *IdsGenerator) *FileFormatClient {
return &FileFormatClient{
context: context,
ids: idsGenerator,
}
}

Expand All @@ -34,7 +35,7 @@ func (c *FileFormatClient) CreateFileFormatWithOptions(t *testing.T, opts *sdk.C
t.Helper()
ctx := context.Background()

id := c.context.newSchemaObjectIdentifier(random.AlphanumericN(12))
id := c.ids.RandomSchemaObjectIdentifier()

err := c.client().Create(ctx, id, opts)
require.NoError(t, err)
Expand Down
74 changes: 74 additions & 0 deletions pkg/acceptance/helpers/ids_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package helpers

import (
"strings"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
)

type IdsGenerator struct {
context *TestClientContext
}

func NewIdsGenerator(context *TestClientContext) *IdsGenerator {
return &IdsGenerator{
context: context,
}
}

func (c *IdsGenerator) DatabaseId() sdk.AccountObjectIdentifier {
return sdk.NewAccountObjectIdentifier(c.context.database)
}

func (c *IdsGenerator) SchemaId() sdk.DatabaseObjectIdentifier {
return sdk.NewDatabaseObjectIdentifier(c.context.database, c.context.schema)
}

func (c *IdsGenerator) WarehouseId() sdk.AccountObjectIdentifier {
return sdk.NewAccountObjectIdentifier(c.context.warehouse)
}

func (c *IdsGenerator) AccountIdentifierWithLocator() sdk.AccountIdentifier {
return sdk.NewAccountIdentifierFromAccountLocator(c.context.client.GetAccountLocator())
}

func (c *IdsGenerator) newSchemaObjectIdentifier(name string) sdk.SchemaObjectIdentifier {
return sdk.NewSchemaObjectIdentifier(c.context.database, c.context.schema, name)
}

func (c *IdsGenerator) RandomAccountObjectIdentifier() sdk.AccountObjectIdentifier {
return sdk.NewAccountObjectIdentifier(c.Alpha())
}

func (c *IdsGenerator) RandomAccountObjectIdentifierWithPrefix(prefix string) sdk.AccountObjectIdentifier {
return sdk.NewAccountObjectIdentifier(c.AlphaWithPrefix(prefix))
}

func (c *IdsGenerator) RandomAccountObjectIdentifierContaining(part string) sdk.AccountObjectIdentifier {
return sdk.NewAccountObjectIdentifier(c.AlphaContaining(part))
}

func (c *IdsGenerator) RandomSchemaObjectIdentifier() sdk.SchemaObjectIdentifier {
return c.newSchemaObjectIdentifier(c.Alpha())
}

func (c *IdsGenerator) Alpha() string {
return c.AlphaN(6)
}

func (c *IdsGenerator) AlphaN(n int) string {
return c.withTestObjectSuffix(strings.ToUpper(random.AlphaN(n)))
}

func (c *IdsGenerator) AlphaContaining(part string) string {
return c.withTestObjectSuffix(c.Alpha() + part)
}

func (c *IdsGenerator) AlphaWithPrefix(prefix string) string {
return c.withTestObjectSuffix(prefix + c.Alpha())
}

func (c *IdsGenerator) withTestObjectSuffix(text string) string {
return text + c.context.testObjectSuffix
}
Loading

0 comments on commit cd4a914

Please sign in to comment.