-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
163 lines (137 loc) · 3.35 KB
/
parser_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import "testing"
func Test_parseDeployment(t *testing.T) {
expectedLength := 1
expectedMap := map[string]string{
"FOO": "bar",
"BAR": "foo",
}
m, err := parseDeployment([]byte(deploymentMock))
if err != nil {
t.Errorf("did not expect an error: %q", err)
}
if len(m) != expectedLength {
t.Fatalf("expecting len(m) = %d, got %d", expectedLength, len(m))
}
for k, v := range expectedMap {
if v != m[0][k] {
t.Errorf("expecting m[%s] = %q, got: %q", k, v, m[0][k])
}
}
}
func Test_parseDeployment_parse_error(t *testing.T) {
expectedLength := 0
m, err := parseDeployment([]byte(invalidYAML))
if err == nil {
t.Errorf("expecting an error")
}
if len(m) != expectedLength {
t.Fatalf("expecting len(m) = %d, got %d", expectedLength, len(m))
}
}
func Test_parseDeploymentWithTemplate(t *testing.T) {
expectedLength := 1
expectedMap := map[string]string{
"FOO": "bar",
"ANSWER": "42",
}
m, err := parseDeploymentWithTemplate([]byte(deploymentWithTemplateMock))
if err != nil {
t.Errorf("did not expect an error: %q", err)
}
if len(m) != expectedLength {
t.Fatalf("expecting len(m) = %d, got %d", expectedLength, len(m))
}
for k, v := range expectedMap {
if v != m[0][k] {
t.Errorf("expecting m[%s] = %q, got: %q", k, v, m[0][k])
}
}
}
func Test_parseDeploymentWithTemplate_helm_workaround(t *testing.T) {
expectedLength := 1
expectedMap := map[string]string{
"FOO": "bar",
"ANSWER": "42",
}
m, err := parseDeploymentWithTemplate(
[]byte(deploymentWithTemplateHelmTemplateMock))
if err != nil {
t.Errorf("did not expect an error: %q", err)
}
if len(m) != expectedLength {
t.Fatalf("expecting len(m) = %d, got %d", expectedLength, len(m))
}
for k, v := range expectedMap {
if v != m[0][k] {
t.Errorf("expecting m[%s] = %q, got: %q", k, v, m[0][k])
}
}
}
func Test_parseDeploymentWithTemplate_parse_error(t *testing.T) {
expectedLength := 0
m, err := parseDeploymentWithTemplate([]byte(invalidYAML))
if err == nil {
t.Errorf("expecting an error")
}
if len(m) != expectedLength {
t.Fatalf("expecting len(m) = %d, got %d", expectedLength, len(m))
}
}
var deploymentMock = `apiVersion: v1
kind: Pod
metadata:
name: Test_parseDeployment
labels:
purpose: testing
spec:
containers:
- name: Test_parseDeployment
image: gcr.io/google-samples/node-hello:1.0
env:
- name: FOO
value: "bar"
- name: BAR
value: "foo"
`
var deploymentWithTemplateMock = `apiVersion: v1
kind: Pod
metadata:
name: Test_parseDeploymentWithTemplate
labels:
purpose: testing
spec:
someKey: some value
template:
spec:
containers:
- name: Testing container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: FOO
value: "bar"
- name: ANSWER
value: "42"
`
var deploymentWithTemplateHelmTemplateMock = `apiVersion: v1
kind: Pod
metadata:
name: Test_parseDeploymentWithTemplate
labels:
purpose: testing
broken_key: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
spec:
someKey: some value
template:
spec:
containers:
- name: Testing container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: FOO
value: "bar"
- name: ANSWER
value: "42"
`
var invalidYAML = `
""INVALID"`