Skip to content

Commit 791b1bd

Browse files
committed
Refactor unit tests in favor of fact service_provider
1 parent 901c61d commit 791b1bd

2 files changed

Lines changed: 54 additions & 49 deletions

File tree

spec/classes/jira_service_spec.rb

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
on_supported_os.each do |os, facts|
77
context "on #{os}" do
88
let(:facts) do
9-
facts.merge(os: { family: facts['osfamily'] })
9+
facts
1010
end
1111
let(:params) do
1212
{ javahome: '/opt/java' }
@@ -15,53 +15,46 @@
1515
'package { "jdk": }'
1616
end
1717

18-
if os == 'RedHat'
19-
context 'default params' do
20-
it { is_expected.to contain_service('jira') }
21-
it { is_expected.to compile.with_all_deps }
22-
it do
23-
is_expected.to contain_file('/etc/init.d/jira').
24-
with_content(%r{Short-Description: Start up JIRA}).
25-
with_content(%r{lockfile=/var/lock/subsys/jira})
26-
end
18+
case facts[:os]['family']
19+
when 'RedHat'
20+
service_file_location = if facts['service_provider'] == 'systemd'
21+
'/usr/lib/systemd/system/jira.service'
22+
else
23+
'/etc/init.d/jira'
24+
end
25+
when 'Debian'
26+
service_file_location = '/lib/systemd/system/jira.service'
27+
end
28+
29+
context 'with defaults for all parameters' do
30+
it { is_expected.to compile.with_all_deps }
31+
32+
case facts['service_provider']
33+
when 'systemd'
2734
it do
28-
is_expected.not_to contain_file('/usr/lib/systemd/system/jira.service').
35+
is_expected.to contain_file(service_file_location).
2936
with_content(%r{Atlassian Systemd Jira Service})
3037
end
31-
it { is_expected.not_to contain_exec('refresh_systemd') }
32-
end
33-
end
34-
if os == 'Debian'
35-
context 'lockfile on Debian' do
36-
it { is_expected.to compile.with_all_deps }
38+
it { is_expected.to contain_exec('refresh_systemd') }
39+
when 'redhat'
3740
it do
38-
is_expected.to contain_file('/etc/init.d/jira').
39-
with_content(%r{/var/lock/jira})
41+
is_expected.to contain_file(service_file_location).
42+
with_content(%r{Short-Description: Start up JIRA}).
43+
with_content(%r{lockfile=/var/lock/subsys/jira})
4044
end
4145
end
42-
end
43-
if os =~ %r{ubuntu}
44-
context 'default params' do
45-
it { is_expected.to compile.with_all_deps }
46-
end
47-
end
48-
if os =~ %r{ubuntu-16}
49-
context 'default params' do
50-
let(:facts) do
51-
facts.merge(operatingsystem: 'Ubuntu', operatingsystemmajrelease: '16.04')
52-
end
5346

54-
it { is_expected.to contain_file('/lib/systemd/system/jira.service') }
55-
end
47+
it { is_expected.to contain_service('jira') }
5648
end
57-
context 'overwriting service_manage param' do
49+
50+
context 'with service_manage set to false' do
5851
let(:params) do
5952
super().merge(service_manage: false)
6053
end
6154

6255
it { is_expected.not_to contain_service('jira') }
6356
end
64-
context 'overwriting service params' do
57+
context 'with service_ensure set to stopped' do
6558
let(:params) do
6659
super().merge(
6760
service_ensure: 'stopped',
@@ -77,21 +70,6 @@
7770
'subscribe' => 'Package[jdk]')
7871
end
7972
end
80-
context 'RedHat/CentOS 7 systemd init script' do
81-
let(:facts) do
82-
{
83-
osfamily: 'RedHat',
84-
operatingsystemmajrelease: '7',
85-
os: { family: 'RedHat' }
86-
}
87-
end
88-
89-
it do
90-
is_expected.to contain_file('/usr/lib/systemd/system/jira.service').
91-
with_content(%r{Atlassian Systemd Jira Service})
92-
end
93-
it { is_expected.to contain_exec('refresh_systemd') }
94-
end
9573
end
9674
end
9775
end

spec/spec_helper.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@
88

99
require 'voxpupuli/test/spec_helper'
1010

11+
# Rough conversion of grepping in the puppet source:
12+
# grep defaultfor lib/puppet/provider/service/*.rb
13+
add_custom_fact(:service_provider, lambda do |_os, facts|
14+
case facts[:os]['family'].downcase
15+
when 'archlinux'
16+
'systemd'
17+
when 'darwin'
18+
'launchd'
19+
when 'debian'
20+
'systemd'
21+
when 'freebsd'
22+
'freebsd'
23+
when 'gentoo'
24+
'openrc'
25+
when 'openbsd'
26+
'openbsd'
27+
when 'redhat'
28+
facts[:operatingsystemrelease].to_i >= 7 ? 'systemd' : 'redhat'
29+
when 'suse'
30+
facts[:operatingsystemmajrelease].to_i >= 12 ? 'systemd' : 'redhat'
31+
when 'windows'
32+
'windows'
33+
else
34+
'init'
35+
end
36+
end)
37+
1138
if File.exist?(File.join(__dir__, 'default_module_facts.yml'))
1239
facts = YAML.load(File.read(File.join(__dir__, 'default_module_facts.yml')))
1340
if facts

0 commit comments

Comments
 (0)