Merge pull request #7510 from hashicorp/f-test-module-inline
terraform: add test helper for inline config loading
This commit is contained in:
commit
86ff2ca7a9
|
@ -2,6 +2,7 @@ package terraform
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -60,6 +61,54 @@ func testModule(t *testing.T, name string) *module.Tree {
|
|||
return mod
|
||||
}
|
||||
|
||||
// testModuleInline takes a map of path -> config strings and yields a config
|
||||
// structure with those files loaded from disk
|
||||
func testModuleInline(t *testing.T, config map[string]string) *module.Tree {
|
||||
cfgPath, err := ioutil.TempDir("", "tf-test")
|
||||
if err != nil {
|
||||
t.Errorf("Error creating temporary directory for config: %s", err)
|
||||
}
|
||||
defer os.RemoveAll(cfgPath)
|
||||
|
||||
for path, configStr := range config {
|
||||
dir := filepath.Dir(path)
|
||||
if dir != "." {
|
||||
err := os.MkdirAll(filepath.Join(cfgPath, dir), os.FileMode(0777))
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating subdir: %s", err)
|
||||
}
|
||||
}
|
||||
// Write the configuration
|
||||
cfgF, err := os.Create(filepath.Join(cfgPath, path))
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating temporary file for config: %s", err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(cfgF, strings.NewReader(configStr))
|
||||
cfgF.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating temporary file for config: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the configuration
|
||||
mod, err := module.NewTreeModule("", cfgPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Error loading configuration: %s", err)
|
||||
}
|
||||
|
||||
// Load the modules
|
||||
modStorage := &getter.FolderStorage{
|
||||
StorageDir: filepath.Join(cfgPath, ".tfmodules"),
|
||||
}
|
||||
err = mod.Load(modStorage, module.GetModeGet)
|
||||
if err != nil {
|
||||
t.Errorf("Error downloading modules: %s", err)
|
||||
}
|
||||
|
||||
return mod
|
||||
}
|
||||
|
||||
func testStringMatch(t *testing.T, s fmt.Stringer, expected string) {
|
||||
actual := strings.TrimSpace(s.String())
|
||||
expected = strings.TrimSpace(expected)
|
||||
|
|
Loading…
Reference in New Issue