Skip to content

Commit

Permalink
Add 'master' parameter for Redis Sentinel config.
Browse files Browse the repository at this point in the history
Available since sensu 0.23 but currently undocumented. See:

sensu/sensu-docs#380
  • Loading branch information
modax committed Jun 10, 2016
1 parent 4877e2e commit bd15ecc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 6 deletions.
12 changes: 12 additions & 0 deletions lib/puppet/provider/sensu_redis_config/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/puppet/type/sensu_redis_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions manifests/redis/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,6 +39,7 @@
db => $sensu::redis_db,
auto_reconnect => $sensu::redis_auto_reconnect,
sentinels => $sentinels,
master => $master,
}

}
7 changes: 5 additions & 2 deletions spec/classes/sensu_redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
:db => 1,
:auto_reconnect => false,
:sentinels => nil,
:master => nil,
)}
end # be configurable without sentinels

Expand All @@ -44,7 +45,8 @@
}, {
'host' => 'redis2.domain.com',
'port' => '5678'
}]
}],
:redis_master => 'master-name'
} }

it { should contain_sensu_redis_config('testhost.domain.com').with(
Expand All @@ -59,7 +61,8 @@
}, {
'host' => 'redis2.domain.com',
'port' => 5678
}]
}],
:master => "master-name"
)}
end # be configurable with sentinels

Expand Down
50 changes: 46 additions & 4 deletions spec/unit/sensu_redis_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => [{
Expand Down Expand Up @@ -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

0 comments on commit bd15ecc

Please sign in to comment.