Merge pull request #7510 from hashicorp/f-test-module-inline

terraform: add test helper for inline config loading
This commit is contained in:
Paul Hinze 2016-07-06 09:35:27 -05:00 committed by GitHub
commit 86ff2ca7a9
1 changed files with 49 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package terraform
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -60,6 +61,54 @@ func testModule(t *testing.T, name string) *module.Tree {
return mod 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) { func testStringMatch(t *testing.T, s fmt.Stringer, expected string) {
actual := strings.TrimSpace(s.String()) actual := strings.TrimSpace(s.String())
expected = strings.TrimSpace(expected) expected = strings.TrimSpace(expected)