-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathfile_data_source_spec.rb
More file actions
351 lines (306 loc) · 11 KB
/
file_data_source_spec.rb
File metadata and controls
351 lines (306 loc) · 11 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
require "spec_helper"
require "tempfile"
# see does not allow Ruby objects in YAML" for the purpose of the following two things
$created_bad_class = false
class BadClassWeShouldNotInstantiate < Hash
def []=(key, value)
$created_bad_class = true
end
end
module LaunchDarkly
module Integrations
describe FileData do
let(:full_flag_1_key) { "flag1" }
let(:full_flag_1_value) { "on" }
let(:flag_value_1_key) { "flag2" }
let(:flag_value_1) { "value2" }
let(:all_flag_keys) { [ full_flag_1_key.to_sym, flag_value_1_key.to_sym ] }
let(:full_segment_1_key) { "seg1" }
let(:all_segment_keys) { [ full_segment_1_key.to_sym ] }
let(:invalid_json) { "My invalid JSON" }
let(:flag_only_json) { <<-EOF
{
"flags": {
"flag1": {
"key": "flag1",
"on": true,
"fallthrough": {
"variation": 2
},
"variations": [ "fall", "off", "on" ]
}
}
}
EOF
}
let(:alternate_flag_only_json) { <<-EOF
{
"flags": {
"flag1": {
"key": "flag1",
"on": false,
"fallthrough": {
"variation": 2
},
"variations": [ "fall", "off", "on" ]
}
}
}
EOF
}
let(:segment_only_json) { <<-EOF
{
"segments": {
"seg1": {
"key": "seg1",
"include": ["user1"]
}
}
}
EOF
}
let(:all_properties_json) { <<-EOF
{
"flags": {
"flag1": {
"key": "flag1",
"on": true,
"fallthrough": {
"variation": 2
},
"variations": [ "fall", "off", "on" ]
}
},
"flagValues": {
"flag2": "value2"
},
"segments": {
"seg1": {
"key": "seg1",
"include": ["user1"]
}
}
}
EOF
}
let(:all_properties_yaml) { <<-EOF
---
flags:
flag1:
key: flag1
"on": true
flagValues:
flag2: value2
segments:
seg1:
key: seg1
include: ["user1"]
EOF
}
let(:unsafe_yaml) { <<-EOF
--- !ruby/hash:BadClassWeShouldNotInstantiate
foo: bar
EOF
}
let(:bad_file_path) { "no-such-file" }
before do
@config = LaunchDarkly::Config.new(logger: $null_log)
@store = @config.feature_store
@executor = SynchronousExecutor.new
@status_broadcaster = LaunchDarkly::Impl::Broadcaster.new(@executor, $null_log)
@flag_change_broadcaster = LaunchDarkly::Impl::Broadcaster.new(@executor, $null_log)
@config.data_source_update_sink = LaunchDarkly::Impl::DataSource::UpdateSink.new(@store, @status_broadcaster, @flag_change_broadcaster)
@tmp_dir = Dir.mktmpdir
end
after do
FileUtils.rm_rf(@tmp_dir)
end
def make_temp_file(content)
# Note that we don't create our files in the default temp file directory, but rather in an empty directory
# that we made. That's because (depending on the platform) the temp file directory may contain huge numbers
# of files, which can make the file watcher perform poorly enough to break the tests.
file = Tempfile.new('flags', @tmp_dir)
IO.write(file, content)
file
end
def with_data_source(options, initialize_to_valid = false)
factory = FileData.data_source(options)
if initialize_to_valid
# If the update sink receives an interrupted signal when the state is
# still initializing, it will continue staying in the initializing phase.
# Therefore, we set the state to valid before this test so we can
# determine if the interrupted signal is actually generated.
@config.data_source_update_sink.update_status(LaunchDarkly::Interfaces::DataSource::Status::VALID, nil)
end
ds = factory.call('', @config)
begin
yield ds
ensure
ds.stop
end
end
it "doesn't load flags prior to start" do
file = make_temp_file('{"flagValues":{"key":"value"}}')
with_data_source({ paths: [ file.path ] }) do |_|
expect(@store.initialized?).to eq(false)
expect(@store.all(LaunchDarkly::FEATURES)).to eq({})
expect(@store.all(LaunchDarkly::SEGMENTS)).to eq({})
end
end
it "loads flags on start - from JSON" do
file = make_temp_file(all_properties_json)
with_data_source({ paths: [ file.path ] }) do |ds|
listener = ListenerSpy.new
@status_broadcaster.add_listener(listener)
ds.start
expect(@store.initialized?).to eq(true)
expect(@store.all(LaunchDarkly::FEATURES).keys).to eq(all_flag_keys)
expect(@store.all(LaunchDarkly::SEGMENTS).keys).to eq(all_segment_keys)
expect(listener.statuses.count).to eq(1)
expect(listener.statuses[0].state).to eq(LaunchDarkly::Interfaces::DataSource::Status::VALID)
end
end
it "loads flags on start - from YAML" do
file = make_temp_file(all_properties_yaml)
with_data_source({ paths: [ file.path ] }) do |ds|
ds.start
expect(@store.initialized?).to eq(true)
expect(@store.all(LaunchDarkly::FEATURES).keys).to eq(all_flag_keys)
expect(@store.all(LaunchDarkly::SEGMENTS).keys).to eq(all_segment_keys)
end
end
it "does not allow Ruby objects in YAML" do
# This tests for the vulnerability described here: https://trailofbits.github.io/rubysec/yaml/index.html
# The file we're loading contains a hash with a custom Ruby class, BadClassWeShouldNotInstantiate (see top
# of file). If we're not loading in safe mode, it will create an instance of that class and call its []=
# method, which we've defined to set $created_bad_class to true. In safe mode, it refuses to parse this file.
file = make_temp_file(unsafe_yaml)
with_data_source({ paths: [file.path ] }) do |ds|
event = ds.start
expect(event.set?).to eq(true)
expect(ds.initialized?).to eq(false)
expect($created_bad_class).to eq(false)
end
end
it "sets start event and initialized on successful load" do
file = make_temp_file(all_properties_json)
with_data_source({ paths: [ file.path ] }) do |ds|
event = ds.start
expect(event.set?).to eq(true)
expect(ds.initialized?).to eq(true)
end
end
it "sets start event and does not set initialized on unsuccessful load" do
with_data_source({ paths: [ bad_file_path ] }) do |ds|
event = ds.start
expect(event.set?).to eq(true)
expect(ds.initialized?).to eq(false)
end
end
it "can load multiple files" do
file1 = make_temp_file(flag_only_json)
file2 = make_temp_file(segment_only_json)
with_data_source({ paths: [ file1.path, file2.path ] }) do |ds|
ds.start
expect(@store.initialized?).to eq(true)
expect(@store.all(LaunchDarkly::FEATURES).keys).to eq([ full_flag_1_key.to_sym ])
expect(@store.all(LaunchDarkly::SEGMENTS).keys).to eq([ full_segment_1_key.to_sym ])
end
end
it "file loading failure results in interrupted status" do
file1 = make_temp_file(flag_only_json)
file2 = make_temp_file(invalid_json)
with_data_source({ paths: [ file1.path, file2.path ] }, true) do |ds|
listener = ListenerSpy.new
@status_broadcaster.add_listener(listener)
ds.start
expect(@store.initialized?).to eq(false)
expect(listener.statuses.count).to eq(1)
expect(listener.statuses[0].state).to eq(LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED)
end
end
it "does not allow duplicate keys" do
file1 = make_temp_file(flag_only_json)
file2 = make_temp_file(flag_only_json)
with_data_source({ paths: [ file1.path, file2.path ] }) do |ds|
ds.start
expect(@store.initialized?).to eq(false)
expect(@store.all(LaunchDarkly::FEATURES).keys).to eq([])
end
end
it "allows duplicate keys and uses the last loaded version when allow-duplicates is true" do
file1 = make_temp_file(flag_only_json)
file2 = make_temp_file(alternate_flag_only_json)
with_data_source({ paths: [ file1.path, file2.path ], allow_duplicates: true }) do |ds|
ds.start
expect(@store.initialized?).to eq(true)
expect(@store.all(LaunchDarkly::FEATURES).keys).to_not eq([])
expect(@store.all(LaunchDarkly::FEATURES)[:flag1][:on]).to eq(false)
end
end
it "does not reload modified file if auto-update is off" do
file = make_temp_file(flag_only_json)
with_data_source({ paths: [ file.path ] }) do |ds|
event = ds.start
expect(event.set?).to eq(true)
expect(@store.all(LaunchDarkly::SEGMENTS).keys).to eq([])
IO.write(file, all_properties_json)
sleep(0.5)
expect(@store.all(LaunchDarkly::SEGMENTS).keys).to eq([])
end
end
def test_auto_reload(options)
file = make_temp_file(flag_only_json)
options[:paths] = [ file.path ]
with_data_source(options) do |ds|
event = ds.start
expect(event.set?).to eq(true)
expect(@store.all(LaunchDarkly::SEGMENTS).keys).to eq([])
sleep(1)
IO.write(file, all_properties_json)
max_time = 10
ok = wait_for_condition(10) { @store.all(LaunchDarkly::SEGMENTS).keys == all_segment_keys }
expect(ok).to eq(true), "Waited #{max_time}s after modifying file and it did not reload"
end
end
it "reloads modified file if auto-update is on" do
test_auto_reload({ auto_update: true })
end
it "reloads modified file in polling mode" do
test_auto_reload({ auto_update: true, force_polling: true, poll_interval: 0.1 })
end
it "evaluates simplified flag with client as expected" do
file = make_temp_file(all_properties_json)
factory = FileData.data_source({ paths: file.path })
config = LaunchDarkly::Config.new(send_events: false, data_source: factory)
client = LaunchDarkly::LDClient.new('sdkKey', config)
begin
value = client.variation(flag_value_1_key, { key: 'user', kind: 'user' }, '')
expect(value).to eq(flag_value_1)
ensure
client.close
end
end
it "evaluates full flag with client as expected" do
file = make_temp_file(all_properties_json)
factory = FileData.data_source({ paths: file.path })
config = LaunchDarkly::Config.new(send_events: false, data_source: factory)
client = LaunchDarkly::LDClient.new('sdkKey', config)
begin
value = client.variation(full_flag_1_key, { key: 'user', kind: 'user' }, '')
expect(value).to eq(full_flag_1_value)
ensure
client.close
end
end
def wait_for_condition(max_time)
deadline = Time.now + max_time
while Time.now < deadline
return true if yield
sleep(0.1)
end
false
end
end
end
end