Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the redisget() function which can get a value for a specified key from a specified redis URL.
- Loading branch information
1 parent
bb7db10
commit 05869f2
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'redis' | ||
|
||
module Puppet::Parser::Functions | ||
newfunction(:redisget, :type => :rvalue, :doc => <<-EOS | ||
Returns the value of the key being looked up or nil if the key does not | ||
exist. Takes two arguments, the first being a string value of the key to be | ||
looked up and the second is the URL to the Redis service. | ||
EOS | ||
) do |args| | ||
|
||
raise(Puppet::ParseError, "redisget(): Wrong number of arguments given (#{args.size} for 2)") if args.size != 2 | ||
|
||
key = args[0] | ||
url = args[1] | ||
|
||
raise(Puppet::ParseError, "redisget(): Wrong argument type given (#{key.class} for String") if key.is_a?(String) == false | ||
raise(Puppet::ParseError, "redisget(): Wrong argument type given (#{url.class} for String") if url.is_a?(String) == false | ||
|
||
redis = Redis.new(:url => url) | ||
redis.get(key) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require 'spec_helper' | ||
require 'mock_redis' | ||
require 'redis' | ||
|
||
REDIS_URL='redis://localhost:6379' | ||
|
||
describe 'redisget' do | ||
context 'should return nil if key does not exist' do | ||
before { | ||
mr = MockRedis.new | ||
Redis.stubs(:new).returns(mr) | ||
mr.set('nonexistent_key', nil) | ||
} | ||
it { is_expected.to run.with_params('nonexistent_key', REDIS_URL).and_return('') } | ||
end | ||
|
||
context 'should return the value of the specified key' do | ||
before { | ||
mr = MockRedis.new | ||
Redis.stubs(:new).returns(mr) | ||
mr.set('key', 'value') | ||
} | ||
it { is_expected.to run.with_params('key', REDIS_URL).and_return('value') } | ||
end | ||
|
||
describe 'with incorrect arguments' do | ||
context 'with no argument specified' do | ||
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } | ||
end | ||
|
||
context 'with only one argument specified' do | ||
it { is_expected.to run.with_params('some_key').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } | ||
end | ||
|
||
context 'with more than two arguments specified' do | ||
it { is_expected.to run.with_params('too', 'many', 'args').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } | ||
end | ||
end | ||
|
||
describe 'when an invalid type (non-string) is specified' do | ||
before(:each) { | ||
mr = MockRedis.new | ||
Redis.stubs(:new).returns(mr) | ||
} | ||
[{ 'ha' => 'sh'}, true, 1, ['an', 'array']].each do |p| | ||
context "specifing first parameter as <#{p}>" do | ||
it { is_expected.to run.with_params(p, REDIS_URL).and_raise_error(Puppet::ParseError, /wrong argument type/i) } | ||
end | ||
|
||
context "specifing second parameter as <#{p}>" do | ||
it { is_expected.to run.with_params('valid', p).and_raise_error(Puppet::ParseError, /wrong argument type/i) } | ||
end | ||
end | ||
end | ||
end |