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
|
package nametemplate
import (
"strings"
"testing"
)
const (
zone = 0
namespace = 1
service = 2
)
// Map of format string :: expected locations of name symbols in the format.
// -1 value indicates that symbol does not exist in format.
var exampleTemplates = map[string][]int{
"{service}.{namespace}.{zone}": []int{2, 1, 0}, // service symbol expected @ position 0, namespace @ 1, zone @ 2
"{namespace}.{zone}": []int{1, 0, -1},
"": []int{-1, -1, -1},
}
func TestSetTemplate(t *testing.T) {
for s, expectedValue := range exampleTemplates {
n := new(NameTemplate)
n.SetTemplate(s)
// check the indexes resulting from calling SetTemplate() against expectedValues
if expectedValue[zone] != -1 {
if n.Element["zone"] != expectedValue[zone] {
t.Errorf("Expected zone at index '%v', instead found at index '%v' for format string '%v'", expectedValue[zone], n.Element["zone"], s)
}
}
}
}
func TestGetServiceFromSegmentArray(t *testing.T) {
var (
n *NameTemplate
formatString string
queryString string
splitQuery []string
expectedService string
actualService string
)
// Case where template contains {service}
n = new(NameTemplate)
formatString = "{service}.{namespace}.{zone}"
n.SetTemplate(formatString)
queryString = "myservice.mynamespace.coredns"
splitQuery = strings.Split(queryString, ".")
expectedService = "myservice"
actualService = n.GetServiceFromSegmentArray(splitQuery)
if actualService != expectedService {
t.Errorf("Expected service name '%v', instead got service name '%v' for query string '%v' and format '%v'", expectedService, actualService, queryString, formatString)
}
// Case where template does not contain {service}
n = new(NameTemplate)
formatString = "{namespace}.{zone}"
n.SetTemplate(formatString)
queryString = "mynamespace.coredns"
splitQuery = strings.Split(queryString, ".")
expectedService = ""
actualService = n.GetServiceFromSegmentArray(splitQuery)
if actualService != expectedService {
t.Errorf("Expected service name '%v', instead got service name '%v' for query string '%v' and format '%v'", expectedService, actualService, queryString, formatString)
}
}
func TestGetZoneFromSegmentArray(t *testing.T) {
var (
n *NameTemplate
formatString string
queryString string
splitQuery []string
expectedZone string
actualZone string
)
// Case where template contains {zone}
n = new(NameTemplate)
formatString = "{service}.{namespace}.{zone}"
n.SetTemplate(formatString)
queryString = "myservice.mynamespace.coredns"
splitQuery = strings.Split(queryString, ".")
expectedZone = "coredns"
actualZone = n.GetZoneFromSegmentArray(splitQuery)
if actualZone != expectedZone {
t.Errorf("Expected zone name '%v', instead got zone name '%v' for query string '%v' and format '%v'", expectedZone, actualZone, queryString, formatString)
}
// Case where template does not contain {zone}
n = new(NameTemplate)
formatString = "{service}.{namespace}"
n.SetTemplate(formatString)
queryString = "mynamespace.coredns"
splitQuery = strings.Split(queryString, ".")
expectedZone = ""
actualZone = n.GetZoneFromSegmentArray(splitQuery)
if actualZone != expectedZone {
t.Errorf("Expected zone name '%v', instead got zone name '%v' for query string '%v' and format '%v'", expectedZone, actualZone, queryString, formatString)
}
// Case where zone is multiple segments
n = new(NameTemplate)
formatString = "{service}.{namespace}.{zone}"
n.SetTemplate(formatString)
queryString = "myservice.mynamespace.coredns.cluster.local"
splitQuery = strings.Split(queryString, ".")
expectedZone = "coredns.cluster.local"
actualZone = n.GetZoneFromSegmentArray(splitQuery)
if actualZone != expectedZone {
t.Errorf("Expected zone name '%v', instead got zone name '%v' for query string '%v' and format '%v'", expectedZone, actualZone, queryString, formatString)
}
}
|