diff --git a/spec/acceptance/redis_cli_task_spec.rb b/spec/acceptance/redis_cli_task_spec.rb index 759a0e0..59019b9 100644 --- a/spec/acceptance/redis_cli_task_spec.rb +++ b/spec/acceptance/redis_cli_task_spec.rb @@ -1,58 +1,47 @@ require 'spec_helper_acceptance' describe 'redis-cli task' do it 'install redis-cli with the class' do pp = <<-EOS - Exec { - path => [ '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', ] - } - - class { '::redis': - manage_repo => true, - } + include redis EOS apply_manifest(pp, catch_failures: true) - - # Apply twice to ensure no errors the second time. - # TODO: not idempotent on Ubuntu 16.04 - unless fact('operatingsystem') == 'Ubuntu' && fact('operatingsystemmajrelease') == '16.04' - apply_manifest(pp, catch_changes: true) - end + apply_manifest(pp, catch_changes: true) end subject do on(master, "bolt task run --modulepath /etc/puppetlabs/code/modules --targets localhost #{task_name} #{params}", acceptable_exit_codes: [0, 1]).stdout end let(:task_name) { 'redis::redis_cli' } describe 'ping' do let(:params) { 'command="ping"' } it 'execute ping' do is_expected.to match(%r{{\s*"status":\s*"PONG"\s*}}) is_expected.to match(%r{Ran on 1 target in .+ sec}) end end describe 'security' do describe 'command with semi colon' do let(:params) { 'command="ping; cat /etc/passwd"' } it 'stops script injections and escapes' do is_expected.to match(%r!{\s*"status":\s*"ERR unknown command ('|`)ping; cat /etc/passwd('|`)!) is_expected.to match(%r{Ran on 1 target in .+ sec}) end end describe 'command with double ampersand' do let(:params) { 'command="ping && cat /etc/passwd"' } it 'stops script injections and escapes' do is_expected.to match(%r!{\s*"status":\s*"ERR unknown command ('|`)ping && cat /etc/passwd('|`)!) is_expected.to match(%r{Ran on 1 target in .+ sec}) end end end end diff --git a/spec/acceptance/suites/default/redis_multi_instances_one_host_spec.rb b/spec/acceptance/suites/default/redis_multi_instances_one_host_spec.rb index 831104a..f727621 100644 --- a/spec/acceptance/suites/default/redis_multi_instances_one_host_spec.rb +++ b/spec/acceptance/suites/default/redis_multi_instances_one_host_spec.rb @@ -1,71 +1,62 @@ require 'spec_helper_acceptance' -# Cant get this to work on Debian, add exception for now -describe 'redis::instance', unless: (fact('operatingsystem') == 'Debian') do +describe 'redis::instance' do case fact('osfamily') when 'Debian' - config_path = '/etc/redis' - manage_repo = false + config_path = '/etc/redis' redis_name = 'redis-server' else redis_name = 'redis' - config_path = '/etc' - manage_repo = true + config_path = '/etc' end it 'runs successfully' do pp = <<-EOS - Exec { - path => [ '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', ] - } - - class { '::redis': - manage_repo => #{manage_repo}, + class { 'redis': default_install => false, } redis::instance {'redis1': - port => 7777, + port => 7777, } redis::instance {'redis2': - port => 8888, + port => 8888, } - EOS # Apply twice to ensure no errors the second time. apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end describe package(redis_name) do it { is_expected.to be_installed } end describe service('redis-server-redis1') do it { is_expected.to be_running } end describe service('redis-server-redis2') do it { is_expected.to be_running } end describe file("#{config_path}/redis-server-redis1.conf") do its(:content) { is_expected.to match %r{port 7777} } end describe file("#{config_path}/redis-server-redis2.conf") do its(:content) { is_expected.to match %r{port 8888} } end context 'redis should respond to ping command' do describe command('redis-cli -h 127.0.0.1 -p 7777 ping') do its(:stdout) { is_expected.to match %r{PONG} } end describe command('redis-cli -h 127.0.0.1 -p 8888 ping') do its(:stdout) { is_expected.to match %r{PONG} } end end end diff --git a/spec/acceptance/suites/default/redis_multi_node_spec.rb b/spec/acceptance/suites/default/redis_multi_node_spec.rb index 1802dd4..6a44c3d 100644 --- a/spec/acceptance/suites/default/redis_multi_node_spec.rb +++ b/spec/acceptance/suites/default/redis_multi_node_spec.rb @@ -1,73 +1,71 @@ require 'spec_helper_acceptance' if hosts.length >= 3 describe 'configuring master and slave redis hosts' do let(:master_ip_address) do # hosts_as('master').inject({}) do |memo,host| # memo[host] = fact_on host, "ipaddress_enp0s8" # end '10.255.33.129' # hardcoding as vagrant ip for now end hosts_as('master').each do |host| context "should be able to configure a host as master on #{host}" do it 'works idempotently with no errors' do pp = <<-EOS # Stop firewall so we can easily connect service {'firewalld': ensure => 'stopped', } class { 'redis': - manage_repo => true, bind => '#{master_ip_address}', requirepass => 'foobared', } EOS apply_manifest_on(host, pp, catch_failures: true) command_to_check = "redis-cli -h #{master_ip_address} -a foobared info replication" on host, command_to_check do expect(stdout).to match(%r{^role:master}) end end end end hosts_as('slave').each do |host| context "should be able to configure a host as master on #{host}" do it 'works idempotently with no errors' do pp = <<-EOS class { 'redis': - manage_repo => true, bind => '127.0.0.1', masterauth => 'foobared', slaveof => '#{master_ip_address} 6379' } EOS apply_manifest_on(host, pp, catch_failures: true) on host, 'redis-cli -h $(facter ipaddress_enp0s8) info replication' do expect(stdout).to match(%r{^role:slave}) end end end end hosts_as('master').each do |host| context "should be able to configure a host as master on #{host}" do it 'works idempotently with no errors' do command_to_check = "redis-cli -h #{master_ip_address} -a foobared info replication" sleep(5) on host, command_to_check do expect(stdout).to match(%r{^connected_slaves:2}) end end end end end end diff --git a/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb b/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb index ab890f4..277242f 100644 --- a/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb +++ b/spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb @@ -1,70 +1,54 @@ require 'spec_helper_acceptance' describe 'redis::sentinel' do redis_name = case fact('osfamily') when 'Debian' 'redis-server' else 'redis' end it 'runs successfully' do pp = <<-EOS - - $master_name = 'mymaster' - $redis_master = '127.0.0.1' - $failover_timeout = 10000 - - # We're testing with `manage_repo` true. In redis-sentinel 5, the daemon has its own rundir - if $::operatingsystem == 'Ubuntu' { - $pidfile = '/var/run/sentinel/redis-sentinel.pid' - } else { - $pidfile = undef - } - - class { 'redis': - manage_repo => true, - } - -> class { 'redis::sentinel': - master_name => $master_name, - redis_host => $redis_master, - failover_timeout => $failover_timeout, - pid_file => $pidfile, + master_name => 'mymaster', + redis_host => '127.0.0.1', + failover_timeout => 10000, } EOS apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) end describe package(redis_name) do it { is_expected.to be_installed } end describe service(redis_name) do it { is_expected.to be_running } end describe service('redis-sentinel') do it { is_expected.to be_running } end case fact('osfamily') when 'Debian' describe package('redis-sentinel') do it { is_expected.to be_installed } end end context 'redis should respond to ping command' do describe command('redis-cli ping') do its(:stdout) { is_expected.to match %r{PONG} } end end context 'redis-sentinel should return correct sentinel master' do describe command('redis-cli -p 26379 SENTINEL masters') do its(:stdout) { is_expected.to match %r{^mymaster} } end end end diff --git a/spec/acceptance/suites/default/redis_spec.rb b/spec/acceptance/suites/default/redis_spec.rb index 4202141..5026973 100644 --- a/spec/acceptance/suites/default/redis_spec.rb +++ b/spec/acceptance/suites/default/redis_spec.rb @@ -1,53 +1,45 @@ require 'spec_helper_acceptance' describe 'redis' do - case fact('osfamily') - when 'Debian' - redis_name = 'redis-server' - manage_repo = false - else - redis_name = 'redis' - manage_repo = true - end + redis_name = case fact('osfamily') + when 'Debian' + 'redis-server' + else + 'redis' + end it 'runs successfully' do pp = <<-EOS - Exec { - path => [ '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', ] - } - - class { '::redis': - manage_repo => #{manage_repo}, - } + include redis EOS # Apply twice to ensure no errors the second time. apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) end it 'returns a fact' do pp = <<-EOS notify{"Redis Version: ${::redis_server_version}":} EOS # Check output for fact string apply_manifest(pp, catch_failures: true) do |r| expect(r.stdout).to match(%r{Redis Version: [\d+.]+}) end end describe package(redis_name) do it { is_expected.to be_installed } end describe service(redis_name) do it { is_expected.to be_running } end context 'redis should respond to ping command' do describe command('redis-cli ping') do its(:stdout) { is_expected.to match %r{PONG} } end end end diff --git a/spec/acceptance/suites/default/redisget_spec.rb b/spec/acceptance/suites/default/redisget_spec.rb index 7bcf773..f7c199c 100644 --- a/spec/acceptance/suites/default/redisget_spec.rb +++ b/spec/acceptance/suites/default/redisget_spec.rb @@ -1,117 +1,110 @@ # rubocop:disable RSpec/MultipleExpectations require 'spec_helper_acceptance' describe 'redis::get() function' do it 'runs successfully' do pp = <<-EOS - Exec { - path => [ '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', ] - } - - class { 'redis': - manage_repo => true, - } + include redis package { 'redis-rubygem' : ensure => '3.3.3', name => 'redis', provider => 'puppet_gem', } - EOS # Apply twice to ensure no errors the second time. apply_manifest(pp, catch_failures: true) apply_manifest(pp, catch_changes: true) shell('redis-cli SET mykey "Hello"') do |result| expect(result.stdout).to match('OK') end shell('redis-cli GET mykey') do |result| expect(result.stdout).to match('Hello') end end it 'returns a value from MyKey with the redis::get() function' do pp = <<-EOS $mykey = redis::get('mykey', 'redis://127.0.0.1:6379') notify{"mykey value: ${mykey}":} EOS # Check output for function return value apply_manifest(pp, catch_failures: true) do |r| expect(r.stdout).to match(%r{mykey value: Hello}) end end it 'returns a value from valid MyKey with the redis::get() function while specifying a default' do pp = <<-EOS $mykey = redis::get('mykey', 'redis://127.0.0.1:6379', 'default_value') notify{"mykey value: ${mykey}":} EOS # Check output for function return value apply_manifest(pp, catch_failures: true) do |r| expect(r.stdout).to match(%r{mykey value: Hello}) end end it 'returns an empty string when value not present with redis::get() function' do pp = <<-EOS $foo_key = redis::get('foo', 'redis://127.0.0.1:6379') if empty($foo_key){ notify{"foo_key value was empty string":} } EOS # Check output for function return value apply_manifest(pp, catch_failures: true) do |r| expect(r.stdout).to match(%r{foo_key value was empty string}) end end it 'returns the specified default value when key not present with redis::get() function' do pp = <<-EOS $foo_key = redis::get('foo', 'redis://127.0.0.1:6379', 'default_value') notify { $foo_key: } EOS # Check output for function return value apply_manifest(pp, catch_failures: true) do |r| expect(r.stdout).to match(%r{default_value}) end end it 'returns the specified default value when connection to redis server fails' do pp = <<-EOS # Bogus port for redis server $foo_key = redis::get('foo', 'redis://127.0.0.1:12345', 'default_value') notify { $foo_key: } EOS # Check output for function return value apply_manifest(pp, catch_failures: true) do |r| expect(r.stdout).to match(%r{default_value}) end end it 'returns an error when specifying a non connectable redis server' do pp = <<-EOS # Bogus port for redis server $foo_key = redis::get('foo', 'redis://127.0.0.1:12345') notify { $foo_key: } EOS # Check output for error when can't connect to bogus redis apply_manifest(pp, acceptable_exit_codes: [1]) do |r| expect(r.stderr).to match(%r{Error connecting to Redis on 127.0.0.1:12345 \(Errno::ECONNREFUSED\)}) end end end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index daad430..fcc7d0a 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -1,21 +1,24 @@ require 'beaker-rspec' require 'beaker/puppet_install_helper' require 'beaker/module_install_helper' run_puppet_install_helper unless ENV['BEAKER_provision'] == 'no' install_module_on(hosts) install_module_dependencies_on(hosts) RSpec.configure do |c| # Readable test descriptions c.formatter = :documentation c.before :suite do hosts.each do |host| - if fact_on(host, 'operatingsystem') == 'Ubuntu' + case fact_on(host, 'operatingsystem') + when 'CentOS' + host.install_package('epel-release') + when 'Ubuntu' host.install_package('software-properties-common') end host.install_package('puppet-bolt') end end end