From 7f501c808656253164dbc850e3486ed2b10e3c1c Mon Sep 17 00:00:00 2001 From: Feng Pan Date: Thu, 14 Sep 2017 15:28:36 -0700 Subject: [PATCH] [synapse] fix bug in generate_backend_stanze --- lib/synapse/config_generator/haproxy.rb | 2 +- spec/lib/synapse/haproxy_spec.rb | 42 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/synapse/config_generator/haproxy.rb b/lib/synapse/config_generator/haproxy.rb index 01a5d303..c128e277 100644 --- a/lib/synapse/config_generator/haproxy.rb +++ b/lib/synapse/config_generator/haproxy.rb @@ -1068,7 +1068,7 @@ def generate_backend_stanza(watcher, config) # The ordering here is important. First we add all the backends in the # disabled state... - @state_cache.backends(watcher).each do |backend_name, backend| + @state_cache.backends(watcher.name).each do |backend_name, backend| backends[backend_name] = backend.merge('enabled' => false) # We remember the haproxy_server_id from a previous reload here. # Note though that if live servers below define haproxy_server_id diff --git a/spec/lib/synapse/haproxy_spec.rb b/spec/lib/synapse/haproxy_spec.rb index fa3633cf..ce4c0a28 100644 --- a/spec/lib/synapse/haproxy_spec.rb +++ b/spec/lib/synapse/haproxy_spec.rb @@ -481,6 +481,48 @@ class MockWatcher; end; expect(subject.generate_backend_stanza(mockwatcher, mockConfig)).to eql(["\nbackend example_service", [], ["\tserver somehost:5555 somehost:5555 id 1 cookie somehost:5555 check inter 2000 rise 3 fall 2"]]) end + describe 'when known backend gets offline' do + let(:mockStateCache) do + mockCache = double(Synapse::ConfigGenerator::Haproxy::HaproxyState) + allow(mockCache).to receive(:backends).with(mockwatcher.name).and_return( + { + "somehost2:5555" => { + "host" => "somehost2", + "port" => 5555, + 'haproxy_server_id' => 10, + } + } + ) + mockCache + end + + before do + allow(mockwatcher).to receive(:config_for_generator).and_return( + { + 'haproxy' => { + 'server_options' => "check inter 2000 rise 3 fall 2", + 'backend_order' => 'no_shuffle', + } + } + ) + subject.instance_variable_set(:@state_cache, mockStateCache) + end + + it 'generates backend stanza with the disabled stat' do + mockConfig = ['mode tcp'] + expect(subject.generate_backend_stanza(mockwatcher, mockConfig)).to eql( + [ + "\nbackend example_service", + ["\tmode tcp"], + [ + "\tserver somehost2:5555 somehost2:5555 id 10 check inter 2000 rise 3 fall 2 disabled", + "\tserver somehost:5555 somehost:5555 id 1 check inter 2000 rise 3 fall 2" + ] + ] + ) + end + end + describe 'generate backend stanza in correct order' do let(:multiple_backends_stanza_map) do {