Skip to content
This repository was archived by the owner on Mar 28, 2019. It is now read-only.

Commit d0ec1c2

Browse files
committed
Merge pull request #159 from danieldreier/refactor_tests_with_spec_facts
Refactor rspec-puppet tests
2 parents 09a569f + daeec8b commit d0ec1c2

File tree

5 files changed

+136
-266
lines changed

5 files changed

+136
-266
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ group :test do
99
gem "puppet-syntax"
1010
gem "puppetlabs_spec_helper"
1111
gem "metadata-json-lint"
12+
gem "puppet_spec_facts", '>= 0.2.1'
1213
end
1314

1415
group :development do

spec/classes/agent_spec.rb

Lines changed: 62 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'spec_helper'
22

33
shared_examples 'agent examples' do
4-
it { should contain_class('puppet::package') }
54
it { should contain_class('puppet::agent') }
65
it { should compile.with_all_deps }
76
it { should contain_ini_setting('report_server').with_value('puppet_reports.example.com') }
@@ -10,97 +9,79 @@
109
it { should contain_package('puppet') }
1110
end
1211

13-
describe 'puppet::agent' do
14-
describe "sample agent configuration from documentation" do
15-
let(:params) {{
16-
:server => 'puppet.example.com',
17-
:report_server => 'puppet_reports.example.com',
18-
:method => 'service',
19-
}}
20-
context 'CentOS, RedHat, Debian, Ubuntu' do
21-
['RedHat', 'CentOS', 'Debian', 'Ubuntu'].each do |operatingsystem|
12+
PuppetSpecFacts.facts_for_platform_by_name(["Debian_wheezy_7.7_amd64_3.7.2_structured", "CentOS_5.11_x86_64_3.7.1_structured", "FreeBSD_10.0-RELEASE_amd64_3.6.2_structured"]).each do |name, facthash|
13+
describe "puppet::agent" do
14+
let(:facts) { facthash }
2215

23-
osfamily = 'Redhat' if ['RedHat', 'CentOS'].include? operatingsystem
24-
osfamily ||= 'Debian'
16+
context "running on #{name}" do
17+
[true, false].each do |manage_repos|
2518

26-
let(:facts) {{
27-
:osfamily => osfamily,
28-
:operatingsystem => operatingsystem,
29-
:lsbdistid => operatingsystem,
30-
:lsbdistcodename => 'lolwut'
31-
}}
32-
context 'running as service' do
19+
context "when manage_repos => #{manage_repos}" do
3320
let(:params) {{
3421
:server => 'puppet.example.com',
3522
:report_server => 'puppet_reports.example.com',
36-
:method => 'service',
23+
:manage_repos => manage_repos,
3724
}}
38-
it_behaves_like "agent examples"
39-
it do
40-
should contain_service('puppet_agent').with({
41-
:ensure => "running"
42-
})
43-
should contain_cron('puppet agent')
44-
end
45-
end
46-
context 'method => "only_service"' do
47-
let(:params) {{
48-
:server => 'puppet.example.com',
49-
:report_server => 'puppet_reports.example.com',
50-
:method => 'only_service',
51-
}}
52-
it_behaves_like "agent examples"
53-
it do
54-
should contain_service('puppet_agent').with({
55-
:ensure => "running"
56-
})
57-
should_not contain_cron('puppet agent')
58-
end
59-
end
60-
context 'method => "none"' do
61-
let(:params) {{
62-
:server => 'puppet.example.com',
63-
:report_server => 'puppet_reports.example.com',
64-
:method => 'only_service',
65-
}}
66-
it_behaves_like "agent examples"
67-
it do
68-
should contain_service('puppet_agent').with({
69-
:ensure => "running"
70-
})
71-
should_not contain_cron('puppet agent')
72-
end
73-
end
74-
context 'method => "cron"' do
75-
let(:params) {{
76-
:server => 'puppet.example.com',
77-
:report_server => 'puppet_reports.example.com',
78-
:method => 'cron',
79-
}}
80-
it_behaves_like "agent examples"
81-
it do
82-
should_not contain_service('puppet_agent').with({
83-
:ensure => "running"
84-
})
85-
should contain_cron('puppet agent').with_command(/puppet agent/)
25+
26+
case facthash['osfamily']
27+
when 'RedHat'
28+
it_behaves_like "agent examples"
29+
it {
30+
is_expected.to contain_class('Puppet::Package::Repository') if manage_repos == true
31+
is_expected.to contain_yumrepo('puppetlabs-products') if manage_repos == true
32+
is_expected.to_not contain_yumrepo('puppetlabs-products') if manage_repos == false
33+
is_expected.to_not contain_apt__source('puppetlabs')
34+
}
35+
when 'Debian'
36+
it_behaves_like "agent examples"
37+
it { is_expected.to contain_class('Puppet::Package::Repository') if manage_repos == true }
38+
it { should_not contain_class('puppetlabs_yum') }
39+
if manage_repos == true
40+
it {
41+
is_expected.to contain_class('puppetlabs_apt')
42+
is_expected.to contain_apt__source('puppetlabs')
43+
}
44+
elsif manage_repos == false
45+
it { is_expected.to_not contain_class('puppetlabs_apt') }
46+
end
47+
when 'FreeBSD'
48+
if manage_repos == false
49+
it {
50+
is_expected.to_not contain_class('puppetlabs_apt')
51+
is_expected.to_not contain_class('puppetlabs_yum')
52+
is_expected.to compile.with_all_deps
53+
}
54+
end
8655
end
8756
end
88-
context 'manage_repos => false' do
89-
let(:params) {{
90-
:server => 'puppet.example.com',
91-
:report_server => 'puppet_reports.example.com',
92-
:manage_repos => false,
93-
}}
94-
it_behaves_like "agent examples"
95-
it do
96-
should contain_service('puppet_agent')
97-
should contain_cron('puppet agent').with_command(/puppet agent/)
98-
should_not contain_yumrepo('puppetlabs-products')
99-
should_not contain_apt__source('puppetlabs')
57+
end
58+
['service','cron','only_service'].each do |agent_method|
59+
manage_repos = false if facthash['osfamily'] == 'FreeBSD'
60+
context "method => #{agent_method}" do
61+
describe "agent configuration on #{facthash["osfamily"]}" do
62+
let(:params) {{
63+
:server => 'puppet.example.com',
64+
:report_server => 'puppet_reports.example.com',
65+
:method => agent_method,
66+
:manage_repos => manage_repos,
67+
}}
68+
69+
it_behaves_like "agent examples"
70+
case agent_method
71+
when 'service'
72+
it { should contain_service('puppet_agent').with({ :ensure => "running" }) }
73+
it { should contain_cron('puppet agent') }
74+
when 'cron'
75+
it { should contain_service('puppet_agent').with({ :ensure => "stopped" }) }
76+
it { should contain_cron('puppet agent') }
77+
when 'only_service'
78+
it { should contain_service('puppet_agent').with({ :ensure => "running" }) }
79+
it { should_not contain_cron('puppet agent') }
80+
end
10081
end
10182
end
102-
10383
end
10484
end
10585
end
10686
end
87+

spec/classes/puppet_spec.rb

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
require 'spec_helper'
22

3-
describe 'puppet' do
4-
context 'supported operating systems' do
5-
['Debian', 'RedHat', 'CentOS'].each do |operatingsystem|
6-
describe "puppet class without any parameters on #{operatingsystem}" do
7-
let(:params) {{ }}
8-
let(:facts) {{
9-
:operatingsystem => operatingsystem,
10-
}}
3+
PuppetSpecFacts.facts_for_platform_by_name([
4+
"Debian_wheezy_7.7_amd64_3.7.2_structured",
5+
"Ubuntu_precise_12.04_amd64_PE-3.3.2_stringified",
6+
"Ubuntu_trusty_14.04_amd64_PE-3.3.2_stringified"]).each do |name, facthash|
117

12-
it { should compile.with_all_deps }
8+
describe 'puppet' do
9+
context 'supported operating systems' do
10+
['Debian', 'RedHat', 'CentOS'].each do |operatingsystem|
11+
describe "puppet class without any parameters on #{operatingsystem}" do
12+
let(:params) {{ }}
13+
let(:facts) { facthash }
1314

14-
it { should contain_class('puppet::params') }
15+
it { should compile.with_all_deps }
16+
it { should contain_class('puppet::params') }
17+
end
1518
end
1619
end
17-
end
1820

19-
context 'unsupported operating system' do
20-
describe 'puppet class without any parameters on OpenVMS' do
21-
let(:facts) {{
22-
:operatingsystem => 'OpenVMS',
23-
}}
21+
context 'unsupported operating system' do
22+
describe 'puppet class without any parameters on OpenVMS' do
23+
let(:facts) {{
24+
:operatingsystem => 'OpenVMS',
25+
}}
2426

25-
it { expect { should contain_package('puppet') }.to raise_error(Puppet::Error, /Sorry, OpenVMS is not supported/) }
27+
it { expect { should contain_package('puppet') }.to raise_error(Puppet::Error, /Sorry, OpenVMS is not supported/) }
28+
end
2629
end
2730
end
2831
end

0 commit comments

Comments
 (0)