Skip to content

Commit 470620c

Browse files
anders-larssonJulien Kassar
authored and
Julien Kassar
committed
Fix spec tests and some bugs found
1 parent d223ae8 commit 470620c

File tree

8 files changed

+358
-40
lines changed

8 files changed

+358
-40
lines changed

Diff for: .fixtures.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fixtures:
2+
repositories:
3+
stdlib:
4+
repo: 'git://github.com/puppetlabs/puppetlabs-stdlib.git'
5+
ref: '4.21.0'
6+
symlinks:
7+
systemd: "#{source_dir}"

Diff for: Gemfile

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
source ENV['GEM_SOURCE'] || 'https://rubygems.org'
2+
3+
if puppetversion = ENV['PUPPET_GEM_VERSION']
4+
gem 'puppet', puppetversion, :require => false
5+
else
6+
gem 'puppet', :require => false
7+
end
8+
9+
gem 'facter', '~> 1.0' if ENV['PUPPET_GEM_VERSION'] < '~> 3.5.0'
10+
gem 'facter', '>= 2.2.0' if ENV['PUPPET_GEM_VERSION'] >= '~> 3.5.0'
11+
12+
gem 'rspec-puppet', '~> 2.0'
13+
gem 'puppet-lint', '~> 2.0'
14+
gem 'puppet-lint-absolute_classname-check', '~> 0.2' if RUBY_VERSION < '1.9'
15+
gem 'puppet-lint-absolute_classname-check', '~> 1.0' if RUBY_VERSION >= '1.9'
16+
gem 'puppet-lint-alias-check'
17+
gem 'puppet-lint-empty_string-check'
18+
gem 'puppet-lint-file_ensure-check'
19+
gem 'puppet-lint-file_source_rights-check'
20+
gem 'puppet-lint-leading_zero-check'
21+
gem 'puppet-lint-spaceship_operator_without_tag-check'
22+
gem 'puppet-lint-trailing_comma-check'
23+
gem 'puppet-lint-undef_in_function-check'
24+
gem 'puppet-lint-unquoted_string-check'
25+
gem 'puppet-lint-variable_contains_upcase'
26+
27+
gem 'rspec', '~> 2.0' if RUBY_VERSION >= '1.8.7' && RUBY_VERSION < '1.9'
28+
gem 'rake', '~> 10.0' if RUBY_VERSION >= '1.8.7' && RUBY_VERSION < '1.9'
29+
gem 'json', '<= 1.8' if RUBY_VERSION < '2.0.0'
30+
gem 'json_pure', '<= 2.0.1' if RUBY_VERSION < '2.0.0'
31+
gem 'metadata-json-lint', '0.0.11' if RUBY_VERSION <= '1.9.3'
32+
gem 'metadata-json-lint' if RUBY_VERSION > '1.9.3'
33+
gem 'public_suffix', '~> 1.1.0' if RUBY_VERSION < '2.1.1' && RUBY_VERSION >= '1.9'
34+
gem 'public_suffix', '1.3.0' if RUBY_VERSION < '1.9'
35+
36+
gem 'puppetlabs_spec_helper', '2.0.2', :require => false if RUBY_VERSION >= '1.8.7' && RUBY_VERSION < '1.9'
37+
gem 'puppetlabs_spec_helper', '>= 2.0.0', :require => false if RUBY_VERSION >= '1.9'
38+
gem 'parallel_tests', '<= 2.9.0', :require => false if RUBY_VERSION < '2.0.0' && RUBY_VERSION >= '1.9'
39+
gem 'parallel_tests', '1.0.9', :require => false if RUBY_VERSION < '1.9'
40+
gem 'parallel', '1.3.3.1', :require => false if RUBY_VERSION < '1.9'
41+
42+
if puppetversion && puppetversion < '5.0' && RUBY_VERSION >= '2.1.9'
43+
gem 'semantic_puppet', :require => false
44+
end

Diff for: Rakefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'puppetlabs_spec_helper/rake_tasks'
2+
require 'puppet-lint/tasks/puppet-lint'
3+
4+
PuppetLint.configuration.send('disable_80chars')
5+
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
6+
7+
desc "Run puppet in noop mode and check for syntax errors."
8+
task :validate do
9+
Dir['manifests/**/*.pp'].each do |manifest|
10+
sh "puppet parser validate --noop #{manifest}"
11+
end
12+
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
13+
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
14+
end
15+
Dir['templates/**/*.erb'].each do |template|
16+
sh "erb -P -x -T '-' #{template} | ruby -c"
17+
end
18+
end

Diff for: manifests/unit.pp

+62-37
Original file line numberDiff line numberDiff line change
@@ -24,59 +24,84 @@
2424

2525
include ::systemd
2626

27-
validate_re($ensure, [ '^present$', '^absent$' ],
28-
"systemd::unit::${name}::ensure is invalid and does not match the regex.")
27+
if is_string($ensure) {
28+
validate_re($ensure, [ '^present$', '^absent$' ],
29+
"systemd::unit::${name}::ensure does not match the regex.")
30+
} else {
31+
fail("systemd::unit::${name}::ensure is not a string.")
32+
}
2933

3034
validate_absolute_path($systemd_path)
3135

32-
if $unit_after != undef {
33-
validate_string($unit_after)
36+
if $unit_after != undef and is_string($unit_after) == false {
37+
fail("systemd::${name}::unit::unit_after is not a string")
3438
}
35-
if $unit_before != undef {
36-
validate_string($unit_before)
39+
if $unit_before != undef and is_string($unit_before) == false {
40+
fail("systemd::${name}::unit::unit_before is not a string")
3741
}
38-
if $unit_description != undef {
39-
validate_string($unit_description)
42+
if $unit_description != undef and is_string($unit_description) == false {
43+
fail("systemd::${name}::unit::unit_description is not a string")
4044
}
41-
if $environment != undef {
42-
validate_string($environment)
45+
if $environment != undef and is_string($environment) == false {
46+
fail("systemd::${name}::unit::environment is not a string")
4347
}
44-
if $group != undef {
45-
validate_string($group)
48+
if $group != undef and is_string($group) == false {
49+
fail("systemd::${name}::unit::group is not a string")
4650
}
47-
if $user != undef {
48-
validate_string($user)
51+
if $user != undef and is_string($user) == false {
52+
fail("systemd::${name}::unit::user is not a string")
4953
}
50-
if $workingdirectory != undef {
51-
validate_string($workingdirectory)
54+
if $service_restart != undef and is_string($service_restart) == false {
55+
fail("systemd::${name}::unit::restart is not a string")
5256
}
53-
validate_re($service_type, [ '^simple$', '^forking$', '^oneshot$', '^dbus$', '^notify$', '^idle$' ],
54-
"systemd::unit::${name}::ensure is invalid and does not match the regex.")
55-
if $service_timeoutstartsec != undef {
56-
if is_string($service_timeoutstartsec) {
57-
$service_timeoutstartsec_real = 0 + $service_timeoutstartsec # str2integer
58-
} else {
59-
$service_timeoutstartsec_real = $service_timeoutstartsec
60-
validate_integer($service_timeoutstartsec_real)
61-
}
57+
if $service_execstart != undef and is_string($service_execstart) == false {
58+
fail("systemd::${name}::unit::service_execstart is not a string")
6259
}
63-
if $service_restart != undef {
64-
validate_string($service_restart)
60+
if $service_execstop != undef and is_string($service_execstop) == false {
61+
fail("systemd::${name}::unit::service_execstop is not a string")
6562
}
66-
if $service_restartsec != undef {
67-
validate_string($service_restartsec)
63+
if $install_wantedby != undef and is_string($install_wantedby) == false {
64+
fail("systemd::${name}::unit::install_wantedby is not a string")
6865
}
69-
if $service_execstartpre != undef {
70-
validate_array($service_execstartpre)
66+
if $workingdirectory != undef and is_string($workingdirectory) == false {
67+
fail("systemd::${name}::unit::workingdirectory is not a string")
7168
}
72-
if $service_execstart != undef {
73-
validate_string($service_execstart)
69+
70+
if $service_execstartpre != undef and is_array($service_execstartpre) == false {
71+
fail("systemd::${name}::unit::service_execstartpre is not an Array")
7472
}
75-
if $service_execstop != undef {
76-
validate_string($service_execstop)
73+
74+
if is_string($service_type) {
75+
validate_re($service_type, [ '^simple$', '^forking$', '^oneshot$', '^dbus$', '^notify$', '^idle$' ],
76+
"systemd::unit::${name}::ensure does not match the regex.")
77+
} else {
78+
fail("systemd::unit::${name}::ensure is not a string.")
7779
}
78-
if $install_wantedby != undef {
79-
validate_string($install_wantedby)
80+
81+
if $service_timeoutstartsec != undef {
82+
if is_string($service_timeoutstartsec) {
83+
validate_re($service_timeoutstartsec, [ '^infinity$', '(([0-9]+h(our)?)?([0-9]+m(in)?)?([0-9]+s(ec)?([0-9]+ms)?)?)' ],
84+
"systemd::unit::${name}::service_timeoutstartsec must match either '^infinity$' or '(([0-9]+h(our)?)?([0-9]+m(in)?)?([0-9]+s(ec)?([0-9]+ms)?)?)'")
85+
$service_timeoutstartsec_real = $service_timeoutstartsec
86+
} elsif is_integer($service_timeoutstartsec) {
87+
validate_integer($service_timeoutstartsec, undef, 0)
88+
$service_timeoutstartsec_real = $service_timeoutstartsec
89+
} else {
90+
fail("system::unit::${name}::service_timeoutstartsec is not a string nor an integer")
91+
}
92+
}
93+
94+
if $service_restartsec != undef {
95+
if is_string($service_restartsec) {
96+
validate_re($service_restartsec, [ '^infinity$', '(([0-9]+h(our)?)?([0-9]+m(in)?)?([0-9]+s(ec)?([0-9]+ms)?)?)' ],
97+
"systemd::unit::${name}::service_restartsec must match either '^infinity$' or '(([0-9]+h(our)?)?([0-9]+m(in)?)?([0-9]+s(ec)?([0-9]+ms)?)?)'")
98+
$service_restartsec_real = $service_restartsec
99+
} elsif is_integer($service_restartsec) {
100+
validate_integer($service_restartsec, undef, 0)
101+
$service_restartsec_real = $service_restartsec
102+
} else {
103+
fail("system::unit::${name}::service_timeoutstartsec is not a string nor an integer")
104+
}
80105
}
81106

82107
file { "${name}_file":

Diff for: spec/classes/init_spec.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'spec_helper'
2+
describe 'systemd' do
3+
4+
# ensure that the class is passive by default
5+
describe 'when all parameters are unset (unsing module defaults)' do
6+
it { should compile.with_all_deps }
7+
it { should contain_class('systemd') }
8+
it { should have_resource_count(1) }
9+
it { should have_systemd__unit_resource_count(0) }
10+
11+
it { should contain_exec('systemd_reload').with({
12+
'command' => '/usr/bin/systemctl daemon-reload',
13+
'refreshonly' => true,
14+
})}
15+
end
16+
17+
describe 'when using parameter units' do
18+
let(:params) do
19+
{
20+
:units => {
21+
'example_unit' => {
22+
'unit_description' => 'Example unit',
23+
'service_execstart' => '/bin/echo',
24+
'install_wantedby' => 'multi-user.target',
25+
}
26+
}
27+
}
28+
end
29+
content = <<-END.gsub(/^\s+\|/, '')
30+
|[Unit]
31+
|Description=Example unit
32+
|
33+
|[Service]
34+
|Type=simple
35+
|ExecStart=/bin/echo
36+
|
37+
|[Install]
38+
|WantedBy=multi-user.target
39+
END
40+
it { should compile.with_all_deps }
41+
it { should contain_class('systemd') }
42+
it { should have_systemd__unit_resource_count(1) }
43+
it { should contain_file('example_unit_file').with({
44+
'ensure' => 'present',
45+
'path' => '/etc/systemd/system/example_unit.service',
46+
'owner' => 'root',
47+
'group' => 'root',
48+
'mode' => '0644',
49+
'content' => content,
50+
})}
51+
it { should contain_service('example_unit_service').with({
52+
'ensure' => 'running',
53+
'name' => 'example_unit',
54+
'enable' => true,
55+
'provider' => 'systemd'
56+
})}
57+
end
58+
59+
end

0 commit comments

Comments
 (0)