Skip to content

Commit

Permalink
Moved TrimLeadingWhitespace from internal to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nfx committed May 30, 2022
1 parent 47081fe commit 413ab4b
Show file tree
Hide file tree
Showing 22 changed files with 171 additions and 153 deletions.
3 changes: 1 addition & 2 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/databrickslabs/terraform-provider-databricks/clusters"
"github.com/databrickslabs/terraform-provider-databricks/common"
"github.com/databrickslabs/terraform-provider-databricks/internal"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ func (a CommandsAPI) Execute(clusterID, language, commandStr string) common.Comm
Summary: fmt.Sprintf("Cluster %s has to be running or resizing, but is %s", clusterID, cluster.State),
}
}
commandStr = internal.TrimLeadingWhitespace(commandStr)
commandStr = TrimLeadingWhitespace(commandStr)
log.Printf("[INFO] Executing %s command on %s:\n%s", language, clusterID, commandStr)
context, err := a.createContext(language, clusterID)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/utils.go → commands/leading_whitespace.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package internal
package commands

import (
"strings"
)

// TrimLeadingWhitespace removes leading whitespace
// TrimLeadingWhitespace removes leading whitespace, so that Python code blocks
// that are embedded into Go code still could be interpreted properly.
func TrimLeadingWhitespace(commandStr string) (newCommand string) {
lines := strings.Split(strings.ReplaceAll(commandStr, "\t", " "), "\n")
leadingWhitespace := 1<<31 - 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package commands

import (
"testing"
Expand Down
10 changes: 5 additions & 5 deletions exporter/importables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/databrickslabs/terraform-provider-databricks/clusters"
"github.com/databrickslabs/terraform-provider-databricks/common"
"github.com/databrickslabs/terraform-provider-databricks/internal"
"github.com/databrickslabs/terraform-provider-databricks/commands"
"github.com/databrickslabs/terraform-provider-databricks/jobs"
"github.com/databrickslabs/terraform-provider-databricks/permissions"
"github.com/databrickslabs/terraform-provider-databricks/policies"
Expand Down Expand Up @@ -598,7 +598,7 @@ func TestNotebookGeneration(t *testing.T) {
assert.NoError(t, err)

ic.generateHclForResources(nil)
assert.Equal(t, internal.TrimLeadingWhitespace(`
assert.Equal(t, commands.TrimLeadingWhitespace(`
resource "databricks_notebook" "firstsecond" {
source = "${path.module}/notebooks/First/Second.py"
path = "/First/Second"
Expand All @@ -625,7 +625,7 @@ func TestGlobalInitScriptGen(t *testing.T) {
})

ic.generateHclForResources(nil)
assert.Equal(t, internal.TrimLeadingWhitespace(`
assert.Equal(t, commands.TrimLeadingWhitespace(`
resource "databricks_global_init_script" "new_importing_things" {
source = "${path.module}/files/new_importing_things.sh"
name = "New: Importing ^ Things"
Expand Down Expand Up @@ -655,7 +655,7 @@ func TestSecretGen(t *testing.T) {
})

ic.generateHclForResources(nil)
assert.Equal(t, internal.TrimLeadingWhitespace(`
assert.Equal(t, commands.TrimLeadingWhitespace(`
resource "databricks_secret" "a_b" {
string_value = var.string_value_a_b
scope = "a"
Expand Down Expand Up @@ -688,7 +688,7 @@ func TestDbfsFileGen(t *testing.T) {
})

ic.generateHclForResources(nil)
assert.Equal(t, internal.TrimLeadingWhitespace(`
assert.Equal(t, commands.TrimLeadingWhitespace(`
resource "databricks_dbfs_file" "_a_0cc175b9c0f1b6a831c399e269772661" {
source = "${path.module}/files/_a_0cc175b9c0f1b6a831c399e269772661"
path = "a"
Expand Down
9 changes: 3 additions & 6 deletions internal/acceptance/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import (
"strings"
"testing"

"github.com/databrickslabs/terraform-provider-databricks/commands"
"github.com/databrickslabs/terraform-provider-databricks/common"
"github.com/databrickslabs/terraform-provider-databricks/internal"
"github.com/databrickslabs/terraform-provider-databricks/internal/compute"

"github.com/databrickslabs/terraform-provider-databricks/qa"

"github.com/databrickslabs/terraform-provider-databricks/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -75,7 +72,7 @@ func Test(t *testing.T, steps []Step, otherVars ...map[string]string) {
stepConfig := ""
for _, s := range steps {
if s.Template != "" {
stepConfig = qa.EnvironmentTemplate(t, s.Template, vars)
stepConfig = EnvironmentTemplate(t, s.Template, vars)
}
ts = append(ts, resource.TestStep{
Config: stepConfig,
Expand Down Expand Up @@ -152,7 +149,7 @@ func AccTest(t *testing.T, tc resource.TestCase) {
if s.Config != "" {
t.Logf("Test %s (%s) step %d config is:\n%s",
t.Name(), cloudEnv, i,
internal.TrimLeadingWhitespace(s.Config))
commands.TrimLeadingWhitespace(s.Config))
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions internal/acceptance/environment_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package acceptance

import (
"errors"
"fmt"
"os"
"regexp"
"strings"
"testing"

"github.com/databrickslabs/terraform-provider-databricks/commands"
"github.com/databrickslabs/terraform-provider-databricks/qa"
)

// For writing a unit test to intercept the errors (t.Fatalf literally ends the test in failure)
func environmentTemplate(t *testing.T, template string, otherVars ...map[string]string) (string, error) {
vars := map[string]string{
"RANDOM": qa.RandomName("t"),
}
if len(otherVars) > 1 {
return "", errors.New("cannot have more than one custom variable map")
}
if len(otherVars) == 1 {
for k, v := range otherVars[0] {
vars[k] = v
}
}
// pullAll otherVars
missing := 0
var varType, varName, value string
r := regexp.MustCompile(`{(env|var).([^{}]*)}`)
for _, variableMatch := range r.FindAllStringSubmatch(template, -1) {
value = ""
varType = variableMatch[1]
varName = variableMatch[2]
switch varType {
case "env":
value = os.Getenv(varName)
case "var":
value = vars[varName]
}
if value == "" {
t.Logf("Missing %s %s variable.", varType, varName)
missing++
continue
}
template = strings.ReplaceAll(template, `{`+varType+`.`+varName+`}`, value)
}
if missing > 0 {
return "", fmt.Errorf("please set %d variables and restart", missing)
}
return commands.TrimLeadingWhitespace(template), nil
}

// EnvironmentTemplate asserts existence and fills in {env.VAR} & {var.RANDOM} placeholders in template
func EnvironmentTemplate(t *testing.T, template string, otherVars ...map[string]string) string {
resp, err := environmentTemplate(t, template, otherVars...)
if err != nil {
t.Skipf(err.Error())
}
return resp
}
44 changes: 44 additions & 0 deletions internal/acceptance/environment_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package acceptance

import (
"fmt"
"os"
"testing"

"github.com/databrickslabs/terraform-provider-databricks/common"
"github.com/databrickslabs/terraform-provider-databricks/qa"
"github.com/stretchr/testify/assert"
)


func TestEnvironmentTemplate(t *testing.T) {
defer common.CleanupEnvironment()
err := os.Setenv("USER", qa.RandomName())
assert.NoError(t, err)

res := EnvironmentTemplate(t, `
resource "user" "me" {
name = "{env.USER}"
email = "{env.USER}+{var.RANDOM}@example.com"
}`)
assert.Equal(t, os.Getenv("USER"), qa.FirstKeyValue(t, res, "name"))
}

func TestEnvironmentTemplate_other_vars(t *testing.T) {
otherVar := map[string]string{"TEST": "value"}
res := EnvironmentTemplate(t, `
resource "user" "me" {
name = "{var.TEST}"
}`, otherVar)
assert.Equal(t, otherVar["TEST"], qa.FirstKeyValue(t, res, "name"))
}

func TestEnvironmentTemplate_unset_env(t *testing.T) {
res, err := environmentTemplate(t, `
resource "user" "me" {
name = "{env.USER}"
email = "{env.USER}+{var.RANDOM}@example.com"
}`)
assert.Equal(t, "", res)
assert.Errorf(t, err, fmt.Sprintf("please set %d variables and restart.", 2))
}
51 changes: 0 additions & 51 deletions qa/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"math/rand"
Expand All @@ -18,7 +17,6 @@ import (

"github.com/Azure/go-autorest/autorest/azure"
"github.com/databrickslabs/terraform-provider-databricks/common"
"github.com/databrickslabs/terraform-provider-databricks/internal"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/hcl"
Expand Down Expand Up @@ -512,55 +510,6 @@ func fixHCL(v interface{}) interface{} {
}
}

// For writing a unit test to intercept the errors (t.Fatalf literally ends the test in failure)
func environmentTemplate(t *testing.T, template string, otherVars ...map[string]string) (string, error) {
vars := map[string]string{
"RANDOM": RandomName("t"),
}
if len(otherVars) > 1 {
return "", errors.New("cannot have more than one custom variable map")
}
if len(otherVars) == 1 {
for k, v := range otherVars[0] {
vars[k] = v
}
}
// pullAll otherVars
missing := 0
var varType, varName, value string
r := regexp.MustCompile(`{(env|var).([^{}]*)}`)
for _, variableMatch := range r.FindAllStringSubmatch(template, -1) {
value = ""
varType = variableMatch[1]
varName = variableMatch[2]
switch varType {
case "env":
value = os.Getenv(varName)
case "var":
value = vars[varName]
}
if value == "" {
t.Logf("Missing %s %s variable.", varType, varName)
missing++
continue
}
template = strings.ReplaceAll(template, `{`+varType+`.`+varName+`}`, value)
}
if missing > 0 {
return "", fmt.Errorf("please set %d variables and restart", missing)
}
return internal.TrimLeadingWhitespace(template), nil
}

// EnvironmentTemplate asserts existence and fills in {env.VAR} & {var.RANDOM} placeholders in template
func EnvironmentTemplate(t *testing.T, template string, otherVars ...map[string]string) string {
resp, err := environmentTemplate(t, template, otherVars...)
if err != nil {
t.Skipf(err.Error())
}
return resp
}

// FirstKeyValue gets it from HCL string
func FirstKeyValue(t *testing.T, str, key string) string {
r := regexp.MustCompile(key + `\s+=\s+"([^"]*)"`)
Expand Down
33 changes: 0 additions & 33 deletions qa/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package qa
import (
"context"
"fmt"
"os"
"testing"

"github.com/databrickslabs/terraform-provider-databricks/common"
Expand All @@ -28,38 +27,6 @@ func TestRandomName(t *testing.T) {
assert.Equal(t, 14, len(n))
}

func TestEnvironmentTemplate(t *testing.T) {
defer common.CleanupEnvironment()
err := os.Setenv("USER", RandomName())
assert.NoError(t, err)

res := EnvironmentTemplate(t, `
resource "user" "me" {
name = "{env.USER}"
email = "{env.USER}+{var.RANDOM}@example.com"
}`)
assert.Equal(t, os.Getenv("USER"), FirstKeyValue(t, res, "name"))
}

func TestEnvironmentTemplate_other_vars(t *testing.T) {
otherVar := map[string]string{"TEST": "value"}
res := EnvironmentTemplate(t, `
resource "user" "me" {
name = "{var.TEST}"
}`, otherVar)
assert.Equal(t, otherVar["TEST"], FirstKeyValue(t, res, "name"))
}

func TestEnvironmentTemplate_unset_env(t *testing.T) {
res, err := environmentTemplate(t, `
resource "user" "me" {
name = "{env.USER}"
email = "{env.USER}+{var.RANDOM}@example.com"
}`)
assert.Equal(t, "", res)
assert.Errorf(t, err, fmt.Sprintf("please set %d variables and restart.", 2))
}

func TestResourceFixture(t *testing.T) {
client, server, err := HttpFixtureClient(t, []HTTPFixture{
{
Expand Down
2 changes: 1 addition & 1 deletion scim/acceptance/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestAccUserResource(t *testing.T) {
if _, ok := os.LookupEnv("CLOUD_ENV"); !ok {
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
}
config := qa.EnvironmentTemplate(t, `
config := acceptance.EnvironmentTemplate(t, `
resource "databricks_user" "first" {
user_name = "eerste+{var.RANDOM}@example.com"
display_name = "Eerste {var.RANDOM}"
Expand Down
5 changes: 2 additions & 3 deletions secrets/acceptance/secret_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/databrickslabs/terraform-provider-databricks/common"
"github.com/databrickslabs/terraform-provider-databricks/internal/acceptance"
"github.com/databrickslabs/terraform-provider-databricks/qa"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
Expand All @@ -24,7 +23,7 @@ func TestAccSecretAclResource(t *testing.T) {
acceptance.AccTest(t, resource.TestCase{
Steps: []resource.TestStep{
{
Config: qa.EnvironmentTemplate(t, `
Config: acceptance.EnvironmentTemplate(t, `
resource "databricks_group" "ds" {
display_name = "data-scientists-{var.RANDOM}"
}
Expand Down Expand Up @@ -69,7 +68,7 @@ func TestAccSecretAclResourceDefaultPrincipal(t *testing.T) {
acceptance.AccTest(t, resource.TestCase{
Steps: []resource.TestStep{
{
Config: qa.EnvironmentTemplate(t, `
Config: acceptance.EnvironmentTemplate(t, `
resource "databricks_secret_scope" "app" {
name = "app-{var.RANDOM}"
initial_manage_principal = "users"
Expand Down
2 changes: 1 addition & 1 deletion secrets/acceptance/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccSecretResource(t *testing.T) {
if _, ok := os.LookupEnv("CLOUD_ENV"); !ok {
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
}
config := qa.EnvironmentTemplate(t, `
config := acceptance.EnvironmentTemplate(t, `
resource "databricks_secret_scope" "this" {
name = "tf-scope-{var.RANDOM}"
}
Expand Down
2 changes: 1 addition & 1 deletion storage/acceptance/adls_gen1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestAzureAccAdlsGen1Mount_correctly_mounts(t *testing.T) {
acceptance.AccTest(t, resource.TestCase{
Steps: []resource.TestStep{
{
Config: qa.EnvironmentTemplate(t, `
Config: acceptance.EnvironmentTemplate(t, `
resource "databricks_secret_scope" "terraform" {
name = "terraform-{var.RANDOM}"
initial_manage_principal = "users"
Expand Down
Loading

0 comments on commit 413ab4b

Please sign in to comment.