diff --git a/lib/puppet/provider/sensu_redis_config/json.rb b/lib/puppet/provider/sensu_redis_config/json.rb index fef18688d6..d0c299ccf6 100644 --- a/lib/puppet/provider/sensu_redis_config/json.rb +++ b/lib/puppet/provider/sensu_redis_config/json.rb @@ -105,6 +105,18 @@ def sentinels=(value) end end + def master + conf['redis']['master'] || :absent + end + + def master=(value) + if value == :absent + conf['redis'].delete 'master' + else + conf['redis']['master'] = value.to_s + end + end + def auto_reconnect conf['redis']['auto_reconnect'] end diff --git a/lib/puppet/type/sensu_redis_config.rb b/lib/puppet/type/sensu_redis_config.rb index c1061c93db..da6b012152 100644 --- a/lib/puppet/type/sensu_redis_config.rb +++ b/lib/puppet/type/sensu_redis_config.rb @@ -114,6 +114,16 @@ def insync?(is) end end + newproperty(:master) do + desc "Redis master name in the sentinel configuration" + # Use absent to ensure that config is flushed + # when property gets unset + defaultto :absent + def insync?(is) + if should.is_a?(Symbol) then should == is else super(is) end + end + end + autorequire(:package) do ['sensu'] end diff --git a/manifests/init.pp b/manifests/init.pp index 686cf4fe44..5363b6b5f5 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -177,6 +177,10 @@ # Array. Redis Sentinel configuration and connection information for one or more Sentinels # Default: Not configured # +# [*redis_master*] +# String. Redis master name in the sentinel configuration +# Default: undef. In the end whatever sensu defaults to, which is "mymaster" currently. +# # [*redis_auto_reconnect*] # Boolean. Reconnect to Redis in the event of a connection failure # Default: true @@ -359,6 +363,7 @@ $redis_db = 0, $redis_auto_reconnect = true, $redis_sentinels = undef, + $redis_master = undef, $api_bind = '0.0.0.0', $api_host = '127.0.0.1', $api_port = 4567, diff --git a/manifests/redis/config.pp b/manifests/redis/config.pp index 600c3e3433..4771d294e8 100644 --- a/manifests/redis/config.pp +++ b/manifests/redis/config.pp @@ -27,6 +27,7 @@ $host = $has_sentinels ? { false => $sensu::redis_host, true => undef, } $port = $has_sentinels ? { false => $sensu::redis_port, true => undef, } $sentinels = $has_sentinels ? { true => $sensu::redis_sentinels, false => undef, } + $master = $has_sentinels ? { true => $sensu::redis_master, false => undef, } sensu_redis_config { $::fqdn: ensure => $ensure, @@ -38,6 +39,7 @@ db => $sensu::redis_db, auto_reconnect => $sensu::redis_auto_reconnect, sentinels => $sentinels, + master => $master, } } diff --git a/spec/classes/sensu_redis_spec.rb b/spec/classes/sensu_redis_spec.rb index 0f08c9db17..91cd538ffe 100644 --- a/spec/classes/sensu_redis_spec.rb +++ b/spec/classes/sensu_redis_spec.rb @@ -30,6 +30,7 @@ :db => 1, :auto_reconnect => false, :sentinels => nil, + :master => nil )} end # be configurable without sentinels @@ -44,7 +45,8 @@ }, { 'host' => 'redis2.domain.com', 'port' => '5678' - }] + }], + :redis_master => 'master-name' } } it { should contain_sensu_redis_config('testhost.domain.com').with( @@ -59,7 +61,8 @@ }, { 'host' => 'redis2.domain.com', 'port' => 5678 - }] + }], + :master => "master-name" )} end # be configurable with sentinels diff --git a/spec/unit/sensu_redis_config_spec.rb b/spec/unit/sensu_redis_config_spec.rb index 550762edfe..b30418ab74 100644 --- a/spec/unit/sensu_redis_config_spec.rb +++ b/spec/unit/sensu_redis_config_spec.rb @@ -51,11 +51,27 @@ def create_type_instance(resource_hash) end describe "sentinels" do - it "defaults (no sentinels)" do - expect(type_instance.parameter(:sentinels).value).to eq([]) + context "with defaults (no sentinels)" do + it "no sentinels" do + expect(type_instance.parameter(:sentinels).value).to eq([]) + end + + it "master.should is :absent" do + expect(type_instance.parameter(:master).should).to eq(:absent) + end + + it "assumes insync? for :absent value" do + expect(type_instance.parameter(:master).safe_insync?(:absent)).to be true + end + + ["abc", "absent"].each do |v| + it "assumes not insync? for specified master value '#{v.inspect}'" do + expect(type_instance.parameter(:master).safe_insync?(v)).to be false + end + end end - context "single value" do + context "with single sentinel" do let :inst do create_type_instance(resource_hash.merge({ :sentinels => { @@ -93,7 +109,7 @@ def create_type_instance(resource_hash) end end - context "multiple values" do + context "with multiple sentinels" do let :inst do create_type_instance(resource_hash.merge({ :sentinels => [{ @@ -136,6 +152,32 @@ def create_type_instance(resource_hash) }])).to be true end end + + context "when master is specified" do + let :inst do + create_type_instance(resource_hash.merge({ + :master => "master-name" + })) + end + + it "master name is propagated" do + expect(inst.parameter(:master).value).to eq("master-name") + end + + it "assumes insync? for the same master value" do + expect(inst.parameter(:master).safe_insync?("master-name")).to be true + end + + [:absent, "", nil].each do |v| + it "assumes not insync? for empty master value #{v.inspect}" do + expect(inst.parameter(:master).safe_insync?(v)).to be false + end + end + + it "assumes not insync? for different master value" do + expect(inst.parameter(:master).safe_insync?("abc")).to be false + end + end end end