@@ -4,6 +4,7 @@ import datadog.trace.api.ConfigOrigin
4
4
import datadog.trace.test.util.DDSpecification
5
5
import org.yaml.snakeyaml.DumperOptions
6
6
import org.yaml.snakeyaml.Yaml
7
+ import spock.lang.Shared
7
8
8
9
import java.nio.file.Path
9
10
import java.nio.file.Files
@@ -18,7 +19,6 @@ class StableConfigSourceTest extends DDSpecification {
18
19
expect :
19
20
config. getKeys(). size() == 0
20
21
config. getConfigId() == null
21
- // How to check that the "file does not exist" error was logged?
22
22
}
23
23
24
24
def " test empty file" () {
@@ -44,75 +44,132 @@ class StableConfigSourceTest extends DDSpecification {
44
44
config. getConfigId() == null
45
45
}
46
46
47
- def " test populated file " () {
47
+ def " test get " () {
48
48
when :
49
- Path filePath
50
- StableConfigSource stableCfg
49
+ Path filePath = tempFile()
50
+ if (filePath == null ) {
51
+ return // fail?
52
+ }
53
+
54
+ def configs = new HashMap<> () << [" DD_SERVICE" : " svc" , " DD_ENV" : " env" , " CONFIG_NO_DD" : " value123" ]
55
+
51
56
try {
52
- filePath = Files . createTempFile( " testFile_ " , " .yaml " )
57
+ writeFileYaml( filePath, " 12345 " , configs )
53
58
} catch (IOException e) {
54
- println " Error creating file: ${ e.message} "
55
- e. printStackTrace()
56
- return // or throw new RuntimeException("File creation failed", e)
59
+ println " Error writing to file: ${ e.message} "
60
+ return // fail?
57
61
}
62
+
63
+ StableConfigSource cfg = new StableConfigSource (filePath. toString(), ConfigOrigin . USER_STABLE_CONFIG )
64
+
65
+ then :
66
+ cfg. getKeys(). size() == 3
67
+ cfg. get(" service" ) == " svc"
68
+ cfg. get(" env" ) == " env"
69
+ cfg. get(" config_no_dd" ) == null
70
+ cfg. get(" config_nonexistent" ) == null
71
+ }
72
+
73
+ def " test file invalid format" () {
74
+ when :
75
+ Path filePath = tempFile()
58
76
if (filePath == null ) {
59
- return
77
+ return // fail?
60
78
}
61
79
62
- DumperOptions options = new DumperOptions ()
63
- options. setDefaultFlowStyle(DumperOptions.FlowStyle . BLOCK )
80
+ try {
81
+ writeFileRaw(filePath, configId, configs)
82
+ } catch (IOException e) {
83
+ println " Error writing to file: ${ e.message} "
84
+ return // fail?
85
+ }
64
86
65
- // Prepare to write the data map to the file in yaml format
66
- Yaml yaml = new Yaml (options)
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)
87
+ StableConfigSource stableCfg = new StableConfigSource (filePath. toString(), ConfigOrigin . USER_STABLE_CONFIG )
88
+
89
+ then :
90
+ stableCfg. getConfigId() == null
91
+ stableCfg. getKeys(). size() == 0
92
+ Files . delete(filePath)
93
+
94
+ where :
95
+ configId | configs
96
+ null | corruptYaml
97
+ " 12345" | " this is not yaml format!"
98
+ }
99
+
100
+ def " test file valid format" () {
101
+ when :
102
+ Path filePath = tempFile()
103
+ if (filePath == null ) {
104
+ return // fail?
85
105
}
86
106
87
107
try {
88
- StandardOpenOption [] openOpts = [StandardOpenOption . WRITE ] as StandardOpenOption []
89
- Files . write(filePath, yamlString. getBytes(), openOpts)
90
- println " YAML written to: $filePath "
108
+ writeFileYaml(filePath, configId, configs)
91
109
} catch (IOException e) {
92
110
println " Error writing to file: ${ e.message} "
93
- // fail fast ?
111
+ return // fail?
94
112
}
95
113
96
- stableCfg = new StableConfigSource (filePath. toString(), ConfigOrigin . USER_STABLE_CONFIG )
114
+ StableConfigSource stableCfg = new StableConfigSource (filePath. toString(), ConfigOrigin . USER_STABLE_CONFIG )
97
115
98
116
then :
99
117
stableCfg. getConfigId() == configId
100
- if (configs == null ) {
101
- stableCfg. getKeys(). size() == 0
102
- } else {
103
- stableCfg. getKeys() . size() == configs. size( )
118
+ stableCfg . getKeys() . size() == configs . size()
119
+ for (key in stableCfg. getKeys()) {
120
+ key = key . substring( 4 ) // Cut `DD_`
121
+ stableCfg. get(key) == configs. get(key )
104
122
}
105
123
Files . delete(filePath)
106
124
107
125
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
126
+ configId | configs
127
+ " " | new HashMap<> ()
128
+ " 12345" | new HashMap<> () << [" DD_KEY_ONE" : " one" , " DD_KEY_TWO" : " two" ]
129
+ }
130
+ // Corrupt YAML string variable used for testing, defined outside the 'where' block for readability
131
+ @Shared
132
+ def corruptYaml = '''
133
+ abc: 123
134
+ def:
135
+ ghi: "jkl"
136
+ lmn: 456
137
+ '''
138
+
139
+ Path tempFile () {
140
+ try {
141
+ return Files . createTempFile(" testFile_" , " .yaml" )
142
+ } catch (IOException e) {
143
+ println " Error creating file: ${ e.message} "
144
+ e. printStackTrace()
145
+ return null // or throw new RuntimeException("File creation failed", e)
146
+ }
147
+ }
148
+
149
+ def writeFileYaml (Path filePath , String configId , Map configs ) {
150
+ DumperOptions options = new DumperOptions ()
151
+ options. setDefaultFlowStyle(DumperOptions.FlowStyle . BLOCK )
152
+
153
+ // Prepare to write the data map to the file in yaml format
154
+ Yaml yaml = new Yaml (options)
155
+ String yamlString
156
+ Map<String , Object > data = new HashMap<> ()
157
+ if (configId != null ) {
158
+ data. put(" config_id" , configId)
159
+ }
160
+ if (configs instanceof HashMap<?, ?> ) {
161
+ data. put(" apm_configuration_default" , configs)
162
+ }
163
+
164
+ yamlString = yaml. dump(data)
165
+
166
+ StandardOpenOption [] openOpts = [StandardOpenOption . WRITE ] as StandardOpenOption []
167
+ Files . write(filePath, yamlString. getBytes(), openOpts)
168
+ }
116
169
170
+ def writeFileRaw (Path filePath , String configId , String configs ) {
171
+ String data = configId + " \n " + configs
172
+ StandardOpenOption [] openOpts = [StandardOpenOption . WRITE ] as StandardOpenOption []
173
+ Files . write(filePath, data. getBytes(), openOpts)
117
174
}
118
175
}
0 commit comments