Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Chore use get or skip in other tests #2570

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export SKIP_SAML_INTEGRATION_TESTS=true
export SKIP_MANAGED_ACCOUNT_INT_TEST=true
export SKIP_MANAGED_ACCOUNT_TEST=true
export TEST_SF_TF_SKIP_SAML_INTEGRATION_TEST=true
export TEST_SF_TF_SKIP_MANAGED_ACCOUNT_TEST=true
export BASE_BINARY_NAME=terraform-provider-snowflake
export TERRAFORM_PLUGINS_DIR=$(HOME)/.terraform.d/plugins
export TERRAFORM_PLUGIN_LOCAL_INSTALL=$(TERRAFORM_PLUGINS_DIR)/$(BASE_BINARY_NAME)
Expand Down Expand Up @@ -56,7 +55,7 @@ sweep: ## destroy the whole architecture; USE ONLY FOR DEVELOPMENT ACCOUNTS
@echo "Are you sure? [y/n]" >&2
@read -r REPLY; \
if echo "$$REPLY" | grep -qG "^[yY]$$"; then \
SNOWFLAKE_ENABLE_SWEEP=1 go test -timeout 300s -run ^TestSweepAll ./pkg/sdk -v; \
TEST_SF_TF_ENABLE_SWEEP=1 go test -timeout 300s -run ^TestSweepAll ./pkg/sdk -v; \
else echo "Aborting..."; \
fi;

Expand Down
33 changes: 33 additions & 0 deletions pkg/acceptance/testenvs/testenvs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ func Test_GetOrSkipTest(t *testing.T) {
})
}

func Test_SkipTestIfSet(t *testing.T) {
// runSkipTestIfSetInGoroutineAndWaitForCompletion is needed because underneath we test t.Skipf, that leads to t.SkipNow() that in turn call runtime.Goexit()
// so we need to be wrapped in a Goroutine.
runSkipTestIfSetInGoroutineAndWaitForCompletion := func(t *testing.T, env testenvs.Env) {
t.Helper()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
testenvs.SkipTestIfSet(t, env, "some good reason")
}()
wg.Wait()
}

t.Run("skip test if env is set", func(t *testing.T) {
t.Setenv(string(testenvs.User), "1")

tut := &testing.T{}
runSkipTestIfSetInGoroutineAndWaitForCompletion(tut, testenvs.User)

require.True(t, tut.Skipped())
})

t.Run("do not skip if env not set", func(t *testing.T) {
t.Setenv(string(testenvs.User), "")

tut := &testing.T{}
runSkipTestIfSetInGoroutineAndWaitForCompletion(tut, testenvs.User)

require.False(t, tut.Skipped())
})
}

func Test_Assertions(t *testing.T) {
// runAssertionInGoroutineAndWaitForCompletion is needed because underneath we test require, that leads to t.FailNow() that in turn call runtime.Goexit()
// so we need to be wrapped in a Goroutine.
Expand Down
26 changes: 26 additions & 0 deletions pkg/acceptance/testenvs/testing_environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ const (
Host env = "TEST_SF_TF_HOST"

BusinessCriticalAccount env = "SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT"

TestAccountCreate env = "TEST_SF_TF_TEST_ACCOUNT_CREATE"
TestFailoverGroups env = "TEST_SF_TF_TEST_FAILOVER_GROUPS"
ResourceMonitorNotifyUsers env = "TEST_SF_TF_RESOURCE_MONITOR_NOTIFY_USERS"

AwsExternalBucketUrl env = "TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL"
AwsExternalKeyId env = "TEST_SF_TF_AWS_EXTERNAL_KEY_ID"
AwsExternalSecretKey env = "TEST_SF_TF_AWS_EXTERNAL_SECRET_KEY" // #nosec G101
AwsExternalRoleArn env = "TEST_SF_TF_AWS_EXTERNAL_ROLE_ARN"
AzureExternalBucketUrl env = "TEST_SF_TF_AZURE_EXTERNAL_BUCKET_URL"
AzureExternalTenantId env = "TEST_SF_TF_AZURE_EXTERNAL_TENANT_ID"
AzureExternalSasToken env = "TEST_SF_TF_AZURE_EXTERNAL_SAS_TOKEN" // #nosec G101
GcsExternalBuckerUrl env = "TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL"

SkipManagedAccountTest env = "TEST_SF_TF_SKIP_MANAGED_ACCOUNT_TEST"
SkipSamlIntegrationTest env = "TEST_SF_TF_SKIP_SAML_INTEGRATION_TEST"

EnableSweep env = "TEST_SF_TF_ENABLE_SWEEP"
)

func GetOrSkipTest(t *testing.T, envName Env) string {
Expand All @@ -27,6 +45,14 @@ func GetOrSkipTest(t *testing.T, envName Env) string {
return env
}

func SkipTestIfSet(t *testing.T, envName Env, reason string) {
t.Helper()
env := os.Getenv(fmt.Sprintf("%v", envName))
if env != "" {
t.Skipf("Skipping %s, because env %v is set. Reason: \"%s\"", t.Name(), envName, reason)
}
}

type Env interface {
xxxProtected()
}
Expand Down
17 changes: 11 additions & 6 deletions pkg/datasources/accounts_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package datasources_test

import (
"os"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAcc_Accounts(t *testing.T) {
if _, ok := os.LookupEnv("SNOWFLAKE_TEST_ACCOUNTS_SHOW"); !ok {
t.Skip("Skipping TestInt_Accounts")
}
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
t.Skipf("Skipping because of: ORGADMIN role is not in current session, cannot read accounts")

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Expand Down
7 changes: 0 additions & 7 deletions pkg/datasources/external_functions_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datasources_test

import (
"os"
"strings"
"testing"

Expand All @@ -14,9 +13,6 @@ import (
)

func TestAcc_ExternalFunctions_basic(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_EXTERNAL_FUNCTION_TESTS"); ok {
t.Skip("Skipping TestAcc_ExternalFunction")
}
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
m := func() map[string]config.Variable {
return map[string]config.Variable{
Expand Down Expand Up @@ -59,9 +55,6 @@ func TestAcc_ExternalFunctions_basic(t *testing.T) {
}

func TestAcc_ExternalFunctions_no_database(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_EXTERNAL_FUNCTION_TESTS"); ok {
t.Skip("Skipping TestAcc_ExternalFunction")
}
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
m := func() map[string]config.Variable {
return map[string]config.Variable{
Expand Down
9 changes: 3 additions & 6 deletions pkg/datasources/failover_groups_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ package datasources_test

import (
"fmt"
"os"
"strings"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAcc_FailoverGroups(t *testing.T) {
if _, ok := os.LookupEnv("SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT"); !ok {
t.Skip("Skipping TestAcc_FailoverGroup since not a business critical account")
}
accountName := os.Getenv("SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT")
accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount)

name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package datasources_test

import (
"fmt"
"os"
"strings"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAcc_SystemGenerateSCIMAccessToken(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_SCIM_INTEGRATION_TESTS"); ok {
t.Skip("Skipping TestAcc_SystemGenerateSCIMAccessToken")
}

scimIntName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Expand Down
3 changes: 3 additions & 0 deletions pkg/internal/snowflakeenvs/snowflake_environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ const (
Role = "SNOWFLAKE_ROLE"
ConfigPath = "SNOWFLAKE_CONFIG_PATH"
Host = "SNOWFLAKE_HOST"

NoInstrumentedSql = "SF_TF_NO_INSTRUMENTED_SQL"
GosnowflakeLogLevel = "SF_TF_GOSNOWFLAKE_LOG_LEVEL"
)
9 changes: 4 additions & 5 deletions pkg/resources/account_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package resources_test

import (
"fmt"
"os"
"strings"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAcc_Account_complete(t *testing.T) {
// SNOWFLAKE_TEST_ACCOUNT_CREATE must be set to 1 to run this test
if _, ok := os.LookupEnv("SNOWFLAKE_TEST_ACCOUNT_CREATE"); !ok {
t.Skip("Skipping TestInt_AccountCreate")
}
_ = testenvs.GetOrSkipTest(t, testenvs.TestAccountCreate)

accountName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
password := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) + "123ABC"

Expand Down
10 changes: 0 additions & 10 deletions pkg/resources/external_function_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resources_test

import (
"fmt"
"os"
"strings"
"testing"

Expand All @@ -17,9 +16,6 @@ import (
)

func TestAcc_ExternalFunction_basic(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_EXTERNAL_FUNCTION_TESTS"); ok {
t.Skip("Skipping TestAcc_ExternalFunction")
}
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

m := func() map[string]config.Variable {
Expand Down Expand Up @@ -91,9 +87,6 @@ func TestAcc_ExternalFunction_basic(t *testing.T) {
}

func TestAcc_ExternalFunction_no_arguments(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_EXTERNAL_FUNCTION_TESTS"); ok {
t.Skip("Skipping TestAcc_ExternalFunction")
}
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

m := func() map[string]config.Variable {
Expand Down Expand Up @@ -161,9 +154,6 @@ func TestAcc_ExternalFunction_no_arguments(t *testing.T) {
}

func TestAcc_ExternalFunction_complete(t *testing.T) {
if _, ok := os.LookupEnv("SKIP_EXTERNAL_FUNCTION_TESTS"); ok {
t.Skip("Skipping TestAcc_ExternalFunction")
}
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

m := func() map[string]config.Variable {
Expand Down
44 changes: 16 additions & 28 deletions pkg/resources/external_table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"slices"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/stretchr/testify/require"
acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
"github.com/stretchr/testify/require"
)

func TestAcc_ExternalTable_basic(t *testing.T) {
shouldSkip, awsBucketURL, awsKeyId, awsSecretKey := externalTableTestEnvs()
if shouldSkip {
t.Skip("Skipping TestAcc_ExternalTable_basic")
}
awsBucketURL, awsKeyId, awsSecretKey := getExternalTableTestEnvsOrSkipTest(t)

name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "snowflake_external_table.test_table"
Expand Down Expand Up @@ -131,10 +127,7 @@ func TestAcc_ExternalTable_basic(t *testing.T) {

// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2310 is fixed
func TestAcc_ExternalTable_CorrectDataTypes(t *testing.T) {
shouldSkip, awsBucketURL, awsKeyId, awsSecretKey := externalTableTestEnvs()
if shouldSkip {
t.Skip("Skipping TestAcc_ExternalTable_CorrectDataTypes")
}
awsBucketURL, awsKeyId, awsSecretKey := getExternalTableTestEnvsOrSkipTest(t)

name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "snowflake_external_table.test_table"
Expand Down Expand Up @@ -192,10 +185,7 @@ func TestAcc_ExternalTable_CorrectDataTypes(t *testing.T) {

// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2293 is fixed
func TestAcc_ExternalTable_CanCreateWithPartitions(t *testing.T) {
shouldSkip, awsBucketURL, awsKeyId, awsSecretKey := externalTableTestEnvs()
if shouldSkip {
t.Skip("Skipping TestAcc_ExternalTable_CanCreateWithPartitions")
}
awsBucketURL, awsKeyId, awsSecretKey := getExternalTableTestEnvsOrSkipTest(t)

name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "snowflake_external_table.test_table"
Expand Down Expand Up @@ -254,10 +244,7 @@ func TestAcc_ExternalTable_CanCreateWithPartitions(t *testing.T) {

// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/1564 is implemented
func TestAcc_ExternalTable_DeltaLake(t *testing.T) {
shouldSkip, awsBucketURL, awsKeyId, awsSecretKey := externalTableTestEnvs()
if shouldSkip {
t.Skip("Skipping TestAcc_ExternalTable_DeltaLake")
}
awsBucketURL, awsKeyId, awsSecretKey := getExternalTableTestEnvsOrSkipTest(t)

name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "snowflake_external_table.test_table"
Expand Down Expand Up @@ -324,11 +311,12 @@ func TestAcc_ExternalTable_DeltaLake(t *testing.T) {
})
}

func externalTableTestEnvs() (bool, string, string, string) {
awsBucketURL := os.Getenv("TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL")
awsKeyId := os.Getenv("TEST_SF_TF_AWS_EXTERNAL_KEY_ID")
awsSecretKey := os.Getenv("TEST_SF_TF_AWS_EXTERNAL_SECRET_KEY")
return awsBucketURL == "" || awsKeyId == "" || awsSecretKey == "", awsBucketURL, awsKeyId, awsSecretKey
func getExternalTableTestEnvsOrSkipTest(t *testing.T) (string, string, string) {
t.Helper()
awsBucketURL := testenvs.GetOrSkipTest(t, testenvs.AwsExternalBucketUrl)
awsKeyId := testenvs.GetOrSkipTest(t, testenvs.AwsExternalKeyId)
awsSecretKey := testenvs.GetOrSkipTest(t, testenvs.AwsExternalSecretKey)
return awsBucketURL, awsKeyId, awsSecretKey
}

func externalTableContainsData(name string, contains func(rows []map[string]*any) bool) func(state *terraform.State) error {
Expand Down
Loading
Loading