-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved
TrimLeadingWhitespace
from internal
to commands
- Loading branch information
Showing
22 changed files
with
171 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package internal | ||
package commands | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.