71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package basic
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/gruntwork-io/terratest/modules/terraform"
|
|
util "github.com/rancher/terraform-provider-file/test"
|
|
)
|
|
|
|
// This tests the example we are giving to docs.
|
|
func TestSnapshotExample(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
id := util.GetId()
|
|
directory := "file_local_snapshot"
|
|
repoRoot, err := util.GetRepoRoot(t)
|
|
if err != nil {
|
|
t.Fatalf("Error getting git root directory: %v", err)
|
|
}
|
|
exampleDir := filepath.Join(repoRoot, "examples", "resources", directory)
|
|
testDir := filepath.Join(repoRoot, "test", "data", id)
|
|
|
|
err = util.Setup(t, id, "test/data")
|
|
if err != nil {
|
|
t.Log("Test failed, tearing down...")
|
|
util.TearDown(t, testDir, &terraform.Options{})
|
|
t.Fatalf("Error creating test data directories: %s", err)
|
|
}
|
|
statePath := filepath.Join(testDir, "tfstate")
|
|
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
|
|
TerraformDir: exampleDir,
|
|
Vars: map[string]interface{}{}, // Examples don't need vars.
|
|
BackendConfig: map[string]interface{}{
|
|
"path": statePath,
|
|
},
|
|
EnvVars: map[string]string{
|
|
"TF_DATA_DIR": testDir,
|
|
"TF_CLI_CONFIG_FILE": filepath.Join(repoRoot, "test", ".terraformrc"),
|
|
"TF_IN_AUTOMATION": "1",
|
|
"TF_CLI_ARGS_init": "-no-color",
|
|
"TF_CLI_ARGS_plan": "-no-color",
|
|
"TF_CLI_ARGS_apply": "-no-color",
|
|
"TF_CLI_ARGS_destroy": "-no-color",
|
|
"TF_CLI_ARGS_output": "-no-color",
|
|
},
|
|
RetryableTerraformErrors: util.GetRetryableTerraformErrors(),
|
|
NoColor: true,
|
|
Upgrade: true,
|
|
})
|
|
|
|
_, err = terraform.InitAndApplyE(t, terraformOptions)
|
|
if err != nil {
|
|
t.Log("Test failed, tearing down...")
|
|
util.TearDown(t, testDir, terraformOptions)
|
|
t.Fatalf("Error creating file: %s", err)
|
|
}
|
|
_, err = terraform.OutputAllE(t, terraformOptions)
|
|
if err != nil {
|
|
t.Log("Output failed, moving along...")
|
|
}
|
|
// if the apply doesn't error then this is good enough for the example
|
|
if t.Failed() {
|
|
t.Log("Test failed...")
|
|
} else {
|
|
t.Log("Test passed...")
|
|
}
|
|
t.Log("Test complete, tearing down...")
|
|
util.TearDown(t, testDir, terraformOptions)
|
|
}
|