-
Notifications
You must be signed in to change notification settings - Fork 30
/
default_spec.rb
184 lines (172 loc) · 5.66 KB
/
default_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# frozen_string_literal: true
require 'spec_helper_acceptance'
describe 'nftables class' do
context 'configure default nftables service' do
it 'works idempotently with no errors' do
pp = <<-EOS
# default mask of firewalld service fails if service is not installed.
# https://tickets.puppetlabs.com/browse/PUP-10814
class { 'nftables':
firewalld_enable => false,
}
$config_path = $facts['os']['family'] ? {
'Archlinux' => '/etc/nftables.conf',
'Debian' => '/etc/nftables.conf',
default => '/etc/sysconfig/nftables.conf',
}
$nft_path = $facts['os']['family'] ? {
'Archlinux' => '/usr/bin/nft',
default => '/usr/sbin/nft',
}
# nftables cannot be started in docker so replace service with a validation only.
systemd::dropin_file{"zzz_docker_nft.conf":
ensure => present,
unit => "nftables.service",
content => [
"[Service]",
"ExecStart=",
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
"ExecReload=",
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f ${config_path}",
"",
].join("\n"),
notify => Service["nftables"],
}
EOS
# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end
describe package('nftables') do
it { is_expected.to be_installed }
end
describe service('nftables') do
it { is_expected.to be_running }
it { is_expected.to be_enabled }
end
describe file('/etc/nftables/puppet.nft', '/etc/systemd/system/nftables.service.d/puppet_nft.conf') do
it { is_expected.to be_file }
end
describe file('/etc/nftables/puppet') do
it { is_expected.to be_directory }
end
end
context 'with bad invalid nft rules' do
it 'puppet fails but should leave nft service running' do
pp = <<-EOS
class{'nftables':
firewalld_enable => false,
}
nftables::rule{'default_out-junk':
content => 'A load of junk',
}
$config_path = $facts['os']['family'] ? {
'Archlinux' => '/etc/nftables.conf',
'Debian' => '/etc/nftables.conf',
default => '/etc/sysconfig/nftables.conf',
}
$nft_path = $facts['os']['family'] ? {
'Archlinux' => '/usr/bin/nft',
default => '/usr/sbin/nft',
}
# nftables cannot be started in docker so replace service with a validation only.
systemd::dropin_file{"zzz_docker_nft.conf":
ensure => present,
unit => "nftables.service",
content => [
"[Service]",
"ExecStart=",
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
"ExecReload=",
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
"",
].join("\n"),
notify => Service["nftables"],
}
EOS
apply_manifest(pp, expect_failures: true)
end
describe service('nftables') do
it { is_expected.to be_running }
it { is_expected.to be_enabled }
end
end
context 'with totally empty firewall' do
it 'no rules validate okay' do
pp = <<-EOS
class{'nftables':
firewalld_enable => false,
inet_filter => false,
nat => false,
}
$config_path = $facts['os']['family'] ? {
'Archlinux' => '/etc/nftables.conf',
'Debian' => '/etc/nftables.conf',
default => '/etc/sysconfig/nftables.conf',
}
$nft_path = $facts['os']['family'] ? {
'Archlinux' => '/usr/bin/nft',
default => '/usr/sbin/nft',
}
# nftables cannot be started in docker so replace service with a validation only.
systemd::dropin_file{"zzz_docker_nft.conf":
ensure => present,
unit => "nftables.service",
content => [
"[Service]",
"ExecStart=",
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
"ExecReload=",
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
"",
].join("\n"),
notify => Service["nftables"],
}
EOS
apply_manifest(pp, catch_failures: true)
end
describe service('nftables') do
it { is_expected.to be_running }
it { is_expected.to be_enabled }
end
end
context 'with custom nat_table_name' do
it 'no rules validate okay' do
pp = <<-EOS
class{'nftables':
firewalld_enable => false,
nat => true,
nat_table_name => 'mycustomtablename',
}
$config_path = $facts['os']['family'] ? {
'Archlinux' => '/etc/nftables.conf',
'Debian' => '/etc/nftables.conf',
default => '/etc/sysconfig/nftables.conf',
}
$nft_path = $facts['os']['family'] ? {
'Archlinux' => '/usr/bin/nft',
default => '/usr/sbin/nft',
}
# nftables cannot be started in docker so replace service with a validation only.
systemd::dropin_file{"zzz_docker_nft.conf":
ensure => present,
unit => "nftables.service",
content => [
"[Service]",
"ExecStart=",
"ExecStart=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
"ExecReload=",
"ExecReload=${nft_path} -c -I /etc/nftables/puppet -f $config_path",
"",
].join("\n"),
notify => Service["nftables"],
}
EOS
apply_manifest(pp, catch_failures: true)
end
describe service('nftables') do
it { is_expected.to be_running }
it { is_expected.to be_enabled }
end
end
end