-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcopy_test.go
218 lines (188 loc) · 5.34 KB
/
copy_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package component
import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
"sort"
"testing"
)
func ExampleCopy_ConfigStyles() {
fmt.Printf("%v\n", parseAndVerify(nil, `
pod.copy.test: /simple/source:/to/target
`))
fmt.Printf("%v\n", parseAndVerify(nil, `
pod.copy.test: >
/etc/conf/logging.conf:/opt/legacy/app/conf/logs.xml
`))
fmt.Printf("%v\n", parseAndVerify(nil, `
pod.copy.test: |
- /etc/conf/ssl.cert:/etc/ssl/myapp.cert
- /etc/conf/ssl.key:/etc/ssl/myapp.key
`))
fmt.Printf("%v\n", sorted(parseAndVerify(nil, `
pod.copy.test: |
/etc/conf/cache.conf: /opt/cache/conf.d/default.conf
/etc/conf/ssl.cert: /etc/ssl/myapp.cert
/etc/conf/ssl.key: /etc/ssl/myapp.key
`)))
// Output:
// [{/simple/source /to/target}]
// [{/etc/conf/logging.conf /opt/legacy/app/conf/logs.xml}]
// [{/etc/conf/ssl.cert /etc/ssl/myapp.cert} {/etc/conf/ssl.key /etc/ssl/myapp.key}]
// [{/etc/conf/cache.conf /opt/cache/conf.d/default.conf} {/etc/conf/ssl.cert /etc/ssl/myapp.cert} {/etc/conf/ssl.key /etc/ssl/myapp.key}]
}
func ExampleCopy_ParsingFailures() {
parseAndExpectFailure("pod.copy.test: /src")
parseAndExpectFailure("pod.copy.test: /src: /dst")
parseAndExpectFailure(`
pod.copy.test: >
/src: /dst: /extra`)
parseAndExpectFailure(`
pod.copy.test: >
/not: /multiline
/so/it: /fails`)
parseAndExpectFailure(`
pod.copy.test: >
- /not:/multiline
- /so/parsed:/wrong`)
parseAndExpectFailure(`
pod.copy.test: |
/number: 1`)
parseAndExpectFailure(`
pod.copy.test: |
- /valid:/ok
- /invalid`)
parseAndExpectFailure(`
pod.copy.test: |
'': /empty`)
parseAndExpectFailure(`
pod.copy.test: >
/should:/have
/been:/with/pipe
/not:/greater/than/sign`)
// Output:
// invalid pod.copy configuration: /src
// yaml: mapping values are not allowed in this context
// yaml: mapping values are not allowed in this context
// yaml: mapping values are not allowed in this context
// invalid pod.copy configuration: [/not:/multiline - /so/parsed:/wrong] [/not:/multiline - /so/parsed:/wrong]
// not a string value: 1 (int)
// invalid pod.copy configuration: [/valid:/ok /invalid] [/invalid]
// invalid pod.copy configuration: map[:/empty] [:/empty]
// invalid pod.copy configuration: /should:/have /been:/with/pipe /not:/greater/than/sign
}
func TestCopy_ParseAsSimpleString(t *testing.T) {
parseAndVerify(t, "pod.copy.test: /src:/dst",
CopyConfig{"/src", "/dst"})
parseAndVerify(t, `
pod.copy.test: >
/new:/line
`, CopyConfig{"/new", "/line"})
}
func TestCopy_ParseAsSequenceOfStrings(t *testing.T) {
parseAndVerify(t, `
pod.copy.test: |
- /one:/first
- /two:/second
`, CopyConfig{"/one", "/first"}, CopyConfig{"/two", "/second"})
parseAndVerify(t, `
pod.copy.test: >
[/one:/first, /two:/second]
`, CopyConfig{"/one", "/first"}, CopyConfig{"/two", "/second"})
}
func TestCopy_ParseAsMappingOfStrings(t *testing.T) {
parseAndVerify(t, `
pod.copy.test: |
/one: /first
/two: /second
`, CopyConfig{"/one", "/first"}, CopyConfig{"/two", "/second"})
parseAndVerify(t, `
pod.copy.test: |
/with: /in
/some: /the
/whitespace: /mapping
`,
CopyConfig{"/with", "/in"},
CopyConfig{"/some", "/the"},
CopyConfig{"/whitespace", "/mapping"})
}
func TestCopy_SameSourceDifferentTargets(t *testing.T) {
parseAndVerify(t, `
pod.copy.test: |
- '/src:/target/1'
- '/src:/target/2'
`, CopyConfig{"/src", "/target/1"}, CopyConfig{"/src", "/target/2"})
}
func parseAndVerify(t *testing.T, yamlConfig string, expectedConfigs ...CopyConfig) []CopyConfig {
definition, err := getCopyDefinition(yamlConfig)
if err != nil {
if t == nil {
fmt.Println("Invalid YAML:", yamlConfig, err)
return nil
}
t.Fatal("Invalid YAML:", yamlConfig, err)
}
parsed, err := parseCopyConfig(definition)
if err != nil {
if t == nil {
fmt.Println("Failed to parse:", definition, err)
return nil
}
t.Fatal("Failed to parse:", definition, err)
}
if expectedConfigs == nil {
return parsed
}
if len(parsed) != len(expectedConfigs) {
if t == nil {
fmt.Println("Parsed length doesn't match expected:", len(parsed), "!=", len(expectedConfigs), definition)
return nil
}
t.Error("Parsed length doesn't match expected:", len(parsed), "!=", len(expectedConfigs), definition)
}
for _, actual := range parsed {
found := false
for _, expected := range expectedConfigs {
if actual.Source == expected.Source && actual.Target == expected.Target {
found = true
break
}
}
if !found {
if t == nil {
fmt.Println("Parsed item", actual, "not found in", expectedConfigs)
return nil
}
t.Error("Parsed item", actual, "not found in", expectedConfigs)
}
}
return parsed
}
func sorted(configs []CopyConfig) []CopyConfig {
sort.Slice(configs, func(i, j int) bool { return configs[i].Source < configs[j].Source })
return configs
}
func parseAndExpectFailure(yamlConfig string) {
definition, err := getCopyDefinition(yamlConfig)
if err != nil {
fmt.Println(err)
return
}
parsed, err := parseCopyConfig(definition)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(fmt.Sprintf("parsed as: %+v", parsed))
}
}
func getCopyDefinition(yamlConfig string) (string, error) {
var config map[string]string
if err := yaml.Unmarshal([]byte(yamlConfig), &config); err != nil {
return "", err
}
definition, ok := config["pod.copy.test"]
if !ok {
return "", errors.New("missing pod.copy.test key in the YAML")
}
return definition, nil
}