| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'spec_helper_acceptance' |
| 4 |
|
| 5 |
describe 'redis::get() function' do |
| 6 |
include_examples 'an idempotent resource' do |
| 7 |
let(:manifest) do |
| 8 |
<<-PUPPET |
| 9 |
include redis |
| 10 |
|
| 11 |
package { 'redis-rubygem' : |
| 12 |
ensure => '3.3.3', |
| 13 |
name => 'redis', |
| 14 |
provider => if fact('aio_agent_version') =~ String[1] { 'puppet_gem' } else { 'gem' }, |
| 15 |
} |
| 16 |
PUPPET |
| 17 |
end |
| 18 |
end |
| 19 |
|
| 20 |
specify do |
| 21 |
expect(command('redis-cli SET mykey "Hello"')). |
| 22 |
to have_attributes(stdout: %r{OK}) |
| 23 |
end |
| 24 |
|
| 25 |
specify do |
| 26 |
expect(command('redis-cli GET mykey')). |
| 27 |
to have_attributes(stdout: %r{Hello}) |
| 28 |
end |
| 29 |
|
| 30 |
context 'with mykey set to Hello' do |
| 31 |
it 'returns a value from MyKey with the redis::get() function' do |
| 32 |
pp = <<-EOS |
| 33 |
$mykey = redis::get('mykey', 'redis://127.0.0.1:6379') |
| 34 |
|
| 35 |
notify{"mykey value: ${mykey}":} |
| 36 |
EOS |
| 37 |
|
| 38 |
# Check output for function return value |
| 39 |
apply_manifest(pp, catch_failures: true) do |r| |
| 40 |
expect(r.stdout).to match(%r{mykey value: Hello}) |
| 41 |
end |
| 42 |
end |
| 43 |
|
| 44 |
it 'returns a value from valid MyKey with the redis::get() function while specifying a default' do |
| 45 |
pp = <<-EOS |
| 46 |
$mykey = redis::get('mykey', 'redis://127.0.0.1:6379', 'default_value') |
| 47 |
|
| 48 |
notify{"mykey value: ${mykey}":} |
| 49 |
EOS |
| 50 |
|
| 51 |
# Check output for function return value |
| 52 |
apply_manifest(pp, catch_failures: true) do |r| |
| 53 |
expect(r.stdout).to match(%r{mykey value: Hello}) |
| 54 |
end |
| 55 |
end |
| 56 |
|
| 57 |
it 'returns an empty string when value not present with redis::get() function' do |
| 58 |
pp = <<-EOS |
| 59 |
$foo_key = redis::get('foo', 'redis://127.0.0.1:6379') |
| 60 |
|
| 61 |
if empty($foo_key){ |
| 62 |
notify{"foo_key value was empty string":} |
| 63 |
} |
| 64 |
EOS |
| 65 |
|
| 66 |
# Check output for function return value |
| 67 |
apply_manifest(pp, catch_failures: true) do |r| |
| 68 |
expect(r.stdout).to match(%r{foo_key value was empty string}) |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
it 'returns the specified default value when key not present with redis::get() function' do |
| 73 |
pp = <<-EOS |
| 74 |
$foo_key = redis::get('foo', 'redis://127.0.0.1:6379', 'default_value') |
| 75 |
|
| 76 |
notify { $foo_key: } |
| 77 |
EOS |
| 78 |
|
| 79 |
# Check output for function return value |
| 80 |
apply_manifest(pp, catch_failures: true) do |r| |
| 81 |
expect(r.stdout).to match(%r{default_value}) |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
it 'returns the specified default value when connection to redis server fails' do |
| 86 |
pp = <<-EOS |
| 87 |
# Bogus port for redis server |
| 88 |
$foo_key = redis::get('foo', 'redis://127.0.0.1:12345', 'default_value') |
| 89 |
|
| 90 |
notify { $foo_key: } |
| 91 |
EOS |
| 92 |
|
| 93 |
# Check output for function return value |
| 94 |
apply_manifest(pp, catch_failures: true) do |r| |
| 95 |
expect(r.stdout).to match(%r{default_value}) |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
it 'returns an error when specifying a non connectable redis server' do |
| 100 |
pp = <<-EOS |
| 101 |
# Bogus port for redis server |
| 102 |
$foo_key = redis::get('foo', 'redis://127.0.0.1:12345') |
| 103 |
|
| 104 |
notify { $foo_key: } |
| 105 |
EOS |
| 106 |
|
| 107 |
# Check output for error when can't connect to bogus redis |
| 108 |
apply_manifest(pp, acceptable_exit_codes: [1]) do |r| |
| 109 |
expect(r.stderr).to match(%r{Error connecting to Redis on 127.0.0.1:12345 \(Errno::ECONNREFUSED\)}) |
| 110 |
end |
| 111 |
end |
| 112 |
end |
| 113 |
end |