Skip to content

Commit e8ca35a

Browse files
authored
Merge pull request voxpupuli#438 from ekohl/modernize-acceptance-tests
Modernize the acceptance tests
2 parents 364fb02 + 5047ec5 commit e8ca35a

File tree

9 files changed

+138
-242
lines changed

9 files changed

+138
-242
lines changed

spec/acceptance/redis_cli_task_spec.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,18 @@
99

1010
let(:task_name) { 'redis::redis_cli' }
1111

12-
it 'install redis-cli with the class' do
13-
pp = <<-EOS
14-
include redis
15-
EOS
16-
17-
apply_manifest(pp, catch_failures: true)
18-
apply_manifest(pp, catch_changes: true)
19-
end
20-
2112
unless fact('os.family') == 'RedHat' && fact('os.release.major').to_i >= 9
13+
include_examples 'an idempotent resource' do
14+
let(:manifest) { 'include redis' }
15+
end
16+
2217
describe 'ping' do
2318
let(:params) { 'command="ping"' }
2419

2520
it 'execute ping' do
26-
is_expected.to match(%r{{\s*"status":\s*"PONG"\s*}})
27-
is_expected.to match(%r{Ran on 1 target in .+ sec})
21+
is_expected.
22+
to match(%r{{\s*"status":\s*"PONG"\s*}}).
23+
and match(%r{Ran on 1 target in .+ sec})
2824
end
2925
end
3026

@@ -33,17 +29,19 @@
3329
let(:params) { 'command="ping; cat /etc/passwd"' }
3430

3531
it 'stops script injections and escapes' do
36-
is_expected.to match(%r!{\s*"status":\s*"ERR unknown command ('|`)ping; cat /etc/passwd('|`)!)
37-
is_expected.to match(%r{Ran on 1 target in .+ sec})
32+
is_expected.
33+
to match(%r!{\s*"status":\s*"ERR unknown command ('|`)ping; cat /etc/passwd('|`)!).
34+
and match(%r{Ran on 1 target in .+ sec})
3835
end
3936
end
4037

4138
describe 'command with double ampersand' do
4239
let(:params) { 'command="ping && cat /etc/passwd"' }
4340

4441
it 'stops script injections and escapes' do
45-
is_expected.to match(%r!{\s*"status":\s*"ERR unknown command ('|`)ping && cat /etc/passwd('|`)!)
46-
is_expected.to match(%r{Ran on 1 target in .+ sec})
42+
is_expected.
43+
to match(%r!{\s*"status":\s*"ERR unknown command ('|`)ping && cat /etc/passwd('|`)!).
44+
and match(%r{Ran on 1 target in .+ sec})
4745
end
4846
end
4947
end

spec/acceptance/suites/default/redis_adminstration_spec.rb

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22

33
require 'spec_helper_acceptance'
44

5+
RSpec::Matchers.define_negated_matcher :execute_without_warning, :execute_with_warning
6+
57
# systcl settings are untestable in docker
68
describe 'redis::administration', unless: default['hypervisor'] =~ %r{docker} do
7-
it 'runs successfully' do
8-
pp = <<-EOS
9-
include redis
10-
include redis::administration
11-
EOS
9+
def execute_with_warning
10+
have_attributes(stderr: %r{WARNING})
11+
end
1212

13-
# Apply twice to ensure no errors the second time.
14-
apply_manifest(pp, catch_failures: true)
15-
apply_manifest(pp, catch_changes: true)
13+
include_examples 'an idempotent resource' do
14+
let(:manifest) { 'include redis, redis::administration' }
1615
end
1716

18-
describe file('/proc/sys/vm/overcommit_memory') do
19-
its(:content) { is_expected.to eq("1\n") }
17+
specify do
18+
expect(file('/proc/sys/vm/overcommit_memory')).
19+
to have_attributes(content: "1\n")
2020
end
2121

22-
describe file('/proc/sys/net/core/somaxconn') do
23-
its(:content) { is_expected.to eq("65535\n") }
22+
specify do
23+
expect(file('/proc/sys/net/core/somaxconn')).
24+
to have_attributes(content: "65535\n")
2425
end
2526

26-
describe command('timeout 1s redis-server --port 7777 --loglevel verbose') do
27-
its(:stderr) { is_expected.not_to match(%r{WARNING}) }
28-
its(:exit_status) { is_expected.to eq(124) }
27+
specify do
28+
expect(command('timeout 1s redis-server --port 7777 --loglevel verbose')).
29+
to execute_without_warning.
30+
and have_attributes(exit_status: 124)
2931
end
3032
end

spec/acceptance/suites/default/redis_debian_run_dir_spec.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

spec/acceptance/suites/default/redis_multi_instances_one_host_spec.rb

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,66 +29,61 @@
2929
'redis'
3030
end
3131

32-
it 'runs successfully' do
33-
pp = <<-EOS
34-
$listening_ports = #{instances}
35-
36-
class { 'redis':
37-
default_install => false,
38-
service_enable => false,
39-
service_ensure => 'stopped',
40-
protected_mode => false,
41-
bind => [],
42-
}
43-
44-
$listening_ports.each |$port| {
45-
$port_string = sprintf('%d',$port)
46-
redis::instance { $port_string:
47-
service_enable => true,
48-
service_ensure => 'running',
49-
port => $port,
50-
bind => $facts['networking']['ip'],
51-
dbfilename => "${port}-dump.rdb",
52-
appendfilename => "${port}-appendonly.aof",
53-
appendfsync => 'always',
54-
require => Class['Redis'],
55-
}
56-
}
57-
58-
EOS
59-
60-
# Apply twice to ensure no errors the second time.
61-
apply_manifest(pp, catch_failures: true)
62-
apply_manifest(pp, catch_changes: true)
32+
include_examples 'an idempotent resource' do
33+
let(:manifest) do
34+
<<~PUPPET
35+
$listening_ports = #{instances}
36+
37+
class { 'redis':
38+
default_install => false,
39+
service_enable => false,
40+
service_ensure => 'stopped',
41+
protected_mode => false,
42+
bind => [],
43+
}
44+
45+
$listening_ports.each |$port| {
46+
$port_string = sprintf('%d',$port)
47+
redis::instance { $port_string:
48+
service_enable => true,
49+
service_ensure => 'running',
50+
port => $port,
51+
bind => $facts['networking']['ip'],
52+
dbfilename => "${port}-dump.rdb",
53+
appendfilename => "${port}-appendonly.aof",
54+
appendfsync => 'always',
55+
require => Class['Redis'],
56+
}
57+
}
58+
PUPPET
59+
end
6360
end
6461

65-
describe package(redis_name) do
66-
it { is_expected.to be_installed }
67-
end
62+
specify { expect(package(redis_name)).to be_installed }
6863

69-
describe service(redis_name) do
70-
it { is_expected.not_to be_enabled }
71-
it { is_expected.not_to be_running }
64+
specify do
65+
expect(service(redis_name)).not_to be_enabled
66+
expect(service(redis_name)).not_to be_running
7267
end
7368

7469
instances.each do |instance|
75-
describe file("/etc/systemd/system/redis-server-#{instance}.service") do
76-
its(:content) { is_expected.to match %r{redis-server-#{instance}.conf} }
70+
specify do
71+
expect(file("/etc/systemd/system/redis-server-#{instance}.service")).
72+
to be_file.
73+
and have_attributes(content: include("redis-server-#{instance}.conf"))
7774
end
7875

79-
describe service("redis-server-#{instance}") do
80-
it { is_expected.to be_enabled }
81-
it { is_expected.to be_running }
82-
end
76+
specify { expect(service("redis-server-#{instance}")).to be_enabled.and be_running }
8377

84-
describe file("#{config_path}/redis-server-#{instance}.conf") do
85-
its(:content) { is_expected.to match %r{port #{instance}} }
78+
specify do
79+
expect(file("#{config_path}/redis-server-#{instance}.conf")).
80+
to be_file.
81+
and have_attributes(content: include("port #{instance}"))
8682
end
8783

88-
context "redis instance #{instance} should respond to ping command" do
89-
describe command("redis-cli -h #{fact('networking.ip')} -p #{instance} ping") do
90-
its(:stdout) { is_expected.to match %r{PONG} }
91-
end
84+
specify "redis instance #{instance} should respond to ping command" do
85+
expect(command("redis-cli -h #{fact('networking.ip')} -p #{instance} ping")).
86+
to have_attributes(stdout: %r{PONG})
9287
end
9388
end
9489
end

spec/acceptance/suites/default/redis_sentinel_one_node_spec.rb

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,38 @@
33
require 'spec_helper_acceptance'
44

55
describe 'redis::sentinel' do
6-
redis_name = case fact('osfamily')
7-
when 'Debian'
8-
'redis-server'
9-
else
10-
'redis'
11-
end
12-
13-
it 'runs successfully' do
14-
pp = <<-EOS
15-
class { 'redis::sentinel':
16-
master_name => 'mymaster',
17-
redis_host => '127.0.0.1',
18-
failover_timeout => 10000,
19-
}
20-
EOS
21-
22-
apply_manifest(pp, catch_failures: true)
23-
apply_manifest(pp, catch_changes: true)
24-
end
25-
26-
describe package(redis_name) do
27-
it { is_expected.to be_installed }
6+
redis_name = fact('os.family') == 'Debian' ? 'redis-server' : 'redis'
7+
8+
include_examples 'an idempotent resource' do
9+
let(:manifest) do
10+
<<-PUPPET
11+
class { 'redis::sentinel':
12+
master_name => 'mymaster',
13+
redis_host => '127.0.0.1',
14+
failover_timeout => 10000,
15+
}
16+
PUPPET
17+
end
2818
end
2919

30-
describe service(redis_name) do
31-
it { is_expected.to be_running }
32-
end
20+
specify { expect(package(redis_name)).to be_installed }
21+
specify { expect(service(redis_name)).to be_running }
3322

34-
describe service('redis-sentinel'), :sentinel do
35-
it { is_expected.to be_running }
23+
specify 'redis should respond to ping command' do
24+
expect(command('redis-cli ping')).
25+
to have_attributes(stdout: %r{PONG})
3626
end
3727

38-
case fact('osfamily')
39-
when 'Debian'
40-
describe package('redis-sentinel') do
41-
it { is_expected.to be_installed }
42-
end
43-
end
28+
specify { expect(service('redis-sentinel')).to be_running }
4429

45-
context 'redis should respond to ping command' do
46-
describe command('redis-cli ping') do
47-
its(:stdout) { is_expected.to match %r{PONG} }
48-
end
30+
if redis_name == 'redis-server'
31+
specify { expect(package('redis-sentinel')).to be_installed }
32+
else
33+
specify { expect(package('redis-sentinel')).not_to be_installed }
4934
end
5035

51-
context 'redis-sentinel should return correct sentinel master' do
52-
describe command('redis-cli -p 26379 SENTINEL masters') do
53-
its(:stdout) { is_expected.to match %r{^mymaster} }
54-
end
36+
specify 'redis-sentinel should return correct sentinel master' do
37+
expect(command('redis-cli -p 26379 SENTINEL masters')).
38+
to have_attributes(stdout: %r{^mymaster})
5539
end
5640
end

spec/acceptance/suites/default/redis_spec.rb

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,10 @@
33
require 'spec_helper_acceptance'
44

55
describe 'redis' do
6-
redis_name = case fact('osfamily')
7-
when 'Debian'
8-
'redis-server'
9-
else
10-
'redis'
11-
end
12-
13-
it 'runs successfully' do
14-
pp = <<-EOS
15-
include redis
16-
EOS
6+
redis_name = fact('os.family') == 'Debian' ? 'redis-server' : 'redis'
177

18-
# Apply twice to ensure no errors the second time.
19-
apply_manifest(pp, catch_failures: true)
20-
apply_manifest(pp, catch_changes: true)
8+
include_examples 'an idempotent resource' do
9+
let(:manifest) { 'include redis' }
2110
end
2211

2312
it 'returns a fact' do
@@ -31,18 +20,12 @@
3120
end
3221
end
3322

34-
describe package(redis_name) do
35-
it { is_expected.to be_installed }
36-
end
37-
38-
describe service(redis_name) do
39-
it { is_expected.to be_running }
40-
end
23+
specify { expect(package(redis_name)).to be_installed }
24+
specify { expect(service(redis_name)).to be_running }
4125

42-
context 'redis should respond to ping command' do
43-
describe command('redis-cli ping') do
44-
its(:stdout) { is_expected.to match %r{PONG} }
45-
end
26+
specify 'redis should respond to ping command' do
27+
expect(command('redis-cli ping')).
28+
to have_attributes(stdout: %r{PONG})
4629
end
4730

4831
it 'runs successfully when using Redis apt repository', if: (fact('os.family') == 'Debian') do

0 commit comments

Comments
 (0)