From 1a4bd24e1ad83d50748171d75bd0a4c9c924ce84 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 6 Jul 2016 08:56:29 -0500 Subject: [PATCH] terraform: add test helper for inline config loading In scenarios with a lot of small configs, it's tedious to fan out actual dir trees in a test-fixtures dir. It also spreads out the context of the test - requiring the reader fetch a bunch of scattered 3 line files in order to understand what is being tested. Our config loading code still only reads from disk, but in the `helper/resource` acc test framework we work around this by writing inline config to temp files and loading it from there. This helper is based on that strategy. Eventually it'd be great to be able to build up a `module.Tree` from config directly, but this gets us the functionality today. Example Usage: testModuleInline(t, map[string]string{ "top.tf": ` module "middle" { source = "./middle" } `, "middle/mid.tf": ` module "bottom" { source = "./bottom" amap { foo = "bar" } } `, "middle/bottom/bot.tf": ` variable "amap" { type = "map" } `, }), --- terraform/terraform_test.go | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index 33412a8ce..c608d3d73 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -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)