-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36448d0
commit 7db9947
Showing
1 changed file
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |