Skip to content

Commit

Permalink
[Internal] Expose several integration test helpers for use in plugin …
Browse files Browse the repository at this point in the history
…framework integration tests (#4310)

## Changes
Plugin Framework resources may have integration tests that depend on
some of the methods defined in `internal/acceptance/init.go`, namely the
isAws/isGcp/isAzure functions, skipF, and load*Env functions. These are
now exported and can be used by those integration test authors.

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->

- [ ] `make test` run locally
- [ ] relevant change in `docs/` folder
- [ ] covered with integration tests in `internal/acceptance`
- [ ] relevant acceptance tests are passing
- [ ] using Go SDK
  • Loading branch information
mgyucht authored Dec 11, 2024
1 parent 609977a commit 35b132b
Show file tree
Hide file tree
Showing 25 changed files with 133 additions and 133 deletions.
4 changes: 2 additions & 2 deletions internal/acceptance/account_rule_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Application ID is mandatory in Azure today.
func getServicePrincipalResource(t *testing.T) string {
if isAzure(t) {
if IsAzure(t) {
return `
resource "databricks_service_principal" "this" {
application_id = "{var.RANDOM_UUID}"
Expand All @@ -30,7 +30,7 @@ func getServicePrincipalResource(t *testing.T) string {
}

func TestMwsAccAccountServicePrincipalRuleSetsFullLifeCycle(t *testing.T) {
loadAccountEnv(t)
LoadAccountEnv(t)
spResource := getServicePrincipalResource(t)
AccountLevel(t, Step{
Template: spResource + `
Expand Down
12 changes: 6 additions & 6 deletions internal/acceptance/budget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ var (
)

func TestMwsAccBudgetCreate(t *testing.T) {
loadAccountEnv(t)
if isGcp(t) {
skipf(t)("not available on GCP")
LoadAccountEnv(t)
if IsGcp(t) {
Skipf(t)("not available on GCP")
}
AccountLevel(t, Step{
Template: fmt.Sprintf(budgetTemplate, "840"),
})
}

func TestMwsAccBudgetUpdate(t *testing.T) {
loadAccountEnv(t)
if isGcp(t) {
skipf(t)("not available on GCP")
LoadAccountEnv(t)
if IsGcp(t) {
Skipf(t)("not available on GCP")
}
AccountLevel(t, Step{
Template: fmt.Sprintf(budgetTemplate, "840"),
Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestUcAccCatalog(t *testing.T) {
loadUcwsEnv(t)
LoadUcwsEnv(t)
UnityWorkspaceLevel(t, Step{
Template: fmt.Sprintf(`
resource "databricks_catalog" "sandbox" {
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestUcAccCatalogIsolated(t *testing.T) {
}

func TestUcAccCatalogUpdate(t *testing.T) {
loadUcwsEnv(t)
LoadUcwsEnv(t)
UnityWorkspaceLevel(t, Step{
Template: fmt.Sprintf(`
resource "databricks_catalog" "sandbox" {
Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func awsClusterTemplate(availability string) string {
}

func TestAccClusterResource_CreateAndUpdateAwsAttributes(t *testing.T) {
loadWorkspaceEnv(t)
if isAws(t) {
LoadWorkspaceEnv(t)
if IsAws(t) {
WorkspaceLevel(t, Step{
Template: awsClusterTemplate("SPOT"),
}, Step{
Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

func TestUcAccCredential(t *testing.T) {
loadUcwsEnv(t)
if isAws(t) {
LoadUcwsEnv(t)
if IsAws(t) {
UnityWorkspaceLevel(t, Step{
Template: `
resource "databricks_credential" "external" {
Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance/custom_app_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ var (
)

func TestMwsAccCustomAppIntegrationCreate(t *testing.T) {
loadAccountEnv(t)
LoadAccountEnv(t)
AccountLevel(t, Step{
Template: fmt.Sprintf(customAppIntegrationTemplate, "30"),
})
}

func TestMwsAccCustomAppIntegrationUpdate(t *testing.T) {
loadAccountEnv(t)
LoadAccountEnv(t)
AccountLevel(t, Step{
Template: fmt.Sprintf(customAppIntegrationTemplate, "30"),
}, Step{
Expand Down
16 changes: 8 additions & 8 deletions internal/acceptance/data_current_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ func checkCurrentConfig(t *testing.T, cloudType string, isAccount string) func(s
}

func TestAccDataCurrentConfig(t *testing.T) {
loadWorkspaceEnv(t)
if isAws(t) {
LoadWorkspaceEnv(t)
if IsAws(t) {
WorkspaceLevel(t, Step{
Template: `data "databricks_current_config" "this" {}`,
Check: checkCurrentConfig(t, "aws", "false"),
})
} else if isAzure(t) {
} else if IsAzure(t) {
WorkspaceLevel(t, Step{
Template: `data "databricks_current_config" "this" {}`,
Check: checkCurrentConfig(t, "azure", "false"),
})
} else if isGcp(t) {
} else if IsGcp(t) {
WorkspaceLevel(t, Step{
Template: `data "databricks_current_config" "this" {}`,
Check: checkCurrentConfig(t, "gcp", "false"),
Expand All @@ -42,18 +42,18 @@ func TestAccDataCurrentConfig(t *testing.T) {
}

func TestMwsAccDataCurrentConfig(t *testing.T) {
loadAccountEnv(t)
if isAws(t) {
LoadAccountEnv(t)
if IsAws(t) {
AccountLevel(t, Step{
Template: `data "databricks_current_config" "this" {}`,
Check: checkCurrentConfig(t, "aws", "true"),
})
} else if isAzure(t) {
} else if IsAzure(t) {
AccountLevel(t, Step{
Template: `data "databricks_current_config" "this" {}`,
Check: checkCurrentConfig(t, "azure", "true"),
})
} else if isGcp(t) {
} else if IsGcp(t) {
AccountLevel(t, Step{
Template: `data "databricks_current_config" "this" {}`,
Check: checkCurrentConfig(t, "gcp", "true"),
Expand Down
10 changes: 5 additions & 5 deletions internal/acceptance/data_mws_network_connectivity_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

func TestAccDataSourceMwsNetworkConnectivityConfigTest(t *testing.T) {
loadWorkspaceEnv(t)
if isGcp(t) {
skipf(t)("GCP not supported")
LoadWorkspaceEnv(t)
if IsGcp(t) {
Skipf(t)("GCP not supported")
}
var sourceRegion string
if isAzure(t) {
if IsAzure(t) {
sourceRegion = "eastus2"
} else if isAws(t) {
} else if IsAws(t) {
sourceRegion = "us-east-2"
}
AccountLevel(t,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
)

func TestAccDataSourceMwsNetworkConnectivityConfigsTest(t *testing.T) {
loadWorkspaceEnv(t)
if isGcp(t) {
skipf(t)("GCP not supported")
LoadWorkspaceEnv(t)
if IsGcp(t) {
Skipf(t)("GCP not supported")
}
var region string
if isAzure(t) {
if IsAzure(t) {
region = "eastus2"
} else if isAws(t) {
} else if IsAws(t) {
region = "us-east-2"
}
AccountLevel(t,
Expand Down
4 changes: 2 additions & 2 deletions internal/acceptance/entitlements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ func makePreconfig(t *testing.T, r entitlementResource) func() {
}

func entitlementsTest(t *testing.T, f func(*testing.T, entitlementResource)) {
loadWorkspaceEnv(t)
LoadWorkspaceEnv(t)
sp := &servicePrincipalResource{}
if isAzure(t) {
if IsAzure(t) {
// A long-lived application is used in Azure.
sp.applicationId = GetEnvOrSkipTest(t, "ACCOUNT_LEVEL_SERVICE_PRINCIPAL_ID")
sp.cleanup = false
Expand Down
48 changes: 24 additions & 24 deletions internal/acceptance/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ func init() {
}

func WorkspaceLevel(t *testing.T, steps ...Step) {
loadWorkspaceEnv(t)
LoadWorkspaceEnv(t)
run(t, steps)
}

func AccountLevel(t *testing.T, steps ...Step) {
loadAccountEnv(t)
LoadAccountEnv(t)
run(t, steps)
}

func UnityWorkspaceLevel(t *testing.T, steps ...Step) {
loadUcwsEnv(t)
LoadUcwsEnv(t)
run(t, steps)
}

func UnityAccountLevel(t *testing.T, steps ...Step) {
loadUcacctEnv(t)
LoadUcacctEnv(t)
run(t, steps)
}

Expand Down Expand Up @@ -125,7 +125,7 @@ func environmentTemplate(t *testing.T, template string, otherVars ...map[string]
"RANDOM_UUID": createUuid(),
}
if len(otherVars) > 1 {
skipf(t)("cannot have more than one custom variable map")
Skipf(t)("cannot have more than one custom variable map")
}
if len(otherVars) == 1 {
for k, v := range otherVars[0] {
Expand All @@ -147,14 +147,14 @@ func environmentTemplate(t *testing.T, template string, otherVars ...map[string]
value = vars[varName]
}
if value == "" {
skipf(t)("Missing %s %s variable.", varType, varName)
Skipf(t)("Missing %s %s variable.", varType, varName)
missing++
continue
}
template = strings.ReplaceAll(template, `{`+varType+`.`+varName+`}`, value)
}
if missing > 0 {
skipf(t)("please set %d variables and restart", missing)
Skipf(t)("please set %d variables and restart", missing)
}
return commands.TrimLeadingWhitespace(template)
}
Expand Down Expand Up @@ -291,7 +291,7 @@ const hexCharset = "0123456789abcdef"
func GetEnvOrSkipTest(t *testing.T, name string) string {
value := os.Getenv(name)
if value == "" {
skipf(t)("Environment variable %s is missing", name)
Skipf(t)("Environment variable %s is missing", name)
}
return value
}
Expand All @@ -300,7 +300,7 @@ func GetEnvInt64OrSkipTest(t *testing.T, name string) int64 {
v := GetEnvOrSkipTest(t, name)
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
skipf(t)("`%s` is not int64: %s", v, err)
Skipf(t)("`%s` is not int64: %s", v, err)
}
return i
}
Expand Down Expand Up @@ -338,7 +338,7 @@ func RandomHex(prefix string, randLen int) string {
return string(b)
}

func skipf(t *testing.T) func(format string, args ...any) {
func Skipf(t *testing.T) func(format string, args ...any) {
if isInDebug() {
// VSCode "debug test" feature doesn't show dlv logs,
// so that we fail here for maintainer productivity.
Expand All @@ -359,59 +359,59 @@ func setDebugLogger() {
}
}

func loadWorkspaceEnv(t *testing.T) {
func LoadWorkspaceEnv(t *testing.T) {
initTest(t, "workspace")
if os.Getenv("DATABRICKS_ACCOUNT_ID") != "" {
skipf(t)("Skipping workspace test on account level")
Skipf(t)("Skipping workspace test on account level")
}
}

func loadAccountEnv(t *testing.T) {
func LoadAccountEnv(t *testing.T) {
initTest(t, "account")
if os.Getenv("DATABRICKS_ACCOUNT_ID") == "" {
skipf(t)("Skipping account test on workspace level")
Skipf(t)("Skipping account test on workspace level")
}
}

func loadUcwsEnv(t *testing.T) {
func LoadUcwsEnv(t *testing.T) {
initTest(t, "ucws")
if os.Getenv("TEST_METASTORE_ID") == "" {
skipf(t)("Skipping non-Unity Catalog test")
Skipf(t)("Skipping non-Unity Catalog test")
}
if os.Getenv("DATABRICKS_ACCOUNT_ID") != "" {
skipf(t)("Skipping workspace test on account level")
Skipf(t)("Skipping workspace test on account level")
}
}

func loadUcacctEnv(t *testing.T) {
func LoadUcacctEnv(t *testing.T) {
initTest(t, "ucacct")
if os.Getenv("TEST_METASTORE_ID") == "" {
skipf(t)("Skipping non-Unity Catalog test")
Skipf(t)("Skipping non-Unity Catalog test")
}
if os.Getenv("DATABRICKS_ACCOUNT_ID") == "" {
skipf(t)("Skipping account test on workspace level")
Skipf(t)("Skipping account test on workspace level")
}
}

func isAws(t *testing.T) bool {
func IsAws(t *testing.T) bool {
awsCloudEnvs := []string{"MWS", "aws", "ucws", "ucacct"}
return isCloudEnvInList(t, awsCloudEnvs)
}

func isAzure(t *testing.T) bool {
func IsAzure(t *testing.T) bool {
azureCloudEnvs := []string{"azure", "azure-ucacct"}
return isCloudEnvInList(t, azureCloudEnvs)
}

func isGcp(t *testing.T) bool {
func IsGcp(t *testing.T) bool {
gcpCloudEnvs := []string{"gcp-accounts", "gcp-ucacct", "gcp-ucws", "gcp"}
return isCloudEnvInList(t, gcpCloudEnvs)
}

func isCloudEnvInList(t *testing.T, cloudEnvs []string) bool {
cloudEnv := os.Getenv("CLOUD_ENV")
if cloudEnv == "" {
skipf(t)("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
Skipf(t)("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
}
return slices.Contains(cloudEnvs, cloudEnv)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/acceptance/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func TestAccJobRunAsUser(t *testing.T) {
}

func TestUcAccJobRunAsServicePrincipal(t *testing.T) {
loadUcwsEnv(t)
LoadUcwsEnv(t)
spId := GetEnvOrSkipTest(t, "ACCOUNT_LEVEL_SERVICE_PRINCIPAL_ID")
UnityWorkspaceLevel(t, Step{
Template: runAsTemplate(`service_principal_name = "` + spId + `"`),
Expand All @@ -373,7 +373,7 @@ func getRunAsAttribute(t *testing.T, ctx context.Context) string {
}

func TestUcAccJobRunAsMutations(t *testing.T) {
loadUcwsEnv(t)
LoadUcwsEnv(t)
spId := GetEnvOrSkipTest(t, "ACCOUNT_LEVEL_SERVICE_PRINCIPAL_ID")
// Note: the attribute must match the type of principal that the test is run as.
ctx := context.Background()
Expand All @@ -396,7 +396,7 @@ func TestUcAccJobRunAsMutations(t *testing.T) {
}

func TestAccRemoveWebhooks(t *testing.T) {
skipf(t)("There is no API to create notification destinations. Once available, add here and enable this test.")
Skipf(t)("There is no API to create notification destinations. Once available, add here and enable this test.")
WorkspaceLevel(t, Step{
Template: `
resource databricks_job test {
Expand Down
Loading

0 comments on commit 35b132b

Please sign in to comment.