helper/resource: compile

This commit is contained in:
Mitchell Hashimoto 2014-09-24 14:23:29 -07:00
parent 7a636551be
commit 9ba39d93b7
1 changed files with 19 additions and 12 deletions

View File

@ -6,10 +6,11 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/terraform"
)
@ -149,22 +150,19 @@ func testStep(
opts terraform.ContextOpts,
state *terraform.State,
step TestStep) (*terraform.State, error) {
// Write the configuration
cfgF, err := ioutil.TempFile("", "tf-test")
cfgPath, err := ioutil.TempDir("", "tf-test")
if err != nil {
return state, fmt.Errorf(
"Error creating temporary file for config: %s", err)
"Error creating temporary directory for config: %s", err)
}
cfgPath := cfgF.Name() + ".tf"
cfgF.Close()
os.Remove(cfgF.Name())
defer os.RemoveAll(cfgPath)
cfgF, err = os.Create(cfgPath)
// Write the configuration
cfgF, err := os.Create(filepath.Join(cfgPath, "main.tf"))
if err != nil {
return state, fmt.Errorf(
"Error creating temporary file for config: %s", err)
}
defer os.Remove(cfgPath)
_, err = io.Copy(cfgF, strings.NewReader(step.Config))
cfgF.Close()
@ -174,14 +172,23 @@ func testStep(
}
// Parse the configuration
config, err := config.Load(cfgPath)
mod, err := module.NewTreeModule("", cfgPath)
if err != nil {
return state, fmt.Errorf(
"Error parsing configuration: %s", err)
"Error loading configuration: %s", err)
}
// Load the modules
modStorage := &module.FolderStorage{
StorageDir: filepath.Join(cfgPath, ".tfmodules"),
}
err = mod.Load(modStorage, module.GetModeGet)
if err != nil {
return state, fmt.Errorf("Error downloading modules: %s", err)
}
// Build the context
opts.Config = config
opts.Module = mod
opts.State = state
ctx := terraform.NewContext(&opts)
if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {