Skip to content

Commit ec10dff

Browse files
dploegerThomasMinor
authored andcommitted
Optimization
Fix parameters to keep backwards compatibility Added tests
1 parent 60c3d46 commit ec10dff

File tree

10 files changed

+409
-137
lines changed

10 files changed

+409
-137
lines changed

manifests/mod/cache.pp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,58 @@
11
# @summary
22
# Installs `mod_cache`
3-
#
3+
#
4+
# @param cache_ignore_headers
5+
# Specifies HTTP header(s) that should not be stored in the cache.
6+
#
7+
# @param cache_default_expire
8+
# The default duration to cache a document when no expiry date is specified.
9+
#
10+
# @param cache_max_expire
11+
# The maximum time in seconds to cache a document
12+
#
13+
# @param cache_ignore_no_lastmod
14+
# Ignore the fact that a response has no Last Modified header.
15+
#
16+
# @param cache_header
17+
# Add an X-Cache header to the response.
18+
#
19+
# @param cache_lock
20+
# Enable the thundering herd lock.
21+
#
22+
# @param cache_ignore_cache_control
23+
# Ignore request to not serve cached content to client
24+
#
425
# @see https://httpd.apache.org/docs/current/mod/mod_cache.html for additional documentation.
526
#
6-
class apache::mod::cache {
27+
class apache::mod::cache (
28+
Optional[String] $cache_ignore_headers = undef,
29+
Optional[Integer] $cache_default_expire = undef,
30+
Optional[Integer] $cache_max_expire = undef,
31+
Optional[Enum['off', 'on', 'Off', 'On']] $cache_ignore_no_lastmod = undef,
32+
Optional[Enum['off', 'on', 'Off', 'On']] $cache_header = undef,
33+
Optional[Enum['off', 'on', 'Off', 'On']] $cache_lock = undef,
34+
Optional[Enum['off', 'on', 'Off', 'On']] $cache_ignore_cache_control = undef,
35+
) {
36+
include apache
737
::apache::mod { 'cache': }
38+
39+
$_configuration_file_name = 'cache.conf'
40+
41+
file { $_configuration_file_name:
42+
ensure => file,
43+
path => "${apache::mod_dir}/${_configuration_file_name}",
44+
mode => $apache::file_mode,
45+
content => epp('apache/mod/cache.conf.epp', {
46+
cache_ignore_headers => $cache_ignore_headers,
47+
cache_default_expire => $cache_default_expire,
48+
cache_max_expire => $cache_max_expire,
49+
cache_ignore_no_lastmod => $cache_ignore_no_lastmod,
50+
cache_header => $cache_header,
51+
cache_lock => $cache_lock,
52+
cache_ignore_cache_control => $cache_ignore_cache_control,
53+
}),
54+
require => Exec["mkdir ${apache::mod_dir}"],
55+
before => File[$apache::mod_dir],
56+
notify => Class['apache::service'],
57+
}
858
}

manifests/mod/cache_disk.pp

Lines changed: 39 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,108 +12,78 @@
1212
# - Debian: /var/cache/apache2/mod_cache_disk
1313
# - FreeBSD: /var/cache/mod_cache_disk
1414
# - Red Hat, Apache 2.4: /var/cache/httpd/proxy
15-
# - Red Hat, Apache 2.2: /var/cache/mod_proxy
1615
#
1716
# @param cache_enable
1817
# Defines an array of directories to cache, the default is none
19-
#
20-
# @param cache_ignore_headers
21-
# Specifies HTTP header(s) that should not be stored in the cache.
22-
#
18+
2319
# @param cache_dir_length
2420
# The number of characters in subdirectory names
2521
#
2622
# @param cache_dir_levels
2723
# The number of levels of subdirectories in the cache.
2824
#
29-
# @param cache_default_expire
30-
# The default duration to cache a document when no expiry date is specified.
31-
#
32-
# @param cache_max_expire
33-
# The maximum time in seconds to cache a document
34-
#
35-
# @param cache_ignore_no_lastmod
36-
# Ignore the fact that a response has no Last Modified header.
37-
#
38-
# @param cache_header
39-
# Add an X-Cache header to the response.
40-
#
41-
# @param cache_lock
42-
# Enable the thundering herd lock.
43-
#
44-
# @param cache_ignore_cache_control
45-
# Ignore request to not serve cached content to client
46-
#
4725
# @param cache_max_filesize
4826
# The maximum size (in bytes) of a document to be placed in the cache
4927
#
28+
# @param cache_ignore_headers
29+
# DEPRECATED Ignore request to not serve cached content to client (included for compatibility reasons to support disk_cache)
30+
#
31+
# @param configuration_file_name
32+
# DEPRECATED Name of module configuration file (used for the compatibility layer for disk_cache)
33+
#
5034
# @note
5135
# Apache 2.2, mod_disk_cache installed. On Apache 2.4, mod_cache_disk installed.
5236
#
53-
# @see https://httpd.apache.org/docs/2.2/mod/mod_disk_cache.html for additional documentation on version 2.2.
54-
#
5537
# @see https://httpd.apache.org/docs/2.4/mod/mod_cache_disk.html for additional documentation on version 2.4.
5638
#
5739
class apache::mod::cache_disk (
58-
$cache_root = undef,
59-
Array[String] $cache_enable = [],
60-
Optional[String] $cache_ignore_headers = undef,
61-
Optional[Integer] $cache_dir_length = undef,
62-
Optional[Integer] $cache_dir_levels = undef,
63-
Optional[Integer] $cache_default_expire = undef,
64-
Optional[Integer] $cache_max_expire = undef,
65-
Optional[Enum['Off', 'On']] $cache_ignore_no_lastmod = undef,
66-
Optional[Enum['off', 'on']] $cache_header = undef,
67-
Optional[Enum['off', 'on']] $cache_lock = undef,
68-
Optional[Enum['Off', 'On']] $cache_ignore_cache_control = undef,
69-
Optional[Integer] $cache_max_filesize = undef,
40+
Optional[String] $cache_root = undef,
41+
Array[String] $cache_enable = [],
42+
Optional[Integer] $cache_dir_length = undef,
43+
Optional[Integer] $cache_dir_levels = undef,
44+
Optional[Integer] $cache_max_filesize = undef,
45+
Optional[String] $cache_ignore_headers = undef,
46+
Optional[String] $configuration_file_name = undef,
7047
) {
7148
include apache
72-
if $cache_root {
73-
$_cache_root = $cache_root
49+
50+
if $cache_ignore_headers {
51+
deprecation(
52+
'apache::mod::cache_disk',
53+
'The parameter cache_ignore_headers is deprecated. Please use apache::mod::cache::cache_ignore_headers instead.'
54+
)
7455
}
75-
elsif versioncmp($apache::apache_version, '2.4') >= 0 {
76-
$_module_name = 'cache_disk'
77-
$_cache_root = $::osfamily ? {
56+
57+
$_cache_root = $cache_root ? {
58+
undef => $facts['os']['family'] ? {
7859
'debian' => '/var/cache/apache2/mod_cache_disk',
7960
'redhat' => '/var/cache/httpd/proxy',
8061
'freebsd' => '/var/cache/mod_cache_disk',
81-
}
62+
},
63+
default => $cache_root,
8264
}
83-
else {
84-
$_module_name = 'disk_cache'
85-
$_cache_root = $::osfamily ? {
86-
'debian' => '/var/cache/apache2/mod_disk_cache',
87-
'redhat' => '/var/cache/mod_proxy',
88-
'freebsd' => '/var/cache/mod_disk_cache',
89-
}
65+
$_configuration_file_name = $configuration_file_name ? {
66+
undef => 'cache_disk.conf',
67+
default => $configuration_file_name
9068
}
91-
$_configuration_file_name = "${_module_name}.conf"
92-
$_class_name = "::apache::mod::${_module_name}"
69+
$_class_name = 'apache::mod::cache_disk'
9370

94-
apache::mod { $_module_name: }
71+
apache::mod { 'cache_disk': }
9572

96-
Class['::apache::mod::cache'] -> Class[$_class_name]
73+
Class['apache::mod::cache'] -> Class[$_class_name]
9774

98-
# Template uses
99-
# - $_cache_root
100-
# - $cache_enable
101-
# - $cache_dir_length
102-
# - $cache_ignore_headers
103-
# - $cache_dir_length
104-
# - $cache_dir_levels
105-
# - $cache_default_expire
106-
# - $cache_max_expire
107-
# - $cache_ignore_no_lastmod
108-
# - $cache_header
109-
# - $cache_lock
110-
# - $cache_ignore_cache_control
111-
# - $cache_max_filesize
11275
file { $_configuration_file_name:
11376
ensure => file,
11477
path => "${apache::mod_dir}/${_configuration_file_name}",
11578
mode => $apache::file_mode,
116-
content => template('apache/mod/cache_disk.conf.erb'),
79+
content => epp('apache/mod/cache_disk.conf.epp', {
80+
cache_root => $_cache_root,
81+
cache_enable => $cache_enable,
82+
cache_dir_length => $cache_dir_length,
83+
cache_dir_levels => $cache_dir_levels,
84+
cache_max_filesize => $cache_max_filesize,
85+
cache_ignore_headers => $cache_ignore_headers,
86+
}),
11787
require => Exec["mkdir ${apache::mod_dir}"],
11888
before => File[$apache::mod_dir],
11989
notify => Class['apache::service'],

manifests/mod/disk_cache.pp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
# @param cache_ignore_headers
1111
# Specifies HTTP header(s) that should not be stored in the cache.
1212
#
13+
# @param default_cache_enable
14+
# Default value is true, which enables "CacheEnable disk /" in disk_cache.conf for the webserver. This would cache
15+
# every request to apache by default for every vhost. If set to false the default cache all behaviour is supressed.
16+
# You can then control this behaviour in individual vhosts by explicitly defining CacheEnable.
17+
#
1318
# @note
1419
# Apache 2.2, mod_disk_cache installed. On Apache 2.4, mod_cache_disk installed.
1520
# This class is deprecated, use mode_cache_disk instead
@@ -21,11 +26,14 @@
2126
Optional[String] $cache_ignore_headers = undef,
2227
Boolean $default_cache_enable = true,
2328
) {
24-
deprecation('apache::mod::disk_cache','This module is deprecated; please use apache::mod::cache_disk')
29+
deprecation('apache::mod::disk_cache', 'This module is deprecated; please use apache::mod::cache_disk')
2530

2631
class { 'apache::mod::cache_disk':
27-
cache_root => $cache_root,
28-
cache_enable => ['/'],
29-
cache_ignore_headers => $cache_ignore_headers,
32+
cache_root => $cache_root,
33+
cache_enable => ['/'],
34+
cache_ignore_headers => $cache_ignore_headers,
35+
cache_dir_length => 1,
36+
cache_dir_levels => 2,
37+
configuration_file_name => 'cache_disk.conf'
3038
}
3139
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'apache::mod::cache_disk', type: :class do
6+
context 'on a Debian OS' do
7+
include_examples 'Debian 11'
8+
9+
let(:params) do
10+
{
11+
cache_enable: ['/'],
12+
}
13+
end
14+
15+
let :pre_condition do
16+
'class{ "apache":
17+
default_mods => ["cache"],
18+
mod_dir => "/tmp/junk",
19+
}'
20+
end
21+
22+
it { is_expected.to compile }
23+
it { is_expected.to contain_class('apache::mod::cache_disk') }
24+
it { is_expected.to contain_class('apache::mod::cache').that_comes_before('Class[Apache::Mod::Cache_disk]') }
25+
it { is_expected.to contain_apache__mod('cache_disk') }
26+
27+
default_config = %r{CacheEnable disk /\nCacheRoot "/var/cache/apache2/mod_cache_disk"}
28+
29+
it {
30+
expect(subject).to contain_file('cache_disk.conf')
31+
.with(content: default_config)
32+
}
33+
34+
describe 'with multiple cache_enable parameters' do
35+
let(:params) do
36+
{
37+
cache_enable: %w[/ /something],
38+
}
39+
end
40+
41+
it {
42+
expect(subject).to contain_file('cache_disk.conf')
43+
.with(content: %r{CacheEnable disk /\nCacheEnable disk /something\nCacheRoot "/var/cache/apache2/mod_cache_disk"})
44+
}
45+
end
46+
47+
describe 'with cache_dir_length' do
48+
let(:params) do
49+
{
50+
cache_dir_length: 2,
51+
cache_enable: ['/'],
52+
}
53+
end
54+
55+
it {
56+
expect(subject).to contain_file('cache_disk.conf')
57+
.with(content: %r{#{default_config}\nCacheDirLength 2})
58+
}
59+
end
60+
61+
describe 'with cache_dir_levels' do
62+
let(:params) do
63+
{
64+
cache_dir_levels: 2,
65+
cache_enable: ['/'],
66+
}
67+
end
68+
69+
it {
70+
expect(subject).to contain_file('cache_disk.conf')
71+
.with(content: %r{#{default_config}\nCacheDirLevels 2})
72+
}
73+
end
74+
end
75+
76+
context 'on a RedHat 8-based OS' do
77+
include_examples 'RedHat 8'
78+
79+
let(:params) do
80+
{
81+
cache_enable: ['/'],
82+
}
83+
end
84+
85+
let :pre_condition do
86+
'class{ "apache":
87+
default_mods => ["cache"],
88+
mod_dir => "/tmp/junk",
89+
}'
90+
end
91+
92+
it { is_expected.to compile }
93+
94+
it {
95+
expect(subject).to contain_file('cache_disk.conf')
96+
.with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/httpd/proxy"})
97+
}
98+
end
99+
100+
context 'on a FreeBSD OS' do
101+
include_examples 'FreeBSD 10'
102+
103+
let(:params) do
104+
{
105+
cache_enable: ['/'],
106+
}
107+
end
108+
109+
let :pre_condition do
110+
'class{ "apache":
111+
default_mods => ["cache"],
112+
mod_dir => "/tmp/junk",
113+
}'
114+
end
115+
116+
it { is_expected.to compile }
117+
118+
it {
119+
expect(subject).to contain_file('cache_disk.conf')
120+
.with(content: %r{CacheEnable disk /\nCacheRoot "/var/cache/mod_cache_disk"})
121+
}
122+
end
123+
end

0 commit comments

Comments
 (0)