Skip to content

Commit

Permalink
Add tests for template_resource.go
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyhightower committed Oct 21, 2013
1 parent 36448d0 commit 7db9947
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions template_resource_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,112 @@
package main

import (
"github.com/coreos/go-etcd/etcd"
"github.com/kelseyhightower/confd/etcdtest"
"io/ioutil"
"os"
"path/filepath"
"testing"
"text/template"
)

// createTempDirs is a helper function which creates temporary directories
// required by confd. createTempDirs returns the path name representing the
// confd confDir.
// It returns an error if any.
func createTempDirs() (string, error) {
confDir, err := ioutil.TempDir("", "")
if err != nil {
return "", err
}
err = os.Mkdir(filepath.Join(confDir, "templates"), 0755)
if err != nil {
return "", err
}
err = os.Mkdir(filepath.Join(confDir, "conf.d"), 0755)
if err != nil {
return "", err
}
return confDir, nil
}

var templateResourceTmpl = `
[template]
src = "{{ .src }}"
dest = "{{ .dest }}"
keys = [
"/foo",
]
`

func TestProcessTemplateResources(t *testing.T) {
// Setup temporary conf, config, and template directories.
tempConfDir, err := createTempDirs()
if err != nil {
t.Errorf("Failed to create temp dirs: %s", err.Error())
}
defer os.RemoveAll(tempConfDir)

// Create the src template.
srcTemplateFile := filepath.Join(tempConfDir, "templates", "foo.tmpl")
err = ioutil.WriteFile(srcTemplateFile, []byte("foo = {{ .foo }}"), 0644)
if err != nil {
t.Error(err.Error())
}

// Create the dest.
destFile, err := ioutil.TempFile("", "")
if err != nil {
t.Errorf("Failed to create destFile: %s", err.Error())
}
defer os.Remove(destFile.Name())

// Create the template resource configuration file.
templateResourcePath := filepath.Join(tempConfDir, "conf.d", "foo.toml")
templateResourceFile, err := os.Create(templateResourcePath)
if err != nil {
t.Errorf(err.Error())
}
tmpl, err := template.New("templateResource").Parse(templateResourceTmpl)
if err != nil {
t.Errorf("Unable to parse template resource template: %s", err.Error())
}
data := make(map[string]string)
data["src"] = "foo.tmpl"
data["dest"] = destFile.Name()
err = tmpl.Execute(templateResourceFile, data)
if err != nil {
t.Errorf(err.Error())
}

// Load the confd configuration settings.
if err := loadConfig(""); err != nil {
t.Errorf(err.Error())
}
config.Confd.Prefix = ""
// Use the temporary tempConfDir from above.
config.Confd.ConfDir = tempConfDir

// Create the stub etcd client.
c := etcdtest.NewClient()
fooResp := []*etcd.Response{
{Key: "/foo", Dir: false, Value: "bar"},
}
c.AddResponse("/foo", fooResp)

// Process the test template resource.
err = ProcessTemplateResources(c)
if err != nil {
t.Errorf(err.Error())
}

// Verify the results.
expected := "foo = bar"
results, err := ioutil.ReadFile(destFile.Name())
if err != nil {
t.Error(err.Error())
}
if string(results) != expected {
t.Errorf("Expected contents of dest == '%s', got %s", expected, string(results))
}
}

0 comments on commit 7db9947

Please sign in to comment.