* add DNS record * revamp tests * lint * cleanup * feat dns test * chore: add DNS Record tests * linting * f
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package testing
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
|
"github.com/hashicorp/terraform-plugin-testing/plancheck"
|
|
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// MarkAccTest marks the test as acceptance test. Useful when executing code before resource.ParallelTest or resource.Test
|
|
// to bring acceptance test check earlier when test environment is required
|
|
func MarkAccTest(t *testing.T) {
|
|
t.Helper()
|
|
if os.Getenv(resource.EnvTfAcc) == "" {
|
|
t.Skipf("Acceptance tests skipped unless env '%s' set", resource.EnvTfAcc)
|
|
return
|
|
}
|
|
}
|
|
|
|
func ImportStep(name string, ignore ...string) resource.TestStep {
|
|
step := resource.TestStep{
|
|
ResourceName: name,
|
|
ImportState: true,
|
|
ImportStateVerify: true,
|
|
}
|
|
|
|
if len(ignore) > 0 {
|
|
step.ImportStateVerifyIgnore = ignore
|
|
}
|
|
|
|
return step
|
|
}
|
|
|
|
// SiteAndIDImportStateIDFunc returns a function that can be used to import resources that require site and id.
|
|
func SiteAndIDImportStateIDFunc(resourceName string) func(*terraform.State) (string, error) {
|
|
return func(s *terraform.State) (string, error) {
|
|
rs, ok := s.RootModule().Resources[resourceName]
|
|
if !ok {
|
|
return "", fmt.Errorf("not found: %s", resourceName)
|
|
}
|
|
id := rs.Primary.Attributes["id"]
|
|
site := rs.Primary.Attributes["site"]
|
|
return site + ":" + id, nil
|
|
}
|
|
}
|
|
|
|
// PreCheck checks if provided environment variables are set. If not, it will fail the test.
|
|
func PreCheck(t *testing.T) {
|
|
variables := []string{
|
|
"UNIFI_USERNAME",
|
|
"UNIFI_PASSWORD",
|
|
"UNIFI_API",
|
|
}
|
|
|
|
for _, variable := range variables {
|
|
value := os.Getenv(variable)
|
|
if value == "" {
|
|
t.Fatalf("`%s` must be set for acceptance tests!", variable)
|
|
}
|
|
}
|
|
}
|
|
|
|
func CheckPlanPreApply(checks ...plancheck.PlanCheck) resource.ConfigPlanChecks {
|
|
return resource.ConfigPlanChecks{
|
|
PreApply: checks,
|
|
}
|
|
}
|
|
|
|
func CheckResourceAction(resourceAddress string, action plancheck.ResourceActionType) resource.ConfigPlanChecks {
|
|
return CheckPlanPreApply(plancheck.ExpectResourceAction(resourceAddress, action))
|
|
}
|
|
|
|
func ComposeConfig(configs ...string) string {
|
|
return strings.Join(configs, "\n")
|
|
}
|