-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathStableConfigSourceTest.groovy
More file actions
225 lines (195 loc) · 8.54 KB
/
StableConfigSourceTest.groovy
File metadata and controls
225 lines (195 loc) · 8.54 KB
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
219
220
221
222
223
224
225
package datadog.trace.bootstrap.config.provider
import static java.util.Collections.singletonMap
import datadog.trace.api.ConfigOrigin
import datadog.trace.bootstrap.config.provider.stableconfig.Rule
import datadog.trace.bootstrap.config.provider.stableconfig.Selector
import datadog.trace.bootstrap.config.provider.stableconfig.StableConfig
import datadog.trace.test.util.DDSpecification
import org.snakeyaml.engine.v2.api.Dump
import org.snakeyaml.engine.v2.api.DumpSettings
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.read.ListAppender
import org.slf4j.LoggerFactory
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption
class StableConfigSourceTest extends DDSpecification {
def "test file doesn't exist"() {
setup:
StableConfigSource config = new StableConfigSource(StableConfigSource.LOCAL_STABLE_CONFIG_PATH, ConfigOrigin.LOCAL_STABLE_CONFIG)
expect:
config.getKeys().size() == 0
config.getConfigId() == null
}
def "test empty file"() {
when:
Path filePath = Files.createTempFile("testFile_", ".yaml")
then:
if (filePath == null) {
throw new AssertionError("Failed to create: " + filePath)
}
when:
StableConfigSource config = new StableConfigSource(filePath.toString(), ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
config.getKeys().size() == 0
config.getConfigId() == null
}
def "test file invalid format"() {
// StableConfigSource must handle the exception thrown by StableConfigParser.parse(filePath) gracefully
when:
Path filePath = Files.createTempFile("testFile_", ".yaml")
then:
if (filePath == null) {
throw new AssertionError("Failed to create: " + filePath)
}
when:
writeFileRaw(filePath, configId, data)
StableConfigSource stableCfg = new StableConfigSource(filePath.toString(), ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
stableCfg.getConfigId() == null
stableCfg.getKeys().size() == 0
Files.delete(filePath)
where:
configId | data
null | corruptYaml
"12345" | "this is not yaml format!"
}
def "test file valid format"() {
when:
Path filePath = Files.createTempFile("testFile_", ".yaml")
then:
if (filePath == null) {
throw new AssertionError("Failed to create: " + filePath)
}
when:
StableConfig stableConfigYaml = new StableConfig(configId, defaultConfigs)
writeFileYaml(filePath, stableConfigYaml)
StableConfigSource stableCfg = new StableConfigSource(filePath.toString(), ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
for (key in defaultConfigs.keySet()) {
String keyString = (String) key
keyString = keyString.substring(4) // Cut `DD_`
stableCfg.get(keyString) == defaultConfigs.get(key)
}
// All configs from MatchingRule should be applied
if (ruleConfigs.contains(sampleMatchingRule)) {
for (key in sampleMatchingRule.getConfiguration().keySet()) {
String keyString = (String) key
keyString = keyString.substring(4) // Cut `DD_`
stableCfg.get(keyString) == defaultConfigs.get(key)
}
}
// None of the configs from NonMatchingRule should be applied
if (ruleConfigs.contains(sampleNonMatchingRule)) {
Set<String> cfgKeys = stableCfg.getKeys()
for (key in sampleMatchingRule.getConfiguration().keySet()) {
String keyString = (String) key
keyString = keyString.substring(4) // Cut `DD_`
!cfgKeys.contains(keyString)
}
}
Files.delete(filePath)
where:
configId | defaultConfigs | ruleConfigs
"" | [:] | Arrays.asList(new Rule())
"12345" | ["DD_KEY_ONE": "one", "DD_KEY_TWO": "two"] | Arrays.asList(sampleMatchingRule, sampleNonMatchingRule)
}
def "test parse invalid logs mapping errors"() {
given:
Logger logbackLogger = (Logger) LoggerFactory.getLogger(datadog.trace.bootstrap.config.provider.StableConfigSource)
def listAppender = new ListAppender<ILoggingEvent>()
listAppender.start()
logbackLogger.addAppender(listAppender)
def tempFile = File.createTempFile("testFile_", ".yaml")
tempFile.text = yaml
when:
def stableCfg = new StableConfigSource(tempFile.absolutePath, ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
stableCfg.config == StableConfigSource.StableConfig.EMPTY
def warnLogs = listAppender.list.findAll { it.level.toString() == 'WARN' }
warnLogs.any { it.formattedMessage.contains(expectedLogSubstring) }
cleanup:
tempFile.delete()
logbackLogger.detachAppender(listAppender)
where:
yaml | expectedLogSubstring
// Missing 'origin' in selector
'''apm_configuration_rules:
- selectors:
- key: "someKey"
matches: ["someValue"]
operator: equals
configuration:
DD_SERVICE: "test"
''' | "Missing 'origin' in selector"
// Missing 'configuration' in rule
'''apm_configuration_rules:
- selectors:
- origin: process_arguments
key: "-Dfoo"
matches: ["bar"]
operator: equals
''' | "Missing 'configuration' in rule"
// Missing 'selectors' in rule
'''apm_configuration_rules:
- configuration:
DD_SERVICE: "test"
''' | "Missing 'selectors' in rule"
// Triggers ClassCastException (selectors should be a list, not a string)
'''apm_configuration_rules:
- selectors: "not-a-list"
configuration:
DD_SERVICE: "test"
''' | "'selectors' must be a list, but got: String"
// configuration present but not a map
'''apm_configuration_rules:
- selectors:
- origin: process_arguments
key: "-Dfoo"
matches: ["bar"]
operator: equals
configuration: "not-a-map"
''' | "'configuration' must be a map, but got: String"
// configuration present but not a map (integer)
'''apm_configuration_rules:
- selectors:
- origin: process_arguments
key: "-Dfoo"
matches: ["bar"]
operator: equals
configuration: 12345
''' | "'configuration' must be a map, but got: Integer"
}
// Corrupt YAML string variable used for testing, defined outside the 'where' block for readability
def static corruptYaml = '''
abc: 123
def:
ghi: "jkl"
lmn: 456
'''
// Matching and non-matching Rules used for testing, defined outside the 'where' block for readability
def static sampleMatchingRule = new Rule(Arrays.asList(new Selector("origin", "language", Arrays.asList("Java"), null)), singletonMap("DD_KEY_THREE", "three"))
def static sampleNonMatchingRule = new Rule(Arrays.asList(new Selector("origin", "language", Arrays.asList("Golang"), null)), singletonMap("DD_KEY_FOUR", "four"))
def writeFileYaml(Path filePath, StableConfig stableConfigs) {
Map<String, Object> yamlData = new HashMap<>()
if (stableConfigs.getConfigId() != null && !stableConfigs.getConfigId().isEmpty()) {
yamlData.put("config_id", stableConfigs.getConfigId())
}
if (stableConfigs.getApmConfigurationDefault() != null && !stableConfigs.getApmConfigurationDefault().isEmpty()) {
yamlData.put("apm_configuration_default", stableConfigs.getApmConfigurationDefault())
}
DumpSettings settings = DumpSettings.builder().build()
Dump dump = new Dump(settings)
String yamlContent = dump.dumpToString(yamlData)
try (FileWriter writer = new FileWriter(filePath.toFile())) {
writer.write(yamlContent)
}
}
// Use this if you want to explicitly write/test configId
def writeFileRaw(Path filePath, String configId, String data) {
data = configId + "\n" + data
StandardOpenOption[] openOpts = [StandardOpenOption.WRITE] as StandardOpenOption[]
Files.write(filePath, data.getBytes(), openOpts)
}
}