Skip to content

Commit 13d249e

Browse files
authored
Fixing up unit tests (#72)
The only unit test that existed did not work, so I fixed that and added tests for most other resources in this module. I did not add tests for the anchors as the anchor pattern is not considered good practice and is something that `pdk validate` complains about. I also added an integration acceptance test. This test actually applies the graylog::server class per the documentation to test that the module behaves as expected. This test could be expanded upon, but this serves as a good starting point.
1 parent df00803 commit 13d249e

11 files changed

+389
-8
lines changed

.fixtures.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@
33
---
44
fixtures:
55
forge_modules:
6-
# stdlib: "puppetlabs/stdlib"
6+
stdlib: "puppetlabs/stdlib"
7+
apt: puppetlabs/apt
8+
yumrepo_core: puppetlabs/yumrepo_core
9+
mongodb: puppet/mongodb
10+
opensearch: puppet/opensearch
11+
archive: puppet/archive
12+
repositories:
13+
facts: 'https://github.com/puppetlabs/puppetlabs-facts.git'
14+
puppet_agent: 'https://github.com/puppetlabs/puppetlabs-puppet_agent.git'
15+
provision: 'https://github.com/puppetlabs/provision.git'

provision.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
default:
3+
provisioner: docker
4+
images:
5+
- litmusimage/ubuntu:22.04
6+
- litmusimage/debian:11
7+
- litmusimage/debian:12
8+
- litmusimage/rockylinux:8
9+
- litmusimage/rockylinux:9
10+
- litmusimage/almalinux:8
11+
- litmusimage/almalinux:9
12+
debian:
13+
provisioner: docker
14+
images:
15+
- litmusimage/ubuntu:22.04
16+
- litmusimage/debian:11
17+
- litmusimage/debian:12
18+
redhat:
19+
provisioner: docker
20+
images:
21+
- litmusimage/rockylinux:8
22+
- litmusimage/rockylinux:9
23+
- litmusimage/almalinux:8
24+
- litmusimage/almalinux:9
25+
single:
26+
provisioner: docker
27+
images:
28+
- litmusimage/ubuntu:22.04

spec/acceptance/integration_spec.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper_acceptance'
4+
5+
describe 'init class' do
6+
context 'applying graylog server class works' do
7+
let(:pp) do
8+
<<-CODE
9+
class { 'graylog::repository':
10+
version => '6.1'
11+
}
12+
-> class { 'graylog::server':
13+
config => {
14+
'password_secret' => 'super secret secret',
15+
'root_password_sha2' => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
16+
}
17+
}
18+
CODE
19+
end
20+
21+
it 'behaves idempotently' do
22+
idempotent_apply(pp)
23+
end
24+
25+
if os[:family] == 'redhat'
26+
27+
# Ensure the yum repo exists and is enabled
28+
describe yumrepo('graylog') do
29+
it { is_expected.to exist }
30+
it { is_expected.to be_enabled }
31+
end
32+
33+
# Ensure the package is found
34+
describe command('dnf -q search graylog-server') do
35+
its(:stdout) { is_expected.to match(%r{Name Exactly Matched: graylog\-server}) }
36+
its(:exit_status) { is_expected.to eq 0 }
37+
end
38+
39+
describe file('/etc/sysconfig/graylog-server') do
40+
it { is_expected.to be_file }
41+
its(:content) { is_expected.to match(%r{\-Xms1g}) }
42+
its(:content) { is_expected.to match(%r{\-Xmx1g}) }
43+
its(:content) { is_expected.to match(%r{GRAYLOG_SERVER_ARGS=""}) }
44+
end
45+
elsif ['debian', 'ubuntu'].include?(os[:family])
46+
47+
# Ensure the repo exists on the filesystem
48+
describe file('/etc/apt/sources.list.d/graylog.list') do
49+
it { is_expected.to be_file }
50+
its(:content) { is_expected.to match(%r{https://downloads.graylog.org/repo/debian}) }
51+
end
52+
53+
# Ensure the package is found
54+
describe command('apt-cache search graylog-server') do
55+
its(:stdout) { is_expected.to match(%r{graylog-server - Graylog server}) }
56+
its(:exit_status) { is_expected.to eq 0 }
57+
end
58+
59+
# Ensure the environment vars file is present on disk and looks correct
60+
describe file('/etc/default/graylog-server') do
61+
it { is_expected.to be_file }
62+
its(:content) { is_expected.to match(%r{\-Xms1g}) }
63+
its(:content) { is_expected.to match(%r{\-Xmx1g}) }
64+
its(:content) { is_expected.to match(%r{GRAYLOG_SERVER_ARGS=""}) }
65+
end
66+
end
67+
68+
describe package('graylog-server') do
69+
it { is_expected.to be_installed }
70+
end
71+
72+
describe file('/etc/graylog/server/server.conf') do
73+
it { is_expected.to be_file }
74+
its(:content) { is_expected.to match(%r{root_password_sha2\s\=\s[a-f0-9]{64}}) }
75+
end
76+
end
77+
end

spec/classes/allinone_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'graylog::allinone' do
6+
on_supported_os.each do |os, os_facts|
7+
context "on #{os}" do
8+
let(:facts) { os_facts }
9+
let(:params) do
10+
{
11+
'opensearch' => {
12+
'version' => '2.15.0',
13+
'settings' => {
14+
'setting_a' => 'value_b'
15+
}
16+
},
17+
'graylog' => {
18+
'major_version' => '6.1',
19+
'config' => {
20+
'password_secret' => 'super secret secret',
21+
'root_password_sha2' => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
22+
}
23+
}
24+
}
25+
end
26+
27+
# Because of an issue with the opensearch module,the allinone class will
28+
# always fail an RedHat family operating systems using modern facts.
29+
case os_facts[:os]['family']
30+
when 'Debian'
31+
it { is_expected.to compile.with_all_deps }
32+
when 'RedHat'
33+
it { is_expected.to compile.and_raise_error(%r{Could not find class ::yum.*}) }
34+
end
35+
end
36+
end
37+
end

spec/classes/init_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
# frozen_string_literal: true
2+
13
require 'spec_helper'
4+
25
describe 'graylog' do
3-
context 'with default values for all parameters' do
4-
it { is_expected.to contain_class('graylog') }
6+
on_supported_os.each do |os, os_facts|
7+
context "on #{os}" do
8+
let(:facts) { os_facts }
9+
10+
it {
11+
is_expected.to compile.and_raise_error(%r{use the \"graylog::server\" class})
12+
}
13+
end
514
end
615
end

spec/classes/params_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'graylog::params' do
6+
on_supported_os.each do |os, os_facts|
7+
context "on #{os}" do
8+
let(:facts) { os_facts }
9+
10+
it { is_expected.to compile.with_all_deps }
11+
end
12+
end
13+
end

spec/classes/repository_spec.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'graylog::repository' do
6+
on_supported_os.each do |os, os_facts|
7+
context "on #{os}" do
8+
let(:facts) { os_facts }
9+
10+
it { is_expected.to compile.with_all_deps }
11+
12+
case os_facts[:os]['family']
13+
when 'Debian'
14+
it {
15+
is_expected.to contain_class('graylog::repository::apt')
16+
}
17+
it {
18+
is_expected.to contain_package('apt-transport-https')
19+
}
20+
it {
21+
is_expected.to contain_file('/etc/apt/trusted.gpg.d/graylog-keyring.gpg')
22+
.with_ensure('file')
23+
.with_owner('root')
24+
.with_group('root')
25+
.with_mode('0444')
26+
.with_source('puppet:///modules/graylog/graylog-keyring.gpg')
27+
.that_notifies('Exec[apt_update]')
28+
}
29+
it {
30+
is_expected.to contain_apt__source('graylog')
31+
.with_ensure('present')
32+
.with_comment('The official Graylog package repository')
33+
.with_location('https://downloads.graylog.org/repo/debian/')
34+
.with_release('stable')
35+
.with_repos('6.1')
36+
.with_include({ 'deb' => true, 'src' => false })
37+
.that_requires(
38+
[
39+
'File[/etc/apt/trusted.gpg.d/graylog-keyring.gpg]',
40+
'Package[apt-transport-https]',
41+
],
42+
)
43+
.that_notifies('Exec[apt_update]')
44+
}
45+
it {
46+
is_expected.to contain_file('/etc/apt/apt.conf.d/01_graylog_proxy')
47+
.with_ensure('file')
48+
}
49+
it {
50+
is_expected.to contain_file_line('Remove graylog config from apt proxy file')
51+
.with_ensure('absent')
52+
.with_path('/etc/apt/apt.conf.d/01_graylog_proxy')
53+
.with_match('graylog')
54+
.with_match_for_absence(true)
55+
.with_multiple(true)
56+
}
57+
when 'RedHat'
58+
it {
59+
is_expected.to contain_class('graylog::repository::yum')
60+
}
61+
it {
62+
is_expected.to contain_file('/etc/pki/rpm-gpg/RPM-GPG-KEY-graylog')
63+
.with_ensure('file')
64+
.with_owner('root')
65+
.with_group('root')
66+
.with_mode('0444')
67+
.with_source('puppet:///modules/graylog/RPM-GPG-KEY-graylog')
68+
}
69+
it {
70+
is_expected.to contain_yumrepo('graylog')
71+
.with_descr('The official Graylog package repository')
72+
.with_baseurl('https://downloads.graylog.org/repo/el/stable/6.1/$basearch/')
73+
.with_enabled(true)
74+
.with_gpgcheck(true)
75+
.that_requires(['File[/etc/pki/rpm-gpg/RPM-GPG-KEY-graylog]'])
76+
}
77+
end
78+
end
79+
end
80+
end

spec/classes/server_spec.rb

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'graylog::server' do
6+
on_supported_os.each do |os, os_facts|
7+
context "on #{os}" do
8+
let(:facts) { os_facts }
9+
let(:params) do
10+
{
11+
'config' => {
12+
'password_secret' => 'super secret secret',
13+
'root_password_sha2' => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
14+
}
15+
}
16+
end
17+
18+
# Test that the class doesn't error when given expected params
19+
it { is_expected.to compile.with_all_deps }
20+
21+
# Ensure that the class has the graylog-server package and that it is
22+
# installed
23+
it {
24+
is_expected.to contain_package('graylog-server')
25+
.with_ensure('installed')
26+
}
27+
28+
# Tests that the server config is managed and has expected content
29+
it {
30+
is_expected.to contain_file('/etc/graylog/server/server.conf')
31+
.with_ensure('file')
32+
.with_owner('graylog')
33+
.with_group('graylog')
34+
.with_mode('0640')
35+
.with_content(%r{password_secret = super secret secret})
36+
.with_content(%r{root_password_sha2\s\=\s[a-f0-9]{64}})
37+
}
38+
39+
# Ensure that the java params are being managed and contain expected
40+
# content
41+
case os_facts[:os]['family']
42+
when 'Debian'
43+
it {
44+
is_expected.to contain_file('/etc/default/graylog-server')
45+
.with_ensure('file')
46+
.with_owner('graylog')
47+
.with_group('graylog')
48+
.with_mode('0640')
49+
.with_content(%r{-Xms1g})
50+
.with_content(%r{-Xmx1g})
51+
}
52+
when 'RedHat'
53+
it {
54+
is_expected.to contain_file('/etc/sysconfig/graylog-server')
55+
.with_ensure('file')
56+
.with_owner('graylog')
57+
.with_group('graylog')
58+
.with_mode('0640')
59+
.with_content(%r{-Xms1g})
60+
.with_content(%r{-Xmx1g})
61+
}
62+
end
63+
64+
# Ensure that the service is being managed
65+
it {
66+
is_expected.to contain_service('graylog-server')
67+
.with_ensure('running')
68+
.with_enable(true)
69+
.with_hasstatus(true)
70+
.with_hasrestart(true)
71+
}
72+
end
73+
74+
context "on #{os} without password_secret" do
75+
let(:facts) { os_facts }
76+
let(:params) do
77+
{
78+
'config' => {
79+
'root_password_sha2' => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
80+
}
81+
}
82+
end
83+
84+
it {
85+
is_expected.to compile.and_raise_error(%r{Missing .*?password_secret})
86+
}
87+
end
88+
89+
context "on #{os} without root_password_sha2" do
90+
let(:facts) { os_facts }
91+
let(:params) do
92+
{
93+
'config' => {
94+
'password_secret' => 'super secret secret',
95+
}
96+
}
97+
end
98+
99+
it {
100+
is_expected.to compile.and_raise_error(%r{Missing .*root_password_sha2})
101+
}
102+
end
103+
104+
context "on #{os} with invalid root_password_sha2" do
105+
let(:facts) { os_facts }
106+
let(:params) do
107+
{
108+
'config' => {
109+
'password_secret' => 'super secret secret',
110+
'root_password_sha2' => 'this is an invalid hash',
111+
}
112+
}
113+
end
114+
115+
it {
116+
is_expected.to compile.and_raise_error(%r{root_password_sha2 parameter does not look like a SHA256 checksum})
117+
}
118+
end
119+
end
120+
end

0 commit comments

Comments
 (0)