-
Notifications
You must be signed in to change notification settings - Fork 3
/
kv_test.go
133 lines (124 loc) · 3.42 KB
/
kv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"reflect"
"testing"
)
func TestAvailableKvBackends(t *testing.T) {
expected_result := []string{
"etcd",
// Add new backends here as they become available.
}
result := availableKvBackends()
if reflect.DeepEqual(result, expected_result) != true {
t.Error("Expected", expected_result, "got", result)
}
}
func TestCreateKvBackend(t *testing.T) {
// Test a valid backend
result, err := CreateKvBackend(map[string]string{
"backend": "etcd",
"host": "10.2.3.4",
"port": "2379",
"prefix": "someprefix",
})
if err != nil {
t.Fatal("Expected no error, got", err)
}
// No easy way to test equality on result.Kapi... its a private type upstream :(
result_name := result.BackendName()
if result_name != "etcd" {
t.Error("Expected a return type of etcd, got", result_name)
}
result_prefix := result.GetPrefix()
if result_prefix != "someprefix" {
t.Error("Expected a .Prefix of someprefix, got", result_prefix)
}
// And a failure
_, err = CreateKvBackend(map[string]string{
"backend": "nonexistent",
})
if err == nil {
t.Fatal("Expected an error, got nil")
}
}
func TestGetKvBackendValueType(t *testing.T) {
expected_result := KvBackendValue{
Host: "10.2.3.4",
Port: 5061,
}
result := getKvBackendValueType("10.2.3.4", 5061)
if reflect.DeepEqual(result, expected_result) != true {
t.Error("Expected", expected_result, "got", result)
}
}
func TestGetKvBackendValueJsonType(t *testing.T) {
expected_result := KvBackendValue{
Host: "10.3.4.5",
Port: 5064,
}
// Test a valid entry
result, err := getKvBackendValueJsonType("{\"host\":\"10.3.4.5\",\"port\":5064}")
if err != nil {
t.Fatal("Expected no error, got", err)
}
if reflect.DeepEqual(result, expected_result) != true {
t.Error("Expected", expected_result, "got", result)
}
// And a failure
_, err = getKvBackendValueJsonType("{\"host\"\"10.3.4.5\",\"port\":5064}")
if err == nil {
t.Fatal("Expected an error, got nil")
}
}
func TestGetKvBackendValueJsonString(t *testing.T) {
expected_result := "{\"host\":\"10.4.5.6\",\"port\":5065}"
// Test a valid entry
result, err := getKvBackendValueJsonString(KvBackendValue{
Host: "10.4.5.6",
Port: 5065,
})
if err != nil {
t.Fatal("Expected no error, got", err)
}
if reflect.DeepEqual(result, expected_result) != true {
t.Error("Expected", expected_result, "got", result)
}
// And a failure
// TODOLATER: find something that will fail this, that still builds.
/*
_, err = getKvBackendValueJsonString(KvBackendValue{
Host: "10.4.5.6",
Port: 5065,
})
if err == nil {
t.Fatal("Expected an error, got nil")
}
*/
}
//
func TestGetKvKeyWithPrefix(t *testing.T) {
expected_result1 := "someprefix/somekey"
result1 := getKvKeyWithPrefix("someprefix", "somekey")
if result1 != expected_result1 {
t.Error("Expected", expected_result1, "got", result1)
}
//
expected_result2 := "anotherprefix"
result2 := getKvKeyWithPrefix("anotherprefix", "")
if result2 != expected_result2 {
t.Error("Expected", expected_result2, "got", result2)
}
}
func TestStripKvKeyPrefix(t *testing.T) {
expected_result1 := "somekey"
result1 := stripKvKeyPrefix("someprefix", "/someprefix/somekey")
if result1 != expected_result1 {
t.Error("Expected", expected_result1, "got", result1)
}
//
expected_result2 := ""
result2 := stripKvKeyPrefix("anotherprefix", "/anotherprefix")
if result2 != expected_result2 {
t.Error("Expected", expected_result2, "got", result2)
}
}