Ejemplo n.º 1
0
func TestBrokenTemplateResourceFile(t *testing.T) {
	log.SetQuiet(true)
	tempFile, err := ioutil.TempFile("", "")
	defer os.Remove(tempFile.Name())
	if err != nil {
		t.Errorf(err.Error())
	}
	_, err = tempFile.WriteString(brokenTemplateResourceConfig)
	if err != nil {
		t.Errorf(err.Error())
	}
	// Create the stub etcd client.
	c := etcdtest.NewClient()
	// Process broken template resource config file.
	_, err = NewTemplateResourceFromPath(tempFile.Name(), c)
	if err == nil {
		t.Errorf("Expected err not to be nil")
	}
}
Ejemplo n.º 2
0
func TestGetValues(t *testing.T) {
	// Use stub etcd client.
	c := etcdtest.NewClient()
	client := &Client{c}

	fooResp := &etcd.Response{
		Action: "GET",
		Node: &etcd.Node{
			Key:   "/foo",
			Dir:   true,
			Value: "",
			Nodes: etcd.Nodes{
				&etcd.Node{Key: "/foo/one", Dir: false, Value: "one"},
				&etcd.Node{Key: "foo/two", Dir: false, Value: "two"},
				&etcd.Node{
					Key:   "/foo/three",
					Dir:   true,
					Value: "",
					Nodes: etcd.Nodes{
						&etcd.Node{Key: "/foo/three/bar", Value: "three_bar", Dir: false},
					},
				},
			},
		},
	}
	quuxResp := &etcd.Response{
		Action: "GET",
		Node:   &etcd.Node{Key: "/quux", Dir: false, Value: "foo"},
	}
	nginxResp := &etcd.Response{
		Action: "GET",
		Node: &etcd.Node{
			Key:   "/nginx",
			Value: "",
			Dir:   true,
			Nodes: etcd.Nodes{
				&etcd.Node{Key: "/nginx/port", Dir: false, Value: "443"},
				&etcd.Node{Key: "/nginx/worker_processes", Dir: false, Value: "4"},
			},
		},
	}

	c.AddResponse("/foo", fooResp)
	c.AddResponse("/quux", quuxResp)
	c.AddResponse("/nginx", nginxResp)
	keys := []string{"/nginx", "/foo", "/quux"}
	values, err := client.GetValues(keys)
	if err != nil {
		t.Error(err.Error())
	}
	if values["/nginx/port"] != "443" {
		t.Errorf("Expected nginx_port to == 443, got %s", values["nginx_port"])
	}
	if values["/nginx/worker_processes"] != "4" {
		t.Errorf("Expected nginx_worker_processes == 4, got %s", values["nginx_worker_processes"])
	}
	if values["/foo/three/bar"] != "three_bar" {
		t.Errorf("Expected foo_three_bar == three_bar, got %s", values["foo_three_bar"])
	}
	if values["/quux"] != "foo" {
		t.Errorf("Expected quux == foo, got %s", values["quux"])
	}
}
Ejemplo n.º 3
0
func TestProcessTemplateResources(t *testing.T) {
	log.SetQuiet(true)
	// 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("templateResourceConfig").Parse(templateResourceConfigTmpl)
	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 := config.LoadConfig(""); err != nil {
		t.Errorf(err.Error())
	}
	config.SetPrefix("")
	// Use the temporary tempConfDir from above.
	config.SetConfDir(tempConfDir)

	// Create the stub etcd client.
	c := etcdtest.NewClient()
	fooResp := &etcd.Response{Action: "GET", Node: &etcd.Node{Key: "/foo", Dir: false, Value: "bar"}}
	c.AddResponse("/foo", fooResp)

	// Process the test template resource.
	runErrors := ProcessTemplateResources(c)
	if len(runErrors) > 0 {
		for _, e := range runErrors {
			t.Errorf(e.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))
	}
}