Skip to content

Commit 9f03e04

Browse files
committed
Additional tests for invalid files
1 parent d5bf963 commit 9f03e04

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigSource.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ private static HashMap<String, Object> readFile(String filePath) {
5454
log.error("Unable to read from stable config file {}, dropping input", filePath);
5555
return new HashMap<>();
5656
}
57-
58-
return yaml.load(input);
57+
try {
58+
return yaml.load(input);
59+
} catch (Exception e) {
60+
log.error("YAML parsing error in stable config file {}: {}", filePath, e.getMessage());
61+
return new HashMap<>();
62+
}
5963
}
6064

61-
private static Map<String, Object> parseStableConfig(HashMap<String, Object> data)
62-
throws IOException {
65+
private static Map<String, Object> parseStableConfig(HashMap<String, Object> data) {
6366
HashMap<String, Object> config = new HashMap<>();
6467
Object apmConfig = data.get("apm_configuration_default");
68+
if (apmConfig == null) {
69+
return config;
70+
}
6571
if (apmConfig instanceof HashMap<?, ?>) {
6672
HashMap<?, ?> tempConfig = (HashMap<?, ?>) apmConfig;
6773
for (Map.Entry<?, ?> entry : tempConfig.entrySet()) {

internal-api/src/test/groovy/datadog/trace/bootstrap/config/provider/StableConfigSourceTest.groovy

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class StableConfigSourceTest extends DDSpecification {
2121
// How to check that the "file does not exist" error was logged?
2222
}
2323

24-
def "test valid file"() {
24+
def "test empty file"() {
2525
// test empty file
2626
when:
2727
Path filePath
@@ -42,28 +42,47 @@ class StableConfigSourceTest extends DDSpecification {
4242
then:
4343
config.getKeys().size() == 0
4444
config.getConfigId() == null
45+
}
4546

46-
// test populated file
47+
def "test populated file"() {
4748
when:
48-
def id = "12345"
49-
def key1 = "dd_first_key"
50-
def val1 = "dd_first_val"
51-
def key2 = "dd_second_key"
52-
def val2 = "dd_second_val"
53-
// Create the map that will be used to populate the config file
54-
Map<String, Object> data = new HashMap<>()
55-
data.put("config_id", id)
56-
data.put("apm_configuration_default", new HashMap<String, Object>() {{
57-
put(key1, val1)
58-
put(key2, val2)
59-
}})
49+
Path filePath
50+
StableConfigSource stableCfg
51+
try {
52+
filePath = Files.createTempFile("testFile_", ".yaml")
53+
} catch (IOException e) {
54+
println "Error creating file: ${e.message}"
55+
e.printStackTrace()
56+
return // or throw new RuntimeException("File creation failed", e)
57+
}
58+
if (filePath == null) {
59+
return
60+
}
6061

6162
DumperOptions options = new DumperOptions()
6263
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)
6364

6465
// Prepare to write the data map to the file in yaml format
6566
Yaml yaml = new Yaml(options)
66-
String yamlString = yaml.dump(data)
67+
String yamlString
68+
if (corrupt == true) {
69+
yamlString = '''
70+
abc: 123
71+
def
72+
ghi "jkl"
73+
lmn: 456
74+
'''
75+
} else {
76+
Map<String, Object> data = new HashMap<>()
77+
if (configId != null) {
78+
data.put("config_id", configId)
79+
}
80+
if (configs != null) {
81+
data.put("apm_configuration_default", configs)
82+
}
83+
84+
yamlString = yaml.dump(data)
85+
}
6786

6887
try {
6988
StandardOpenOption[] openOpts = [StandardOpenOption.WRITE] as StandardOpenOption[]
@@ -74,12 +93,26 @@ class StableConfigSourceTest extends DDSpecification {
7493
// fail fast?
7594
}
7695

96+
stableCfg = new StableConfigSource(filePath.toString(), ConfigOrigin.USER_STABLE_CONFIG)
97+
7798
then:
78-
StableConfigSource config2 = new StableConfigSource(filePath.toString(), ConfigOrigin.USER_STABLE_CONFIG)
79-
config2.getConfigId() == id
80-
config2.getKeys().size() == 2
81-
config2.get(key1) == val1
82-
config2.get(key2) == val2
99+
stableCfg.getConfigId() == configId
100+
if (configs == null) {
101+
stableCfg.getKeys().size() == 0
102+
} else {
103+
stableCfg.getKeys().size() == configs.size()
104+
}
83105
Files.delete(filePath)
106+
107+
where:
108+
key_one = "key_one"
109+
val_one = "val_one"
110+
key_two = "key_two"
111+
val_two = "val_2"
112+
configId | configs | corrupt
113+
null | null | true
114+
"" | new HashMap<>() | false
115+
"12345" | new HashMap<>() << ["key_one": "val_one", "key_two": "val_two"] | false
116+
84117
}
85118
}

0 commit comments

Comments
 (0)