Commit 6914baa4 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Merge pull request #364 from mwhooker/362

provisioner/chef-solo: template expansion of `json` 
parents 8019cd18 d2e8bf47
......@@ -5,6 +5,7 @@ import (
"strconv"
"testing"
"time"
"encoding/json"
)
func TestConfigTemplateProcess_timestamp(t *testing.T) {
......@@ -47,6 +48,39 @@ func TestConfigTemplateProcess_user(t *testing.T) {
}
}
func TestJsonTemplateProcess_user(t *testing.T) {
tpl, err := NewConfigTemplate()
if err != nil {
t.Fatalf("err: %s", err)
}
tpl.UserVars["foo"] = "bar"
jsonData := make(map[string]interface{})
jsonData["key"] = map[string]string{
"key1": "{{user `foo`}}",
}
jsonBytes, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
t.Fatalf("err: %s", err)
}
var jsonString = string(jsonBytes)
jsonString, err = tpl.Process(jsonString, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
var dat map[string]map[string]interface{}
if err := json.Unmarshal([]byte(jsonString), &dat); err != nil {
t.Fatalf("err: %s", err)
}
if dat["key"]["key1"] != "bar" {
t.Fatalf("found %s instead", dat["key"]["key1"])
}
}
func TestConfigTemplateValidate(t *testing.T) {
tpl, err := NewConfigTemplate()
if err != nil {
......
......@@ -95,7 +95,6 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
sliceTemplates := map[string][]string{
"cookbook_paths": p.config.CookbookPaths,
"remote_cookbook_paths": p.config.RemoteCookbookPaths,
"run_list": p.config.RunList,
}
for n, slice := range sliceTemplates {
......@@ -237,9 +236,14 @@ func (p *Provisioner) createJson(ui packer.Ui, comm packer.Communicator) (string
return "", err
}
jsonBytesProcessed, err := p.config.tpl.Process(string(jsonBytes), nil)
if err != nil {
return "", err
}
// Upload the bytes
remotePath := filepath.Join(p.config.StagingDir, "node.json")
if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes)); err != nil {
if err := comm.Upload(remotePath, bytes.NewReader([]byte(jsonBytesProcessed))); err != nil {
return "", err
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment