Skip to content

Commit 227c88f

Browse files
committed
Add spec tests for all existing resources
1 parent dc774fd commit 227c88f

File tree

8 files changed

+241
-25
lines changed

8 files changed

+241
-25
lines changed

manifests/collect.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"puppet_${metrics_type}-metrics.service",
1717
"puppet_${metrics_type}-metrics.timer",
1818
"puppet_${metrics_type}-tidy.service",
19-
"puppet_${metrics_type}-tidy.timer"
19+
"puppet_${metrics_type}-tidy.timer",
2020
]
2121
service { $services:
2222
ensure => stopped,
@@ -27,7 +27,7 @@
2727
"/etc/systemd/system/puppet_${metrics_type}-metrics.service",
2828
"/etc/systemd/system/puppet_${metrics_type}-metrics.timer",
2929
"/etc/systemd/system/puppet_${metrics_type}-tidy.service",
30-
"/etc/systemd/system/puppet_${metrics_type}-tidy.timer"
30+
"/etc/systemd/system/puppet_${metrics_type}-tidy.timer",
3131
]
3232

3333
file { $files:

manifests/pe_metric.pp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Optional[String] $metrics_server_hostname = undef,
1717
Optional[Integer] $metrics_server_port = undef,
1818
Optional[String] $metrics_server_db_name = undef,
19+
Optional[Hash] $env_vars = undef,
1920
) {
2021
$metrics_output_dir = "${puppet_metrics_collector::output_dir}/${metrics_type}"
2122

@@ -84,11 +85,12 @@
8485
$tidy_command = "${puppet_metrics_collector::scripts_dir}/metrics_tidy -d ${metrics_output_dir} -r ${retention_days}"
8586

8687
puppet_metrics_collector::collect { $metrics_type:
88+
ensure => $metric_ensure,
8789
metrics_command => $metrics_command,
8890
tidy_command => $tidy_command,
8991
metric_ensure => $metric_ensure,
9092
minute => $cron_minute,
91-
env_vars => undef,
93+
env_vars => $env_vars,
9294
notify => Exec['puppet_metrics_collector_daemon_reload'],
9395
}
9496

manifests/sar_metric.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
$tidy_command = "${puppet_metrics_collector::system::scripts_dir}/metrics_tidy -d ${metrics_output_dir} -r ${retention_days}"
6868

6969
puppet_metrics_collector::collect { $metrics_type:
70+
ensure => $metric_ensure,
7071
metrics_command => $metrics_command,
7172
tidy_command => $tidy_command,
7273
metric_ensure => $metric_ensure,

manifests/system.pp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,13 @@
133133
ensure => absent,
134134
}
135135

136-
$legacy_sar = ['system_memory']
137-
puppet_metrics_collector::collect { $legacy_sar:
138-
ensure => absent,
139-
metrics_command => 'foo',
140-
tidy_command => 'bar',
136+
# Legacy sar cleanup
137+
puppet_metrics_collector::sar_metric { 'system_memory' :
138+
metric_ensure => 'absent',
139+
cron_minute => '0',
140+
retention_days => 0,
141+
collection_frequency => 0,
142+
polling_frequency_seconds => 0,
143+
metrics_shipping_command => 'foo',
141144
}
142145
}

manifests/system/sar.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
collection_frequency => $collection_frequency,
1818
polling_frequency_seconds => $polling_frequency_seconds,
1919
metrics_shipping_command => $metrics_shipping_command,
20+
# This ensures that sar reports the time field as one 24 hour field, instead of a 12 hour format with spaces
2021
env_vars => { 'LC_TIME' => 'POSIX' },
2122
}
2223
}

spec/classes/init_spec.rb

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,83 @@
99
end
1010
end
1111

12+
context 'with default parameters' do
13+
it {
14+
execs = [
15+
'migrate /opt/puppetlabs/pe_metric_curl_cron_jobs directory',
16+
'puppet_metrics_collector_daemon_reload',
17+
]
18+
execs.each { |exec| is_expected.to contain_exec(exec) }
19+
20+
files = [
21+
'/opt/puppetlabs/puppet-metrics-collector',
22+
'/opt/puppetlabs/puppet-metrics-collector/config',
23+
'/opt/puppetlabs/puppet-metrics-collector/scripts',
24+
'/opt/puppetlabs/puppet-metrics-collector/scripts/create-metrics-archive',
25+
'/opt/puppetlabs/puppet-metrics-collector/scripts/json2timeseriesdb',
26+
'/opt/puppetlabs/puppet-metrics-collector/scripts/metrics_tidy',
27+
'/opt/puppetlabs/puppet-metrics-collector/scripts/pe_metrics.rb',
28+
'/opt/puppetlabs/puppet-metrics-collector/scripts/tk_metrics',
29+
'/opt/puppetlabs/puppet-metrics-collector/scripts/puma_metrics',
30+
]
31+
files.each { |file| is_expected.to contain_file(file) }
32+
33+
legacy_files = [
34+
'/opt/puppetlabs/puppet-metrics-collector/bin',
35+
'/opt/puppetlabs/bin/puppet-metrics-collector',
36+
]
37+
legacy_files.each { |file| is_expected.to contain_file(file).with_ensure('absent') }
38+
39+
['ace', 'bolt', 'orchestrator', 'console', 'puppetdb', 'puppetserver'].each do |service|
40+
is_expected.to contain_class("puppet_metrics_collector::service::#{service}")
41+
is_expected.to contain_puppet_metrics_collector__collect(service)
42+
is_expected.to contain_puppet_metrics_collector__pe_metric(service)
43+
44+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-tidy.service")
45+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-tidy.timer")
46+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.service")
47+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.timer")
48+
49+
is_expected.to contain_service("puppet_#{service}-metrics.service")
50+
is_expected.to contain_service("puppet_#{service}-metrics.timer")
51+
is_expected.to contain_service("puppet_#{service}-tidy.service")
52+
is_expected.to contain_service("puppet_#{service}-tidy.timer")
53+
54+
files = [
55+
"/opt/puppetlabs/puppet-metrics-collector/#{service}",
56+
"/opt/puppetlabs/puppet-metrics-collector/config/#{service}.yaml",
57+
]
58+
files.each { |file| is_expected.to contain_file(file) }
59+
60+
legacy_files = [
61+
"/opt/puppetlabs/puppet-metrics-collector/scripts/#{service}_config.yaml",
62+
"/opt/puppetlabs/puppet-metrics-collector/scripts/#{service}_metrics.sh",
63+
"/opt/puppetlabs/puppet-metrics-collector/scripts/#{service}_metrics",
64+
"/opt/puppetlabs/puppet-metrics-collector/scripts/#{service}_metrics_tidy",
65+
]
66+
legacy_files.each { |file| is_expected.to contain_file(file).with_ensure('absent') }
67+
end
68+
69+
legacy_crons = [
70+
'ace_metrics_tidy',
71+
'ace_metrics_collection',
72+
'activemq_metrics_collection',
73+
'activemq_metrics_tidy',
74+
'bolt_metrics_collection',
75+
'bolt_metrics_tidy',
76+
'console_metrics_collection',
77+
'console_metrics_tidy',
78+
'orchestrator_metrics_collection',
79+
'orchestrator_metrics_tidy',
80+
'puppetdb_metrics_collection',
81+
'puppetdb_metrics_tidy',
82+
'puppetserver_metrics_collection',
83+
'puppetserver_metrics_tidy',
84+
]
85+
legacy_crons.each { |cron| is_expected.to contain_cron(cron).with_ensure('absent') }
86+
}
87+
end
88+
1289
context 'when systemd is not the init provider' do
1390
let(:facts) { { puppet_metrics_collector: { have_systemd: false } } }
1491

@@ -30,8 +107,10 @@
30107
context 'when customizing the collection frequency' do
31108
let(:params) { { collection_frequency: 10 } }
32109

33-
['ace', 'bolt', 'orchestrator', 'console', 'puppetdb', 'puppetserver'].each do |service|
34-
it { is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.timer").with_content(%r{OnCalendar=.*0\/10}) }
35-
end
110+
it {
111+
['ace', 'bolt', 'orchestrator', 'console', 'puppetdb', 'puppetserver'].each do |service|
112+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.timer").with_content(%r{OnCalendar=.*0\/10})
113+
end
114+
}
36115
end
37116
end

spec/classes/puppet_metrics_collector_system_spec.rb

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,93 @@
22

33
describe 'puppet_metrics_collector::system' do
44
context 'with default parameters' do
5-
it { is_expected.not_to contain_package('sysstat') }
6-
it { is_expected.not_to contain_package('open-vm-tools') }
5+
it {
6+
is_expected.not_to contain_package('sysstat')
7+
is_expected.not_to contain_package('open-vm-tools')
8+
}
79
end
810

911
context 'with sysstat' do
1012
context 'already installed' do
1113
let(:pre_condition) { 'package{"sysstat": }' }
1214
let(:facts) { { puppet_metrics_collector: { have_sysstat: true, have_systemd: true } } }
1315

14-
it { is_expected.to contain_class('puppet_metrics_collector::system::cpu') }
15-
it { is_expected.to contain_class('puppet_metrics_collector::system::memory') }
16-
it { is_expected.to contain_class('puppet_metrics_collector::system::processes') }
16+
it {
17+
is_expected.not_to contain_class('puppet_metrics_collector::system::cpu')
18+
is_expected.not_to contain_class('puppet_metrics_collector::system::memory')
19+
is_expected.to contain_class('puppet_metrics_collector::system::sar')
20+
is_expected.to contain_class('puppet_metrics_collector::system::processes')
21+
is_expected.to contain_file('/opt/puppetlabs/puppet-metrics-collector/scripts/system_metrics')
22+
23+
legacy_crons = [
24+
'system_cpu_metrics_collection',
25+
'system_cpu_metrics_tidy',
26+
'system_processes_metrics_collection',
27+
'system_processes_metrics_tidy',
28+
'system_memory_metrics_collection',
29+
'system_memory_metrics_tidy',
30+
]
31+
legacy_crons.each { |cron| is_expected.to contain_cron(cron).with_ensure('absent') }
32+
33+
['system_cpu', 'system_processes'].each do |service|
34+
is_expected.to contain_puppet_metrics_collector__collect(service)
35+
is_expected.to contain_puppet_metrics_collector__sar_metric(service)
36+
37+
is_expected.to contain_file("/opt/puppetlabs/puppet-metrics-collector/#{service}")
38+
39+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-tidy.service")
40+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-tidy.timer")
41+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.service")
42+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.timer")
43+
44+
is_expected.to contain_service("puppet_#{service}-metrics.service")
45+
is_expected.to contain_service("puppet_#{service}-metrics.timer")
46+
is_expected.to contain_service("puppet_#{service}-tidy.service")
47+
is_expected.to contain_service("puppet_#{service}-tidy.timer")
48+
end
49+
50+
legacy_files = [
51+
'/opt/puppetlabs/puppet-metrics-collector/scripts/system_processes_metrics_tidy',
52+
'/opt/puppetlabs/puppet-metrics-collector/scripts/system_memory_metrics_tidy',
53+
'/opt/puppetlabs/puppet-metrics-collector/scripts/system_cpu_metrics_tidy',
54+
'/opt/puppetlabs/puppet-metrics-collector/scripts/generate_system_metrics',
55+
]
56+
legacy_files.each { |file| is_expected.to contain_file(file).with_ensure('absent') }
57+
58+
['system_memory'].each do |service|
59+
is_expected.to contain_puppet_metrics_collector__sar_metric(service).with_metric_ensure('absent')
60+
is_expected.to contain_puppet_metrics_collector__collect(service).with_ensure('absent')
61+
62+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-tidy.service").with_ensure('absent')
63+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-tidy.timer").with_ensure('absent')
64+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.service").with_ensure('absent')
65+
is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.timer").with_ensure('absent')
66+
is_expected.to contain_file("/opt/puppetlabs/puppet-metrics-collector/#{service}").with_ensure('absent')
67+
68+
is_expected.to contain_service("puppet_#{service}-metrics.service").with_ensure('stopped')
69+
is_expected.to contain_service("puppet_#{service}-metrics.timer").with_ensure('stopped')
70+
is_expected.to contain_service("puppet_#{service}-tidy.service").with_ensure('stopped')
71+
is_expected.to contain_service("puppet_#{service}-tidy.timer").with_ensure('stopped')
72+
end
73+
}
1774
end
1875

1976
context 'not installed and managed' do
2077
let(:params) { { manage_sysstat: true } }
2178
let(:facts) { { puppet_metrics_collector: { have_sysstat: false, have_systemd: true } } }
2279

2380
it { is_expected.to contain_package('sysstat') }
24-
it { is_expected.to contain_class('puppet_metrics_collector::system::cpu') }
25-
it { is_expected.to contain_class('puppet_metrics_collector::system::memory') }
81+
it { is_expected.not_to contain_class('puppet_metrics_collector::system::cpu') }
82+
it { is_expected.not_to contain_class('puppet_metrics_collector::system::memory') }
83+
it { is_expected.to contain_class('puppet_metrics_collector::system::sar') }
2684
it { is_expected.to contain_class('puppet_metrics_collector::system::processes') }
2785
end
2886

2987
context 'not installed and not managed' do
3088
it { is_expected.not_to contain_package('sysstat') }
3189
it { is_expected.not_to contain_class('puppet_metrics_collector::system::cpu') }
3290
it { is_expected.not_to contain_class('puppet_metrics_collector::system::memory') }
91+
it { is_expected.not_to contain_class('puppet_metrics_collector::system::sar') }
3392
it { is_expected.not_to contain_class('puppet_metrics_collector::system::processes') }
3493
end
3594
end
@@ -42,8 +101,33 @@
42101
context 'when the virtual fact reports vmware' do
43102
let(:facts) { { virtual: 'vmware' } }
44103

45-
it { is_expected.to contain_class('puppet_metrics_collector::system::vmware') }
46-
it { is_expected.not_to contain_package('open-vm-tools') }
104+
it {
105+
is_expected.to contain_class('puppet_metrics_collector::system::vmware')
106+
is_expected.not_to contain_package('open-vm-tools')
107+
108+
is_expected.to contain_exec('puppet_metrics_collector_system_daemon_reload')
109+
110+
legacy_crons = [
111+
'vmware_metrics_collection',
112+
'vmware_metrics_tidy',
113+
]
114+
legacy_crons.each { |cron| is_expected.to contain_cron(cron).with_ensure('absent') }
115+
116+
is_expected.to contain_puppet_metrics_collector__collect('vmware')
117+
118+
is_expected.to contain_file('/opt/puppetlabs/puppet-metrics-collector/vmware')
119+
is_expected.to contain_file('/opt/puppetlabs/puppet-metrics-collector/scripts/vmware_metrics')
120+
121+
is_expected.to contain_file('/etc/systemd/system/puppet_vmware-tidy.service')
122+
is_expected.to contain_file('/etc/systemd/system/puppet_vmware-tidy.timer')
123+
is_expected.to contain_file('/etc/systemd/system/puppet_vmware-metrics.service')
124+
is_expected.to contain_file('/etc/systemd/system/puppet_vmware-metrics.timer')
125+
126+
is_expected.to contain_service('puppet_vmware-metrics.service')
127+
is_expected.to contain_service('puppet_vmware-metrics.timer')
128+
is_expected.to contain_service('puppet_vmware-tidy.service')
129+
is_expected.to contain_service('puppet_vmware-tidy.timer')
130+
}
47131

48132
context 'when management of VMware Tools is enabled' do
49133
let(:params) { { manage_vmware_tools: true, vmware_tools_pkg: 'foo-tools' } }
@@ -67,7 +151,29 @@
67151
context 'when /opt/puppetlabs/server/bin/psql is present' do
68152
let(:facts) { { puppet_metrics_collector: { have_pe_psql: true, have_systemd: true } } }
69153

70-
it { is_expected.to contain_service('puppet_postgres-metrics.timer').with_ensure('running') }
154+
it {
155+
is_expected.to contain_class('puppet_metrics_collector::system::postgres')
156+
is_expected.to contain_puppet_metrics_collector__collect('postgres')
157+
158+
is_expected.to contain_file('/opt/puppetlabs/puppet-metrics-collector/postgres')
159+
is_expected.to contain_file('/opt/puppetlabs/puppet-metrics-collector/scripts/psql_metrics')
160+
161+
is_expected.to contain_file('/etc/systemd/system/puppet_postgres-tidy.service')
162+
is_expected.to contain_file('/etc/systemd/system/puppet_postgres-tidy.timer')
163+
is_expected.to contain_file('/etc/systemd/system/puppet_postgres-metrics.service')
164+
is_expected.to contain_file('/etc/systemd/system/puppet_postgres-metrics.timer')
165+
166+
is_expected.to contain_service('puppet_postgres-metrics.service')
167+
is_expected.to contain_service('puppet_postgres-metrics.timer')
168+
is_expected.to contain_service('puppet_postgres-tidy.service')
169+
is_expected.to contain_service('puppet_postgres-tidy.timer')
170+
171+
legacy_crons = [
172+
'postgres_metrics_collection',
173+
'postgres_metrics_tidy',
174+
]
175+
legacy_crons.each { |cron| is_expected.to contain_cron(cron).with_ensure('absent') }
176+
}
71177
end
72178

73179
context 'when /opt/puppetlabs/server/bin/psql is absent' do
@@ -113,7 +219,7 @@ class {'puppet_metrics_collector':
113219
end
114220
let(:facts) { { puppet_metrics_collector: { have_sysstat: true, have_systemd: true } } }
115221

116-
it { is_expected.not_to contain_puppet_metrics_collector__collect('system_cpu').with_metrics_command(%r{--influx-db\s+puppet_metrics}) }
222+
it { is_expected.not_to contain_puppet_metrics_collector__collect('sar').with_metrics_command(%r{--influx-db\s+puppet_metrics}) }
117223
end
118224

119225
context 'when customizing the collection frequency' do
@@ -123,7 +229,7 @@ class {'puppet_metrics_collector':
123229
end
124230
let(:params) { { collection_frequency: 10 } }
125231

126-
['system_cpu', 'system_memory', 'system_processes', 'postgres', 'vmware'].each do |service|
232+
['system_cpu', 'system_processes', 'postgres', 'vmware'].each do |service|
127233
it { is_expected.to contain_file("/etc/systemd/system/puppet_#{service}-metrics.timer").with_content(%r{OnCalendar=.*0\/10}) }
128234
end
129235
end

spec/defines/pe_metric_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,32 @@
1313
expect(subject).to compile
1414
end
1515

16-
context 'when capturing Puppet server metrics' do
17-
it { is_expected.to contain_service('puppet_puppetserver-metrics.timer').with_ensure('running') }
16+
context 'with default parameters' do
17+
it {
18+
is_expected.to contain_service('puppet_test-service-metrics.timer').with_ensure('running')
19+
is_expected.to contain_service('puppet_test-service-metrics.service')
20+
21+
is_expected.to contain_service('puppet_test-service-tidy.timer').with_ensure('running')
22+
is_expected.to contain_service('puppet_test-service-tidy.service')
23+
24+
is_expected.to contain_puppet_metrics_collector__collect('test-service')
25+
26+
files = [
27+
'/opt/puppetlabs/puppet-metrics-collector/scripts/test-service_config.yaml',
28+
'/opt/puppetlabs/puppet-metrics-collector/scripts/test-service_metrics.sh',
29+
'/opt/puppetlabs/puppet-metrics-collector/scripts/test-service_metrics',
30+
'/opt/puppetlabs/puppet-metrics-collector/scripts/test-service_metrics_tidy',
31+
'/opt/puppetlabs/puppet-metrics-collector/test-service',
32+
'/etc/systemd/system/puppet_test-service-metrics.service',
33+
'/etc/systemd/system/puppet_test-service-tidy.service',
34+
'/etc/systemd/system/puppet_test-service-tidy.timer',
35+
36+
]
37+
files.each { |file| is_expected.to contain_file(file) }
38+
39+
is_expected.to contain_cron('test-service_metrics_collection').with_ensure('absent')
40+
is_expected.to contain_cron('test-service_metrics_tidy').with_ensure('absent')
41+
}
1842
end
1943

2044
context 'when not capturing metrics' do

0 commit comments

Comments
 (0)