Skip to content

Commit c7cb5e3

Browse files
authored
Merge pull request #306 from airbnb/rushy--haproxy-validation-config
Ensure that haproxy check parameters are set together
2 parents f21e553 + da324e6 commit c7cb5e3

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

lib/synapse/config_generator/haproxy.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -816,19 +816,24 @@ def initialize(opts)
816816
@opts['do_socket'] = true unless @opts.key?('do_socket')
817817
@opts['do_checks'] = false unless @opts.key?('do_checks')
818818
@opts['do_reloads'] = true unless @opts.key?('do_reloads')
819-
req_pairs = {
820-
'do_writes' => 'config_file_path',
821-
'do_socket' => 'socket_file_path',
822-
'do_checks' => 'check_command',
823-
'do_reloads' => 'reload_command'
819+
req_groups = {
820+
'do_writes' => ['config_file_path'],
821+
'do_socket' => ['socket_file_path'],
822+
'do_checks' => ['check_command', 'candidate_config_file_path'],
823+
'do_reloads' => ['reload_command'],
824824
}
825825

826-
req_pairs.each do |cond, req|
827-
if @opts[cond]
828-
raise ArgumentError, "the `#{req}` option is required when `#{cond}` is true" unless @opts[req]
826+
req_groups.each do |cond, reqs|
827+
reqs.each do |req_config|
828+
if @opts[cond]
829+
raise ArgumentError, "the `#{req_config}` option is required when `#{cond}` is true" unless @opts[req_config]
830+
end
829831
end
830832
end
831833

834+
# Default candidate_config_file_path so that write_config can do atomic writes.
835+
@opts['candidate_config_file_path'] = "#{@opts['config_file_path']}.tmp" unless @opts.key?('candidate_config_file_path')
836+
832837
# socket_file_path can be a string or a list
833838
# lets make a new option which is always a list (plural)
834839
@opts['socket_file_paths'] = [@opts['socket_file_path']].flatten

spec/lib/synapse/haproxy_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,47 @@ class MockWatcher; end;
272272
expect{Synapse::ConfigGenerator::Haproxy.new(conf)}.
273273
to raise_error(ArgumentError, "the `#{value}` option is required when `#{key}` is true")
274274
end
275+
end
275276

277+
context 'when do_checks is true' do
278+
let(:invalid_conf) {
279+
{
280+
'global' => [],
281+
'defaults' => [],
282+
'do_reloads' => false,
283+
'do_socket' => false,
284+
'do_writes' => false,
285+
'do_checks' => true,
286+
}
287+
}
288+
let(:conf_additions) {
289+
{
290+
'check_command' => 'haproxy -c -f /etc/haproxy/haproxy-candidate.cfg',
291+
'candidate_config_file_path' => '/etc/haproxy/haproxy-candidate.cfg',
292+
}
293+
}
294+
295+
context 'when check parameters are not set together' do
296+
it 'raises an error' do
297+
conf_additions.each do |key, value|
298+
conf = invalid_conf.clone
299+
conf[key] = value
300+
expect{Synapse::ConfigGenerator::Haproxy.new(conf)}.
301+
to raise_error(ArgumentError)
302+
end
303+
end
304+
end
305+
306+
context 'when check parameters are set together' do
307+
it 'does not raise an error' do
308+
conf = invalid_conf.clone
309+
conf_additions.each do |key, value|
310+
conf[key] = value
311+
end
312+
313+
expect{Synapse::ConfigGenerator::Haproxy.new(conf)}.not_to raise_error
314+
end
315+
end
276316
end
277317

278318
it 'properly defaults do_writes, do_socket, do_checks, do_reloads, use_nerve_weights' do
@@ -793,6 +833,20 @@ class MockWatcher; end;
793833
allow(File).to receive(:read).with('candidate_config_file').and_return('candidate-haproxy-config')
794834
}
795835

836+
context 'when candidate_config_file_path is not set' do
837+
before {
838+
config['haproxy'].delete('candidate_config_file_path')
839+
config['haproxy']['do_checks'] = false
840+
}
841+
842+
it 'still succeeds' do
843+
allow(FileUtils).to receive(:mv)
844+
845+
expect(File).to receive(:write).with('config_file.tmp', 'new-config')
846+
expect{subject.write_config('new-config')}.not_to raise_error
847+
end
848+
end
849+
796850
context 'when config changes' do
797851
it 'writes candidate config' do
798852
allow(FileUtils).to receive(:mv)
@@ -883,6 +937,7 @@ class MockWatcher; end;
883937

884938
config['haproxy']['do_checks'] = true
885939
config['haproxy']['check_command'] = 'haproxy_check_mock'
940+
config['haproxy']['candidate_config_file_path'] = 'candidate-haproxy-config-file'
886941
}
887942

888943
it 'calls the supplied command' do

0 commit comments

Comments
 (0)