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"
        }
      `,
    }),
This commit is contained in:
Paul Hinze 2016-07-06 08:56:29 -05:00
parent 3b732131d2
commit 1a4bd24e1a
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
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)