Skip to content

Commit 9b36adf

Browse files
author
Matthew Fisher
committed
Merge pull request #3859 from Joshua-Anderson/config-testing
test(deisctl): add tests to config package
2 parents 68415ae + 5f23556 commit 9b36adf

3 files changed

Lines changed: 120 additions & 12 deletions

File tree

deisctl/config/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package config
33
// Client interface used for configuration
44
type Client interface {
55
Get(string) (string, error)
6-
Set(string) (string, error)
6+
Set(string, string) (string, error)
7+
Delete(string) error
78
}

deisctl/config/config.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package config
33
import (
44
"encoding/base64"
55
"fmt"
6+
"io"
67
"io/ioutil"
8+
"os"
79
"strings"
810

911
"github.com/deis/deis/deisctl/utils"
@@ -20,7 +22,12 @@ var b64Keys = []string{"/deis/platform/sshPrivateKey"}
2022

2123
// Config runs the config subcommand
2224
func Config(target string, action string, key []string) error {
23-
return doConfig(target, action, key)
25+
client, err := getEtcdClient()
26+
if err != nil {
27+
return err
28+
}
29+
30+
return doConfig(target, action, key, client, os.Stdout)
2431
}
2532

2633
// CheckConfig looks for a value at a keyspace path
@@ -40,15 +47,12 @@ func CheckConfig(root string, k string) error {
4047
return nil
4148
}
4249

43-
func doConfig(target string, action string, key []string) error {
44-
client, err := getEtcdClient()
45-
if err != nil {
46-
return err
47-
}
48-
50+
func doConfig(target string, action string, key []string, client Client, w io.Writer) error {
4951
rootPath := "/deis/" + target + "/"
5052

5153
var vals []string
54+
var err error
55+
5256
if action == "set" {
5357
vals, err = doConfigSet(client, rootPath, key)
5458
} else if action == "rm" {
@@ -62,12 +66,12 @@ func doConfig(target string, action string, key []string) error {
6266

6367
// print results
6468
for _, v := range vals {
65-
fmt.Printf("%v\n", v)
69+
fmt.Fprintf(w, "%v\n", v)
6670
}
6771
return nil
6872
}
6973

70-
func doConfigSet(client *etcdClient, root string, kvs []string) ([]string, error) {
74+
func doConfigSet(client Client, root string, kvs []string) ([]string, error) {
7175
var result []string
7276

7377
for _, kv := range kvs {
@@ -94,7 +98,7 @@ func doConfigSet(client *etcdClient, root string, kvs []string) ([]string, error
9498
return result, nil
9599
}
96100

97-
func doConfigGet(client *etcdClient, root string, keys []string) ([]string, error) {
101+
func doConfigGet(client Client, root string, keys []string) ([]string, error) {
98102
var result []string
99103
for _, k := range keys {
100104
val, err := client.Get(root + k)
@@ -106,7 +110,7 @@ func doConfigGet(client *etcdClient, root string, keys []string) ([]string, erro
106110
return result, nil
107111
}
108112

109-
func doConfigRm(client *etcdClient, root string, keys []string) ([]string, error) {
113+
func doConfigRm(client Client, root string, keys []string) ([]string, error) {
110114
var result []string
111115
for _, k := range keys {
112116
err := client.Delete(root + k)

deisctl/config/config_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,115 @@
11
package config
22

33
import (
4+
"bytes"
45
"encoding/base64"
6+
"fmt"
57
"io/ioutil"
68
"os"
79
"testing"
810
)
911

12+
type KVPair struct {
13+
key string
14+
value string
15+
}
16+
17+
type MockStore []KVPair
18+
19+
type mockClient struct {
20+
expected MockStore
21+
}
22+
23+
func (m mockClient) Get(key string) (value string, err error) {
24+
for _, expect := range m.expected {
25+
if expect.key == key {
26+
return expect.value, nil
27+
}
28+
}
29+
return "", fmt.Errorf("%s does not exist", m.expected)
30+
}
31+
32+
func (m mockClient) Set(key, value string) (returnedValue string, err error) {
33+
for _, expect := range m.expected {
34+
if expect.key == key {
35+
return value, nil
36+
}
37+
}
38+
return "", fmt.Errorf("%s does not exist", m.expected)
39+
}
40+
41+
func (m mockClient) Delete(key string) (err error) {
42+
for _, expect := range m.expected {
43+
if expect.key == key {
44+
return nil
45+
}
46+
}
47+
return fmt.Errorf("%s does not exist", m.expected)
48+
}
49+
50+
func TestGetConfig(t *testing.T) {
51+
52+
testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}, {key: "/deis/controller/port", value: "8000"}}}
53+
testWriter := bytes.Buffer{}
54+
55+
err := doConfig("controller", "get", []string{"testing", "port"}, testMock, &testWriter)
56+
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
61+
expected := "foo\n8000\n"
62+
output := testWriter.String()
63+
if output != expected {
64+
t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
65+
}
66+
}
67+
68+
func TestGetConfigError(t *testing.T) {
69+
testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}}}
70+
testWriter := bytes.Buffer{}
71+
72+
err := doConfig("controller", "get", []string{"port"}, testMock, &testWriter)
73+
74+
if err == nil {
75+
t.Fatal("Error Expected")
76+
}
77+
}
78+
79+
func TestSetConfig(t *testing.T) {
80+
testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}, {key: "/deis/controller/port", value: "8000"}}}
81+
testWriter := bytes.Buffer{}
82+
83+
err := doConfig("controller", "set", []string{"testing=bar", "port=1000"}, testMock, &testWriter)
84+
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
89+
expected := "bar\n1000\n"
90+
output := testWriter.String()
91+
if output != expected {
92+
t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
93+
}
94+
}
95+
96+
func TestDeleteConfig(t *testing.T) {
97+
testMock := mockClient{expected: MockStore{{key: "/deis/controller/testing", value: "foo"}, {key: "/deis/controller/port", value: "8000"}}}
98+
testWriter := bytes.Buffer{}
99+
100+
err := doConfig("controller", "rm", []string{"testing", "port"}, testMock, &testWriter)
101+
102+
if err != nil {
103+
t.Fatal(err)
104+
}
105+
106+
expected := "testing\nport\n"
107+
output := testWriter.String()
108+
if output != expected {
109+
t.Error(fmt.Errorf("Expected: '%s', Got:'%s'", expected, output))
110+
}
111+
}
112+
10113
// TestConfigSSHPrivateKey ensures private keys are base64 encoded from file path
11114
func TestConfigSSHPrivateKey(t *testing.T) {
12115

0 commit comments

Comments
 (0)