Skip to content

Commit

Permalink
Add redisget() (#179)
Browse files Browse the repository at this point in the history
Add the redisget() function which can get a value for a specified key
from a specified redis URL.
  • Loading branch information
ghoneycutt authored and petems committed May 3, 2017
1 parent bb7db10 commit 05869f2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -32,6 +32,8 @@ group :test do
gem 'puppet-syntax', :require => false, git: 'https://github.com/gds-operations/puppet-syntax.git'
gem 'pry'
gem 'rb-readline'
gem 'redis', :require => false
gem 'mock_redis', :require => false
end

group :system_tests do
Expand Down
1 change: 1 addition & 0 deletions LICENSE
@@ -1,4 +1,5 @@
Copyright (C) 2012 Tom De Vylder
Copyright (C) 2017 Garrett Honeycutt <code@garretthoneycutt.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -67,6 +67,17 @@ With adjustments:
failover_timeout => 30000,
}

## `redisget()` function

`redisget()` takes two arguments that are strings. The first is the key
to be looked up and the second is the URL to the Redis service.

```puppet
$version = redisget('version.myapp', 'redis://redis.example.com:6379')
```

You must have the 'redis' gem installed on your puppet master.

## Unit testing

Plain RSpec:
Expand Down
22 changes: 22 additions & 0 deletions lib/puppet/parser/functions/redisget.rb
@@ -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
55 changes: 55 additions & 0 deletions spec/functions/redisget_spec.rb
@@ -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

0 comments on commit 05869f2

Please sign in to comment.