-
Notifications
You must be signed in to change notification settings - Fork 357
Expand file tree
/
Copy pathStableConfigSourceTest.groovy
More file actions
170 lines (140 loc) · 4.89 KB
/
Copy pathStableConfigSourceTest.groovy
File metadata and controls
170 lines (140 loc) · 4.89 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
package datadog.trace.bootstrap.config.provider
import datadog.trace.api.ConfigOrigin
import datadog.trace.test.util.DDSpecification
import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml
import spock.lang.Shared
import java.nio.file.Path
import java.nio.file.Files
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 = tempFile()
if (filePath == null) {
throw new AssertionError("Failed to create test file")
}
StableConfigSource config = new StableConfigSource(filePath.toString(), ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
config.getKeys().size() == 0
config.getConfigId() == null
}
def "test get"() {
when:
Path filePath = tempFile()
if (filePath == null) {
throw new AssertionError("Failed to create test file")
}
def configs = new HashMap<>() << ["DD_SERVICE": "svc", "DD_ENV": "env", "CONFIG_NO_DD": "value123"]
try {
writeFileYaml(filePath, "12345", configs)
} catch (IOException e) {
throw new AssertionError("Failed to write to file: ${e.message}")
}
StableConfigSource cfg = new StableConfigSource(filePath.toString(), ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
cfg.get("service") == "svc"
cfg.get("env") == "env"
cfg.get("config_no_dd") == null
cfg.get("config_nonexistent") == null
cfg.getKeys().size() == 3
cfg.getConfigId() == "12345"
Files.delete(filePath)
}
def "test file invalid format"() {
when:
Path filePath = tempFile()
if (filePath == null) {
throw new AssertionError("Failed to create test file")
}
try {
writeFileRaw(filePath, configId, configs)
} catch (IOException e) {
throw new AssertionError("Failed to write to file: ${e.message}")
}
StableConfigSource stableCfg = new StableConfigSource(filePath.toString(), ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
stableCfg.getConfigId() == null
stableCfg.getKeys().size() == 0
Files.delete(filePath)
where:
configId | configs
null | corruptYaml
"12345" | "this is not yaml format!"
}
def "test file valid format"() {
when:
Path filePath = tempFile()
if (filePath == null) {
throw new AssertionError("Failed to create test file")
}
try {
writeFileYaml(filePath, configId, configs)
} catch (IOException e) {
throw new AssertionError("Failed to write to file: ${e.message}")
}
StableConfigSource stableCfg = new StableConfigSource(filePath.toString(), ConfigOrigin.LOCAL_STABLE_CONFIG)
then:
for (key in configs.keySet()) {
String keyString = (String) key
keyString = keyString.substring(4) // Cut `DD_`
stableCfg.get(keyString) == configs.get(key)
}
Files.delete(filePath)
where:
configId | configs
"" | new HashMap<>()
"12345" | new HashMap<>() << ["DD_KEY_ONE": "one", "DD_KEY_TWO": "two"]
}
// Corrupt YAML string variable used for testing, defined outside the 'where' block for readability
@Shared
def corruptYaml = '''
abc: 123
def:
ghi: "jkl"
lmn: 456
'''
static Path tempFile() {
try {
return Files.createTempFile("testFile_", ".yaml")
} catch (IOException e) {
println "Error creating file: ${e.message}"
e.printStackTrace()
return null // or throw new RuntimeException("File creation failed", e)
}
}
static writeFileYaml(Path filePath, String configId, Map configs) {
DumperOptions options = new DumperOptions()
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
// Prepare to write the data map to the file in yaml format
Yaml yaml = new Yaml(options)
String yamlString
Map<String, Object> data = new HashMap<>()
if (configId != null) {
data.put("config_id", configId)
}
if (configs instanceof HashMap<?, ?>) {
data.put("apm_configuration_default", configs)
}
yamlString = yaml.dump(data)
StandardOpenOption[] openOpts = [StandardOpenOption.WRITE] as StandardOpenOption[]
Files.write(filePath, yamlString.getBytes(), openOpts)
}
// Use this if you want to explicitly write/test configId
def writeFileRaw(Path filePath, String configId, String configs) {
String data = configId + "\n" + configs
StandardOpenOption[] openOpts = [StandardOpenOption.WRITE] as StandardOpenOption[]
Files.write(filePath, data.getBytes(), openOpts)
}
static writeFileRaw(Path filePath, String data) {
StandardOpenOption[] openOpts = [StandardOpenOption.WRITE] as StandardOpenOption[]
Files.write(filePath, data.getBytes(), openOpts)
}
}