Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / spec / acceptance / default_spec.rb @ 0c9bc308

Historique | Voir | Annoter | Télécharger (5,14 ko)

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