Skip to content

Commit

Permalink
Fixing the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Apr 27, 2014
1 parent 0034d10 commit 0152e29
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 50 deletions.
2 changes: 1 addition & 1 deletion consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewConsulClient(addr string) (*Client, error) {
return c, nil
}

// GetValues queries Consul for keys prefixed by prefix.
// GetValues queries Consul for keys
func (c *Client) GetValues(keys []string) (map[string]interface{}, error) {
vars := make(map[string]interface{})
for _, key := range keys {
Expand Down
6 changes: 5 additions & 1 deletion etcd/etcdutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (

// Client is a wrapper around the etcd client
type Client struct {
client *etcd.Client
client EtcdClient
}

type EtcdClient interface {
Get(key string, sort, recurse bool) (*etcd.Response, error)
}

// NewEtcdClient returns an *etcd.Client with a connection to named machines.
Expand Down
52 changes: 14 additions & 38 deletions etcd/etcdutil/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,23 @@ import (
"testing"
)

type PathToKeyTest struct {
key, prefix, expected string
}

var pathToKeyTests = []PathToKeyTest{
// Without prefix
{"/nginx/port", "", "nginx_port"},
{"/nginx/worker_processes", "", "nginx_worker_processes"},
{"/foo/bar/mat/zoo", "", "foo_bar_mat_zoo"},
// With prefix
{"/prefix/nginx/port", "/prefix", "nginx_port"},
// With prefix and trailing slash
{"/prefix/nginx/port", "/prefix/", "nginx_port"},
}

func TestPathToKey(t *testing.T) {
for _, pt := range pathToKeyTests {
result := pathToKey(pt.key, pt.prefix)
if result != pt.expected {
t.Errorf("Expected pathToKey(%s, %s) to == %s, got %s",
pt.key, pt.prefix, pt.expected, result)
}
}
}

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,
Key: "/foo",
Dir: true,
Value: "",
Nodes: etcd.Nodes{
etcd.Node{Key: "/foo/one",Dir: false,Value: "one"},
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,
Key: "/foo/three",
Dir: true,
Value: "",
Nodes: etcd.Nodes{
etcd.Node{Key: "/foo/three/bar", Value: "three_bar", Dir: false},
Expand All @@ -60,14 +36,14 @@ func TestGetValues(t *testing.T) {
}
quuxResp := &etcd.Response{
Action: "GET",
Node: &etcd.Node{Key:"/quux", Dir: false, Value: "foo"},
Node: &etcd.Node{Key: "/quux", Dir: false, Value: "foo"},
}
nginxResp := &etcd.Response{
Action: "GET",
Node: &etcd.Node{
Key: "/nginx",
Key: "/nginx",
Value: "",
Dir: true,
Dir: true,
Nodes: etcd.Nodes{
etcd.Node{Key: "/nginx/port", Dir: false, Value: "443"},
etcd.Node{Key: "/nginx/worker_processes", Dir: false, Value: "4"},
Expand All @@ -79,20 +55,20 @@ func TestGetValues(t *testing.T) {
c.AddResponse("/quux", quuxResp)
c.AddResponse("/nginx", nginxResp)
keys := []string{"/nginx", "/foo", "/quux"}
values, err := GetValues(c, "", keys)
values, err := client.GetValues(keys)
if err != nil {
t.Error(err.Error())
}
if values["nginx_port"] != "443" {
if values["/nginx/port"] != "443" {
t.Errorf("Expected nginx_port to == 443, got %s", values["nginx_port"])
}
if values["nginx_worker_processes"] != "4" {
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" {
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" {
if values["/quux"] != "foo" {
t.Errorf("Expected quux == foo, got %s", values["quux"])
}
}
68 changes: 58 additions & 10 deletions resource/template/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,39 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"text/template"

"github.com/coreos/go-etcd/etcd"
"github.com/kelseyhightower/confd/config"
"github.com/kelseyhightower/confd/etcd/etcdtest"
"github.com/kelseyhightower/confd/log"
)

type MockKey struct {
Key string
Value string
}

type MockStore struct {
Keys []*MockKey
}

func (m *MockStore) GetValues(keys []string) (map[string]interface{}, error) {
vals := make(map[string]interface{})
for _, key := range keys {
for _, set := range m.Keys {
if strings.HasPrefix(set.Key, key) {
vals[set.Key] = set.Value
}
}
}
return vals, nil
}

func (m *MockStore) AddKey(key string, value string) {
m.Keys = append(m.Keys, &MockKey{key, value})
}

// createTempDirs is a helper function which creates temporary directories
// required by confd. createTempDirs returns the path name representing the
// confd confDir.
Expand Down Expand Up @@ -107,9 +131,8 @@ func TestProcessTemplateResources(t *testing.T) {
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)
c := &MockStore{}
c.AddKey("/foo", "bar")

// Process the test template resource.
runErrors := ProcessTemplateResources(c)
Expand Down Expand Up @@ -181,9 +204,8 @@ func TestProcessTemplateResourcesNoop(t *testing.T) {
config.SetNoop(true)

// 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)
c := &MockStore{}
c.AddKey("/foo", "bar")

// Process the test template resource.
runErrors := ProcessTemplateResources(c)
Expand Down Expand Up @@ -214,8 +236,9 @@ func TestBrokenTemplateResourceFile(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
// Create the stub etcd client.
c := etcdtest.NewClient()
// Create the stub client.
c := &MockStore{}

// Process broken template resource config file.
_, err = NewTemplateResourceFromPath(tempFile.Name(), c)
if err == nil {
Expand Down Expand Up @@ -297,3 +320,28 @@ func TestIsFileExist(t *testing.T) {
t.Errorf("Expected IsFileExist(%s) to be true, got %v", f.Name(), result)
}
}

type PathToKeyTest struct {
key, prefix, expected string
}

var pathToKeyTests = []PathToKeyTest{
// Without prefix
{"/nginx/port", "", "nginx_port"},
{"/nginx/worker_processes", "", "nginx_worker_processes"},
{"/foo/bar/mat/zoo", "", "foo_bar_mat_zoo"},
// With prefix
{"/prefix/nginx/port", "/prefix", "nginx_port"},
// With prefix and trailing slash
{"/prefix/nginx/port", "/prefix/", "nginx_port"},
}

func TestPathToKey(t *testing.T) {
for _, pt := range pathToKeyTests {
result := pathToKey(pt.key, pt.prefix)
if result != pt.expected {
t.Errorf("Expected pathToKey(%s, %s) to == %s, got %s",
pt.key, pt.prefix, pt.expected, result)
}
}
}

0 comments on commit 0152e29

Please sign in to comment.