diff --git a/.fixtures.yml b/.fixtures.yml index 1703f2e9d..8d87db8ea 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -1,7 +1,7 @@ fixtures: repositories: - apt: "git://github.com/puppetlabs/puppetlabs-apt.git" - concat: "git://github.com/puppetlabs/puppetlabs-concat.git" - stdlib: "git://github.com/puppetlabs/puppetlabs-stdlib.git" + apt: "https://github.com/puppetlabs/puppetlabs-apt.git" + concat: "https://github.com/puppetlabs/puppetlabs-concat.git" + stdlib: "https://github.com/puppetlabs/puppetlabs-stdlib.git" symlinks: nginx: "#{source_dir}" diff --git a/manifests/resource/mailhost.pp b/manifests/resource/mailhost.pp index 7b66546f8..b2b3466a0 100644 --- a/manifests/resource/mailhost.pp +++ b/manifests/resource/mailhost.pp @@ -131,7 +131,7 @@ # Add IPv6 Logic Check - Nginx service will not start if ipv6 is enabled # and support does not exist for it in the kernel. - if ($ipv6_enable and !$::ipaddress6) { + if ($ipv6_enable and !$facts['ipaddress6']) { warning('nginx: IPv6 support is not enabled or configured properly') } diff --git a/spec/classes/nginx_spec.rb b/spec/classes/nginx_spec.rb index 4cdcdc515..55708966e 100644 --- a/spec/classes/nginx_spec.rb +++ b/spec/classes/nginx_spec.rb @@ -1,1092 +1,1060 @@ require 'spec_helper' describe 'nginx' do - let :facts do - { - osfamily: 'RedHat' - } - end - - let :params do - { - nginx_upstreams: { 'upstream1' => { 'members' => ['localhost:3000'] } }, - nginx_servers: { 'test2.local' => { 'www_root' => '/' } }, - nginx_servers_defaults: { 'listen_options' => 'default_server' }, - nginx_locations: { 'test2.local' => { 'server' => 'test2.local', 'www_root' => '/' } }, - nginx_locations_defaults: { 'expires' => '@12h34m' }, - nginx_mailhosts: { 'smtp.test2.local' => { 'auth_http' => 'server2.example/cgi-bin/auth', 'protocol' => 'smtp', 'listen_port' => 587 } }, - nginx_mailhosts_defaults: { 'listen_options' => 'default_server_smtp' }, - nginx_streamhosts: { 'streamhost1' => { 'proxy' => 'streamproxy' } } - } - end - - describe 'with defaults' do - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_class('nginx') } - it { is_expected.to contain_class('nginx::config').that_requires('Class[nginx::package]') } - it { is_expected.to contain_class('nginx::service').that_subscribes_to('Class[nginx::package]') } - it { is_expected.to contain_class('nginx::service').that_subscribes_to('Class[nginx::config]') } - it { is_expected.to contain_nginx__resource__upstream('upstream1') } - it { is_expected.to contain_nginx__resource__server('test2.local') } - it { is_expected.to contain_nginx__resource__server('test2.local').with_listen_options('default_server') } - it { is_expected.to contain_nginx__resource__location('test2.local') } - it { is_expected.to contain_nginx__resource__location('test2.local').with_expires('@12h34m') } - it { is_expected.to contain_nginx__resource__mailhost('smtp.test2.local') } - it { is_expected.to contain_nginx__resource__mailhost('smtp.test2.local').with_listen_options('default_server_smtp') } - it { is_expected.to contain_nginx__resource__streamhost('streamhost1').with_proxy('streamproxy') } - end - - context 'nginx::package' do - shared_examples 'redhat' do |operatingsystem| - let(:facts) { { operatingsystem: operatingsystem, osfamily: 'RedHat', operatingsystemmajrelease: '6' } } - context 'using defaults' do - it { is_expected.to contain_package('nginx') } - it do - is_expected.to contain_yumrepo('nginx-release').with( - 'baseurl' => "http://nginx.org/packages/#{operatingsystem == 'CentOS' ? 'centos' : 'rhel'}/6/$basearch/", - 'descr' => 'nginx repo', - 'enabled' => '1', - 'gpgcheck' => '1', - 'priority' => '1', - 'gpgkey' => 'http://nginx.org/keys/nginx_signing.key' - ) - end - it do - is_expected.to contain_yumrepo('passenger').with( - 'ensure' => 'absent' - ) - end - it { is_expected.to contain_yumrepo('nginx-release').that_comes_before('Package[nginx]') } - it { is_expected.to contain_yumrepo('passenger').that_comes_before('Package[nginx]') } - end - - context 'package_source => nginx-mainline' do - let(:params) { { package_source: 'nginx-mainline' } } - it do - is_expected.to contain_yumrepo('nginx-release').with( - 'baseurl' => "http://nginx.org/packages/mainline/#{operatingsystem == 'CentOS' ? 'centos' : 'rhel'}/6/$basearch/" - ) - end - it do - is_expected.to contain_yumrepo('passenger').with( - 'ensure' => 'absent' - ) - end - it { is_expected.to contain_yumrepo('nginx-release').that_comes_before('Package[nginx]') } - it { is_expected.to contain_yumrepo('passenger').that_comes_before('Package[nginx]') } - end - - context 'package_source => passenger' do - let(:params) { { package_source: 'passenger' } } - it do - is_expected.to contain_yumrepo('passenger').with( - 'baseurl' => 'https://oss-binaries.phusionpassenger.com/yum/passenger/el/6/$basearch', - 'gpgcheck' => '0', - 'repo_gpgcheck' => '1', - 'gpgkey' => 'https://packagecloud.io/gpg.key' - ) - end - it do - is_expected.to contain_yumrepo('nginx-release').with( - 'ensure' => 'absent' - ) - end - it { is_expected.to contain_yumrepo('passenger').that_comes_before('Package[nginx]') } - it { is_expected.to contain_yumrepo('nginx-release').that_comes_before('Package[nginx]') } - it { is_expected.to contain_package('passenger').with('ensure' => 'present') } - end - - describe 'installs the requested passenger package version' do - let(:params) { { package_source: 'passenger', passenger_package_ensure: '4.1.0-1.el9' } } - - it 'installs specified version exactly' do - is_expected.to contain_package('passenger').with('ensure' => '4.1.0-1.el9') - end - end - - context 'manage_repo => false' do - let(:facts) { { operatingsystem: operatingsystem, osfamily: 'RedHat', operatingsystemmajrelease: '7' } } - let(:params) { { manage_repo: false } } - it { is_expected.to contain_package('nginx') } - it { is_expected.not_to contain_yumrepo('nginx-release') } - end - - context 'RedHat / CentOS 5 with package_source => passenger' do - let(:facts) { { operatingsystem: operatingsystem, osfamily: 'RedHat', operatingsystemmajrelease: '5' } } - - let(:params) { { package_source: 'passenger', manage_repo: true } } - it 'we fail' do - expect { catalogue }.to raise_error(Puppet::Error, %r{is unsupported with \$package_source}) - end - end - - describe 'installs the requested package version' do - let(:facts) { { operatingsystem: 'redhat', osfamily: 'redhat', operatingsystemmajrelease: '7' } } - let(:params) { { package_ensure: '3.0.0' } } - - it 'installs 3.0.0 exactly' do - is_expected.to contain_package('nginx').with('ensure' => '3.0.0') - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts end - end - shared_examples 'debian' do |operatingsystem, lsbdistcodename, lsbdistid, operatingsystemmajrelease| - let(:facts) do + let :params do { - operatingsystem: operatingsystem, - operatingsystemmajrelease: operatingsystemmajrelease, - osfamily: 'Debian', - lsbdistcodename: lsbdistcodename, - lsbdistid: lsbdistid + nginx_upstreams: { 'upstream1' => { 'members' => ['localhost:3000'] } }, + nginx_servers: { 'test2.local' => { 'www_root' => '/' } }, + nginx_servers_defaults: { 'listen_options' => 'default_server' }, + nginx_locations: { 'test2.local' => { 'server' => 'test2.local', 'www_root' => '/' } }, + nginx_locations_defaults: { 'expires' => '@12h34m' }, + nginx_mailhosts: { 'smtp.test2.local' => { 'auth_http' => 'server2.example/cgi-bin/auth', 'protocol' => 'smtp', 'listen_port' => 587 } }, + nginx_mailhosts_defaults: { 'listen_options' => 'default_server_smtp' }, + nginx_streamhosts: { 'streamhost1' => { 'proxy' => 'streamproxy' } } } end - context 'using defaults' do - it { is_expected.to contain_package('nginx') } - it { is_expected.not_to contain_package('passenger') } - it do - is_expected.to contain_apt__source('nginx').with( - 'location' => "https://nginx.org/packages/#{operatingsystem.downcase}", - 'repos' => 'nginx', - 'key' => { 'id' => '573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62' } - ) - end - end - - context 'package_source => nginx-mainline' do - let(:params) { { package_source: 'nginx-mainline' } } - it do - is_expected.to contain_apt__source('nginx').with( - 'location' => "https://nginx.org/packages/mainline/#{operatingsystem.downcase}" - ) - end - end - - context "package_source => 'passenger'" do - let(:params) { { package_source: 'passenger' } } - it { is_expected.to contain_package('nginx') } - it { is_expected.to contain_package('passenger') } - it do - is_expected.to contain_apt__source('nginx').with( - 'location' => 'https://oss-binaries.phusionpassenger.com/apt/passenger', - 'repos' => 'main', - 'key' => { 'id' => '16378A33A6EF16762922526E561F9B9CAC40B2F7' } - ) - end + describe 'with defaults' do + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('nginx') } + it { is_expected.to contain_class('nginx::config').that_requires('Class[nginx::package]') } + it { is_expected.to contain_class('nginx::service').that_subscribes_to('Class[nginx::package]') } + it { is_expected.to contain_class('nginx::service').that_subscribes_to('Class[nginx::config]') } + it { is_expected.to contain_nginx__resource__upstream('upstream1') } + it { is_expected.to contain_nginx__resource__server('test2.local') } + it { is_expected.to contain_nginx__resource__server('test2.local').with_listen_options('default_server') } + it { is_expected.to contain_nginx__resource__location('test2.local') } + it { is_expected.to contain_nginx__resource__location('test2.local').with_expires('@12h34m') } + it { is_expected.to contain_nginx__resource__mailhost('smtp.test2.local') } + it { is_expected.to contain_nginx__resource__mailhost('smtp.test2.local').with_listen_options('default_server_smtp') } + it { is_expected.to contain_nginx__resource__streamhost('streamhost1').with_proxy('streamproxy') } end - context 'manage_repo => false' do - let(:params) { { manage_repo: false } } - it { is_expected.to contain_package('nginx') } - it { is_expected.not_to contain_apt__source('nginx') } - it { is_expected.not_to contain_package('passenger') } - end - end - - context 'redhat' do - it_behaves_like 'redhat', 'CentOS' - it_behaves_like 'redhat', 'RedHat' - end - - context 'debian' do - it_behaves_like 'debian', 'Debian', 'wheezy', 'Debian', '6' - it_behaves_like 'debian', 'Ubuntu', 'precise', 'Ubuntu', '12.04' - end - - context 'other' do - let(:facts) { { operatingsystem: 'xxx', osfamily: 'linux' } } - it { is_expected.to contain_package('nginx') } - end - end - - context 'nginx::service' do - let :params do - { - service_ensure: 'running', - service_name: 'nginx', - service_manage: true - } - end - - context 'using default parameters' do - it do - is_expected.to contain_service('nginx').with( - ensure: 'running', - enable: true, - hasstatus: true, - hasrestart: true - ) - end + context 'nginx::package' do + case facts[:osfamily] + when 'RedHat' + context 'using defaults' do + it { is_expected.to contain_package('nginx') } + it do + is_expected.to contain_yumrepo('nginx-release').with( + 'baseurl' => "http://nginx.org/packages/#{facts[:operatingsystem] == 'CentOS' ? 'centos' : 'rhel'}/#{facts[:operatingsystemmajrelease]}/$basearch/", + 'descr' => 'nginx repo', + 'enabled' => '1', + 'gpgcheck' => '1', + 'priority' => '1', + 'gpgkey' => 'http://nginx.org/keys/nginx_signing.key' + ) + end + it do + is_expected.to contain_yumrepo('passenger').with( + 'ensure' => 'absent' + ) + end + it { is_expected.to contain_yumrepo('nginx-release').that_comes_before('Package[nginx]') } + it { is_expected.to contain_yumrepo('passenger').that_comes_before('Package[nginx]') } + end - it { is_expected.to contain_service('nginx').without_restart } - end + context 'package_source => nginx-mainline' do + let(:params) { { package_source: 'nginx-mainline' } } + it do + is_expected.to contain_yumrepo('nginx-release').with( + 'baseurl' => "http://nginx.org/packages/mainline/#{facts[:operatingsystem] == 'CentOS' ? 'centos' : 'rhel'}/#{facts[:operatingsystemmajrelease]}/$basearch/" + ) + end + it do + is_expected.to contain_yumrepo('passenger').with( + 'ensure' => 'absent' + ) + end + it { is_expected.to contain_yumrepo('nginx-release').that_comes_before('Package[nginx]') } + it { is_expected.to contain_yumrepo('passenger').that_comes_before('Package[nginx]') } + end - context "when service_restart => 'a restart command'" do - let :params do - { - service_restart: 'a restart command', - service_ensure: 'running', - service_name: 'nginx' - } - end - it { is_expected.to contain_service('nginx').with_restart('a restart command') } - end + context 'package_source => passenger' do + let(:params) { { package_source: 'passenger' } } + it do + is_expected.to contain_yumrepo('passenger').with( + 'baseurl' => "https://oss-binaries.phusionpassenger.com/yum/passenger/el/#{facts[:operatingsystemmajrelease]}/$basearch", + 'gpgcheck' => '0', + 'repo_gpgcheck' => '1', + 'gpgkey' => 'https://packagecloud.io/gpg.key' + ) + end + it do + is_expected.to contain_yumrepo('nginx-release').with( + 'ensure' => 'absent' + ) + end + it { is_expected.to contain_yumrepo('passenger').that_comes_before('Package[nginx]') } + it { is_expected.to contain_yumrepo('nginx-release').that_comes_before('Package[nginx]') } + it { is_expected.to contain_package('passenger').with('ensure' => 'present') } + end - describe "when service_name => 'nginx14" do - let :params do - { - service_name: 'nginx14' - } - end - it { is_expected.to contain_service('nginx').with_name('nginx14') } - end + describe 'installs the requested passenger package version' do + let(:params) { { package_source: 'passenger', passenger_package_ensure: '4.1.0-1.el9' } } - describe 'when service_manage => false' do - let :params do - { - service_manage: false - } - end - it { is_expected.not_to contain_service('nginx') } - end - end + it 'installs specified version exactly' do + is_expected.to contain_package('passenger').with('ensure' => '4.1.0-1.el9') + end + end - # nginx::config - context 'nginx::config' do - context 'with defaults' do - it do - is_expected.to contain_file('/etc/nginx').only_with( - path: '/etc/nginx', - ensure: 'directory', - owner: 'root', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/etc/nginx/conf.d').only_with( - path: '/etc/nginx/conf.d', - ensure: 'directory', - owner: 'root', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/etc/nginx/conf.stream.d').only_with( - path: '/etc/nginx/conf.stream.d', - ensure: 'directory', - owner: 'root', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/etc/nginx/conf.mail.d').only_with( - path: '/etc/nginx/conf.mail.d', - ensure: 'directory', - owner: 'root', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/var/nginx').with( - ensure: 'directory', - owner: 'root', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/var/nginx/client_body_temp').with( - ensure: 'directory', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/var/nginx/proxy_temp').with( - ensure: 'directory', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/etc/nginx/nginx.conf').with( - ensure: 'file', - owner: 'root', - group: 'root', - mode: '0644' - ) - end - it do - is_expected.to contain_file('/tmp/nginx.d').with( - ensure: 'absent', - purge: true, - recurse: true - ) - end - it do - is_expected.to contain_file('/tmp/nginx.mail.d').with( - ensure: 'absent', - purge: true, - recurse: true - ) - end - it { is_expected.to contain_file('/var/nginx/client_body_temp').with(owner: 'nginx') } - it { is_expected.to contain_file('/var/nginx/proxy_temp').with(owner: 'nginx') } - it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_content %r{^user nginx;} } + context 'manage_repo => false' do + let(:params) { { manage_repo: false } } + it { is_expected.to contain_package('nginx') } + it { is_expected.not_to contain_yumrepo('nginx-release') } + end - it do - is_expected.to contain_file('/var/log/nginx').with( - ensure: 'directory', - owner: 'nginx', - group: 'nginx', - mode: '0750' - ) - end + describe 'installs the requested package version' do + let(:params) { { package_ensure: '3.0.0' } } - describe 'nginx.conf template content' do - [ - { - title: 'should not set user', - attr: 'super_user', - value: false, - notmatch: %r{user} - }, - { - title: 'should set user', - attr: 'daemon_user', - value: 'test-user', - match: 'user test-user;' - }, - { - title: 'should not set daemon', - attr: 'daemon', - value: :undef, - notmatch: %r{^\s*daemon\s+} - }, - { - title: 'should set daemon on', - attr: 'daemon', - value: 'on', - match: %r{^daemon\s+on;$} - }, - { - title: 'should set daemon off', - attr: 'daemon', - value: 'off', - match: %r{^daemon\s+off;$} - }, - { - title: 'should set worker_processes', - attr: 'worker_processes', - value: 4, - match: 'worker_processes 4;' - }, - { - title: 'should set worker_processes', - attr: 'worker_processes', - value: 'auto', - match: 'worker_processes auto;' - }, - { - title: 'should set worker_rlimit_nofile', - attr: 'worker_rlimit_nofile', - value: 10_000, - match: 'worker_rlimit_nofile 10000;' - }, - { - title: 'should set error_log', - attr: 'nginx_error_log', - value: '/path/to/error.log', - match: 'error_log /path/to/error.log error;' - }, - { - title: 'should set multiple error_logs', - attr: 'nginx_error_log', - value: ['/path/to/error.log', 'syslog:server=localhost'], - match: [ - 'error_log /path/to/error.log error;', - 'error_log syslog:server=localhost error;' - ] - }, - { - title: 'should set error_log severity level', - attr: 'nginx_error_log_severity', - value: 'warn', - match: 'error_log /var/log/nginx/error.log warn;' - }, - { - title: 'should set pid', - attr: 'pid', - value: '/path/to/pid', - match: 'pid /path/to/pid;' - }, - { - title: 'should not set pid', - attr: 'pid', - value: false, - notmatch: %r{pid} - }, - { - title: 'should set accept_mutex on', - attr: 'accept_mutex', - value: 'on', - match: ' accept_mutex on;' - }, - { - title: 'should set accept_mutex off', - attr: 'accept_mutex', - value: 'off', - match: ' accept_mutex off;' - }, - { - title: 'should set accept_mutex_delay', - attr: 'accept_mutex_delay', - value: '500s', - match: ' accept_mutex_delay 500s;' - }, - { - title: 'should set worker_connections', - attr: 'worker_connections', - value: 100, - match: ' worker_connections 100;' - }, - { - title: 'should set log formats', - attr: 'log_format', - value: { - 'format1' => 'FORMAT1', - 'format2' => 'FORMAT2' - }, - match: [ - ' log_format format1 \'FORMAT1\';', - ' log_format format2 \'FORMAT2\';' - ] - }, - { - title: 'should not set log formats', - attr: 'log_format', - value: {}, - notmatch: %r{log_format} - }, - { - title: 'should set multi_accept', - attr: 'multi_accept', - value: 'on', - match: %r{\s*multi_accept\s+on;} - }, - { - title: 'should not set multi_accept', - attr: 'multi_accept', - value: 'off', - notmatch: %r{multi_accept} - }, - { - title: 'should set events_use', - attr: 'events_use', - value: 'eventport', - match: %r{\s*use\s+eventport;} - }, - { - title: 'should set access_log', - attr: 'http_access_log', - value: '/path/to/access.log', - match: ' access_log /path/to/access.log;' - }, - { - title: 'should set multiple access_logs', - attr: 'http_access_log', - value: ['/path/to/access.log', 'syslog:server=localhost'], - match: [ - ' access_log /path/to/access.log;', - ' access_log syslog:server=localhost;' - ] - }, - { - title: 'should set custom log format', - attr: 'http_format_log', - value: 'mycustomformat', - match: ' access_log /var/log/nginx/access.log mycustomformat;' - }, - { - title: 'should set sendfile', - attr: 'sendfile', - value: 'on', - match: ' sendfile on;' - }, - { - title: 'should not set sendfile', - attr: 'sendfile', - value: false, - notmatch: %r{sendfile} - }, - { - title: 'should set server_tokens', - attr: 'server_tokens', - value: 'on', - match: ' server_tokens on;' - }, - { - title: 'should set types_hash_max_size', - attr: 'types_hash_max_size', - value: 10, - match: ' types_hash_max_size 10;' - }, - { - title: 'should set types_hash_bucket_size', - attr: 'types_hash_bucket_size', - value: 10, - match: ' types_hash_bucket_size 10;' - }, - { - title: 'should set server_names_hash_bucket_size', - attr: 'names_hash_bucket_size', - value: 10, - match: ' server_names_hash_bucket_size 10;' - }, - { - title: 'should set server_names_hash_max_size', - attr: 'names_hash_max_size', - value: 10, - match: ' server_names_hash_max_size 10;' - }, - { - title: 'should set keepalive_timeout', - attr: 'keepalive_timeout', - value: '123', - match: ' keepalive_timeout 123;' - }, - { - title: 'should set keepalive_requests', - attr: 'keepalive_requests', - value: '345', - match: ' keepalive_requests 345;' - }, - { - title: 'should set client_body_timeout', - attr: 'client_body_timeout', - value: '888', - match: ' client_body_timeout 888;' - }, - { - title: 'should set send_timeout', - attr: 'send_timeout', - value: '963', - match: ' send_timeout 963;' - }, - { - title: 'should set lingering_timeout', - attr: 'lingering_timeout', - value: '385', - match: ' lingering_timeout 385;' - }, - { - title: 'should set tcp_nodelay', - attr: 'http_tcp_nodelay', - value: 'on', - match: ' tcp_nodelay on;' - }, - { - title: 'should set tcp_nopush', - attr: 'http_tcp_nopush', - value: 'on', - match: ' tcp_nopush on;' - }, - { - title: 'should set gzip', - attr: 'gzip', - value: 'on', - match: ' gzip on;' - }, - { - title: 'should not set gzip', - attr: 'gzip', - value: 'off', - notmatch: %r{gzip} - }, - { - title: 'should set gzip_buffers', - attr: 'gzip_buffers', - value: '32 4k', - match: ' gzip_buffers 32 4k;' - }, - { - title: 'should set gzip_comp_level', - attr: 'gzip_comp_level', - value: 5, - match: ' gzip_comp_level 5;' - }, - { - title: 'should set gzip_disable', - attr: 'gzip_disable', - value: 'MSIE [1-6]\.(?!.*SV1)', - match: ' gzip_disable MSIE [1-6]\.(?!.*SV1);' - }, - { - title: 'should set gzip_min_length', - attr: 'gzip_min_length', - value: '10', - match: ' gzip_min_length 10;' - }, - { - title: 'should set gzip_http_version', - attr: 'gzip_http_version', - value: '1.0', - match: ' gzip_http_version 1.0;' - }, - { - title: 'should set gzip_proxied', - attr: 'gzip_proxied', - value: 'any', - match: ' gzip_proxied any;' - }, - { - title: 'should set gzip_types (array)', - attr: 'gzip_types', - value: ['text/plain', 'text/html'], - match: ' gzip_types text/plain text/html;' - }, - { - title: 'should set gzip_types (string)', - attr: 'gzip_types', - value: ['text/plain'], - match: ' gzip_types text/plain;' - }, - { - title: 'should set gzip_vary', - attr: 'gzip_vary', - value: 'on', - match: ' gzip_vary on;' - }, - { - title: 'should set proxy_cache_path', - attr: 'proxy_cache_path', - value: '/path/to/proxy.cache', - match: %r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;} - }, - { - title: 'should set fastcgi_cache_path', - attr: 'fastcgi_cache_path', - value: '/path/to/proxy.cache', - match: %r{\s*fastcgi_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d3:100m max_size=500m inactive=20m;} - }, - { - title: 'should set fastcgi_cache_use_stale', - attr: 'fastcgi_cache_use_stale', - value: 'invalid_header', - match: ' fastcgi_cache_use_stale invalid_header;' - }, - { - title: 'should contain ordered appended directives from hash', - attr: 'http_cfg_prepend', - value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1;', - ' test2 test value 2;' - ] - }, - { - title: 'should contain duplicate appended directives from list of hashes', - attr: 'http_cfg_prepend', - value: [['allow', 'test value 1'], ['allow', 'test value 2']], - match: [ - ' allow test value 1;', - ' allow test value 2;' - ] - }, - { - title: 'should contain duplicate appended directives from array values', - attr: 'http_cfg_prepend', - value: { 'test1' => ['test value 1', 'test value 2', 'test value 3'] }, - match: [ - ' test1 test value 1;', - ' test1 test value 2;' - ] - }, - { - title: 'should contain ordered appended directives from hash', - attr: 'http_cfg_append', - value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1;', - ' test2 test value 2;' - ] - }, - { - title: 'should contain duplicate appended directives from list of hashes', - attr: 'http_cfg_append', - value: [['allow', 'test value 1'], ['allow', 'test value 2']], - match: [ - ' allow test value 1;', - ' allow test value 2;' - ] - }, - { - title: 'should contain duplicate appended directives from array values', - attr: 'http_cfg_append', - value: { 'test1' => ['test value 1', 'test value 2', 'test value 3'] }, - match: [ - ' test1 test value 1;', - ' test1 test value 2;' - ] - }, - { - title: 'should contain ordered appended directives from hash', - attr: 'nginx_cfg_prepend', - value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, - match: [ - 'allow test value 3;', - 'test1 test value 1;', - 'test2 test value 2;' - ] - }, - { - title: 'should contain duplicate appended directives from list of hashes', - attr: 'nginx_cfg_prepend', - value: [['allow', 'test value 1'], ['allow', 'test value 2']], - match: [ - 'allow test value 1;', - 'allow test value 2;' - ] - }, - { - title: 'should contain duplicate appended directives from array values', - attr: 'nginx_cfg_prepend', - value: { 'test1' => ['test value 1', 'test value 2', 'test value 3'] }, - match: [ - 'test1 test value 1;', - 'test1 test value 2;', - 'test1 test value 3;' - ] - }, - { - title: 'should set pid', - attr: 'pid', - value: '/path/to/pid', - match: 'pid /path/to/pid;' - }, - { - title: 'should set mail', - attr: 'mail', - value: true, - match: 'mail {' - }, - { - title: 'should not set mail', - attr: 'mail', - value: false, - notmatch: %r{mail} - }, - { - title: 'should set proxy_buffers', - attr: 'proxy_buffers', - value: '50 5k', - match: ' proxy_buffers 50 5k;' - }, - { - title: 'should set proxy_buffer_size', - attr: 'proxy_buffer_size', - value: '2k', - match: ' proxy_buffer_size 2k;' - }, - { - title: 'should set proxy_http_version', - attr: 'proxy_http_version', - value: '1.1', - match: ' proxy_http_version 1.1;' - }, - { - title: 'should not set proxy_http_version', - attr: 'proxy_http_version', - value: nil, - notmatch: 'proxy_http_version' - }, - { - title: 'should contain ordered appended proxy_set_header directives', - attr: 'proxy_set_header', - value: %w(header1 header2), - match: [ - ' proxy_set_header header1;', - ' proxy_set_header header2;' - ] - }, - { - title: 'should contain ordered appended proxy_hide_header directives', - attr: 'proxy_hide_header', - value: %w(header1 header2), - match: [ - ' proxy_hide_header header1;', - ' proxy_hide_header header2;' - ] - }, - { - title: 'should contain ordered appended proxy_pass_header directives', - attr: 'proxy_pass_header', - value: %w(header1 header2), - match: [ - ' proxy_pass_header header1;', - ' proxy_pass_header header2;' - ] - }, - { - title: 'should set client_body_temp_path', - attr: 'client_body_temp_path', - value: '/path/to/body_temp', - match: ' client_body_temp_path /path/to/body_temp;' - }, - { - title: 'should set proxy_temp_path', - attr: 'proxy_temp_path', - value: '/path/to/proxy_temp', - match: ' proxy_temp_path /path/to/proxy_temp;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { { param[:attr].to_sym => param[:value] } } + it 'installs 3.0.0 exactly' do + is_expected.to contain_package('nginx').with('ensure' => '3.0.0') + end + end - it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_mode('0644') } - it param[:title] do - matches = Array(param[:match]) + when 'Debian' + context 'using defaults' do + it { is_expected.to contain_package('nginx') } + it { is_expected.not_to contain_package('passenger') } + it do + is_expected.to contain_apt__source('nginx').with( + 'location' => "https://nginx.org/packages/#{facts[:operatingsystem].downcase}", + 'repos' => 'nginx', + 'key' => { 'id' => '573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62' } + ) + end + end - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_file('/etc/nginx/nginx.conf').with_content(item) } - else - lines = catalogue.resource('file', '/etc/nginx/nginx.conf').send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - end + context 'package_source => nginx-mainline' do + let(:params) { { package_source: 'nginx-mainline' } } + it do + is_expected.to contain_apt__source('nginx').with( + 'location' => "https://nginx.org/packages/mainline/#{facts[:operatingsystem].downcase}" + ) + end + end - Array(param[:notmatch]).each do |item| - is_expected.to contain_file('/etc/nginx/nginx.conf').without_content(item) - end + context "package_source => 'passenger'" do + let(:params) { { package_source: 'passenger' } } + it { is_expected.to contain_package('nginx') } + it { is_expected.to contain_package('passenger') } + it do + is_expected.to contain_apt__source('nginx').with( + 'location' => 'https://oss-binaries.phusionpassenger.com/apt/passenger', + 'repos' => 'main', + 'key' => { 'id' => '16378A33A6EF16762922526E561F9B9CAC40B2F7' } + ) end end + + context 'manage_repo => false' do + let(:params) { { manage_repo: false } } + it { is_expected.to contain_package('nginx') } + it { is_expected.not_to contain_apt__source('nginx') } + it { is_expected.not_to contain_package('passenger') } + end + else + it { is_expected.to contain_package('nginx') } end end - context 'when proxy_cache_path is /path/to/proxy.cache and loader_files is 1000' do - let(:params) do + context 'nginx::service' do + let :params do { - conf_dir: '/path/to/nginx', - proxy_cache_path: '/path/to/proxy.cache', - proxy_cache_loader_files: 1000 + service_ensure: 'running', + service_name: 'nginx', + service_manage: true } end - it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m loader_files=1000;}) } - end - - context 'when proxy_cache_path is /path/to/nginx and loader_sleep is 50ms' do - let(:params) { { conf_dir: '/path/to/nginx', proxy_cache_path: '/path/to/proxy.cache', proxy_cache_loader_sleep: '50ms' } } - it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m loader_sleep=50ms;}) } - end - - context 'when proxy_cache_path is /path/to/nginx and loader_threshold is 300ms' do - let(:params) { { conf_dir: '/path/to/nginx', proxy_cache_path: '/path/to/proxy.cache', proxy_cache_loader_threshold: '300ms' } } - it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m loader_threshold=300ms;}) } - end - - context 'when conf_dir is /path/to/nginx' do - let(:params) { { conf_dir: '/path/to/nginx' } } - it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/mime\.types;}) } - it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/conf\.d/\*\.conf;}) } - it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/sites-enabled/\*;}) } - end - - context 'when confd_purge true' do - let(:params) { { confd_purge: true } } - it do - is_expected.to contain_file('/etc/nginx/conf.d').with( - purge: true, - recurse: true - ) - end - end - context 'when confd_purge false' do - let(:params) { { confd_purge: false } } - it do - is_expected.to contain_file('/etc/nginx/conf.d').without( - %w( - ignore - purge - recurse + context 'using default parameters' do + it do + is_expected.to contain_service('nginx').with( + ensure: 'running', + enable: true, + hasstatus: true, + hasrestart: true ) - ) - end - end + end - context 'when confd_only true' do - let(:params) { { confd_only: true } } - it do - is_expected.to contain_file('/etc/nginx/conf.d').without( - %w( - ignore - purge - recurse - ) - ) - is_expected.not_to contain_file('/etc/nginx/sites-available') - is_expected.not_to contain_file('/etc/nginx/sites-enabled') - is_expected.to contain_file('/etc/nginx/nginx.conf').without_content(%r{include /path/to/nginx/sites-enabled/\*;}) - is_expected.not_to contain_file('/etc/nginx/streams-available') - is_expected.not_to contain_file('/etc/nginx/streams-enabled') + it { is_expected.to contain_service('nginx').without_restart } end - end - context 'when server_purge true' do - let(:params) { { server_purge: true } } - it do - is_expected.to contain_file('/etc/nginx/sites-available').with( - purge: true, - recurse: true - ) - end - it do - is_expected.to contain_file('/etc/nginx/sites-enabled').with( - purge: true, - recurse: true - ) + context "when service_restart => 'a restart command'" do + let :params do + { + service_restart: 'a restart command', + service_ensure: 'running', + service_name: 'nginx' + } + end + it { is_expected.to contain_service('nginx').with_restart('a restart command') } end - end - context 'when confd_purge true, server_purge true, and confd_only true' do - let(:params) do - { - confd_purge: true, - confd_only: true, - server_purge: true - } - end - it do - is_expected.to contain_file('/etc/nginx/conf.d').with( - purge: true, - recurse: true - ) + describe "when service_name => 'nginx14" do + let :params do + { + service_name: 'nginx14' + } + end + it { is_expected.to contain_service('nginx').with_name('nginx14') } end - it do - is_expected.to contain_file('/etc/nginx/conf.stream.d').with( - purge: true, - recurse: true - ) + + describe 'when service_manage => false' do + let :params do + { + service_manage: false + } + end + it { is_expected.not_to contain_service('nginx') } end end - context 'when confd_purge true, server_purge default (false), confd_only true' do - let(:params) do - { - confd_purge: true, - confd_only: true - } - end - it do - is_expected.to contain_file('/etc/nginx/conf.d').without( - %w( - purge + # nginx::config + context 'nginx::config' do + context 'with defaults' do + it do + is_expected.to contain_file('/etc/nginx').only_with( + path: '/etc/nginx', + ensure: 'directory', + owner: 'root', + group: 'root', + mode: '0644' ) - ) - end - it do - is_expected.to contain_file('/etc/nginx/conf.stream.d').without( - %w( - purge + end + it do + is_expected.to contain_file('/etc/nginx/conf.d').only_with( + path: '/etc/nginx/conf.d', + ensure: 'directory', + owner: 'root', + group: 'root', + mode: '0644' ) - ) - end - end - - context 'when server_purge false' do - let(:params) { { server_purge: false } } - it do - is_expected.to contain_file('/etc/nginx/sites-available').without( - %w( - ignore - purge - recurse + end + it do + is_expected.to contain_file('/etc/nginx/conf.stream.d').only_with( + path: '/etc/nginx/conf.stream.d', + ensure: 'directory', + owner: 'root', + group: 'root', + mode: '0644' ) - ) - end - it do - is_expected.to contain_file('/etc/nginx/sites-enabled').without( - %w( - ignore - purge - recurse + end + it do + is_expected.to contain_file('/etc/nginx/conf.mail.d').only_with( + path: '/etc/nginx/conf.mail.d', + ensure: 'directory', + owner: 'root', + group: 'root', + mode: '0644' ) - ) - end - it do - is_expected.to contain_file('/var/log/nginx').without( - %w( - ignore - purge - recurse + end + it do + is_expected.to contain_file('/var/nginx').with( + ensure: 'directory', + owner: 'root', + group: 'root', + mode: '0644' ) - ) - end - it do - is_expected.to contain_file('/etc/nginx/streams-available').without( - %w( - ignore - purge - recurse + end + it do + is_expected.to contain_file('/var/nginx/client_body_temp').with( + ensure: 'directory', + group: 'root', + mode: '0644' ) - ) - end - it do - is_expected.to contain_file('/etc/nginx/streams-enabled').without( - %w( - ignore - purge - recurse + end + it do + is_expected.to contain_file('/var/nginx/proxy_temp').with( + ensure: 'directory', + group: 'root', + mode: '0644' ) - ) - end - end + end + it do + is_expected.to contain_file('/etc/nginx/nginx.conf').with( + ensure: 'file', + owner: 'root', + group: 'root', + mode: '0644' + ) + end + it do + is_expected.to contain_file('/tmp/nginx.d').with( + ensure: 'absent', + purge: true, + recurse: true + ) + end + it do + is_expected.to contain_file('/tmp/nginx.mail.d').with( + ensure: 'absent', + purge: true, + recurse: true + ) + end + case facts[:osfamily] + when 'RedHat' + it { is_expected.to contain_file('/var/nginx/client_body_temp').with(owner: 'nginx') } + it { is_expected.to contain_file('/var/nginx/proxy_temp').with(owner: 'nginx') } + it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_content %r{^user nginx;} } + it do + is_expected.to contain_file('/var/log/nginx').with( + ensure: 'directory', + owner: 'nginx', + group: 'nginx', + mode: '0750' + ) + end + when 'Debian' + it { is_expected.to contain_file('/var/nginx/client_body_temp').with(owner: 'www-data') } + it { is_expected.to contain_file('/var/nginx/proxy_temp').with(owner: 'www-data') } + it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_content %r{^user www-data;} } + it do + is_expected.to contain_file('/var/log/nginx').with( + ensure: 'directory', + owner: 'www-data', + group: 'adm', + mode: '0750' + ) + end + end - context 'when daemon_user = www-data' do - let(:params) { { daemon_user: 'www-data' } } - it { is_expected.to contain_file('/var/nginx/client_body_temp').with(owner: 'www-data') } - it { is_expected.to contain_file('/var/nginx/proxy_temp').with(owner: 'www-data') } - it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_content %r{^user www-data;} } - end + describe 'nginx.conf template content' do + [ + { + title: 'should not set user', + attr: 'super_user', + value: false, + notmatch: %r{user} + }, + { + title: 'should set user', + attr: 'daemon_user', + value: 'test-user', + match: 'user test-user;' + }, + { + title: 'should not set daemon', + attr: 'daemon', + value: :undef, + notmatch: %r{^\s*daemon\s+} + }, + { + title: 'should set daemon on', + attr: 'daemon', + value: 'on', + match: %r{^daemon\s+on;$} + }, + { + title: 'should set daemon off', + attr: 'daemon', + value: 'off', + match: %r{^daemon\s+off;$} + }, + { + title: 'should set worker_processes', + attr: 'worker_processes', + value: 4, + match: 'worker_processes 4;' + }, + { + title: 'should set worker_processes', + attr: 'worker_processes', + value: 'auto', + match: 'worker_processes auto;' + }, + { + title: 'should set worker_rlimit_nofile', + attr: 'worker_rlimit_nofile', + value: 10_000, + match: 'worker_rlimit_nofile 10000;' + }, + { + title: 'should set error_log', + attr: 'nginx_error_log', + value: '/path/to/error.log', + match: 'error_log /path/to/error.log error;' + }, + { + title: 'should set multiple error_logs', + attr: 'nginx_error_log', + value: ['/path/to/error.log', 'syslog:server=localhost'], + match: [ + 'error_log /path/to/error.log error;', + 'error_log syslog:server=localhost error;' + ] + }, + { + title: 'should set error_log severity level', + attr: 'nginx_error_log_severity', + value: 'warn', + match: 'error_log /var/log/nginx/error.log warn;' + }, + { + title: 'should set pid', + attr: 'pid', + value: '/path/to/pid', + match: 'pid /path/to/pid;' + }, + { + title: 'should not set pid', + attr: 'pid', + value: false, + notmatch: %r{pid} + }, + { + title: 'should set accept_mutex on', + attr: 'accept_mutex', + value: 'on', + match: ' accept_mutex on;' + }, + { + title: 'should set accept_mutex off', + attr: 'accept_mutex', + value: 'off', + match: ' accept_mutex off;' + }, + { + title: 'should set accept_mutex_delay', + attr: 'accept_mutex_delay', + value: '500s', + match: ' accept_mutex_delay 500s;' + }, + { + title: 'should set worker_connections', + attr: 'worker_connections', + value: 100, + match: ' worker_connections 100;' + }, + { + title: 'should set log formats', + attr: 'log_format', + value: { + 'format1' => 'FORMAT1', + 'format2' => 'FORMAT2' + }, + match: [ + ' log_format format1 \'FORMAT1\';', + ' log_format format2 \'FORMAT2\';' + ] + }, + { + title: 'should not set log formats', + attr: 'log_format', + value: {}, + notmatch: %r{log_format} + }, + { + title: 'should set multi_accept', + attr: 'multi_accept', + value: 'on', + match: %r{\s*multi_accept\s+on;} + }, + { + title: 'should not set multi_accept', + attr: 'multi_accept', + value: 'off', + notmatch: %r{multi_accept} + }, + { + title: 'should set events_use', + attr: 'events_use', + value: 'eventport', + match: %r{\s*use\s+eventport;} + }, + { + title: 'should set access_log', + attr: 'http_access_log', + value: '/path/to/access.log', + match: ' access_log /path/to/access.log;' + }, + { + title: 'should set multiple access_logs', + attr: 'http_access_log', + value: ['/path/to/access.log', 'syslog:server=localhost'], + match: [ + ' access_log /path/to/access.log;', + ' access_log syslog:server=localhost;' + ] + }, + { + title: 'should set custom log format', + attr: 'http_format_log', + value: 'mycustomformat', + match: ' access_log /var/log/nginx/access.log mycustomformat;' + }, + { + title: 'should set sendfile', + attr: 'sendfile', + value: 'on', + match: ' sendfile on;' + }, + { + title: 'should not set sendfile', + attr: 'sendfile', + value: false, + notmatch: %r{sendfile} + }, + { + title: 'should set server_tokens', + attr: 'server_tokens', + value: 'on', + match: ' server_tokens on;' + }, + { + title: 'should set types_hash_max_size', + attr: 'types_hash_max_size', + value: 10, + match: ' types_hash_max_size 10;' + }, + { + title: 'should set types_hash_bucket_size', + attr: 'types_hash_bucket_size', + value: 10, + match: ' types_hash_bucket_size 10;' + }, + { + title: 'should set server_names_hash_bucket_size', + attr: 'names_hash_bucket_size', + value: 10, + match: ' server_names_hash_bucket_size 10;' + }, + { + title: 'should set server_names_hash_max_size', + attr: 'names_hash_max_size', + value: 10, + match: ' server_names_hash_max_size 10;' + }, + { + title: 'should set keepalive_timeout', + attr: 'keepalive_timeout', + value: '123', + match: ' keepalive_timeout 123;' + }, + { + title: 'should set keepalive_requests', + attr: 'keepalive_requests', + value: '345', + match: ' keepalive_requests 345;' + }, + { + title: 'should set client_body_timeout', + attr: 'client_body_timeout', + value: '888', + match: ' client_body_timeout 888;' + }, + { + title: 'should set send_timeout', + attr: 'send_timeout', + value: '963', + match: ' send_timeout 963;' + }, + { + title: 'should set lingering_timeout', + attr: 'lingering_timeout', + value: '385', + match: ' lingering_timeout 385;' + }, + { + title: 'should set tcp_nodelay', + attr: 'http_tcp_nodelay', + value: 'on', + match: ' tcp_nodelay on;' + }, + { + title: 'should set tcp_nopush', + attr: 'http_tcp_nopush', + value: 'on', + match: ' tcp_nopush on;' + }, + { + title: 'should set gzip', + attr: 'gzip', + value: 'on', + match: ' gzip on;' + }, + { + title: 'should not set gzip', + attr: 'gzip', + value: 'off', + notmatch: %r{gzip} + }, + { + title: 'should set gzip_buffers', + attr: 'gzip_buffers', + value: '32 4k', + match: ' gzip_buffers 32 4k;' + }, + { + title: 'should set gzip_comp_level', + attr: 'gzip_comp_level', + value: 5, + match: ' gzip_comp_level 5;' + }, + { + title: 'should set gzip_disable', + attr: 'gzip_disable', + value: 'MSIE [1-6]\.(?!.*SV1)', + match: ' gzip_disable MSIE [1-6]\.(?!.*SV1);' + }, + { + title: 'should set gzip_min_length', + attr: 'gzip_min_length', + value: '10', + match: ' gzip_min_length 10;' + }, + { + title: 'should set gzip_http_version', + attr: 'gzip_http_version', + value: '1.0', + match: ' gzip_http_version 1.0;' + }, + { + title: 'should set gzip_proxied', + attr: 'gzip_proxied', + value: 'any', + match: ' gzip_proxied any;' + }, + { + title: 'should set gzip_types (array)', + attr: 'gzip_types', + value: ['text/plain', 'text/html'], + match: ' gzip_types text/plain text/html;' + }, + { + title: 'should set gzip_types (string)', + attr: 'gzip_types', + value: ['text/plain'], + match: ' gzip_types text/plain;' + }, + { + title: 'should set gzip_vary', + attr: 'gzip_vary', + value: 'on', + match: ' gzip_vary on;' + }, + { + title: 'should set proxy_cache_path', + attr: 'proxy_cache_path', + value: '/path/to/proxy.cache', + match: %r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m;} + }, + { + title: 'should set fastcgi_cache_path', + attr: 'fastcgi_cache_path', + value: '/path/to/proxy.cache', + match: %r{\s*fastcgi_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d3:100m max_size=500m inactive=20m;} + }, + { + title: 'should set fastcgi_cache_use_stale', + attr: 'fastcgi_cache_use_stale', + value: 'invalid_header', + match: ' fastcgi_cache_use_stale invalid_header;' + }, + { + title: 'should contain ordered appended directives from hash', + attr: 'http_cfg_prepend', + value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from list of hashes', + attr: 'http_cfg_prepend', + value: [['allow', 'test value 1'], ['allow', 'test value 2']], + match: [ + ' allow test value 1;', + ' allow test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from array values', + attr: 'http_cfg_prepend', + value: { 'test1' => ['test value 1', 'test value 2', 'test value 3'] }, + match: [ + ' test1 test value 1;', + ' test1 test value 2;' + ] + }, + { + title: 'should contain ordered appended directives from hash', + attr: 'http_cfg_append', + value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from list of hashes', + attr: 'http_cfg_append', + value: [['allow', 'test value 1'], ['allow', 'test value 2']], + match: [ + ' allow test value 1;', + ' allow test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from array values', + attr: 'http_cfg_append', + value: { 'test1' => ['test value 1', 'test value 2', 'test value 3'] }, + match: [ + ' test1 test value 1;', + ' test1 test value 2;' + ] + }, + { + title: 'should contain ordered appended directives from hash', + attr: 'nginx_cfg_prepend', + value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, + match: [ + 'allow test value 3;', + 'test1 test value 1;', + 'test2 test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from list of hashes', + attr: 'nginx_cfg_prepend', + value: [['allow', 'test value 1'], ['allow', 'test value 2']], + match: [ + 'allow test value 1;', + 'allow test value 2;' + ] + }, + { + title: 'should contain duplicate appended directives from array values', + attr: 'nginx_cfg_prepend', + value: { 'test1' => ['test value 1', 'test value 2', 'test value 3'] }, + match: [ + 'test1 test value 1;', + 'test1 test value 2;', + 'test1 test value 3;' + ] + }, + { + title: 'should set pid', + attr: 'pid', + value: '/path/to/pid', + match: 'pid /path/to/pid;' + }, + { + title: 'should set mail', + attr: 'mail', + value: true, + match: 'mail {' + }, + { + title: 'should not set mail', + attr: 'mail', + value: false, + notmatch: %r{mail} + }, + { + title: 'should set proxy_buffers', + attr: 'proxy_buffers', + value: '50 5k', + match: ' proxy_buffers 50 5k;' + }, + { + title: 'should set proxy_buffer_size', + attr: 'proxy_buffer_size', + value: '2k', + match: ' proxy_buffer_size 2k;' + }, + { + title: 'should set proxy_http_version', + attr: 'proxy_http_version', + value: '1.1', + match: ' proxy_http_version 1.1;' + }, + { + title: 'should not set proxy_http_version', + attr: 'proxy_http_version', + value: nil, + notmatch: 'proxy_http_version' + }, + { + title: 'should contain ordered appended proxy_set_header directives', + attr: 'proxy_set_header', + value: %w(header1 header2), + match: [ + ' proxy_set_header header1;', + ' proxy_set_header header2;' + ] + }, + { + title: 'should contain ordered appended proxy_hide_header directives', + attr: 'proxy_hide_header', + value: %w(header1 header2), + match: [ + ' proxy_hide_header header1;', + ' proxy_hide_header header2;' + ] + }, + { + title: 'should contain ordered appended proxy_pass_header directives', + attr: 'proxy_pass_header', + value: %w(header1 header2), + match: [ + ' proxy_pass_header header1;', + ' proxy_pass_header header2;' + ] + }, + { + title: 'should set client_body_temp_path', + attr: 'client_body_temp_path', + value: '/path/to/body_temp', + match: ' client_body_temp_path /path/to/body_temp;' + }, + { + title: 'should set proxy_temp_path', + attr: 'proxy_temp_path', + value: '/path/to/proxy_temp', + match: ' proxy_temp_path /path/to/proxy_temp;' + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { { param[:attr].to_sym => param[:value] } } - context 'when log_dir is non-default' do - let(:params) { { log_dir: '/foo/bar' } } + it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_mode('0644') } + it param[:title] do + matches = Array(param[:match]) - it { is_expected.to contain_file('/foo/bar').with(ensure: 'directory') } - it do - is_expected.to contain_file('/etc/nginx/nginx.conf').with_content( - %r{access_log /foo/bar/access.log;} - ) - end - it do - is_expected.to contain_file('/etc/nginx/nginx.conf').with_content( - %r{error_log /foo/bar/error.log error;} - ) - end - end + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_file('/etc/nginx/nginx.conf').with_content(item) } + else + lines = catalogue.resource('file', '/etc/nginx/nginx.conf').send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end - context 'when log_mode is non-default' do - let(:params) { { log_mode: '0771' } } + Array(param[:notmatch]).each do |item| + is_expected.to contain_file('/etc/nginx/nginx.conf').without_content(item) + end + end + end + end + end - it { is_expected.to contain_file('/var/log/nginx').with(mode: '0771') } - end - end + context 'when proxy_cache_path is /path/to/proxy.cache and loader_files is 1000' do + let(:params) do + { + conf_dir: '/path/to/nginx', + proxy_cache_path: '/path/to/proxy.cache', + proxy_cache_loader_files: 1000 + } + end + it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m loader_files=1000;}) } + end + + context 'when proxy_cache_path is /path/to/nginx and loader_sleep is 50ms' do + let(:params) { { conf_dir: '/path/to/nginx', proxy_cache_path: '/path/to/proxy.cache', proxy_cache_loader_sleep: '50ms' } } + it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m loader_sleep=50ms;}) } + end + + context 'when proxy_cache_path is /path/to/nginx and loader_threshold is 300ms' do + let(:params) { { conf_dir: '/path/to/nginx', proxy_cache_path: '/path/to/proxy.cache', proxy_cache_loader_threshold: '300ms' } } + it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{\s+proxy_cache_path\s+/path/to/proxy.cache levels=1 keys_zone=d2:100m max_size=500m inactive=20m loader_threshold=300ms;}) } + end + + context 'when conf_dir is /path/to/nginx' do + let(:params) { { conf_dir: '/path/to/nginx' } } + it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/mime\.types;}) } + it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/conf\.d/\*\.conf;}) } + it { is_expected.to contain_file('/path/to/nginx/nginx.conf').with_content(%r{include /path/to/nginx/sites-enabled/\*;}) } + end + + context 'when confd_purge true' do + let(:params) { { confd_purge: true } } + it do + is_expected.to contain_file('/etc/nginx/conf.d').with( + purge: true, + recurse: true + ) + end + end - context 'With RedHat facts' do - let(:facts) { { operatingsystem: 'redhat', osfamily: 'RedHat', operatingsystemmajrelease: '6' } } + context 'when confd_purge false' do + let(:params) { { confd_purge: false } } + it do + is_expected.to contain_file('/etc/nginx/conf.d').without( + %w( + ignore + purge + recurse + ) + ) + end + end + + context 'when confd_only true' do + let(:params) { { confd_only: true } } + it do + is_expected.to contain_file('/etc/nginx/conf.d').without( + %w( + ignore + purge + recurse + ) + ) + is_expected.not_to contain_file('/etc/nginx/sites-available') + is_expected.not_to contain_file('/etc/nginx/sites-enabled') + is_expected.to contain_file('/etc/nginx/nginx.conf').without_content(%r{include /path/to/nginx/sites-enabled/\*;}) + is_expected.not_to contain_file('/etc/nginx/streams-available') + is_expected.not_to contain_file('/etc/nginx/streams-enabled') + end + end + + context 'when server_purge true' do + let(:params) { { server_purge: true } } + it do + is_expected.to contain_file('/etc/nginx/sites-available').with( + purge: true, + recurse: true + ) + end + it do + is_expected.to contain_file('/etc/nginx/sites-enabled').with( + purge: true, + recurse: true + ) + end + end + + context 'when confd_purge true, server_purge true, and confd_only true' do + let(:params) do + { + confd_purge: true, + confd_only: true, + server_purge: true + } + end + it do + is_expected.to contain_file('/etc/nginx/conf.d').with( + purge: true, + recurse: true + ) + end + it do + is_expected.to contain_file('/etc/nginx/conf.stream.d').with( + purge: true, + recurse: true + ) + end + end + + context 'when confd_purge true, server_purge default (false), confd_only true' do + let(:params) do + { + confd_purge: true, + confd_only: true + } + end + it do + is_expected.to contain_file('/etc/nginx/conf.d').without( + %w( + purge + ) + ) + end + it do + is_expected.to contain_file('/etc/nginx/conf.stream.d').without( + %w( + purge + ) + ) + end + end + + context 'when server_purge false' do + let(:params) { { server_purge: false } } + it do + is_expected.to contain_file('/etc/nginx/sites-available').without( + %w( + ignore + purge + recurse + ) + ) + end + it do + is_expected.to contain_file('/etc/nginx/sites-enabled').without( + %w( + ignore + purge + recurse + ) + ) + end + it do + is_expected.to contain_file('/var/log/nginx').without( + %w( + ignore + purge + recurse + ) + ) + end + it do + is_expected.to contain_file('/etc/nginx/streams-available').without( + %w( + ignore + purge + recurse + ) + ) + end + it do + is_expected.to contain_file('/etc/nginx/streams-enabled').without( + %w( + ignore + purge + recurse + ) + ) + end + end + + context 'when daemon_user = www-data' do + let(:params) { { daemon_user: 'www-data' } } + it { is_expected.to contain_file('/var/nginx/client_body_temp').with(owner: 'www-data') } + it { is_expected.to contain_file('/var/nginx/proxy_temp').with(owner: 'www-data') } + it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_content %r{^user www-data;} } + end + + context 'when log_dir is non-default' do + let(:params) { { log_dir: '/foo/bar' } } - it do - is_expected.to contain_file('/var/log/nginx').with( - ensure: 'directory', - owner: 'nginx', - group: 'nginx', - mode: '0750' - ) + it { is_expected.to contain_file('/foo/bar').with(ensure: 'directory') } + it do + is_expected.to contain_file('/etc/nginx/nginx.conf').with_content( + %r{access_log /foo/bar/access.log;} + ) + end + it do + is_expected.to contain_file('/etc/nginx/nginx.conf').with_content( + %r{error_log /foo/bar/error.log error;} + ) + end + end + + context 'when log_mode is non-default' do + let(:params) { { log_mode: '0771' } } + + it { is_expected.to contain_file('/var/log/nginx').with(mode: '0771') } + end + end end end end diff --git a/spec/default_module_facts.yml b/spec/default_module_facts.yml index 5d286a736..6c7303fb0 100644 --- a/spec/default_module_facts.yml +++ b/spec/default_module_facts.yml @@ -1,6 +1,2 @@ --- -osfamily: "Debian" -operatingsystem: "Debian" -operatingsystemmajrelease: "8" -lsbdistid: "Debian" -lsbdistcodename: "jessie" +ipaddress6: '::' diff --git a/spec/defines/resource_geo_spec.rb b/spec/defines/resource_geo_spec.rb index 774918803..cec243e94 100644 --- a/spec/defines/resource_geo_spec.rb +++ b/spec/defines/resource_geo_spec.rb @@ -1,121 +1,128 @@ require 'spec_helper' describe 'nginx::resource::geo' do - let :title do - 'client_network' - end - - let :pre_condition do - [ - 'include ::nginx' - ] - end - - let :default_params do - { - default: 'extra', - networks: { - '172.16.0.0/12' => 'intra', - '192.168.0.0/16' => 'intra', - '10.0.0.0/8' => 'intra' - }, - proxies: ['1.2.3.4', '4.3.2.1'] - } - end - - describe 'os-independent items' do - describe 'basic assumptions' do - let(:params) { default_params } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end + let :title do + 'client_network' + end - it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").that_requires('File[/etc/nginx/conf.d]') } - it do - is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with( - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'ensure' => 'file', - 'content' => %r{geo \$#{title}} - ) + let :pre_condition do + [ + 'include ::nginx' + ] end - end - describe 'geo.conf template content' do - [ - { - title: 'should set address', - attr: 'address', - value: '$remote_addr', - match: 'geo $remote_addr $client_network {' - }, - { - title: 'should set ranges', - attr: 'ranges', - value: true, - match: ' ranges;' - }, - { - title: 'should set default', - attr: 'default', - value: 'extra', - match: [' default extra;'] - }, + let :default_params do { - title: 'should contain ordered network directives', - attr: 'networks', - value: { - '192.168.0.0/16' => 'intra', + default: 'extra', + networks: { '172.16.0.0/12' => 'intra', + '192.168.0.0/16' => 'intra', '10.0.0.0/8' => 'intra' }, - match: [ - ' 10.0.0.0/8 intra;', - ' 172.16.0.0/12 intra;', - ' 192.168.0.0/16 intra;' - ] - }, - { - title: 'should set multiple proxies', - attr: 'proxies', - value: ['1.2.3.4', '4.3.2.1'], - match: [ - ' proxy 1.2.3.4;', - ' proxy 4.3.2.1;' - ] - }, - { - title: 'should set proxy_recursive', - attr: 'proxy_recursive', - value: true, - match: ' proxy_recursive;' - }, - { - title: 'should set delete', - attr: 'delete', - value: '192.168.0.0/16', - match: ' delete 192.168.0.0/16;' + proxies: ['1.2.3.4', '4.3.2.1'] } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + end - it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_mode('0644') } - it param[:title] do - verify_contents(catalogue, "/etc/nginx/conf.d/#{title}-geo.conf", Array(param[:match])) - Array(param[:notmatch]).each do |item| - is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").without_content(item) - end + describe 'os-independent items' do + describe 'basic assumptions' do + let(:params) { default_params } + + it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").that_requires('File[/etc/nginx/conf.d]') } + it do + is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with( + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'ensure' => 'file', + 'content' => %r{geo \$#{title}} + ) end end - end - context 'when ensure => absent' do - let :params do - default_params.merge( - ensure: 'absent' - ) - end + describe 'geo.conf template content' do + [ + { + title: 'should set address', + attr: 'address', + value: '$remote_addr', + match: 'geo $remote_addr $client_network {' + }, + { + title: 'should set ranges', + attr: 'ranges', + value: true, + match: ' ranges;' + }, + { + title: 'should set default', + attr: 'default', + value: 'extra', + match: [' default extra;'] + }, + { + title: 'should contain ordered network directives', + attr: 'networks', + value: { + '192.168.0.0/16' => 'intra', + '172.16.0.0/12' => 'intra', + '10.0.0.0/8' => 'intra' + }, + match: [ + ' 10.0.0.0/8 intra;', + ' 172.16.0.0/12 intra;', + ' 192.168.0.0/16 intra;' + ] + }, + { + title: 'should set multiple proxies', + attr: 'proxies', + value: ['1.2.3.4', '4.3.2.1'], + match: [ + ' proxy 1.2.3.4;', + ' proxy 4.3.2.1;' + ] + }, + { + title: 'should set proxy_recursive', + attr: 'proxy_recursive', + value: true, + match: ' proxy_recursive;' + }, + { + title: 'should set delete', + attr: 'delete', + value: '192.168.0.0/16', + match: ' delete 192.168.0.0/16;' + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_ensure('absent') } + it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_mode('0644') } + it param[:title] do + verify_contents(catalogue, "/etc/nginx/conf.d/#{title}-geo.conf", Array(param[:match])) + Array(param[:notmatch]).each do |item| + is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").without_content(item) + end + end + end + end + + context 'when ensure => absent' do + let :params do + default_params.merge( + ensure: 'absent' + ) + end + + it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_ensure('absent') } + end + end end end end diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index bbb8d7162..66b8a77d3 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -2,975 +2,983 @@ require 'digest/md5' describe 'nginx::resource::location' do - let :title do - 'rspec-test' - end - let :pre_condition do - [ - 'include ::nginx' - ] - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end - describe 'os-independent items' do - describe 'basic assumptions' do - let :params do - { - www_root: '/var/www/rspec', - server: 'server1' - } + let :title do + 'rspec-test' + end + let :pre_condition do + [ + 'include ::nginx' + ] end - it { is_expected.to contain_class('nginx::config') } - it { is_expected.to contain_concat__fragment('server1-500-33c6aa94600c830ad2d316bb4db36724').with_content(%r{location rspec-test}) } - it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } - it { is_expected.not_to contain_concat__fragment('server1-800-rspec-test-ssl') } - it { is_expected.not_to contain_file('/etc/nginx/rspec-test_htpasswd') } - end + describe 'os-independent items' do + describe 'basic assumptions' do + let :params do + { + www_root: '/var/www/rspec', + server: 'server1' + } + end - describe 'server/location_header template content' do - [ - { - title: 'should set the location', - attr: 'location', - value: 'my_location', - match: ' location my_location {' - }, - { - title: 'should not set internal', - attr: 'internal', - value: false, - notmatch: %r{internal;} - }, - { - title: 'should set internal', - attr: 'internal', - value: true, - match: ' internal;' - }, - { - title: 'should not set mp4', - attr: 'mp4', - value: false, - notmatch: %r{mp4;} - }, - { - title: 'should set mp4', - attr: 'mp4', - value: true, - match: ' mp4;' - }, - { - title: 'should not set flv', - attr: 'flv', - value: false, - notmatch: %r{flv;} - }, - { - title: 'should set flv', - attr: 'flv', - value: true, - match: ' flv;' - }, - { - title: 'should set location_satisfy', - attr: 'location_satisfy', - value: 'any', - match: ' satisfy any;' - }, - { - title: 'should set expires', - attr: 'expires', - value: '33d', - match: ' expires 33d;' - }, - { - title: 'should set location_allow', - attr: 'location_allow', - value: %w(127.0.0.1 10.0.0.1), - match: [ - ' allow 127.0.0.1;', - ' allow 10.0.0.1;' - ] - }, - { - title: 'should set location_deny', - attr: 'location_deny', - value: %w(127.0.0.1 10.0.0.1), - match: [ - ' deny 127.0.0.1;', - ' deny 10.0.0.1;' - ] - }, - { - title: 'should contain ordered prepended directives', - attr: 'location_cfg_prepend', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], - 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], - 'subtest2' => '"sub test value2"' } }, - match: [ - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;', - ' test3 subtest1 "sub test value1a";', - ' test3 subtest1 "sub test value1b";', - ' test3 subtest2 "sub test value2";' - ] - }, - { - title: 'should contain custom prepended directives', - attr: 'location_custom_cfg_prepend', - value: { 'test1' => 'bar', 'test2' => %w(foobar barbaz), - 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], - 'subtest2' => '"sub test value2"' } }, - match: [ - %r{^[ ]+test1\s+bar}, - %r{^[ ]+test2\s+foobar}, - %r{^[ ]+test2\s+barbaz}, - %r{^[ ]+test3\s+subtest1 "sub test value1a"}, - %r{^[ ]+test3\s+subtest1 "sub test value1b"}, - %r{^[ ]+test3\s+subtest2 "sub test value2"} - ] - }, - { - title: 'should contain raw_prepend directives', - attr: 'raw_prepend', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - }, - { - title: 'should contain rewrite rules', - attr: 'rewrite_rules', - value: [ - '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last', - '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.ra last', - '^/users/(.*)$ /show?user=$1? last' - ], - match: [ - %r{rewrite \^\(\/download\/\.\*\)\/media\/\(\.\*\)\\\.\.\*\$ \$1\/mp3\/\$2\.mp3 last}, - %r{rewrite \^\(\/download\/\.\*\)\/media\/\(\.\*\)\\\.\.\*\$ \$1\/mp3\/\$2\.ra last}, - %r{rewrite \^\/users\/\(\.\*\)\$ \/show\?user=\$1\? last} - ] - }, - { - title: 'should not set rewrite_rules', - attr: 'rewrite_rules', - value: [], - notmatch: %r{rewrite} - }, - { - title: 'should set auth_basic', - attr: 'auth_basic', - value: 'value', - match: ' auth_basic "value";' - }, - { - title: 'should set auth_basic_user_file', - attr: 'auth_basic_user_file', - value: 'value', - match: ' auth_basic_user_file value;' - }, - { - title: 'should set auth_request', - attr: 'auth_request', - value: 'value', - match: %r{\s+auth_request\s+value;} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:default_params) { { location: 'location', proxy: 'proxy_value', server: 'server1' } } - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + it { is_expected.to contain_class('nginx::config') } + it { is_expected.to contain_concat__fragment('server1-500-33c6aa94600c830ad2d316bb4db36724').with_content(%r{location rspec-test}) } + it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } + it { is_expected.not_to contain_concat__fragment('server1-800-rspec-test-ssl') } + it { is_expected.not_to contain_file('/etc/nginx/rspec-test_htpasswd') } + end + + describe 'server/location_header template content' do + [ + { + title: 'should set the location', + attr: 'location', + value: 'my_location', + match: ' location my_location {' + }, + { + title: 'should not set internal', + attr: 'internal', + value: false, + notmatch: %r{internal;} + }, + { + title: 'should set internal', + attr: 'internal', + value: true, + match: ' internal;' + }, + { + title: 'should not set mp4', + attr: 'mp4', + value: false, + notmatch: %r{mp4;} + }, + { + title: 'should set mp4', + attr: 'mp4', + value: true, + match: ' mp4;' + }, + { + title: 'should not set flv', + attr: 'flv', + value: false, + notmatch: %r{flv;} + }, + { + title: 'should set flv', + attr: 'flv', + value: true, + match: ' flv;' + }, + { + title: 'should set location_satisfy', + attr: 'location_satisfy', + value: 'any', + match: ' satisfy any;' + }, + { + title: 'should set expires', + attr: 'expires', + value: '33d', + match: ' expires 33d;' + }, + { + title: 'should set location_allow', + attr: 'location_allow', + value: %w(127.0.0.1 10.0.0.1), + match: [ + ' allow 127.0.0.1;', + ' allow 10.0.0.1;' + ] + }, + { + title: 'should set location_deny', + attr: 'location_deny', + value: %w(127.0.0.1 10.0.0.1), + match: [ + ' deny 127.0.0.1;', + ' deny 10.0.0.1;' + ] + }, + { + title: 'should contain ordered prepended directives', + attr: 'location_cfg_prepend', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], + 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], + 'subtest2' => '"sub test value2"' } }, + match: [ + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;', + ' test3 subtest1 "sub test value1a";', + ' test3 subtest1 "sub test value1b";', + ' test3 subtest2 "sub test value2";' + ] + }, + { + title: 'should contain custom prepended directives', + attr: 'location_custom_cfg_prepend', + value: { 'test1' => 'bar', 'test2' => %w(foobar barbaz), + 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], + 'subtest2' => '"sub test value2"' } }, + match: [ + %r{^[ ]+test1\s+bar}, + %r{^[ ]+test2\s+foobar}, + %r{^[ ]+test2\s+barbaz}, + %r{^[ ]+test3\s+subtest1 "sub test value1a"}, + %r{^[ ]+test3\s+subtest1 "sub test value1b"}, + %r{^[ ]+test3\s+subtest2 "sub test value2"} + ] + }, + { + title: 'should contain raw_prepend directives', + attr: 'raw_prepend', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + }, + { + title: 'should contain rewrite rules', + attr: 'rewrite_rules', + value: [ + '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last', + '^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.ra last', + '^/users/(.*)$ /show?user=$1? last' + ], + match: [ + %r{rewrite \^\(\/download\/\.\*\)\/media\/\(\.\*\)\\\.\.\*\$ \$1\/mp3\/\$2\.mp3 last}, + %r{rewrite \^\(\/download\/\.\*\)\/media\/\(\.\*\)\\\.\.\*\$ \$1\/mp3\/\$2\.ra last}, + %r{rewrite \^\/users\/\(\.\*\)\$ \/show\?user=\$1\? last} + ] + }, + { + title: 'should not set rewrite_rules', + attr: 'rewrite_rules', + value: [], + notmatch: %r{rewrite} + }, + { + title: 'should set auth_basic', + attr: 'auth_basic', + value: 'value', + match: ' auth_basic "value";' + }, + { + title: 'should set auth_basic_user_file', + attr: 'auth_basic_user_file', + value: 'value', + match: ' auth_basic_user_file value;' + }, + { + title: 'should set auth_request', + attr: 'auth_request', + value: 'value', + match: %r{\s+auth_request\s+value;} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:default_params) { { location: 'location', proxy: 'proxy_value', server: 'server1' } } + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end end end end - end - end - describe 'server/location_footer template content' do - [ - { - title: 'should contain ordered appended directives', - attr: 'location_cfg_append', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], - 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], - 'subtest2' => '"sub test value2"' } }, - match: [ - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;', - ' test3 subtest1 "sub test value1a";', - ' test3 subtest1 "sub test value1b";', - ' test3 subtest2 "sub test value2";' - ] - }, - { - title: 'should contain include directives', - attr: 'include', - value: ['/file1', '/file2'], - match: [ - %r{^\s+include\s+/file1;}, - %r{^\s+include\s+/file2;} - ] - }, - { - title: 'should contain custom appended directives', - attr: 'location_custom_cfg_append', - value: { 'test1' => 'bar', 'test2' => %w(foobar barbaz), - 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], - 'subtest2' => '"sub test value2"' } }, - match: [ - %r{^[ ]+test1\s+bar}, - %r{^[ ]+test2\s+foobar}, - %r{^[ ]+test2\s+barbaz}, - %r{^[ ]+test3\s+subtest1 "sub test value1a"}, - %r{^[ ]+test3\s+subtest1 "sub test value1b"}, - %r{^[ ]+test3\s+subtest2 "sub test value2"} - ] - }, - { - title: 'should contain raw_append directives', - attr: 'raw_append', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:default_params) { { location: 'location', proxy: 'proxy_value', server: 'server1' } } - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) - end - end - - it 'ends with a closing brace' do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - content = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content] - expect(content.split("\n").reject { |l| l =~ %r{^(\s*#|$)} }.last.strip).to eq('}') + describe 'server/location_footer template content' do + [ + { + title: 'should contain ordered appended directives', + attr: 'location_cfg_append', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], + 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], + 'subtest2' => '"sub test value2"' } }, + match: [ + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;', + ' test3 subtest1 "sub test value1a";', + ' test3 subtest1 "sub test value1b";', + ' test3 subtest2 "sub test value2";' + ] + }, + { + title: 'should contain include directives', + attr: 'include', + value: ['/file1', '/file2'], + match: [ + %r{^\s+include\s+/file1;}, + %r{^\s+include\s+/file2;} + ] + }, + { + title: 'should contain custom appended directives', + attr: 'location_custom_cfg_append', + value: { 'test1' => 'bar', 'test2' => %w(foobar barbaz), + 'test3' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], + 'subtest2' => '"sub test value2"' } }, + match: [ + %r{^[ ]+test1\s+bar}, + %r{^[ ]+test2\s+foobar}, + %r{^[ ]+test2\s+barbaz}, + %r{^[ ]+test3\s+subtest1 "sub test value1a"}, + %r{^[ ]+test3\s+subtest1 "sub test value1b"}, + %r{^[ ]+test3\s+subtest2 "sub test value2"} + ] + }, + { + title: 'should contain raw_append directives', + attr: 'raw_append', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:default_params) { { location: 'location', proxy: 'proxy_value', server: 'server1' } } + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end + + it 'ends with a closing brace' do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + content = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content] + expect(content.split("\n").reject { |l| l =~ %r{^(\s*#|$)} }.last.strip).to eq('}') + end + end end end - end - end - describe 'server_location_alias template content' do - let :default_params do - { - location: 'location', - server: 'server1', - location_alias: 'value' - } - end + describe 'server_location_alias template content' do + let :default_params do + { + location: 'location', + server: 'server1', + location_alias: 'value' + } + end - context 'location_alias template with default params' do - let(:params) { default_params } - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')) } - it 'sets alias' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). - with_content(%r{^\s+alias\s+value;}) - end - it "doesn't set try_files" do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). - without_content(%r{^\s+try_files[^;]+;}) - end - it "doesn't set autoindex" do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). - without_content(%r{^[ ]+autoindex[^;]+;}) - end - end + context 'location_alias template with default params' do + let(:params) { default_params } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')) } + it 'sets alias' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). + with_content(%r{^\s+alias\s+value;}) + end + it "doesn't set try_files" do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). + without_content(%r{^\s+try_files[^;]+;}) + end + it "doesn't set autoindex" do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). + without_content(%r{^[ ]+autoindex[^;]+;}) + end + end - [ - { - title: 'should set autoindex', - attr: 'autoindex', - value: 'on', - match: ' autoindex on;' - }, - { - title: 'should set try_file(s)', - attr: 'try_files', - value: %w(name1 name2), - match: ' try_files name1 name2;' - }, - { - title: 'should set index_file(s)', - attr: 'index_files', - value: %w(name1 name2), - match: ' index name1 name2;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + [ + { + title: 'should set autoindex', + attr: 'autoindex', + value: 'on', + match: ' autoindex on;' + }, + { + title: 'should set try_file(s)', + attr: 'try_files', + value: %w(name1 name2), + match: ' try_files name1 name2;' + }, + { + title: 'should set index_file(s)', + attr: 'index_files', + value: %w(name1 name2), + match: ' index name1 name2;' + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end end end end - end - end - describe 'server_location_directory template content' do - let :default_params do - { - location: 'location', - www_root: '/var/www/root', - server: 'server1' - } - end + describe 'server_location_directory template content' do + let :default_params do + { + location: 'location', + www_root: '/var/www/root', + server: 'server1' + } + end - [ - { - title: 'should set www_root', - attr: 'www_root', - value: '/', - match: ' root /;' - }, - { - title: 'should set try_file(s)', - attr: 'try_files', - value: %w(name1 name2), - match: ' try_files name1 name2;' - }, - { - title: 'should set index_file(s)', - attr: 'index_files', - value: %w(name1 name2), - match: ' index name1 name2;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + [ + { + title: 'should set www_root', + attr: 'www_root', + value: '/', + match: ' root /;' + }, + { + title: 'should set try_file(s)', + attr: 'try_files', + value: %w(name1 name2), + match: ' try_files name1 name2;' + }, + { + title: 'should set index_file(s)', + attr: 'index_files', + value: %w(name1 name2), + match: ' index name1 name2;' + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end end end - end - end - context "when autoindex is 'on'" do - let(:params) { default_params.merge(autoindex: 'on') } - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')) } - it 'sets autoindex' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). - with_content(%r{^[ ]+autoindex\s+on;}) - end - end + context "when autoindex is 'on'" do + let(:params) { default_params.merge(autoindex: 'on') } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')) } + it 'sets autoindex' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). + with_content(%r{^[ ]+autoindex\s+on;}) + end + end - context 'when autoindex is not set' do - let(:params) { default_params } - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')) } - it 'does not set autoindex' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). - without_content(%r{^[ ]+autoindex[^;]+;}) + context 'when autoindex is not set' do + let(:params) { default_params } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')) } + it 'does not set autoindex' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')). + without_content(%r{^[ ]+autoindex[^;]+;}) + end + end end - end - end - describe 'server_location_empty template content' do - [ - { - title: 'should contain ordered config directives', - attr: 'location_custom_cfg', - value: { 'test1' => ['test value 1a', 'test value 1b'], 'test2' => 'test value 2', 'allow' => 'test value 3', - 'test4' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], - 'subtest2' => '"sub test value2"' } }, - match: [ - ' allow test value 3;', - ' test1 test value 1a;', - ' test1 test value 1b;', - ' test2 test value 2;', - ' test4 subtest1 "sub test value1a";', - ' test4 subtest1 "sub test value1b";', - ' test4 subtest2 "sub test value2";' - ] - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:default_params) { { location: 'location', location_custom_cfg: { 'test1' => 'value1' }, server: 'server1' } } - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + describe 'server_location_empty template content' do + [ + { + title: 'should contain ordered config directives', + attr: 'location_custom_cfg', + value: { 'test1' => ['test value 1a', 'test value 1b'], 'test2' => 'test value 2', 'allow' => 'test value 3', + 'test4' => { 'subtest1' => ['"sub test value1a"', '"sub test value1b"'], + 'subtest2' => '"sub test value2"' } }, + match: [ + ' allow test value 3;', + ' test1 test value 1a;', + ' test1 test value 1b;', + ' test2 test value 2;', + ' test4 subtest1 "sub test value1a";', + ' test4 subtest1 "sub test value1b";', + ' test4 subtest2 "sub test value2";' + ] + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:default_params) { { location: 'location', location_custom_cfg: { 'test1' => 'value1' }, server: 'server1' } } + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end end end end - end - end - describe 'server_location_fastcgi template content' do - let :default_params do - { - location: 'location', - fastcgi: 'localhost:9000', - server: 'server1' - } - end + describe 'server_location_fastcgi template content' do + let :default_params do + { + location: 'location', + fastcgi: 'localhost:9000', + server: 'server1' + } + end - [ - { - title: 'should set www_root', - attr: 'www_root', - value: '/', - match: %r{\s+root\s+/;} - }, - { - title: 'should set fastcgi_split_path', - attr: 'fastcgi_split_path', - value: 'value', - match: %r{\s+fastcgi_split_path_info\s+value;} - }, - { - title: 'should set try_file(s)', - attr: 'try_files', - value: %w(name1 name2), - match: %r{\s+try_files\s+name1 name2;} - }, - { - title: 'should set fastcgi_params', - attr: 'fastcgi_params', - value: 'value', - match: %r{\s+include\s+value;} - }, - { - title: 'should set fastcgi_pass', - attr: 'fastcgi', - value: 'value', - match: %r{\s+fastcgi_pass\s+value;} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + [ + { + title: 'should set www_root', + attr: 'www_root', + value: '/', + match: %r{\s+root\s+/;} + }, + { + title: 'should set fastcgi_split_path', + attr: 'fastcgi_split_path', + value: 'value', + match: %r{\s+fastcgi_split_path_info\s+value;} + }, + { + title: 'should set try_file(s)', + attr: 'try_files', + value: %w(name1 name2), + match: %r{\s+try_files\s+name1 name2;} + }, + { + title: 'should set fastcgi_params', + attr: 'fastcgi_params', + value: 'value', + match: %r{\s+include\s+value;} + }, + { + title: 'should set fastcgi_pass', + attr: 'fastcgi', + value: 'value', + match: %r{\s+fastcgi_pass\s+value;} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end end end - end - end - context "when fastcgi_script is 'value'" do - let(:params) { default_params.merge(fastcgi_script: 'value') } - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it 'sets fastcgi_script' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - with_content(%r{^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+value;}) - end - end + context "when fastcgi_script is 'value'" do + let(:params) { default_params.merge(fastcgi_script: 'value') } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it 'sets fastcgi_script' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + with_content(%r{^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+value;}) + end + end - context 'when fastcgi_script is not set' do - let(:params) { default_params } - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it 'does not set fastcgi_script' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - without_content(%r{^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+.+?;}) - end - end + context 'when fastcgi_script is not set' do + let(:params) { default_params } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it 'does not set fastcgi_script' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + without_content(%r{^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+.+?;}) + end + end - context "when fastcgi_param is {'CUSTOM_PARAM' => 'value'}" do - let(:params) { default_params.merge(fastcgi_param: { 'CUSTOM_PARAM' => 'value', 'CUSTOM_PARAM2' => 'value2' }) } - it 'sets fastcgi_param' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - with_content(%r{fastcgi_param\s+CUSTOM_PARAM\s+value;}). - with_content(%r{fastcgi_param\s+CUSTOM_PARAM2\s+value2;}) - end - end + context "when fastcgi_param is {'CUSTOM_PARAM' => 'value'}" do + let(:params) { default_params.merge(fastcgi_param: { 'CUSTOM_PARAM' => 'value', 'CUSTOM_PARAM2' => 'value2' }) } + it 'sets fastcgi_param' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + with_content(%r{fastcgi_param\s+CUSTOM_PARAM\s+value;}). + with_content(%r{fastcgi_param\s+CUSTOM_PARAM2\s+value2;}) + end + end - context 'when fastcgi_param is {\'HTTP_PROXY\' => ""}' do - let(:params) { default_params.merge(fastcgi_param: { 'HTTP_PROXY' => '""' }) } - it 'sets fastcgi_param' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - with_content(%r{fastcgi_param\s+HTTP_PROXY\s+"";}) - end - end + context 'when fastcgi_param is {\'HTTP_PROXY\' => ""}' do + let(:params) { default_params.merge(fastcgi_param: { 'HTTP_PROXY' => '""' }) } + it 'sets fastcgi_param' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + with_content(%r{fastcgi_param\s+HTTP_PROXY\s+"";}) + end + end - context 'when fastcgi_param is not set' do - let(:params) { default_params } - it 'does not set fastcgi_param' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - without_content(%r{fastcgi_param\s+CUSTOM_PARAM\s+.+?;}). - without_content(%r{fastcgi_param\s+CUSTOM_PARAM2\s+.+?;}) - end - it 'does not add comment # Enable custom fastcgi_params' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - without_content(%r{# Enable custom fastcgi_params\s+}) + context 'when fastcgi_param is not set' do + let(:params) { default_params } + it 'does not set fastcgi_param' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + without_content(%r{fastcgi_param\s+CUSTOM_PARAM\s+.+?;}). + without_content(%r{fastcgi_param\s+CUSTOM_PARAM2\s+.+?;}) + end + it 'does not add comment # Enable custom fastcgi_params' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + without_content(%r{# Enable custom fastcgi_params\s+}) + end + end end - end - end - describe 'server_location_uwsgi template content' do - let :default_params do - { - location: 'location', - uwsgi: 'unix:/home/project/uwsgi.socket', - server: 'server1' - } - end + describe 'server_location_uwsgi template content' do + let :default_params do + { + location: 'location', + uwsgi: 'unix:/home/project/uwsgi.socket', + server: 'server1' + } + end - [ - { - title: 'should set www_root', - attr: 'www_root', - value: '/', - match: %r{\s+root\s+/;} - }, - { - title: 'should set try_file(s)', - attr: 'try_files', - value: %w(name1 name2), - match: %r{\s+try_files\s+name1 name2;} - }, - { - title: 'should set uwsgi_params', - attr: 'uwsgi_params', - value: 'value', - match: %r{\s+include\s+value;} - }, - { - title: 'should set uwsgi_pass', - attr: 'uwsgi', - value: 'value', - match: %r{\s+uwsgi_pass\s+value;} - }, - { - title: 'should set uwsgi_read_timeout', - attr: 'uwsgi_read_timeout', - value: '300s', - match: %r{\s+uwsgi_read_timeout\s+300s;} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + [ + { + title: 'should set www_root', + attr: 'www_root', + value: '/', + match: %r{\s+root\s+/;} + }, + { + title: 'should set try_file(s)', + attr: 'try_files', + value: %w(name1 name2), + match: %r{\s+try_files\s+name1 name2;} + }, + { + title: 'should set uwsgi_params', + attr: 'uwsgi_params', + value: 'value', + match: %r{\s+include\s+value;} + }, + { + title: 'should set uwsgi_pass', + attr: 'uwsgi', + value: 'value', + match: %r{\s+uwsgi_pass\s+value;} + }, + { + title: 'should set uwsgi_read_timeout', + attr: 'uwsgi_read_timeout', + value: '300s', + match: %r{\s+uwsgi_read_timeout\s+300s;} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end end end - end - end - context "when uwsgi_param is {'CUSTOM_PARAM' => 'value'}" do - let(:params) { default_params.merge(uwsgi_param: { 'CUSTOM_PARAM' => 'value', 'CUSTOM_PARAM2' => 'value2' }) } - it 'sets uwsgi_param' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - with_content(%r{uwsgi_param\s+CUSTOM_PARAM\s+value;}). - with_content(%r{uwsgi_param\s+CUSTOM_PARAM2\s+value2;}) - end - end + context "when uwsgi_param is {'CUSTOM_PARAM' => 'value'}" do + let(:params) { default_params.merge(uwsgi_param: { 'CUSTOM_PARAM' => 'value', 'CUSTOM_PARAM2' => 'value2' }) } + it 'sets uwsgi_param' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + with_content(%r{uwsgi_param\s+CUSTOM_PARAM\s+value;}). + with_content(%r{uwsgi_param\s+CUSTOM_PARAM2\s+value2;}) + end + end - context 'when uwsgi_param is {\'HTTP_PROXY\' => ""}' do - let(:params) { default_params.merge(uwsgi_param: { 'HTTP_PROXY' => '""' }) } - it 'sets uwsgi_param' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - with_content(%r{uwsgi_param\s+HTTP_PROXY\s+"";}) - end - end + context 'when uwsgi_param is {\'HTTP_PROXY\' => ""}' do + let(:params) { default_params.merge(uwsgi_param: { 'HTTP_PROXY' => '""' }) } + it 'sets uwsgi_param' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + with_content(%r{uwsgi_param\s+HTTP_PROXY\s+"";}) + end + end - context 'when uwsgi_param is not set' do - let(:params) { default_params } - it 'does not set uwsgi_param' do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - without_content(%r{^\s+uwsgi_param\s+}) + context 'when uwsgi_param is not set' do + let(:params) { default_params } + it 'does not set uwsgi_param' do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + without_content(%r{^\s+uwsgi_param\s+}) + end + end end - end - end - describe 'server_location_proxy template content' do - [ - { - title: 'should set proxy_redirect', - attr: 'proxy_redirect', - value: 'value', - match: %r{^\s+proxy_redirect\s+value;} - }, - { - title: 'should not set proxy_redirect', - attr: 'proxy_redirect', - value: :undef, - notmatch: %r{proxy_redirect\b} - }, - { - title: 'should set proxy_cache', - attr: 'proxy_cache', - value: 'value', - match: %r{^\s+proxy_cache\s+value;} - }, - { - title: 'should set proxy_cache_valid when string', - attr: 'proxy_cache_valid', - value: 'value', - match: %r{^\s+proxy_cache_valid\s+value;} - }, - { - title: 'should set proxy_cache_valid when array of strings', - attr: 'proxy_cache_valid', - value: %w(value1 value2), - match: [ - %r{^\s+proxy_cache_valid\s+value1;}, - %r{^\s+proxy_cache_valid\s+value2;} - ] - }, - { - title: 'should set proxy_cache_key', - attr: 'proxy_cache_key', - value: 'value', - match: %r{^\s+proxy_cache_key\s+value;} - }, - { - title: 'should set proxy_cache_use_stale', - attr: 'proxy_cache_use_stale', - value: 'value', - match: %r{^\s+proxy_cache_use_stale\s+value;} - }, - { - title: 'should set proxy_pass', - attr: 'proxy', - value: 'value', - match: %r{^\s+proxy_pass\s+value;} - }, - { - title: 'should set proxy_read_timeout', - attr: 'proxy_read_timeout', - value: 'value', - match: %r{\s+proxy_read_timeout\s+value;} - }, - { - title: 'should set proxy_connect_timeout', - attr: 'proxy_connect_timeout', - value: 'value', - match: %r{\s+proxy_connect_timeout\s+value;} - }, - { - title: 'should set proxy_read_timeout', - attr: 'proxy_read_timeout', - value: 'value', - match: %r{\s+proxy_read_timeout\s+value;} - }, - { - title: 'should set proxy headers', - attr: 'proxy_set_header', - value: ['X-TestHeader1 value1', 'X-TestHeader2 value2'], - match: [ - %r{^\s+proxy_set_header\s+X-TestHeader1 value1;}, - %r{^\s+proxy_set_header\s+X-TestHeader2 value2;} - ] - }, - { - title: 'should hide proxy headers', - attr: 'proxy_hide_header', - value: ['X-TestHeader1 value1', 'X-TestHeader2 value2'], - match: [ - %r{^\s+proxy_hide_header\s+X-TestHeader1 value1;}, - %r{^\s+proxy_hide_header\s+X-TestHeader2 value2;} - ] - }, - { - title: 'should pass proxy headers', - attr: 'proxy_pass_header', - value: ['X-TestHeader1 value1', 'X-TestHeader2 value2'], - match: [ - %r{^\s+proxy_pass_header\s+X-TestHeader1 value1;}, - %r{^\s+proxy_pass_header\s+X-TestHeader2 value2;} - ] - }, - { - title: 'should set proxy_http_version', - attr: 'proxy_http_version', - value: 'value', - match: %r{\s+proxy_http_version\s+value;} - }, - { - title: 'should set proxy_method', - attr: 'proxy_method', - value: 'value', - match: %r{\s+proxy_method\s+value;} - }, - { - title: 'should set proxy_set_body', - attr: 'proxy_set_body', - value: 'value', - match: %r{\s+proxy_set_body\s+value;} - }, - { - title: 'should set proxy_buffering', - attr: 'proxy_buffering', - value: 'on', - match: %r{\s+proxy_buffering\s+on;} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:default_params) { { location: 'location', proxy: 'proxy_value', server: 'server1' } } - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } - it param[:title] do - fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } - else - lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") - expect(lines & matches).to eq(matches) - end - - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + describe 'server_location_proxy template content' do + [ + { + title: 'should set proxy_redirect', + attr: 'proxy_redirect', + value: 'value', + match: %r{^\s+proxy_redirect\s+value;} + }, + { + title: 'should not set proxy_redirect', + attr: 'proxy_redirect', + value: :undef, + notmatch: %r{proxy_redirect\b} + }, + { + title: 'should set proxy_cache', + attr: 'proxy_cache', + value: 'value', + match: %r{^\s+proxy_cache\s+value;} + }, + { + title: 'should set proxy_cache_valid when string', + attr: 'proxy_cache_valid', + value: 'value', + match: %r{^\s+proxy_cache_valid\s+value;} + }, + { + title: 'should set proxy_cache_valid when array of strings', + attr: 'proxy_cache_valid', + value: %w(value1 value2), + match: [ + %r{^\s+proxy_cache_valid\s+value1;}, + %r{^\s+proxy_cache_valid\s+value2;} + ] + }, + { + title: 'should set proxy_cache_key', + attr: 'proxy_cache_key', + value: 'value', + match: %r{^\s+proxy_cache_key\s+value;} + }, + { + title: 'should set proxy_cache_use_stale', + attr: 'proxy_cache_use_stale', + value: 'value', + match: %r{^\s+proxy_cache_use_stale\s+value;} + }, + { + title: 'should set proxy_pass', + attr: 'proxy', + value: 'value', + match: %r{^\s+proxy_pass\s+value;} + }, + { + title: 'should set proxy_read_timeout', + attr: 'proxy_read_timeout', + value: 'value', + match: %r{\s+proxy_read_timeout\s+value;} + }, + { + title: 'should set proxy_connect_timeout', + attr: 'proxy_connect_timeout', + value: 'value', + match: %r{\s+proxy_connect_timeout\s+value;} + }, + { + title: 'should set proxy_read_timeout', + attr: 'proxy_read_timeout', + value: 'value', + match: %r{\s+proxy_read_timeout\s+value;} + }, + { + title: 'should set proxy headers', + attr: 'proxy_set_header', + value: ['X-TestHeader1 value1', 'X-TestHeader2 value2'], + match: [ + %r{^\s+proxy_set_header\s+X-TestHeader1 value1;}, + %r{^\s+proxy_set_header\s+X-TestHeader2 value2;} + ] + }, + { + title: 'should hide proxy headers', + attr: 'proxy_hide_header', + value: ['X-TestHeader1 value1', 'X-TestHeader2 value2'], + match: [ + %r{^\s+proxy_hide_header\s+X-TestHeader1 value1;}, + %r{^\s+proxy_hide_header\s+X-TestHeader2 value2;} + ] + }, + { + title: 'should pass proxy headers', + attr: 'proxy_pass_header', + value: ['X-TestHeader1 value1', 'X-TestHeader2 value2'], + match: [ + %r{^\s+proxy_pass_header\s+X-TestHeader1 value1;}, + %r{^\s+proxy_pass_header\s+X-TestHeader2 value2;} + ] + }, + { + title: 'should set proxy_http_version', + attr: 'proxy_http_version', + value: 'value', + match: %r{\s+proxy_http_version\s+value;} + }, + { + title: 'should set proxy_method', + attr: 'proxy_method', + value: 'value', + match: %r{\s+proxy_method\s+value;} + }, + { + title: 'should set proxy_set_body', + attr: 'proxy_set_body', + value: 'value', + match: %r{\s+proxy_set_body\s+value;} + }, + { + title: 'should set proxy_buffering', + attr: 'proxy_buffering', + value: 'on', + match: %r{\s+proxy_buffering\s+on;} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:default_params) { { location: 'location', proxy: 'proxy_value', server: 'server1' } } + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it param[:title] do + fragment = 'server1-500-' + Digest::MD5.hexdigest(params[:location].to_s) + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment(fragment).with_content(item) } + else + lines = catalogue.resource('concat::fragment', fragment).send(:parameters)[:content].split("\n") + expect(lines & matches).to eq(matches) + end + + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end end end - end - end - context 'when proxy_cache_valid is 10m' do - let :params do - { - location: 'location', - proxy: 'proxy_value', - server: 'server1', - proxy_cache: 'true', - proxy_cache_valid: '10m' - } + context 'when proxy_cache_valid is 10m' do + let :params do + { + location: 'location', + proxy: 'proxy_value', + server: 'server1', + proxy_cache: 'true', + proxy_cache_valid: '10m' + } + end + + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')).with_content(%r{proxy_cache_valid\s+10m;}) } + end end - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('location')).with_content(%r{proxy_cache_valid\s+10m;}) } - end - end + describe 'server_location_stub_status template content' do + let(:params) { { location: 'location', stub_status: true, server: 'server1' } } + it do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + with_content(%r{stub_status\s+on}) + end + end - describe 'server_location_stub_status template content' do - let(:params) { { location: 'location', stub_status: true, server: 'server1' } } - it do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - with_content(%r{stub_status\s+on}) - end - end + context 'attribute resources' do + context 'when fastcgi => "localhost:9000"' do + let(:params) { { fastcgi: 'localhost:9000', server: 'server1' } } - context 'attribute resources' do - context 'when fastcgi => "localhost:9000"' do - let(:params) { { fastcgi: 'localhost:9000', server: 'server1' } } + it { is_expected.to contain_file('/etc/nginx/fastcgi.conf').with_mode('0644') } + end - it { is_expected.to contain_file('/etc/nginx/fastcgi.conf').with_mode('0644') } - end + context 'when fastcgi_params is non-default' do + let(:params) do + { + location: 'location', + fastcgi: 'localhost:9000', + fastcgi_params: '/etc/nginx/mycustomparams', + server: 'server1' + } + end - context 'when fastcgi_params is non-default' do - let(:params) do - { - location: 'location', - fastcgi: 'localhost:9000', - fastcgi_params: '/etc/nginx/mycustomparams', - server: 'server1' - } - end + it { is_expected.not_to contain_file('/etc/nginx/mycustomparams') } + it do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + with_content(%r{include\s+/etc/nginx/mycustomparams;}) + end + end - it { is_expected.not_to contain_file('/etc/nginx/mycustomparams') } - it do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - with_content(%r{include\s+/etc/nginx/mycustomparams;}) - end - end + context 'when fastcgi_params is undef' do + let(:params) do + { + location: 'location', + fastcgi: 'localhost:9000', + fastcgi_params: nil, + server: 'server1' + } + end - context 'when fastcgi_params is undef' do - let(:params) do - { - location: 'location', - fastcgi: 'localhost:9000', - fastcgi_params: nil, - server: 'server1' - } - end + it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } + it do + is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). + without_content(%r{include\s+/etc/nginx/fastcgi.conf;}) + end + end - it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } - it do - is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)). - without_content(%r{include\s+/etc/nginx/fastcgi.conf;}) - end - end + context 'when uwsgi => "unix:/home/project/uwsgi.socket"' do + let(:params) { { uwsgi: 'uwsgi_upstream', server: 'server1' } } - context 'when uwsgi => "unix:/home/project/uwsgi.socket"' do - let(:params) { { uwsgi: 'uwsgi_upstream', server: 'server1' } } + it { is_expected.to contain_file('/etc/nginx/uwsgi_params') } + end - it { is_expected.to contain_file('/etc/nginx/uwsgi_params') } - end + context 'when uwsgi_params is non-default' do + let(:params) do + { + uwsgi: 'uwsgi_upstream', + uwsgi_params: '/etc/nginx/bogusparams', + server: 'server1' + } + end - context 'when uwsgi_params is non-default' do - let(:params) do - { - uwsgi: 'uwsgi_upstream', - uwsgi_params: '/etc/nginx/bogusparams', - server: 'server1' - } - end + it { is_expected.not_to contain_file('/etc/nginx/uwsgi_params') } + end - it { is_expected.not_to contain_file('/etc/nginx/uwsgi_params') } - end + context 'when ssl_only => true' do + let(:params) { { ssl_only: true, server: 'server1', www_root: '/' } } + it { is_expected.not_to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('rspec-test')) } + end - context 'when ssl_only => true' do - let(:params) { { ssl_only: true, server: 'server1', www_root: '/' } } - it { is_expected.not_to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('rspec-test')) } - end + context 'when ssl_only => false' do + let(:params) { { ssl_only: false, server: 'server1', www_root: '/' } } - context 'when ssl_only => false' do - let(:params) { { ssl_only: false, server: 'server1', www_root: '/' } } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('rspec-test')) } + end - it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('rspec-test')) } - end + context 'when ssl => true' do + let(:params) { { ssl: true, server: 'server1', www_root: '/' } } - context 'when ssl => true' do - let(:params) { { ssl: true, server: 'server1', www_root: '/' } } + it { is_expected.to contain_concat__fragment('server1-800-' + Digest::MD5.hexdigest('rspec-test') + '-ssl') } + end - it { is_expected.to contain_concat__fragment('server1-800-' + Digest::MD5.hexdigest('rspec-test') + '-ssl') } - end + context 'when ssl => false' do + let(:params) { { ssl: false, server: 'server1', www_root: '/' } } - context 'when ssl => false' do - let(:params) { { ssl: false, server: 'server1', www_root: '/' } } + it { is_expected.not_to contain_concat__fragment('server1-800-' + Digest::MD5.hexdigest('rspec-test') + '-ssl') } + end - it { is_expected.not_to contain_concat__fragment('server1-800-' + Digest::MD5.hexdigest('rspec-test') + '-ssl') } - end + context 'www_root and proxy are set' do + let :params do + { + server: 'server1', + www_root: '/', + proxy: 'http://localhost:8000/uri/' + } + end - context 'www_root and proxy are set' do - let :params do - { - server: 'server1', - www_root: '/', - proxy: 'http://localhost:8000/uri/' - } - end + it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, %r{Cannot define both directory and proxy in server1:rspec-test}) } + end - it { expect { is_expected.to contain_class('nginx::resource::location') }.to raise_error(Puppet::Error, %r{Cannot define both directory and proxy in server1:rspec-test}) } - end + context 'when server name is sanitized' do + let(:title) { 'www.rspec-location.com' } + let :params do + { + server: 'www rspec-server com', + www_root: '/', + ssl: true + } + end - context 'when server name is sanitized' do - let(:title) { 'www.rspec-location.com' } - let :params do - { - server: 'www rspec-server com', - www_root: '/', - ssl: true - } - end + it { is_expected.to contain_concat__fragment('www_rspec-server_com-500-' + Digest::MD5.hexdigest('www.rspec-location.com')).with_target('/etc/nginx/sites-available/www_rspec-server_com.conf') } + it { is_expected.to contain_concat__fragment('www_rspec-server_com-800-' + Digest::MD5.hexdigest('www.rspec-location.com') + '-ssl').with_target('/etc/nginx/sites-available/www_rspec-server_com.conf') } + end - it { is_expected.to contain_concat__fragment('www_rspec-server_com-500-' + Digest::MD5.hexdigest('www.rspec-location.com')).with_target('/etc/nginx/sites-available/www_rspec-server_com.conf') } - it { is_expected.to contain_concat__fragment('www_rspec-server_com-800-' + Digest::MD5.hexdigest('www.rspec-location.com') + '-ssl').with_target('/etc/nginx/sites-available/www_rspec-server_com.conf') } - end + context 'when ensure => absent' do + let :params do + { + server: 'server1', + www_root: '/', + ensure: 'absent' + } + end - context 'when ensure => absent' do - let :params do - { - server: 'server1', - www_root: '/', - ensure: 'absent' - } - end + it { is_expected.not_to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('rspec-test')) } + end - it { is_expected.not_to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest('rspec-test')) } - end + context 'when ensure => absent and ssl => true' do + let :params do + { + ssl: true, + server: 'server1', + www_root: '/', + ensure: 'absent' + } + end - context 'when ensure => absent and ssl => true' do - let :params do - { - ssl: true, - server: 'server1', - www_root: '/', - ensure: 'absent' - } + it { is_expected.not_to contain_concat__fragment('server1-800-' + Digest::MD5.hexdigest('rspec-test') + '-ssl') } + end end - - it { is_expected.not_to contain_concat__fragment('server1-800-' + Digest::MD5.hexdigest('rspec-test') + '-ssl') } end end end diff --git a/spec/defines/resource_mailhost_spec.rb b/spec/defines/resource_mailhost_spec.rb index 1c8bc2103..0baff1262 100644 --- a/spec/defines/resource_mailhost_spec.rb +++ b/spec/defines/resource_mailhost_spec.rb @@ -1,714 +1,720 @@ require 'spec_helper' describe 'nginx::resource::mailhost' do - let(:title) { 'www.rspec.example.com' } - let(:facts) { { ipaddress6: '::' } } - let :default_params do - { - listen_port: 25, - ipv6_enable: true - } - end - let(:pre_condition) { ['include ::nginx'] } - - describe 'os-independent items' do - describe 'basic assumptions' do - let(:params) { default_params } - it { is_expected.to contain_class('nginx') } - it { is_expected.to contain_concat("/etc/nginx/conf.mail.d/#{title}.conf").that_requires('File[/etc/nginx/conf.mail.d]') } - it do - is_expected.to contain_concat("/etc/nginx/conf.mail.d/#{title}.conf").with('owner' => 'root', - 'group' => 'root', - 'mode' => '0644') + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts end - it { is_expected.to contain_concat__fragment("#{title}-header") } - it { is_expected.not_to contain_concat__fragment("#{title}-ssl") } - end - - describe 'mailhost template content' do - [ - { - title: 'should set the IPv4 listen IP', - attr: 'listen_ip', - value: '127.0.0.1', - match: ' listen 127.0.0.1:25;' - }, - { - title: 'should set the IPv4 listen port', - attr: 'listen_port', - value: 45, - match: ' listen *:45;' - }, - { - title: 'should set the IPv4 listen options', - attr: 'listen_options', - value: 'spdy default', - match: ' listen *:25 spdy default;' - }, - { - title: 'should enable IPv6', - attr: 'ipv6_enable', - value: true, - match: ' listen [::]:80 default ipv6only=on;' - }, - { - title: 'should not enable IPv6', - attr: 'ipv6_enable', - value: false, - notmatch: %r{ listen \[::\]:80 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen IP', - attr: 'ipv6_listen_ip', - value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - match: ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80 default ipv6only=on;' - }, - { - title: 'should set the IPv6 listen port', - attr: 'ipv6_listen_port', - value: 45, - match: ' listen [::]:45 default ipv6only=on;' - }, - { - title: 'should set the IPv6 listen options', - attr: 'ipv6_listen_options', - value: 'spdy', - match: ' listen [::]:80 spdy;' - }, - { - title: 'should set servername(s)', - attr: 'server_name', - value: %w(name1 name2), - match: ' server_name name1 name2;' - }, - { - title: 'should set protocol', - attr: 'protocol', - value: 'test-protocol', - match: ' protocol test-protocol;' - }, - { - title: 'should set xclient', - attr: 'xclient', - value: 'test-xclient', - match: ' xclient test-xclient;' - }, + let(:title) { 'www.rspec.example.com' } + let :default_params do { - title: 'should set auth_http', - attr: 'auth_http', - value: 'test-auth_http', - match: ' auth_http test-auth_http;' - }, - { - title: 'should set auth_http_header', - attr: 'auth_http_header', - value: 'X-Auth-Key "secret_string"', - match: ' auth_http_header X-Auth-Key "secret_string";' - }, - { - title: 'should set starttls', - attr: 'starttls', - value: 'on', - match: ' starttls on;' - }, - { - title: 'should set starttls', - attr: 'starttls', - value: 'only', - match: ' starttls only;' - }, - { - title: 'should not enable SSL', - attr: 'starttls', - value: 'off', - notmatch: %r{ ssl_session_timeout 5m;} - }, - { - title: 'should contain raw_prepend directives', - attr: 'raw_prepend', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - }, - { - title: 'should contain raw_append directives', - attr: 'raw_append', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - }, - { - title: 'should contain ordered prepended directives', - attr: 'mailhost_cfg_prepend', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'test3' => 'test value 3' }, - match: [ - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;', - ' test3 test value 3;' - ] - }, - { - title: 'should contain ordered appended directives', - attr: 'mailhost_cfg_append', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'test3' => 'test value 3' }, - match: [ - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;', - ' test3 test value 3;' - ] + listen_port: 25, + ipv6_enable: true } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :default_params do - { - listen_port: 25, - ipv6_enable: true, - ssl_cert: 'dummy.crt', - ssl_key: 'dummy.key' - } + end + let(:pre_condition) { ['include ::nginx'] } + + describe 'os-independent items' do + describe 'basic assumptions' do + let(:params) { default_params } + it { is_expected.to contain_class('nginx') } + it { is_expected.to contain_concat("/etc/nginx/conf.mail.d/#{title}.conf").that_requires('File[/etc/nginx/conf.mail.d]') } + it do + is_expected.to contain_concat("/etc/nginx/conf.mail.d/#{title}.conf").with('owner' => 'root', + 'group' => 'root', + 'mode' => '0644') end - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - it { is_expected.to contain_concat__fragment("#{title}-header") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - end - end + it { is_expected.not_to contain_concat__fragment("#{title}-ssl") } end - end - end - describe 'mailhost template content for imap' do - [ - { - title: 'should set imap_auth', - attr: 'imap_auth', - value: 'login', - match: ' imap_auth login;' - }, - { - title: 'should set imap_capabilities', - attr: 'imap_capabilities', - value: ['"SIZE 52428800"', 'IMAP4rev1', 'UIDPLUS'], - match: ' imap_capabilities "SIZE 52428800" IMAP4rev1 UIDPLUS;' - }, - { - title: 'should set imap_client_buffer', - attr: 'imap_client_buffer', - value: '8k', - match: ' imap_client_buffer 8k;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :default_params do + describe 'mailhost template content' do + [ + { + title: 'should set the IPv4 listen IP', + attr: 'listen_ip', + value: '127.0.0.1', + match: ' listen 127.0.0.1:25;' + }, + { + title: 'should set the IPv4 listen port', + attr: 'listen_port', + value: 45, + match: ' listen *:45;' + }, + { + title: 'should set the IPv4 listen options', + attr: 'listen_options', + value: 'spdy default', + match: ' listen *:25 spdy default;' + }, + { + title: 'should enable IPv6', + attr: 'ipv6_enable', + value: true, + match: ' listen [::]:80 default ipv6only=on;' + }, + { + title: 'should not enable IPv6', + attr: 'ipv6_enable', + value: false, + notmatch: %r{ listen \[::\]:80 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen IP', + attr: 'ipv6_listen_ip', + value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + match: ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:80 default ipv6only=on;' + }, + { + title: 'should set the IPv6 listen port', + attr: 'ipv6_listen_port', + value: 45, + match: ' listen [::]:45 default ipv6only=on;' + }, { - listen_port: 25, - ipv6_enable: true, - protocol: 'imap' + title: 'should set the IPv6 listen options', + attr: 'ipv6_listen_options', + value: 'spdy', + match: ' listen [::]:80 spdy;' + }, + { + title: 'should set servername(s)', + attr: 'server_name', + value: %w(name1 name2), + match: ' server_name name1 name2;' + }, + { + title: 'should set protocol', + attr: 'protocol', + value: 'test-protocol', + match: ' protocol test-protocol;' + }, + { + title: 'should set xclient', + attr: 'xclient', + value: 'test-xclient', + match: ' xclient test-xclient;' + }, + { + title: 'should set auth_http', + attr: 'auth_http', + value: 'test-auth_http', + match: ' auth_http test-auth_http;' + }, + { + title: 'should set auth_http_header', + attr: 'auth_http_header', + value: 'X-Auth-Key "secret_string"', + match: ' auth_http_header X-Auth-Key "secret_string";' + }, + { + title: 'should set starttls', + attr: 'starttls', + value: 'on', + match: ' starttls on;' + }, + { + title: 'should set starttls', + attr: 'starttls', + value: 'only', + match: ' starttls only;' + }, + { + title: 'should not enable SSL', + attr: 'starttls', + value: 'off', + notmatch: %r{ ssl_session_timeout 5m;} + }, + { + title: 'should contain raw_prepend directives', + attr: 'raw_prepend', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + }, + { + title: 'should contain raw_append directives', + attr: 'raw_append', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + }, + { + title: 'should contain ordered prepended directives', + attr: 'mailhost_cfg_prepend', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'test3' => 'test value 3' }, + match: [ + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;', + ' test3 test value 3;' + ] + }, + { + title: 'should contain ordered appended directives', + attr: 'mailhost_cfg_append', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'test3' => 'test value 3' }, + match: [ + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;', + ' test3 test value 3;' + ] } - end - - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment("#{title}-header") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :default_params do + { + listen_port: 25, + ipv6_enable: true, + ssl_cert: 'dummy.crt', + ssl_key: 'dummy.key' + } + end + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-header") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + end end end end - end - end - describe 'mailhost template content for pop3' do - [ - { - title: 'should set pop3_auth', - attr: 'pop3_auth', - value: 'login', - match: ' pop3_auth login;' - }, - { - title: 'should set pop3_capabilities', - attr: 'pop3_capabilities', - value: %w(TOP USER UIDL), - match: ' pop3_capabilities TOP USER UIDL;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :default_params do + describe 'mailhost template content for imap' do + [ + { + title: 'should set imap_auth', + attr: 'imap_auth', + value: 'login', + match: ' imap_auth login;' + }, { - listen_port: 25, - ipv6_enable: true, - protocol: 'pop3' + title: 'should set imap_capabilities', + attr: 'imap_capabilities', + value: ['"SIZE 52428800"', 'IMAP4rev1', 'UIDPLUS'], + match: ' imap_capabilities "SIZE 52428800" IMAP4rev1 UIDPLUS;' + }, + { + title: 'should set imap_client_buffer', + attr: 'imap_client_buffer', + value: '8k', + match: ' imap_client_buffer 8k;' } - end - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment("#{title}-header") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :default_params do + { + listen_port: 25, + ipv6_enable: true, + protocol: 'imap' + } + end + + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-header") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + end end end end - end - end - describe 'mailhost template content for smtp' do - [ - { - title: 'should set smtp_auth', - attr: 'smtp_auth', - value: 'login', - match: ' smtp_auth login;' - }, - { - title: 'should set smtp_capabilities', - attr: 'smtp_capabilities', - value: %w(8BITMIME PIPELINING HELP), - match: ' smtp_capabilities 8BITMIME PIPELINING HELP;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :default_params do + describe 'mailhost template content for pop3' do + [ { - listen_port: 25, - ipv6_enable: true, - protocol: 'smtp' + title: 'should set pop3_auth', + attr: 'pop3_auth', + value: 'login', + match: ' pop3_auth login;' + }, + { + title: 'should set pop3_capabilities', + attr: 'pop3_capabilities', + value: %w(TOP USER UIDL), + match: ' pop3_capabilities TOP USER UIDL;' } - end - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment("#{title}-header") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :default_params do + { + listen_port: 25, + ipv6_enable: true, + protocol: 'pop3' + } + end + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-header") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + end end end end - end - end - describe 'mailhost template content (SSL enabled)' do - [ - { - title: 'should set starttls', - attr: 'starttls', - value: 'on', - match: ' starttls on;' - }, - { - title: 'should set starttls', - attr: 'starttls', - value: 'only', - match: ' starttls only;' - }, - { - title: 'should not enable SSL', - attr: 'starttls', - value: 'off', - notmatch: %r{ ssl_session_timeout 5m;} - }, - { - title: 'should set ssl_certificate', - attr: 'ssl_cert', - value: 'test-ssl-cert', - match: ' ssl_certificate test-ssl-cert;' - }, - { - title: 'should set ssl_certificate_key', - attr: 'ssl_key', - value: 'test-ssl-cert-key', - match: ' ssl_certificate_key test-ssl-cert-key;' - }, - { - title: 'should set ssl_ciphers', - attr: 'ssl_ciphers', - value: 'ECDHE-ECDSA-CHACHA20-POLY1305', - match: ' ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305;' - }, - { - title: 'should set ssl_prefer_server_ciphers to on', - attr: 'ssl_prefer_server_ciphers', - value: 'on', - match: ' ssl_prefer_server_ciphers on;' - }, - { - title: 'should set ssl_prefer_server_ciphers to off', - attr: 'ssl_prefer_server_ciphers', - value: 'off', - match: ' ssl_prefer_server_ciphers off;' - }, - { - title: 'should set ssl_client_certificate', - attr: 'ssl_client_cert', - value: 'client-cert', - match: ' ssl_client_certificate client-cert;' - }, - { - title: 'should set ssl_crl', - attr: 'ssl_crl', - value: 'crl-file', - match: ' ssl_crl crl-file;' - }, - { - title: 'should set ssl_dhparam', - attr: 'ssl_dhparam', - value: 'dhparam-file', - match: ' ssl_dhparam dhparam-file;' - }, - { - title: 'should set ssl_ecdh_curve', - attr: 'ssl_ecdh_curve', - value: 'secp521r1', - match: ' ssl_ecdh_curve secp521r1;' - }, - { - title: 'should set ssl_client_certificate', - attr: 'ssl_client_cert', - value: 'client-cert', - match: ' ssl_client_certificate client-cert;' - }, - { - title: 'should set ssl_password_file', - attr: 'ssl_password_file', - value: 'password-file', - match: ' ssl_password_file password-file;' - }, - { - title: 'should set ssl_protocols', - attr: 'ssl_protocols', - value: 'TLSv1.2', - match: ' ssl_protocols TLSv1.2;' - }, - { - title: 'should set ssl_session_cache', - attr: 'ssl_session_cache', - value: 'none', - match: ' ssl_session_cache none;' - }, - { - title: 'should set ssl_session_ticket_key', - attr: 'ssl_session_ticket_key', - value: 'key-file', - match: ' ssl_session_ticket_key key-file;' - }, - { - title: 'should set ssl_session_tickets', - attr: 'ssl_session_tickets', - value: 'on', - match: ' ssl_session_tickets on;' - }, - { - title: 'should set ssl_session_timeout', - attr: 'ssl_session_timeout', - value: '20m', - match: ' ssl_session_timeout 20m;' - }, - { - title: 'should set ssl_trusted_certificate', - attr: 'ssl_trusted_cert', - value: 'trust-cert', - match: ' ssl_trusted_certificate trust-cert;' - }, - { - title: 'should set ssl_verify_depth', - attr: 'ssl_verify_depth', - value: 2, - match: ' ssl_verify_depth 2;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :default_params do - { - listen_port: 25, - starttls: 'on', - ssl_cert: 'dummy.crt', - ssl_key: 'dummy.key' + describe 'mailhost template content for smtp' do + [ + { + title: 'should set smtp_auth', + attr: 'smtp_auth', + value: 'login', + match: ' smtp_auth login;' + }, + { + title: 'should set smtp_capabilities', + attr: 'smtp_capabilities', + value: %w(8BITMIME PIPELINING HELP), + match: ' smtp_capabilities 8BITMIME PIPELINING HELP;' } - end - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment("#{title}-header") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :default_params do + { + listen_port: 25, + ipv6_enable: true, + protocol: 'smtp' + } + end + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-header") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + end end end end - end - end - describe 'mailhost_ssl template content' do - [ - { - title: 'should set the IPv4 SSL listen port', - attr: 'ssl_port', - value: 45, - match: ' listen *:45;' - }, - { - title: 'should enable IPv6', - attr: 'ipv6_enable', - value: true, - match: ' listen [::]:587 default ipv6only=on;' - }, - { - title: 'should not enable IPv6', - attr: 'ipv6_enable', - value: false, - notmatch: %r{ listen\s+\[::\]:587 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen IP', - attr: 'ipv6_listen_ip', - value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - match: ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:587 default ipv6only=on;' - }, - { - title: 'should set the IPv6 ssl port', - attr: 'ssl_port', - value: 45, - match: ' listen [::]:45 default ipv6only=on;' - }, - { - title: 'should set the IPv6 listen options', - attr: 'ipv6_listen_options', - value: 'spdy', - match: ' listen [::]:587 spdy;' - }, - { - title: 'should set servername(s)', - attr: 'server_name', - value: %w(name1 name2), - match: ' server_name name1 name2;' - }, - { - title: 'should set protocol', - attr: 'protocol', - value: 'test-protocol', - match: ' protocol test-protocol;' - }, - { - title: 'should set xclient', - attr: 'xclient', - value: 'test-xclient', - match: ' xclient test-xclient;' - }, - { - title: 'should set auth_http', - attr: 'auth_http', - value: 'test-auth_http', - match: ' auth_http test-auth_http;' - }, - { - title: 'should set auth_http_header', - attr: 'auth_http_header', - value: 'X-Auth-Key "secret_string"', - match: ' auth_http_header X-Auth-Key "secret_string";' - }, - { - title: 'should set ssl_protocols', - attr: 'ssl_protocols', - value: 'test-ssl-protocol', - match: ' ssl_protocols test-ssl-protocol;' - }, - { - title: 'should set ssl_ciphers', - attr: 'ssl_ciphers', - value: 'test-ssl-ciphers', - match: ' ssl_ciphers test-ssl-ciphers;' - }, - { - title: 'should set ssl_certificate', - attr: 'ssl_cert', - value: 'test-ssl-cert', - match: ' ssl_certificate test-ssl-cert;' - }, - { - title: 'should set ssl_certificate_key', - attr: 'ssl_key', - value: 'test-ssl-cert-key', - match: ' ssl_certificate_key test-ssl-cert-key;' - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :default_params do - { - listen_port: 25, - ssl_port: 587, - ipv6_enable: true, - ssl: true, - ssl_protocols: 'default-protocols', - ssl_ciphers: 'default-ciphers', - ssl_cert: 'dummy.crt', - ssl_key: 'dummy.key' + describe 'mailhost template content (SSL enabled)' do + [ + { + title: 'should set starttls', + attr: 'starttls', + value: 'on', + match: ' starttls on;' + }, + { + title: 'should set starttls', + attr: 'starttls', + value: 'only', + match: ' starttls only;' + }, + { + title: 'should not enable SSL', + attr: 'starttls', + value: 'off', + notmatch: %r{ ssl_session_timeout 5m;} + }, + { + title: 'should set ssl_certificate', + attr: 'ssl_cert', + value: 'test-ssl-cert', + match: ' ssl_certificate test-ssl-cert;' + }, + { + title: 'should set ssl_certificate_key', + attr: 'ssl_key', + value: 'test-ssl-cert-key', + match: ' ssl_certificate_key test-ssl-cert-key;' + }, + { + title: 'should set ssl_ciphers', + attr: 'ssl_ciphers', + value: 'ECDHE-ECDSA-CHACHA20-POLY1305', + match: ' ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305;' + }, + { + title: 'should set ssl_prefer_server_ciphers to on', + attr: 'ssl_prefer_server_ciphers', + value: 'on', + match: ' ssl_prefer_server_ciphers on;' + }, + { + title: 'should set ssl_prefer_server_ciphers to off', + attr: 'ssl_prefer_server_ciphers', + value: 'off', + match: ' ssl_prefer_server_ciphers off;' + }, + { + title: 'should set ssl_client_certificate', + attr: 'ssl_client_cert', + value: 'client-cert', + match: ' ssl_client_certificate client-cert;' + }, + { + title: 'should set ssl_crl', + attr: 'ssl_crl', + value: 'crl-file', + match: ' ssl_crl crl-file;' + }, + { + title: 'should set ssl_dhparam', + attr: 'ssl_dhparam', + value: 'dhparam-file', + match: ' ssl_dhparam dhparam-file;' + }, + { + title: 'should set ssl_ecdh_curve', + attr: 'ssl_ecdh_curve', + value: 'secp521r1', + match: ' ssl_ecdh_curve secp521r1;' + }, + { + title: 'should set ssl_client_certificate', + attr: 'ssl_client_cert', + value: 'client-cert', + match: ' ssl_client_certificate client-cert;' + }, + { + title: 'should set ssl_password_file', + attr: 'ssl_password_file', + value: 'password-file', + match: ' ssl_password_file password-file;' + }, + { + title: 'should set ssl_protocols', + attr: 'ssl_protocols', + value: 'TLSv1.2', + match: ' ssl_protocols TLSv1.2;' + }, + { + title: 'should set ssl_session_cache', + attr: 'ssl_session_cache', + value: 'none', + match: ' ssl_session_cache none;' + }, + { + title: 'should set ssl_session_ticket_key', + attr: 'ssl_session_ticket_key', + value: 'key-file', + match: ' ssl_session_ticket_key key-file;' + }, + { + title: 'should set ssl_session_tickets', + attr: 'ssl_session_tickets', + value: 'on', + match: ' ssl_session_tickets on;' + }, + { + title: 'should set ssl_session_timeout', + attr: 'ssl_session_timeout', + value: '20m', + match: ' ssl_session_timeout 20m;' + }, + { + title: 'should set ssl_trusted_certificate', + attr: 'ssl_trusted_cert', + value: 'trust-cert', + match: ' ssl_trusted_certificate trust-cert;' + }, + { + title: 'should set ssl_verify_depth', + attr: 'ssl_verify_depth', + value: 2, + match: ' ssl_verify_depth 2;' } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :default_params do + { + listen_port: 25, + starttls: 'on', + ssl_cert: 'dummy.crt', + ssl_key: 'dummy.key' + } + end + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-header") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + end + end end - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment("#{title}-ssl") } - it param[:title] do - matches = Array(param[:match]) + end - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-ssl").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) + describe 'mailhost_ssl template content' do + [ + { + title: 'should set the IPv4 SSL listen port', + attr: 'ssl_port', + value: 45, + match: ' listen *:45;' + }, + { + title: 'should enable IPv6', + attr: 'ipv6_enable', + value: true, + match: ' listen [::]:587 default ipv6only=on;' + }, + { + title: 'should not enable IPv6', + attr: 'ipv6_enable', + value: false, + notmatch: %r{ listen\s+\[::\]:587 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen IP', + attr: 'ipv6_listen_ip', + value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + match: ' listen [2001:0db8:85a3:0000:0000:8a2e:0370:7334]:587 default ipv6only=on;' + }, + { + title: 'should set the IPv6 ssl port', + attr: 'ssl_port', + value: 45, + match: ' listen [::]:45 default ipv6only=on;' + }, + { + title: 'should set the IPv6 listen options', + attr: 'ipv6_listen_options', + value: 'spdy', + match: ' listen [::]:587 spdy;' + }, + { + title: 'should set servername(s)', + attr: 'server_name', + value: %w(name1 name2), + match: ' server_name name1 name2;' + }, + { + title: 'should set protocol', + attr: 'protocol', + value: 'test-protocol', + match: ' protocol test-protocol;' + }, + { + title: 'should set xclient', + attr: 'xclient', + value: 'test-xclient', + match: ' xclient test-xclient;' + }, + { + title: 'should set auth_http', + attr: 'auth_http', + value: 'test-auth_http', + match: ' auth_http test-auth_http;' + }, + { + title: 'should set auth_http_header', + attr: 'auth_http_header', + value: 'X-Auth-Key "secret_string"', + match: ' auth_http_header X-Auth-Key "secret_string";' + }, + { + title: 'should set ssl_protocols', + attr: 'ssl_protocols', + value: 'test-ssl-protocol', + match: ' ssl_protocols test-ssl-protocol;' + }, + { + title: 'should set ssl_ciphers', + attr: 'ssl_ciphers', + value: 'test-ssl-ciphers', + match: ' ssl_ciphers test-ssl-ciphers;' + }, + { + title: 'should set ssl_certificate', + attr: 'ssl_cert', + value: 'test-ssl-cert', + match: ' ssl_certificate test-ssl-cert;' + }, + { + title: 'should set ssl_certificate_key', + attr: 'ssl_key', + value: 'test-ssl-cert-key', + match: ' ssl_certificate_key test-ssl-cert-key;' + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :default_params do + { + listen_port: 25, + ssl_port: 587, + ipv6_enable: true, + ssl: true, + ssl_protocols: 'default-protocols', + ssl_ciphers: 'default-ciphers', + ssl_cert: 'dummy.crt', + ssl_key: 'dummy.key' + } + end + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-ssl") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-ssl").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + end end end end - end - end - context 'attribute resources' do - context 'SSL cert missing and ssl => true' do - let(:params) do - default_params.merge( - ssl: true, ssl_key: 'key' - ) - end + context 'attribute resources' do + context 'SSL cert missing and ssl => true' do + let(:params) do + default_params.merge( + ssl: true, ssl_key: 'key' + ) + end - it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } - end + it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } + end - context 'SSL key missing and ssl => true' do - let :params do - default_params.merge(ssl: true, - ssl_cert: 'cert') - end + context 'SSL key missing and ssl => true' do + let :params do + default_params.merge(ssl: true, + ssl_cert: 'cert') + end - it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } - end + it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } + end - context "SSL cert missing and starttls => 'on'" do - let :params do - default_params.merge(starttls: 'on', - ssl_key: 'key') - end + context "SSL cert missing and starttls => 'on'" do + let :params do + default_params.merge(starttls: 'on', + ssl_key: 'key') + end - it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } - end + it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } + end - context "SSL key missing and starttls => 'on'" do - let :params do - default_params.merge(starttls: 'on', - ssl_cert: 'cert') - end + context "SSL key missing and starttls => 'on'" do + let :params do + default_params.merge(starttls: 'on', + ssl_cert: 'cert') + end - it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } - end + it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } + end - context "SSL cert missing and starttls => 'only'" do - let :params do - default_params.merge(starttls: 'only', - ssl_key: 'key') - end + context "SSL cert missing and starttls => 'only'" do + let :params do + default_params.merge(starttls: 'only', + ssl_key: 'key') + end - it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } - end + it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } + end - context "SSL key missing and starttls => 'only'" do - let :params do - default_params.merge(starttls: 'only', - ssl_cert: 'cert') - end + context "SSL key missing and starttls => 'only'" do + let :params do + default_params.merge(starttls: 'only', + ssl_cert: 'cert') + end - it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } - end + it { expect { is_expected.to contain_class('nginx::resource::server') }.to raise_error(Puppet::Error, %r{nginx: SSL certificate/key \(ssl_cert/ssl_cert\) and/or SSL Private must be defined and exist on the target system\(s\)}) } + end - context 'when listen_port != ssl_port' do - let :params do - default_params.merge(listen_port: 80, - ssl_port: 443) - end + context 'when listen_port != ssl_port' do + let :params do + default_params.merge(listen_port: 80, + ssl_port: 443) + end - it { is_expected.to contain_concat__fragment("#{title}-header") } - end + it { is_expected.to contain_concat__fragment("#{title}-header") } + end - context 'when listen_port != "ssl_port"' do - let :params do - default_params.merge(listen_port: 80, - ssl_port: 443) - end + context 'when listen_port != "ssl_port"' do + let :params do + default_params.merge(listen_port: 80, + ssl_port: 443) + end - it { is_expected.to contain_concat__fragment("#{title}-header") } - end + it { is_expected.to contain_concat__fragment("#{title}-header") } + end - context 'when listen_port == ssl_port' do - let :params do - default_params.merge(listen_port: 80, - ssl_port: 80) - end + context 'when listen_port == ssl_port' do + let :params do + default_params.merge(listen_port: 80, + ssl_port: 80) + end - it { is_expected.not_to contain_concat__fragment("#{title}-header") } - end + it { is_expected.not_to contain_concat__fragment("#{title}-header") } + end - context 'when listen_port == "ssl_port"' do - let :params do - default_params.merge(listen_port: 80, - ssl_port: 80) - end + context 'when listen_port == "ssl_port"' do + let :params do + default_params.merge(listen_port: 80, + ssl_port: 80) + end - it { is_expected.not_to contain_concat__fragment("#{title}-header") } - end + it { is_expected.not_to contain_concat__fragment("#{title}-header") } + end - context 'when ssl => true' do - let :params do - default_params.merge(ensure: 'absent', - ssl: true, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert') - end + context 'when ssl => true' do + let :params do + default_params.merge(ensure: 'absent', + ssl: true, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert') + end - it { is_expected.to contain_concat__fragment("#{title}-header") } - it { is_expected.to contain_concat__fragment("#{title}-ssl") } - end + it { is_expected.to contain_concat__fragment("#{title}-header") } + it { is_expected.to contain_concat__fragment("#{title}-ssl") } + end - context 'when ssl => false' do - let :params do - default_params.merge(ensure: 'absent', - ssl: false) - end + context 'when ssl => false' do + let :params do + default_params.merge(ensure: 'absent', + ssl: false) + end - it { is_expected.to contain_concat__fragment("#{title}-header") } - it { is_expected.not_to contain_concat__fragment("#{title}-ssl") } + it { is_expected.to contain_concat__fragment("#{title}-header") } + it { is_expected.not_to contain_concat__fragment("#{title}-ssl") } + end + end end end end diff --git a/spec/defines/resource_map_spec.rb b/spec/defines/resource_map_spec.rb index 3515bd720..80159e4ef 100644 --- a/spec/defines/resource_map_spec.rb +++ b/spec/defines/resource_map_spec.rb @@ -1,108 +1,115 @@ require 'spec_helper' describe 'nginx::resource::map' do - let :title do - 'backend_pool' - end - - let :default_params do - { - string: '$uri', - default: 'pool_a', - mappings: { - 'foo' => 'pool_b', - 'bar' => 'pool_c', - 'baz' => 'pool_d' - } - } - end - - let :pre_condition do - [ - 'include ::nginx' - ] - end - - describe 'os-independent items' do - describe 'basic assumptions' do - let(:params) { default_params } - - it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").that_requires('File[/etc/nginx/conf.d]') } - it do - is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").with( - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'ensure' => 'file', - 'content' => %r{map \$uri \$#{title}} - ) + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end + let :title do + 'backend_pool' end - end - describe 'map.conf template content' do - [ - { - title: 'should set hostnames', - attr: 'hostnames', - value: true, - match: ' hostnames;' - }, + let :default_params do { - title: 'should set default', - attr: 'default', - value: 'pool_a', - match: [' default pool_a;'] - }, - { - title: 'should contain ordered mappings when supplied as a hash', - attr: 'mappings', - value: { + string: '$uri', + default: 'pool_a', + mappings: { 'foo' => 'pool_b', 'bar' => 'pool_c', 'baz' => 'pool_d' - }, - match: [ - ' foo pool_b;', - ' bar pool_c;', - ' baz pool_d;' - ] - }, - { - title: 'should contain mappings in input order when supplied as an array of hashes', - attr: 'mappings', - value: [ - { 'key' => 'foo', 'value' => 'pool_b' }, - { 'key' => 'bar', 'value' => 'pool_c' }, - { 'key' => 'baz', 'value' => 'pool_d' } - ], - match: [ - ' foo pool_b;', - ' bar pool_c;', - ' baz pool_d;' - ] + } } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + end - it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").with_mode('0644') } - it param[:title] do - verify_contents(catalogue, "/etc/nginx/conf.d/#{title}-map.conf", Array(param[:match])) - Array(param[:notmatch]).each do |item| - is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").without_content(item) - end - end - end + let :pre_condition do + [ + 'include ::nginx' + ] end - context 'when ensure => absent' do - let :params do - default_params.merge( - ensure: 'absent' - ) + describe 'os-independent items' do + describe 'basic assumptions' do + let(:params) { default_params } + + it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").that_requires('File[/etc/nginx/conf.d]') } + it do + is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").with( + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'ensure' => 'file', + 'content' => %r{map \$uri \$#{title}} + ) + end end - it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").with_ensure('absent') } + describe 'map.conf template content' do + [ + { + title: 'should set hostnames', + attr: 'hostnames', + value: true, + match: ' hostnames;' + }, + { + title: 'should set default', + attr: 'default', + value: 'pool_a', + match: [' default pool_a;'] + }, + { + title: 'should contain ordered mappings when supplied as a hash', + attr: 'mappings', + value: { + 'foo' => 'pool_b', + 'bar' => 'pool_c', + 'baz' => 'pool_d' + }, + match: [ + ' foo pool_b;', + ' bar pool_c;', + ' baz pool_d;' + ] + }, + { + title: 'should contain mappings in input order when supplied as an array of hashes', + attr: 'mappings', + value: [ + { 'key' => 'foo', 'value' => 'pool_b' }, + { 'key' => 'bar', 'value' => 'pool_c' }, + { 'key' => 'baz', 'value' => 'pool_d' } + ], + match: [ + ' foo pool_b;', + ' bar pool_c;', + ' baz pool_d;' + ] + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").with_mode('0644') } + it param[:title] do + verify_contents(catalogue, "/etc/nginx/conf.d/#{title}-map.conf", Array(param[:match])) + Array(param[:notmatch]).each do |item| + is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").without_content(item) + end + end + end + end + + context 'when ensure => absent' do + let :params do + default_params.merge( + ensure: 'absent' + ) + end + + it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").with_ensure('absent') } + end + end end end end diff --git a/spec/defines/resource_server_spec.rb b/spec/defines/resource_server_spec.rb index b6800f2e6..2ce119d84 100644 --- a/spec/defines/resource_server_spec.rb +++ b/spec/defines/resource_server_spec.rb @@ -1,1321 +1,1322 @@ require 'spec_helper' describe 'nginx::resource::server' do - let :title do - 'www.rspec.example.com' - end - - let :default_params do - { - www_root: '/', - ipv6_enable: true, - listen_unix_socket_enable: true, - fastcgi_index: 'index.php' - } - end - - let :facts do - { - ipaddress6: '::' - } - end - - let :pre_condition do - [ - 'include ::nginx' - ] - end - - describe 'os-independent items' do - describe 'basic assumptions' do - let(:params) { default_params } - it { is_expected.to contain_class('nginx') } - it do - is_expected.to contain_concat("/etc/nginx/sites-available/#{title}.conf").with('owner' => 'root', - 'group' => 'root', - 'mode' => '0644') - end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{access_log\s+/var/log/nginx/www\.rspec\.example\.com\.access\.log combined;}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{error_log\s+/var/log/nginx/www\.rspec\.example\.com\.error\.log}) } - it { is_expected.to contain_concat__fragment("#{title}-footer") } - it { is_expected.to contain_nginx__resource__location("#{title}-default") } - it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } - it do - is_expected.to contain_file("#{title}.conf symlink").with('ensure' => 'link', - 'path' => "/etc/nginx/sites-enabled/#{title}.conf", - 'target' => "/etc/nginx/sites-available/#{title}.conf") + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts end - end - - describe 'with $confd_only enabled' do - let(:pre_condition) { 'class { "nginx": confd_only => true }' } - let(:params) { default_params } - it { is_expected.to contain_class('nginx') } - it do - is_expected.to contain_concat("/etc/nginx/conf.d/#{title}.conf").with('owner' => 'root', - 'group' => 'root', - 'mode' => '0644') - is_expected.not_to contain_file('/etc/nginx/sites-enabled') - is_expected.not_to contain_file('/etc/nginx/sites-available') + let :title do + 'www.rspec.example.com' end - end - describe 'server_header template content' do - [ - { - title: 'should not contain www to non-www rewrite', - attr: 'rewrite_www_to_non_www', - value: false, - notmatch: %r{ - ^ - \s+server_name\s+www\.rspec\.example\.com;\n - \s+return\s+301\s+http://rspec\.example\.com\$request_uri; - }x - }, - { - title: 'should contain www to non-www rewrite', - attr: 'rewrite_www_to_non_www', - value: true, - match: %r{ - ^ - \s+server_name\s+www\.rspec\.example\.com;\n - \s+return\s+301\s+http://rspec\.example\.com\$request_uri; - }x - }, - { - title: 'should set the IPv4 listen IP', - attr: 'listen_ip', - value: '127.0.0.1', - match: %r{\s+listen\s+127.0.0.1:80;} - }, - { - title: 'should set the IPv4 listen port', - attr: 'listen_port', - value: 45, - match: %r{\s+listen\s+\*:45;} - }, - { - title: 'should set the IPv4 listen options', - attr: 'listen_options', - value: 'spdy default', - match: %r{\s+listen\s+\*:80 spdy default;} - }, - { - title: 'should enable IPv6', - attr: 'ipv6_enable', - value: true, - match: %r{\s+listen\s+\[::\]:80 default ipv6only=on;} - }, - { - title: 'should not enable IPv6', - attr: 'ipv6_enable', - value: false, - notmatch: %r{\slisten \[::\]:80 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen IP', - attr: 'ipv6_listen_ip', - value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - match: %r{\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:80 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen port', - attr: 'ipv6_listen_port', - value: 45, - match: %r{\s+listen\s+\[::\]:45 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen options', - attr: 'ipv6_listen_options', - value: 'spdy', - match: %r{\s+listen\s+\[::\]:80 spdy;} - }, - { - title: 'should enable listening on unix socket', - attr: 'listen_unix_socket_enable', - value: true, - match: %r{\s+listen\s+unix:/var/run/nginx\.sock;} - }, - { - title: 'should not enable listening on unix socket', - attr: 'listen_unix_socket_enable', - value: false, - notmatch: %r{\s+listen\s+unix:/var/run/nginx\.sock;} - }, - { - title: 'should set the listen unix socket', - attr: 'listen_unix_socket', - value: '/var/run/puppet_nginx.sock', - match: %r{\s+listen\s+unix:/var/run/puppet_nginx\.sock;} - }, - { - title: 'should set the listen unix socket options', - attr: 'listen_unix_socket_options', - value: 'spdy', - match: %r{\s+listen\s+unix:/var/run/nginx\.sock spdy;} - }, + let :default_params do { - title: 'should set servername(s)', - attr: 'server_name', - value: ['www.foo.com', 'foo.com'], - match: %r{\s+server_name\s+www.foo.com foo.com;} - }, - { - title: 'should rewrite www servername to non-www', - attr: 'rewrite_www_to_non_www', - value: true, - match: %r{\s+server_name\s+rspec.example.com;} - }, - { - title: 'should not rewrite www servername to non-www', - attr: 'rewrite_www_to_non_www', - value: false, - match: %r{\s+server_name\s+www.rspec.example.com;} - }, - { - title: 'should set auth_basic', - attr: 'auth_basic', - value: 'value', - match: %r{\s+auth_basic\s+"value";} - }, - { - title: 'should set auth_basic_user_file', - attr: 'auth_basic_user_file', - value: 'value', - match: %r{\s+auth_basic_user_file\s+value;} - }, - { - title: 'should set auth_request', - attr: 'auth_request', - value: 'value', - match: %r{\s+auth_request\s+value;} - }, - { - title: 'should set the client_body_timeout', - attr: 'client_body_timeout', - value: 'value', - match: %r{^\s+client_body_timeout\s+value;} - }, - { - title: 'should set the client_header_timeout', - attr: 'client_header_timeout', - value: 'value', - match: %r{^\s+client_header_timeout\s+value;} - }, - { - title: 'should set the gzip_types', - attr: 'gzip_types', - value: 'value', - match: %r{^\s+gzip_types\s+value;} - }, - { - title: 'should contain raw_prepend directives', - attr: 'raw_prepend', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - }, - { - title: 'should contain ordered prepended directives', - attr: 'server_cfg_prepend', - value: { 'test1' => ['test value 1a', 'test value 1b'], 'test2' => 'test value 2', 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1a;', - ' test1 test value 1b;', - ' test2 test value 2;' - ] - }, - { - title: 'should set root', - attr: 'use_default_location', - value: false, - match: ' root /;' - }, - { - title: 'should not set root', - attr: 'use_default_location', - value: true, - notmatch: %r{ root /;} - }, - { - title: 'should force https (SSL) redirect', - attr: 'ssl_redirect', - value: true, - match: %r{ return 301 https://\$host\$request_uri;} - }, - { - title: 'should not force https (SSL) redirect', - attr: 'ssl_redirect', - value: false, - notmatch: %r{\s*return\s+301} - }, - { - title: 'should set access_log', - attr: 'access_log', - value: '/path/to/access.log', - match: ' access_log /path/to/access.log combined;' - }, - { - title: 'should set multiple access_log directives', - attr: 'access_log', - value: ['/path/to/log/1', 'syslog:server=localhost'], - match: [ - ' access_log /path/to/log/1 combined;', - ' access_log syslog:server=localhost combined;' - ] - }, - { - title: 'should set access_log off', - attr: 'access_log', - value: 'off', - match: ' access_log off;' - }, - { - title: 'should set access_log to syslog', - attr: 'access_log', - value: 'syslog:server=localhost', - match: ' access_log syslog:server=localhost combined;' - }, - { - title: 'should set format_log custom_format', - attr: 'format_log', - value: 'custom', - match: ' access_log /var/log/nginx/www.rspec.example.com.access.log custom;' - }, - { - title: 'should not include access_log in server when set to absent', - attr: 'access_log', - value: 'absent', - notmatch: 'access_log' - }, - { - title: 'should set error_log', - attr: 'error_log', - value: '/path/to/error.log', - match: ' error_log /path/to/error.log;' - }, - { - title: 'should allow multiple error_log directives', - attr: 'error_log', - value: ['/path/to/error.log', 'syslog:server=localhost'], - match: [ - ' error_log /path/to/error.log;', - ' error_log syslog:server=localhost;' - ] - }, - { - title: 'should not include error_log in server when set to absent', - attr: 'error_log', - value: 'absent', - notmatch: 'error_log' - }, - { - title: 'should set error_pages', - attr: 'error_pages', - value: { '503' => '/foo.html' }, - match: ' error_page 503 /foo.html;' + www_root: '/', + ipv6_enable: true, + listen_unix_socket_enable: true, + fastcgi_index: 'index.php' } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - - it { is_expected.to contain_concat__fragment("#{title}-header") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - end - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment("#{title}-header").without_content(item) - end - end - end end - end - describe 'server_footer template content' do - [ - { - title: 'should not contain www to non-www rewrite', - attr: 'rewrite_www_to_non_www', - value: false, - notmatch: %r{ - ^ - \s+server_name\s+www\.rspec\.example\.com;\n - \s+return\s+301\s+https://rspec\.example\.com\$request_uri; - }x - }, - { - title: 'should contain include directives', - attr: 'include_files', - value: ['/file1', '/file2'], - match: [ - %r{^\s+include\s+/file1;}, - %r{^\s+include\s+/file2;} - ] - }, - { - title: 'should contain ordered appended directives', - attr: 'server_cfg_append', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;' - ] - }, - { - title: 'should contain raw_append directives', - attr: 'raw_append', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + let :pre_condition do + [ + 'include ::nginx' + ] + end + describe 'os-independent items' do + describe 'basic assumptions' do + let(:params) { default_params } + it { is_expected.to contain_class('nginx') } + it do + is_expected.to contain_concat("/etc/nginx/sites-available/#{title}.conf").with('owner' => 'root', + 'group' => 'root', + 'mode' => '0644') + end + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{access_log\s+/var/log/nginx/www\.rspec\.example\.com\.access\.log combined;}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{error_log\s+/var/log/nginx/www\.rspec\.example\.com\.error\.log}) } it { is_expected.to contain_concat__fragment("#{title}-footer") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-footer").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-footer").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - end - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment("#{title}-footer").without_content(item) - end + it { is_expected.to contain_nginx__resource__location("#{title}-default") } + it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } + it do + is_expected.to contain_file("#{title}.conf symlink").with('ensure' => 'link', + 'path' => "/etc/nginx/sites-enabled/#{title}.conf", + 'target' => "/etc/nginx/sites-available/#{title}.conf") end end - end - end - describe 'server_ssl_header template content' do - [ - { - title: 'should not contain www to non-www rewrite', - attr: 'rewrite_www_to_non_www', - value: false, - notmatch: %r{ - ^ - \s+server_name\s+www\.rspec\.example\.com;\n - \s+return\s+301\s+https://rspec\.example\.com\$request_uri; - }x - }, - { - title: 'should contain www to non-www rewrite', - attr: 'rewrite_www_to_non_www', - value: true, - match: %r{ - ^ - \s+server_name\s+www\.rspec\.example\.com;\n - \s+return\s+301\s+https://rspec\.example\.com\$request_uri; - }x - }, - { - title: 'should set the IPv4 listen IP', - attr: 'listen_ip', - value: '127.0.0.1', - match: %r{\s+listen\s+127.0.0.1:443 ssl;} - }, - { - title: 'should set the IPv4 SSL listen port', - attr: 'ssl_port', - value: 45, - match: %r{\s+listen\s+\*:45 ssl;} - }, - { - title: 'should set SPDY', - attr: 'spdy', - value: 'on', - match: %r{\s+listen\s+\*:443 ssl spdy;} - }, - { - title: 'should not set SPDY', - attr: 'spdy', - value: 'off', - match: %r{\s+listen\s+\*:443 ssl;} - }, - { - title: 'should set HTTP2', - attr: 'http2', - value: 'on', - match: %r{\s+listen\s+\*:443 ssl http2;} - }, - { - title: 'should not set HTTP2', - attr: 'http2', - value: 'off', - match: %r{\s+listen\s+\*:443 ssl;} - }, - { - title: 'should set the IPv4 listen options', - attr: 'listen_options', - value: 'default', - match: %r{\s+listen\s+\*:443 ssl default;} - }, - { - title: 'should enable IPv6', - attr: 'ipv6_enable', - value: true, - match: %r{\s+listen\s+\[::\]:443 ssl default ipv6only=on;} - }, - { - title: 'should disable IPv6', - attr: 'ipv6_enable', - value: false, - notmatch: %r{ listen \[::\]:443 ssl default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen IP', - attr: 'ipv6_listen_ip', - value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - match: %r{\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:443 ssl default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen port', - attr: 'ssl_port', - value: 45, - match: %r{\s+listen\s+\[::\]:45 ssl default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen options', - attr: 'ipv6_listen_options', - value: 'spdy default', - match: %r{\s+listen\s+\[::\]:443 ssl spdy default;} - }, - { - title: 'should set servername(s)', - attr: 'server_name', - value: ['www.foo.com', 'foo.com'], - match: %r{\s+server_name\s+www.foo.com foo.com;} - }, - { - title: 'should rewrite www servername to non-www', - attr: 'rewrite_www_to_non_www', - value: true, - match: %r{\s+server_name\s+rspec.example.com;} - }, - { - title: 'should not rewrite www servername to non-www', - attr: 'rewrite_www_to_non_www', - value: false, - match: %r{\s+server_name\s+www.rspec.example.com;} - }, - { - title: 'should set the SSL buffer size', - attr: 'ssl_buffer_size', - value: '4k', - match: ' ssl_buffer_size 4k;' - }, - { - title: 'should set the SSL client certificate file', - attr: 'ssl_client_cert', - value: '/tmp/client_certificate', - match: %r{\s+ssl_client_certificate\s+/tmp/client_certificate;} - }, - { - title: 'should set the SSL CRL file', - attr: 'ssl_crl', - value: '/tmp/crl', - match: %r{\s+ssl_crl\s+/tmp/crl;} - }, - { - title: 'should set the SSL DH parameters file', - attr: 'ssl_dhparam', - value: '/tmp/dhparam', - match: %r{\s+ssl_dhparam\s+/tmp/dhparam;} - }, - { - title: 'should set the SSL stapling file', - attr: 'ssl_stapling_file', - value: '/tmp/stapling_file', - match: %r{\s+ssl_stapling_file\s+/tmp/stapling_file;} - }, - { - title: 'should set the SSL trusted certificate file', - attr: 'ssl_trusted_cert', - value: '/tmp/trusted_certificate', - match: %r{\s+ssl_trusted_certificate\s+/tmp/trusted_certificate;} - }, - { - title: 'should set the SSL cache', - attr: 'ssl_cache', - value: 'shared:SSL:1m', - match: %r{\s+ssl_session_cache\s+shared:SSL:1m;} - }, - { - title: 'should set the SSL timeout', - attr: 'ssl_session_timeout', - value: '30m', - match: ' ssl_session_timeout 30m;' - }, - { - title: 'should set the SSL protocols', - attr: 'ssl_protocols', - value: 'TLSv1', - match: %r{\s+ssl_protocols\s+TLSv1;} - }, - { - title: 'should set the SSL ciphers', - attr: 'ssl_ciphers', - value: 'HIGH', - match: %r{\s+ssl_ciphers\s+HIGH;} - }, - { - title: 'should set ssl_prefer_server_ciphers on', - attr: 'ssl_prefer_server_ciphers', - value: 'on', - match: %r{\s+ssl_prefer_server_ciphers\s+on;} - }, - { - title: 'should set ssl_prefer_server_ciphers off', - attr: 'ssl_prefer_server_ciphers', - value: 'off', - match: %r{\s+ssl_prefer_server_ciphers\s+off;} - }, - { - title: 'should set auth_basic', - attr: 'auth_basic', - value: 'value', - match: %r{\s+auth_basic\s+"value";} - }, - { - title: 'should set auth_basic_user_file', - attr: 'auth_basic_user_file', - value: 'value', - match: %r{\s+auth_basic_user_file\s+"value";} - }, - { - title: 'should set auth_request', - attr: 'auth_request', - value: 'value', - match: %r{\s+auth_request\s+value;} - }, - { - title: 'should set the client_body_timeout', - attr: 'client_body_timeout', - value: 'value', - match: %r{^\s+client_body_timeout\s+value;} - }, - { - title: 'should set the client_header_timeout', - attr: 'client_header_timeout', - value: 'value', - match: %r{^\s+client_header_timeout\s+value;} - }, - { - title: 'should set the gzip_types', - attr: 'gzip_types', - value: 'value', - match: %r{^\s+gzip_types\s+value;} - }, - { - title: 'should set access_log', - attr: 'access_log', - value: '/path/to/access.log', - match: ' access_log /path/to/access.log combined;' - }, - { - title: 'should set multiple access_log directives', - attr: 'access_log', - value: ['/path/to/log/1', 'syslog:server=localhost'], - match: [ - ' access_log /path/to/log/1 combined;', - ' access_log syslog:server=localhost combined;' - ] - }, - { - title: 'should set access_log off', - attr: 'access_log', - value: 'off', - match: ' access_log off;' - }, - { - title: 'should not include access_log in server when set to absent', - attr: 'access_log', - value: 'absent', - notmatch: 'access_log' - }, - { - title: 'should set access_log to syslog', - attr: 'access_log', - value: 'syslog:server=localhost', - match: ' access_log syslog:server=localhost combined;' - }, - { - title: 'should set format_log custom_format', - attr: 'format_log', - value: 'custom', - match: ' access_log /var/log/nginx/ssl-www.rspec.example.com.access.log custom;' - }, - { - title: 'should set error_log', - attr: 'error_log', - value: '/path/to/error.log', - match: ' error_log /path/to/error.log;' - }, - { - title: 'should allow multiple error_log directives', - attr: 'error_log', - value: ['/path/to/error.log', 'syslog:server=localhost'], - match: [ - ' error_log /path/to/error.log;', - ' error_log syslog:server=localhost;' - ] - }, - { - title: 'should not include error_log in server when set to absent', - attr: 'error_log', - value: 'absent', - notmatch: 'error_log' - }, - { - title: 'should set error_pages', - attr: 'error_pages', - value: { '503' => '/foo.html' }, - match: ' error_page 503 /foo.html;' - }, - { - title: 'should contain raw_prepend directives', - attr: 'raw_prepend', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - }, - { - title: 'should contain ordered prepend directives', - attr: 'server_cfg_prepend', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;' - ] - }, - { - title: 'should contain ordered ssl prepend directives', - attr: 'server_cfg_ssl_prepend', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;' - ] - }, - { - title: 'should set root', - attr: 'use_default_location', - value: false, - match: ' root /;' - }, - { - title: 'should not set root', - attr: 'use_default_location', - value: true, - notmatch: %r{ root /;} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :params do - default_params.merge(param[:attr].to_sym => param[:value], - :ssl => true, - :ssl_key => 'dummy.key', - :ssl_cert => 'dummy.crt') + describe 'with $confd_only enabled' do + let(:pre_condition) { 'class { "nginx": confd_only => true }' } + let(:params) { default_params } + it { is_expected.to contain_class('nginx') } + it do + is_expected.to contain_concat("/etc/nginx/conf.d/#{title}.conf").with('owner' => 'root', + 'group' => 'root', + 'mode' => '0644') + is_expected.not_to contain_file('/etc/nginx/sites-enabled') + is_expected.not_to contain_file('/etc/nginx/sites-available') end - it { is_expected.to contain_concat__fragment("#{title}-ssl-header") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-ssl-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - end - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment("#{title}-ssl-header").without_content(item) + end + + describe 'server_header template content' do + [ + { + title: 'should not contain www to non-www rewrite', + attr: 'rewrite_www_to_non_www', + value: false, + notmatch: %r{ + ^ + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+http://rspec\.example\.com\$request_uri; + }x + }, + { + title: 'should contain www to non-www rewrite', + attr: 'rewrite_www_to_non_www', + value: true, + match: %r{ + ^ + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+http://rspec\.example\.com\$request_uri; + }x + }, + { + title: 'should set the IPv4 listen IP', + attr: 'listen_ip', + value: '127.0.0.1', + match: %r{\s+listen\s+127.0.0.1:80;} + }, + { + title: 'should set the IPv4 listen port', + attr: 'listen_port', + value: 45, + match: %r{\s+listen\s+\*:45;} + }, + { + title: 'should set the IPv4 listen options', + attr: 'listen_options', + value: 'spdy default', + match: %r{\s+listen\s+\*:80 spdy default;} + }, + { + title: 'should enable IPv6', + attr: 'ipv6_enable', + value: true, + match: %r{\s+listen\s+\[::\]:80 default ipv6only=on;} + }, + { + title: 'should not enable IPv6', + attr: 'ipv6_enable', + value: false, + notmatch: %r{\slisten \[::\]:80 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen IP', + attr: 'ipv6_listen_ip', + value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + match: %r{\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:80 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen port', + attr: 'ipv6_listen_port', + value: 45, + match: %r{\s+listen\s+\[::\]:45 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen options', + attr: 'ipv6_listen_options', + value: 'spdy', + match: %r{\s+listen\s+\[::\]:80 spdy;} + }, + { + title: 'should enable listening on unix socket', + attr: 'listen_unix_socket_enable', + value: true, + match: %r{\s+listen\s+unix:/var/run/nginx\.sock;} + }, + { + title: 'should not enable listening on unix socket', + attr: 'listen_unix_socket_enable', + value: false, + notmatch: %r{\s+listen\s+unix:/var/run/nginx\.sock;} + }, + { + title: 'should set the listen unix socket', + attr: 'listen_unix_socket', + value: '/var/run/puppet_nginx.sock', + match: %r{\s+listen\s+unix:/var/run/puppet_nginx\.sock;} + }, + { + title: 'should set the listen unix socket options', + attr: 'listen_unix_socket_options', + value: 'spdy', + match: %r{\s+listen\s+unix:/var/run/nginx\.sock spdy;} + }, + { + title: 'should set servername(s)', + attr: 'server_name', + value: ['www.foo.com', 'foo.com'], + match: %r{\s+server_name\s+www.foo.com foo.com;} + }, + { + title: 'should rewrite www servername to non-www', + attr: 'rewrite_www_to_non_www', + value: true, + match: %r{\s+server_name\s+rspec.example.com;} + }, + { + title: 'should not rewrite www servername to non-www', + attr: 'rewrite_www_to_non_www', + value: false, + match: %r{\s+server_name\s+www.rspec.example.com;} + }, + { + title: 'should set auth_basic', + attr: 'auth_basic', + value: 'value', + match: %r{\s+auth_basic\s+"value";} + }, + { + title: 'should set auth_basic_user_file', + attr: 'auth_basic_user_file', + value: 'value', + match: %r{\s+auth_basic_user_file\s+value;} + }, + { + title: 'should set auth_request', + attr: 'auth_request', + value: 'value', + match: %r{\s+auth_request\s+value;} + }, + { + title: 'should set the client_body_timeout', + attr: 'client_body_timeout', + value: 'value', + match: %r{^\s+client_body_timeout\s+value;} + }, + { + title: 'should set the client_header_timeout', + attr: 'client_header_timeout', + value: 'value', + match: %r{^\s+client_header_timeout\s+value;} + }, + { + title: 'should set the gzip_types', + attr: 'gzip_types', + value: 'value', + match: %r{^\s+gzip_types\s+value;} + }, + { + title: 'should contain raw_prepend directives', + attr: 'raw_prepend', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + }, + { + title: 'should contain ordered prepended directives', + attr: 'server_cfg_prepend', + value: { 'test1' => ['test value 1a', 'test value 1b'], 'test2' => 'test value 2', 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1a;', + ' test1 test value 1b;', + ' test2 test value 2;' + ] + }, + { + title: 'should set root', + attr: 'use_default_location', + value: false, + match: ' root /;' + }, + { + title: 'should not set root', + attr: 'use_default_location', + value: true, + notmatch: %r{ root /;} + }, + { + title: 'should force https (SSL) redirect', + attr: 'ssl_redirect', + value: true, + match: %r{ return 301 https://\$host\$request_uri;} + }, + { + title: 'should not force https (SSL) redirect', + attr: 'ssl_redirect', + value: false, + notmatch: %r{\s*return\s+301} + }, + { + title: 'should set access_log', + attr: 'access_log', + value: '/path/to/access.log', + match: ' access_log /path/to/access.log combined;' + }, + { + title: 'should set multiple access_log directives', + attr: 'access_log', + value: ['/path/to/log/1', 'syslog:server=localhost'], + match: [ + ' access_log /path/to/log/1 combined;', + ' access_log syslog:server=localhost combined;' + ] + }, + { + title: 'should set access_log off', + attr: 'access_log', + value: 'off', + match: ' access_log off;' + }, + { + title: 'should set access_log to syslog', + attr: 'access_log', + value: 'syslog:server=localhost', + match: ' access_log syslog:server=localhost combined;' + }, + { + title: 'should set format_log custom_format', + attr: 'format_log', + value: 'custom', + match: ' access_log /var/log/nginx/www.rspec.example.com.access.log custom;' + }, + { + title: 'should not include access_log in server when set to absent', + attr: 'access_log', + value: 'absent', + notmatch: 'access_log' + }, + { + title: 'should set error_log', + attr: 'error_log', + value: '/path/to/error.log', + match: ' error_log /path/to/error.log;' + }, + { + title: 'should allow multiple error_log directives', + attr: 'error_log', + value: ['/path/to/error.log', 'syslog:server=localhost'], + match: [ + ' error_log /path/to/error.log;', + ' error_log syslog:server=localhost;' + ] + }, + { + title: 'should not include error_log in server when set to absent', + attr: 'error_log', + value: 'absent', + notmatch: 'error_log' + }, + { + title: 'should set error_pages', + attr: 'error_pages', + value: { '503' => '/foo.html' }, + match: ' error_page 503 /foo.html;' + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-header") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment("#{title}-header").without_content(item) + end + end end end end - end - end - describe 'server_ssl_footer template content' do - [ - { - title: 'should not contain www to non-www rewrite', - attr: 'rewrite_www_to_non_www', - value: false, - notmatch: %r{ - ^ - \s+server_name\s+www\.rspec\.example\.com;\n - \s+return\s+301\s+https://rspec\.example\.com\$request_uri; - }x - }, - { - title: 'should contain include directives', - attr: 'include_files', - value: ['/file1', '/file2'], - match: [ - %r{^\s+include\s+/file1;}, - %r{^\s+include\s+/file2;} - ] - }, - { - title: 'should contain ordered appended directives', - attr: 'server_cfg_append', - value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1;', - ' test2 test value 2;' - ] - }, - { - title: 'should contain raw_append directives', - attr: 'raw_append', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - }, - { - title: 'should contain ordered ssl appended directives', - attr: 'server_cfg_ssl_append', - value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, - match: [ - ' allow test value 3;', - ' test1 test value 1;', - ' test2 test value 2a;', - ' test2 test value 2b;' - ] - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let :params do - default_params.merge(param[:attr].to_sym => param[:value], - :ssl => true, - :ssl_key => 'dummy.key', - :ssl_cert => 'dummy.crt') + describe 'server_footer template content' do + [ + { + title: 'should not contain www to non-www rewrite', + attr: 'rewrite_www_to_non_www', + value: false, + notmatch: %r{ + ^ + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+https://rspec\.example\.com\$request_uri; + }x + }, + { + title: 'should contain include directives', + attr: 'include_files', + value: ['/file1', '/file2'], + match: [ + %r{^\s+include\s+/file1;}, + %r{^\s+include\s+/file2;} + ] + }, + { + title: 'should contain ordered appended directives', + attr: 'server_cfg_append', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;' + ] + }, + { + title: 'should contain raw_append directives', + attr: 'raw_append', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + + it { is_expected.to contain_concat__fragment("#{title}-footer") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-footer").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-footer").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment("#{title}-footer").without_content(item) + end + end + end end + end - it { is_expected.to contain_concat__fragment("#{title}-ssl-footer") } - it param[:title] do - matches = Array(param[:match]) - - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl-footer").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-ssl-footer").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - end - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment("#{title}-ssl-footer").without_content(item) + describe 'server_ssl_header template content' do + [ + { + title: 'should not contain www to non-www rewrite', + attr: 'rewrite_www_to_non_www', + value: false, + notmatch: %r{ + ^ + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+https://rspec\.example\.com\$request_uri; + }x + }, + { + title: 'should contain www to non-www rewrite', + attr: 'rewrite_www_to_non_www', + value: true, + match: %r{ + ^ + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+https://rspec\.example\.com\$request_uri; + }x + }, + { + title: 'should set the IPv4 listen IP', + attr: 'listen_ip', + value: '127.0.0.1', + match: %r{\s+listen\s+127.0.0.1:443 ssl;} + }, + { + title: 'should set the IPv4 SSL listen port', + attr: 'ssl_port', + value: 45, + match: %r{\s+listen\s+\*:45 ssl;} + }, + { + title: 'should set SPDY', + attr: 'spdy', + value: 'on', + match: %r{\s+listen\s+\*:443 ssl spdy;} + }, + { + title: 'should not set SPDY', + attr: 'spdy', + value: 'off', + match: %r{\s+listen\s+\*:443 ssl;} + }, + { + title: 'should set HTTP2', + attr: 'http2', + value: 'on', + match: %r{\s+listen\s+\*:443 ssl http2;} + }, + { + title: 'should not set HTTP2', + attr: 'http2', + value: 'off', + match: %r{\s+listen\s+\*:443 ssl;} + }, + { + title: 'should set the IPv4 listen options', + attr: 'listen_options', + value: 'default', + match: %r{\s+listen\s+\*:443 ssl default;} + }, + { + title: 'should enable IPv6', + attr: 'ipv6_enable', + value: true, + match: %r{\s+listen\s+\[::\]:443 ssl default ipv6only=on;} + }, + { + title: 'should disable IPv6', + attr: 'ipv6_enable', + value: false, + notmatch: %r{ listen \[::\]:443 ssl default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen IP', + attr: 'ipv6_listen_ip', + value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + match: %r{\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:443 ssl default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen port', + attr: 'ssl_port', + value: 45, + match: %r{\s+listen\s+\[::\]:45 ssl default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen options', + attr: 'ipv6_listen_options', + value: 'spdy default', + match: %r{\s+listen\s+\[::\]:443 ssl spdy default;} + }, + { + title: 'should set servername(s)', + attr: 'server_name', + value: ['www.foo.com', 'foo.com'], + match: %r{\s+server_name\s+www.foo.com foo.com;} + }, + { + title: 'should rewrite www servername to non-www', + attr: 'rewrite_www_to_non_www', + value: true, + match: %r{\s+server_name\s+rspec.example.com;} + }, + { + title: 'should not rewrite www servername to non-www', + attr: 'rewrite_www_to_non_www', + value: false, + match: %r{\s+server_name\s+www.rspec.example.com;} + }, + { + title: 'should set the SSL buffer size', + attr: 'ssl_buffer_size', + value: '4k', + match: ' ssl_buffer_size 4k;' + }, + { + title: 'should set the SSL client certificate file', + attr: 'ssl_client_cert', + value: '/tmp/client_certificate', + match: %r{\s+ssl_client_certificate\s+/tmp/client_certificate;} + }, + { + title: 'should set the SSL CRL file', + attr: 'ssl_crl', + value: '/tmp/crl', + match: %r{\s+ssl_crl\s+/tmp/crl;} + }, + { + title: 'should set the SSL DH parameters file', + attr: 'ssl_dhparam', + value: '/tmp/dhparam', + match: %r{\s+ssl_dhparam\s+/tmp/dhparam;} + }, + { + title: 'should set the SSL stapling file', + attr: 'ssl_stapling_file', + value: '/tmp/stapling_file', + match: %r{\s+ssl_stapling_file\s+/tmp/stapling_file;} + }, + { + title: 'should set the SSL trusted certificate file', + attr: 'ssl_trusted_cert', + value: '/tmp/trusted_certificate', + match: %r{\s+ssl_trusted_certificate\s+/tmp/trusted_certificate;} + }, + { + title: 'should set the SSL cache', + attr: 'ssl_cache', + value: 'shared:SSL:1m', + match: %r{\s+ssl_session_cache\s+shared:SSL:1m;} + }, + { + title: 'should set the SSL timeout', + attr: 'ssl_session_timeout', + value: '30m', + match: ' ssl_session_timeout 30m;' + }, + { + title: 'should set the SSL protocols', + attr: 'ssl_protocols', + value: 'TLSv1', + match: %r{\s+ssl_protocols\s+TLSv1;} + }, + { + title: 'should set the SSL ciphers', + attr: 'ssl_ciphers', + value: 'HIGH', + match: %r{\s+ssl_ciphers\s+HIGH;} + }, + { + title: 'should set ssl_prefer_server_ciphers on', + attr: 'ssl_prefer_server_ciphers', + value: 'on', + match: %r{\s+ssl_prefer_server_ciphers\s+on;} + }, + { + title: 'should set ssl_prefer_server_ciphers off', + attr: 'ssl_prefer_server_ciphers', + value: 'off', + match: %r{\s+ssl_prefer_server_ciphers\s+off;} + }, + { + title: 'should set auth_basic', + attr: 'auth_basic', + value: 'value', + match: %r{\s+auth_basic\s+"value";} + }, + { + title: 'should set auth_basic_user_file', + attr: 'auth_basic_user_file', + value: 'value', + match: %r{\s+auth_basic_user_file\s+"value";} + }, + { + title: 'should set auth_request', + attr: 'auth_request', + value: 'value', + match: %r{\s+auth_request\s+value;} + }, + { + title: 'should set the client_body_timeout', + attr: 'client_body_timeout', + value: 'value', + match: %r{^\s+client_body_timeout\s+value;} + }, + { + title: 'should set the client_header_timeout', + attr: 'client_header_timeout', + value: 'value', + match: %r{^\s+client_header_timeout\s+value;} + }, + { + title: 'should set the gzip_types', + attr: 'gzip_types', + value: 'value', + match: %r{^\s+gzip_types\s+value;} + }, + { + title: 'should set access_log', + attr: 'access_log', + value: '/path/to/access.log', + match: ' access_log /path/to/access.log combined;' + }, + { + title: 'should set multiple access_log directives', + attr: 'access_log', + value: ['/path/to/log/1', 'syslog:server=localhost'], + match: [ + ' access_log /path/to/log/1 combined;', + ' access_log syslog:server=localhost combined;' + ] + }, + { + title: 'should set access_log off', + attr: 'access_log', + value: 'off', + match: ' access_log off;' + }, + { + title: 'should not include access_log in server when set to absent', + attr: 'access_log', + value: 'absent', + notmatch: 'access_log' + }, + { + title: 'should set access_log to syslog', + attr: 'access_log', + value: 'syslog:server=localhost', + match: ' access_log syslog:server=localhost combined;' + }, + { + title: 'should set format_log custom_format', + attr: 'format_log', + value: 'custom', + match: ' access_log /var/log/nginx/ssl-www.rspec.example.com.access.log custom;' + }, + { + title: 'should set error_log', + attr: 'error_log', + value: '/path/to/error.log', + match: ' error_log /path/to/error.log;' + }, + { + title: 'should allow multiple error_log directives', + attr: 'error_log', + value: ['/path/to/error.log', 'syslog:server=localhost'], + match: [ + ' error_log /path/to/error.log;', + ' error_log syslog:server=localhost;' + ] + }, + { + title: 'should not include error_log in server when set to absent', + attr: 'error_log', + value: 'absent', + notmatch: 'error_log' + }, + { + title: 'should set error_pages', + attr: 'error_pages', + value: { '503' => '/foo.html' }, + match: ' error_page 503 /foo.html;' + }, + { + title: 'should contain raw_prepend directives', + attr: 'raw_prepend', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + }, + { + title: 'should contain ordered prepend directives', + attr: 'server_cfg_prepend', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;' + ] + }, + { + title: 'should contain ordered ssl prepend directives', + attr: 'server_cfg_ssl_prepend', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;' + ] + }, + { + title: 'should set root', + attr: 'use_default_location', + value: false, + match: ' root /;' + }, + { + title: 'should not set root', + attr: 'use_default_location', + value: true, + notmatch: %r{ root /;} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :params do + default_params.merge(param[:attr].to_sym => param[:value], + :ssl => true, + :ssl_key => 'dummy.key', + :ssl_cert => 'dummy.crt') + end + it { is_expected.to contain_concat__fragment("#{title}-ssl-header") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-ssl-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment("#{title}-ssl-header").without_content(item) + end + end end end end - end - end - context 'attribute resources' do - context 'with SSL enabled, www rewrite to naked domain with multiple server_names' do - let(:title) { 'foo.com' } - let(:params) do - { - ssl: true, - ssl_cert: 'cert', - ssl_key: 'key', - server_name: %w(www.foo.com bar.foo.com foo.com), - use_default_location: false, - rewrite_www_to_non_www: true - } + describe 'server_ssl_footer template content' do + [ + { + title: 'should not contain www to non-www rewrite', + attr: 'rewrite_www_to_non_www', + value: false, + notmatch: %r{ + ^ + \s+server_name\s+www\.rspec\.example\.com;\n + \s+return\s+301\s+https://rspec\.example\.com\$request_uri; + }x + }, + { + title: 'should contain include directives', + attr: 'include_files', + value: ['/file1', '/file2'], + match: [ + %r{^\s+include\s+/file1;}, + %r{^\s+include\s+/file2;} + ] + }, + { + title: 'should contain ordered appended directives', + attr: 'server_cfg_append', + value: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2;' + ] + }, + { + title: 'should contain raw_append directives', + attr: 'raw_append', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + }, + { + title: 'should contain ordered ssl appended directives', + attr: 'server_cfg_ssl_append', + value: { 'test1' => 'test value 1', 'test2' => ['test value 2a', 'test value 2b'], 'allow' => 'test value 3' }, + match: [ + ' allow test value 3;', + ' test1 test value 1;', + ' test2 test value 2a;', + ' test2 test value 2b;' + ] + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let :params do + default_params.merge(param[:attr].to_sym => param[:value], + :ssl => true, + :ssl_key => 'dummy.key', + :ssl_cert => 'dummy.crt') + end + + it { is_expected.to contain_concat__fragment("#{title}-ssl-footer") } + it param[:title] do + matches = Array(param[:match]) + + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-ssl-footer").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-ssl-footer").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment("#{title}-ssl-footer").without_content(item) + end + end + end + end end - it "sets the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do - is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;}) - end - end + context 'attribute resources' do + context 'with SSL enabled, www rewrite to naked domain with multiple server_names' do + let(:title) { 'foo.com' } + let(:params) do + { + ssl: true, + ssl_cert: 'cert', + ssl_key: 'key', + server_name: %w(www.foo.com bar.foo.com foo.com), + use_default_location: false, + rewrite_www_to_non_www: true + } + end - context 'with SSL disabled, www rewrite to naked domain with multiple server_names' do - let(:title) { 'foo.com' } - let(:params) do - { - server_name: %w(www.foo.com bar.foo.com foo.com), - use_default_location: false, - rewrite_www_to_non_www: true - } - end + it "sets the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do + is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;}) + end + end - it "sets the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do - is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;}) - end - end + context 'with SSL disabled, www rewrite to naked domain with multiple server_names' do + let(:title) { 'foo.com' } + let(:params) do + { + server_name: %w(www.foo.com bar.foo.com foo.com), + use_default_location: false, + rewrite_www_to_non_www: true + } + end - context 'ssl_redirect' do - let(:params) { { ssl_redirect: true } } + it "sets the server_name of the rewrite server stanza to every server_name with 'www.' stripped" do + is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{^\s+server_name\s+foo.com\s+bar.foo.com\s+foo.com;}) + end + end - it { is_expected.to contain_concat__fragment("#{title}-header").without_content(%r{^\s*index\s+}) } - it { is_expected.to contain_concat__fragment("#{title}-header").without_content(%r{^\s*location\s+}) } - end + context 'ssl_redirect' do + let(:params) { { ssl_redirect: true } } - context 'ssl_redirect with alternate port' do - let(:params) { { ssl_redirect: true, ssl_port: 8888 } } + it { is_expected.to contain_concat__fragment("#{title}-header").without_content(%r{^\s*index\s+}) } + it { is_expected.to contain_concat__fragment("#{title}-header").without_content(%r{^\s*location\s+}) } + end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host:8888\$request_uri;}) } - end + context 'ssl_redirect with alternate port' do + let(:params) { { ssl_redirect: true, ssl_port: 8888 } } - context 'ssl_redirect with standard port set explicitly' do - let(:params) { { ssl_redirect: true, ssl_port: 443 } } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host:8888\$request_uri;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host\$request_uri;}) } - end + context 'ssl_redirect with standard port set explicitly' do + let(:params) { { ssl_redirect: true, ssl_port: 443 } } - context 'ssl_redirect with overridden port' do - let(:params) { { ssl_redirect: true, ssl_redirect_port: 8878 } } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host\$request_uri;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host:8878\$request_uri;}) } - end + context 'ssl_redirect with overridden port' do + let(:params) { { ssl_redirect: true, ssl_redirect_port: 8878 } } - context 'ssl_redirect with ssl_port set and overridden redirect port' do - let(:params) do - { - ssl_redirect: true, - ssl_redirect_port: 9787, - ssl_port: 9783 - } - end + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host:8878\$request_uri;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host:9787\$request_uri;}) } - end + context 'ssl_redirect with ssl_port set and overridden redirect port' do + let(:params) do + { + ssl_redirect: true, + ssl_redirect_port: 9787, + ssl_port: 9783 + } + end - context 'ssl_redirect should set ssl_only when ssl => true' do - let(:params) do - { - ssl_redirect: true, - ssl: true, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.crt' - } - end + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{ return 301 https://\$host:9787\$request_uri;}) } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) } - end + context 'ssl_redirect should set ssl_only when ssl => true' do + let(:params) do + { + ssl_redirect: true, + ssl: true, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.crt' + } + end - context 'ssl_redirect should not include default location when ssl => false' do - let(:params) do - { - ssl_redirect: true, - ssl: false - } - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) } + end - it { is_expected.not_to contain_nginx__resource__location("#{title}-default") } - end + context 'ssl_redirect should not include default location when ssl => false' do + let(:params) do + { + ssl_redirect: true, + ssl: false + } + end - context 'SSL cert and key are both set to fully qualified paths' do - let(:params) { { ssl: true, ssl_cert: '/tmp/foo.crt', ssl_key: '/tmp/foo.key:' } } + it { is_expected.not_to contain_nginx__resource__location("#{title}-default") } + end - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate\s+/tmp/foo.crt}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate_key\s+/tmp/foo.key}) } - end + context 'SSL cert and key are both set to fully qualified paths' do + let(:params) { { ssl: true, ssl_cert: '/tmp/foo.crt', ssl_key: '/tmp/foo.key:' } } - context 'SSL cert and key are both set to false' do - let(:params) { { ssl: true, ssl_cert: false, ssl_key: false } } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate\s+/tmp/foo.crt}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate_key\s+/tmp/foo.key}) } + end - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").without_content(%r{ssl_certificate}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").without_content(%r{ssl_certificate_key}) } - end + context 'SSL cert and key are both set to false' do + let(:params) { { ssl: true, ssl_cert: false, ssl_key: false } } - context 'when use_default_location => true' do - let :params do - default_params.merge(use_default_location: true) - end + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").without_content(%r{ssl_certificate}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").without_content(%r{ssl_certificate_key}) } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default") } - end + context 'when use_default_location => true' do + let :params do + default_params.merge(use_default_location: true) + end - context 'when use_default_location => false' do - let :params do - default_params.merge(use_default_location: false) - end + it { is_expected.to contain_nginx__resource__location("#{title}-default") } + end - it { is_expected.not_to contain_nginx__resource__location("#{title}-default") } - end + context 'when use_default_location => false' do + let :params do + default_params.merge(use_default_location: false) + end - context 'when location_cfg_prepend => { key => value }' do - let :params do - default_params.merge(location_cfg_prepend: { 'key' => 'value' }) - end + it { is_expected.not_to contain_nginx__resource__location("#{title}-default") } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_location_cfg_prepend('key' => 'value') } - end + context 'when location_cfg_prepend => { key => value }' do + let :params do + default_params.merge(location_cfg_prepend: { 'key' => 'value' }) + end - context "when location_raw_prepend => [ 'foo;' ]" do - let :params do - default_params.merge(location_raw_prepend: ['foo;']) - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_location_cfg_prepend('key' => 'value') } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_raw_prepend(['foo;']) } - end + context "when location_raw_prepend => [ 'foo;' ]" do + let :params do + default_params.merge(location_raw_prepend: ['foo;']) + end - context "when location_raw_append => [ 'foo;' ]" do - let :params do - default_params.merge(location_raw_append: ['foo;']) - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_raw_prepend(['foo;']) } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_raw_append(['foo;']) } - end + context "when location_raw_append => [ 'foo;' ]" do + let :params do + default_params.merge(location_raw_append: ['foo;']) + end - context 'when location_cfg_append => { key => value }' do - let :params do - default_params.merge(location_cfg_append: { 'key' => 'value' }) - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_raw_append(['foo;']) } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_location_cfg_append('key' => 'value') } - end + context 'when location_cfg_append => { key => value }' do + let :params do + default_params.merge(location_cfg_append: { 'key' => 'value' }) + end - context 'when fastcgi => "localhost:9000"' do - let :params do - default_params.merge(fastcgi: 'localhost:9000') - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_location_cfg_append('key' => 'value') } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_params('/etc/nginx/fastcgi.conf') } - it { is_expected.to contain_file('/etc/nginx/fastcgi.conf').with_mode('0644') } - end + context 'when fastcgi => "localhost:9000"' do + let :params do + default_params.merge(fastcgi: 'localhost:9000') + end - context 'when fastcgi_params is non-default' do - let :params do - default_params.merge(fastcgi: 'localhost:9000', - fastcgi_params: '/etc/nginx/mycustomparams') - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_params('/etc/nginx/fastcgi.conf') } + it { is_expected.to contain_file('/etc/nginx/fastcgi.conf').with_mode('0644') } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_params('/etc/nginx/mycustomparams') } - it { is_expected.not_to contain_file('/etc/nginx/mycustomparams') } - end + context 'when fastcgi_params is non-default' do + let :params do + default_params.merge(fastcgi: 'localhost:9000', + fastcgi_params: '/etc/nginx/mycustomparams') + end - context 'when fastcgi_params is not defined' do - let :params do - default_params.merge(fastcgi: 'localhost:9000', - fastcgi_params: nil) - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_params('/etc/nginx/mycustomparams') } + it { is_expected.not_to contain_file('/etc/nginx/mycustomparams') } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_params('nil') } - it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } - end + context 'when fastcgi_params is not defined' do + let :params do + default_params.merge(fastcgi: 'localhost:9000', + fastcgi_params: nil) + end - context 'when fastcgi_index => "index.php"' do - let :params do - default_params.merge(fastcgi_index: 'index.php') - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_params('nil') } + it { is_expected.not_to contain_file('/etc/nginx/fastcgi.conf') } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_index('index.php') } - end + context 'when fastcgi_index => "index.php"' do + let :params do + default_params.merge(fastcgi_index: 'index.php') + end - context 'when fastcgi_param => {key => value}' do - let :params do - default_params.merge(fastcgi_param: { 'key' => 'value' }) - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_index('index.php') } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_param('key' => 'value') } - end + context 'when fastcgi_param => {key => value}' do + let :params do + default_params.merge(fastcgi_param: { 'key' => 'value' }) + end - context 'when uwsgi => "uwsgi_upstream"' do - let :params do - default_params.merge(uwsgi: 'uwsgi_upstream') - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_fastcgi_param('key' => 'value') } + end - it { is_expected.to contain_file('/etc/nginx/uwsgi_params').with_mode('0644') } - end + context 'when uwsgi => "uwsgi_upstream"' do + let :params do + default_params.merge(uwsgi: 'uwsgi_upstream') + end - context 'when uwsgi_params is non-default' do - let :params do - default_params.merge(uwsgi: 'uwsgi_upstream', - uwsgi_params: '/etc/nginx/bogusparams') - end + it { is_expected.to contain_file('/etc/nginx/uwsgi_params').with_mode('0644') } + end - it { is_expected.not_to contain_file('/etc/nginx/bogusparams') } - end + context 'when uwsgi_params is non-default' do + let :params do + default_params.merge(uwsgi: 'uwsgi_upstream', + uwsgi_params: '/etc/nginx/bogusparams') + end - context 'when listen_port == ssl_port but ssl = false' do - let :params do - default_params.merge(listen_port: 80, - ssl_port: 80, - ssl: false) - end + it { is_expected.not_to contain_file('/etc/nginx/bogusparams') } + end - # TODO: implement test after this can be tested - # msg = %r{nginx: ssl must be true if listen_port is the same as ssl_port} - it 'Testing for warnings not yet implemented in classes' - end + context 'when listen_port == ssl_port but ssl = false' do + let :params do + default_params.merge(listen_port: 80, + ssl_port: 80, + ssl: false) + end - context 'when listen_port != ssl_port' do - let :params do - default_params.merge(listen_port: 80, - ssl_port: 443) - end + # TODO: implement test after this can be tested + # msg = %r{nginx: ssl must be true if listen_port is the same as ssl_port} + it 'Testing for warnings not yet implemented in classes' + end - it { is_expected.to contain_concat__fragment("#{title}-header") } - it { is_expected.to contain_concat__fragment("#{title}-footer") } - end + context 'when listen_port != ssl_port' do + let :params do + default_params.merge(listen_port: 80, + ssl_port: 443) + end - context 'when ensure => absent' do - let :params do - default_params.merge(ensure: 'absent', - ssl: true, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert') - end + it { is_expected.to contain_concat__fragment("#{title}-header") } + it { is_expected.to contain_concat__fragment("#{title}-footer") } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ensure('absent') } - it { is_expected.to contain_file("#{title}.conf symlink").with_ensure('absent') } - end + context 'when ensure => absent' do + let :params do + default_params.merge(ensure: 'absent', + ssl: true, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert') + end - context 'when ssl => true and ssl_port == listen_port' do - let :params do - default_params.merge(ssl: true, - listen_port: 80, - ssl_port: 80, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert') - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ensure('absent') } + it { is_expected.to contain_file("#{title}.conf symlink").with_ensure('absent') } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log combined;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate\s+dummy.cert;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate_key\s+dummy.key;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-footer") } - end + context 'when ssl => true and ssl_port == listen_port' do + let :params do + default_params.merge(ssl: true, + listen_port: 80, + ssl_port: 80, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert') + end - context 'when ssl_client_cert is set' do - let :params do - default_params.merge(ssl: true, - listen_port: 80, - ssl_port: 80, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert', - ssl_client_cert: 'client.cert', - ssl_verify_client: 'optional') - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log combined;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate\s+dummy.cert;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_certificate_key\s+dummy.key;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-footer") } + end - it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log combined;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_verify_client\s+optional;}) } - end - context 'when passenger_cgi_param is set' do - let :params do - default_params.merge(passenger_cgi_param: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }) - end + context 'when ssl_client_cert is set' do + let :params do + default_params.merge(ssl: true, + listen_port: 80, + ssl_port: 80, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert', + ssl_client_cert: 'client.cert', + ssl_verify_client: 'optional') + end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_cgi_param test1 test value 1;}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_cgi_param test2 test value 2;}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_cgi_param test3 test value 3;}) } - end + it { is_expected.to contain_nginx__resource__location("#{title}-default").with_ssl_only(true) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{access_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.access\.log combined;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{error_log\s+/var/log/nginx/ssl-www\.rspec\.example\.com\.error\.log}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{ssl_verify_client\s+optional;}) } + end + context 'when passenger_cgi_param is set' do + let :params do + default_params.merge(passenger_cgi_param: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }) + end - context 'when passenger_cgi_param is set and ssl => true' do - let :params do - default_params.merge(passenger_cgi_param: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }, - ssl: true, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert') - end + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_cgi_param test1 test value 1;}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_cgi_param test2 test value 2;}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_cgi_param test3 test value 3;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_cgi_param test1 test value 1;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_cgi_param test2 test value 2;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_cgi_param test3 test value 3;}) } - end + context 'when passenger_cgi_param is set and ssl => true' do + let :params do + default_params.merge(passenger_cgi_param: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }, + ssl: true, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert') + end - context 'when passenger_set_header is set' do - let :params do - default_params.merge(passenger_set_header: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }) - end + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_cgi_param test1 test value 1;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_cgi_param test2 test value 2;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_cgi_param test3 test value 3;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_header test1 test value 1;}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_header test2 test value 2;}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_header test3 test value 3;}) } - end + context 'when passenger_set_header is set' do + let :params do + default_params.merge(passenger_set_header: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }) + end - context 'when passenger_set_header is set and ssl => true' do - let :params do - default_params.merge(passenger_set_header: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }, - ssl: true, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert') - end + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_header test1 test value 1;}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_header test2 test value 2;}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_set_header test3 test value 3;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_header test1 test value 1;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_header test2 test value 2;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_header test3 test value 3;}) } - end + context 'when passenger_set_header is set and ssl => true' do + let :params do + default_params.merge(passenger_set_header: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }, + ssl: true, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert') + end - context 'when passenger_env_var is set' do - let :params do - default_params.merge(passenger_env_var: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }) - end + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_header test1 test value 1;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_header test2 test value 2;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_set_header test3 test value 3;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_env_var test1 test value 1;}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_env_var test2 test value 2;}) } - it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_env_var test3 test value 3;}) } - end + context 'when passenger_env_var is set' do + let :params do + default_params.merge(passenger_env_var: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }) + end - context 'when passenger_env_var is set and ssl => true' do - let :params do - default_params.merge(passenger_env_var: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }, - ssl: true, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert') - end + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_env_var test1 test value 1;}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_env_var test2 test value 2;}) } + it { is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{passenger_env_var test3 test value 3;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_env_var test1 test value 1;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_env_var test2 test value 2;}) } - it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_env_var test3 test value 3;}) } - end + context 'when passenger_env_var is set and ssl => true' do + let :params do + default_params.merge(passenger_env_var: { 'test1' => 'test value 1', 'test2' => 'test value 2', 'test3' => 'test value 3' }, + ssl: true, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert') + end - context 'when passenger_pre_start is a string' do - let :params do - default_params.merge(passenger_pre_start: 'http://example.com:80/test/me') - end + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_env_var test1 test value 1;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_env_var test2 test value 2;}) } + it { is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{passenger_env_var test3 test value 3;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-footer").with_content(%r{passenger_pre_start http://example.com:80/test/me;}) } - end + context 'when passenger_pre_start is a string' do + let :params do + default_params.merge(passenger_pre_start: 'http://example.com:80/test/me') + end - context 'when passenger_pre_start is an array' do - let :params do - default_params.merge(passenger_pre_start: ['http://example.com:80/test/me', 'http://example.com:3009/foo/bar']) - end + it { is_expected.to contain_concat__fragment("#{title}-footer").with_content(%r{passenger_pre_start http://example.com:80/test/me;}) } + end - it { is_expected.to contain_concat__fragment("#{title}-footer").with_content(%r{passenger_pre_start http://example.com:80/test/me;}) } - it { is_expected.to contain_concat__fragment("#{title}-footer").with_content(%r{passenger_pre_start http://example.com:3009/foo/bar;}) } - end + context 'when passenger_pre_start is an array' do + let :params do + default_params.merge(passenger_pre_start: ['http://example.com:80/test/me', 'http://example.com:3009/foo/bar']) + end - context 'when server name is sanitized' do - let(:title) { 'www rspec-server com' } - let(:params) { default_params } + it { is_expected.to contain_concat__fragment("#{title}-footer").with_content(%r{passenger_pre_start http://example.com:80/test/me;}) } + it { is_expected.to contain_concat__fragment("#{title}-footer").with_content(%r{passenger_pre_start http://example.com:3009/foo/bar;}) } + end - it { is_expected.to contain_concat('/etc/nginx/sites-available/www_rspec-server_com.conf') } - end + context 'when server name is sanitized' do + let(:title) { 'www rspec-server com' } + let(:params) { default_params } - context 'when add_header is set' do - let :params do - default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' }) - end + it { is_expected.to contain_concat('/etc/nginx/sites-available/www_rspec-server_com.conf') } + end - it 'has correctly ordered entries in the config' do - is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n}) - end - end + context 'when add_header is set' do + let :params do + default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' }) + end - context 'when add_header is set and ssl => true' do - let :params do - default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' }, - ssl: true, - ssl_key: 'dummy.key', - ssl_cert: 'dummy.cert') - end + it 'has correctly ordered entries in the config' do + is_expected.to contain_concat__fragment("#{title}-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n}) + end + end - it 'has correctly ordered entries in the config' do - is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n}) + context 'when add_header is set and ssl => true' do + let :params do + default_params.merge(add_header: { 'header3' => 'test value 3', 'header2' => 'test value 2', 'header1' => 'test value 1' }, + ssl: true, + ssl_key: 'dummy.key', + ssl_cert: 'dummy.cert') + end + + it 'has correctly ordered entries in the config' do + is_expected.to contain_concat__fragment("#{title}-ssl-header").with_content(%r{\s+add_header\s+"header1" "test value 1";\n\s+add_header\s+"header2" "test value 2";\n\s+add_header\s+"header3" "test value 3";\n}) + end + end end - end - end - describe 'with locations' do - context 'simple location' do - let(:params) do - { - use_default_location: false, - locations: { - 'one' => { - 'location_custom_cfg' => {}, - 'location' => '/one', - 'expires' => '@12h34m' + describe 'with locations' do + context 'simple location' do + let(:params) do + { + use_default_location: false, + locations: { + 'one' => { + 'location_custom_cfg' => {}, + 'location' => '/one', + 'expires' => '@12h34m' + } + } } - } - } - end - it { is_expected.to contain_nginx__resource__location('one') } - it { is_expected.to contain_nginx__resource__location('one').with_location('/one') } - it { is_expected.to contain_nginx__resource__location('one').with_expires('@12h34m') } - end + end + it { is_expected.to contain_nginx__resource__location('one') } + it { is_expected.to contain_nginx__resource__location('one').with_location('/one') } + it { is_expected.to contain_nginx__resource__location('one').with_expires('@12h34m') } + end - context 'multiple locations' do - let(:params) do - { - use_default_location: false, - locations: { - 'one' => { - 'location_custom_cfg' => {}, - 'location' => '/one', - 'expires' => '@12h34m' - }, - 'two' => { - 'location_custom_cfg' => {}, - 'location' => '= /two', - 'expires' => '@23h45m' + context 'multiple locations' do + let(:params) do + { + use_default_location: false, + locations: { + 'one' => { + 'location_custom_cfg' => {}, + 'location' => '/one', + 'expires' => '@12h34m' + }, + 'two' => { + 'location_custom_cfg' => {}, + 'location' => '= /two', + 'expires' => '@23h45m' + } + } } - } - } - end - it { is_expected.to contain_nginx__resource__location('one') } - it { is_expected.to contain_nginx__resource__location('one').with_location('/one') } - it { is_expected.to contain_nginx__resource__location('one').with_expires('@12h34m') } - it { is_expected.to contain_nginx__resource__location('two') } - it { is_expected.to contain_nginx__resource__location('two').with_location('= /two') } - it { is_expected.to contain_nginx__resource__location('two').with_expires('@23h45m') } - end + end + it { is_expected.to contain_nginx__resource__location('one') } + it { is_expected.to contain_nginx__resource__location('one').with_location('/one') } + it { is_expected.to contain_nginx__resource__location('one').with_expires('@12h34m') } + it { is_expected.to contain_nginx__resource__location('two') } + it { is_expected.to contain_nginx__resource__location('two').with_location('= /two') } + it { is_expected.to contain_nginx__resource__location('two').with_expires('@23h45m') } + end - context 'with locations default' do - let(:params) do - { - www_root: '/toplevel', - locations_defaults: { - 'www_root' => '/overwrite', - 'expires' => '@12h34m' - }, - locations: { - 'one' => { - 'location_custom_cfg' => {}, - 'location' => '/one' - }, - 'two' => { - 'location_custom_cfg' => {}, - 'location' => '= /two' + context 'with locations default' do + let(:params) do + { + www_root: '/toplevel', + locations_defaults: { + 'www_root' => '/overwrite', + 'expires' => '@12h34m' + }, + locations: { + 'one' => { + 'location_custom_cfg' => {}, + 'location' => '/one' + }, + 'two' => { + 'location_custom_cfg' => {}, + 'location' => '= /two' + } + } } - } - } + end + it { is_expected.to contain_nginx__resource__location('one') } + it { is_expected.to contain_nginx__resource__location('one').with_location('/one') } + it { is_expected.to contain_nginx__resource__location('one').with_www_root('/overwrite') } + it { is_expected.to contain_nginx__resource__location('one').with_expires('@12h34m') } + it { is_expected.to contain_nginx__resource__location('two') } + it { is_expected.to contain_nginx__resource__location('two').with_location('= /two') } + it { is_expected.to contain_nginx__resource__location('two').with_www_root('/overwrite') } + it { is_expected.to contain_nginx__resource__location('two').with_expires('@12h34m') } + end end - it { is_expected.to contain_nginx__resource__location('one') } - it { is_expected.to contain_nginx__resource__location('one').with_location('/one') } - it { is_expected.to contain_nginx__resource__location('one').with_www_root('/overwrite') } - it { is_expected.to contain_nginx__resource__location('one').with_expires('@12h34m') } - it { is_expected.to contain_nginx__resource__location('two') } - it { is_expected.to contain_nginx__resource__location('two').with_location('= /two') } - it { is_expected.to contain_nginx__resource__location('two').with_www_root('/overwrite') } - it { is_expected.to contain_nginx__resource__location('two').with_expires('@12h34m') } end end end diff --git a/spec/defines/resource_stream_spec.rb b/spec/defines/resource_stream_spec.rb index 29dc8762b..37743c25c 100644 --- a/spec/defines/resource_stream_spec.rb +++ b/spec/defines/resource_stream_spec.rb @@ -1,141 +1,142 @@ require 'spec_helper' describe 'nginx::resource::streamhost' do - let :title do - 'www.rspec.example.com' - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end + let :title do + 'www.rspec.example.com' + end - let :default_params do - { - ipv6_enable: true - } - end + let :default_params do + { + ipv6_enable: true + } + end - let :facts do - { - ipaddress6: '::' - } - end + let :pre_condition do + [ + 'include ::nginx' + ] + end - let :pre_condition do - [ - 'include ::nginx' - ] - end + describe 'os-independent items' do + describe 'basic assumptions' do + let(:params) { default_params } + it { is_expected.to contain_class('nginx') } + it do + is_expected.to contain_concat("/etc/nginx/streams-available/#{title}.conf").with('owner' => 'root', + 'group' => 'root', + 'mode' => '0644') + end + it do + is_expected.to contain_file("#{title}.conf symlink").with('ensure' => 'link', + 'path' => "/etc/nginx/streams-enabled/#{title}.conf", + 'target' => "/etc/nginx/streams-available/#{title}.conf") + end + end - describe 'os-independent items' do - describe 'basic assumptions' do - let(:params) { default_params } - it { is_expected.to contain_class('nginx') } - it do - is_expected.to contain_concat("/etc/nginx/streams-available/#{title}.conf").with('owner' => 'root', + describe 'when confd_only true' do + let(:pre_condition) { 'class { "nginx": confd_only => true }' } + let(:params) { default_params } + it { is_expected.to contain_class('nginx') } + it do + is_expected.to contain_concat("/etc/nginx/conf.stream.d/#{title}.conf").with('owner' => 'root', 'group' => 'root', - 'mode' => '0644') - end - it do - is_expected.to contain_file("#{title}.conf symlink").with('ensure' => 'link', - 'path' => "/etc/nginx/streams-enabled/#{title}.conf", - 'target' => "/etc/nginx/streams-available/#{title}.conf") - end - end - - describe 'when confd_only true' do - let(:pre_condition) { 'class { "nginx": confd_only => true }' } - let(:params) { default_params } - it { is_expected.to contain_class('nginx') } - it do - is_expected.to contain_concat("/etc/nginx/conf.stream.d/#{title}.conf").with('owner' => 'root', - 'group' => 'root', - 'mode' => '0644') - end - end + 'mode' => '0644') + end + end - describe 'server_header template content' do - [ - { - title: 'should set the IPv4 listen IP', - attr: 'listen_ip', - value: '127.0.0.1', - match: %r{\s+listen\s+127.0.0.1:80;} - }, - { - title: 'should set the IPv4 listen port', - attr: 'listen_port', - value: 45, - match: %r{\s+listen\s+\*:45;} - }, - { - title: 'should set the IPv4 listen options', - attr: 'listen_options', - value: 'spdy default', - match: %r{\s+listen\s+\*:80 spdy default;} - }, - { - title: 'should enable IPv6', - attr: 'ipv6_enable', - value: true, - match: %r{\s+listen\s+\[::\]:80 default ipv6only=on;} - }, - { - title: 'should not enable IPv6', - attr: 'ipv6_enable', - value: false, - notmatch: %r{\slisten \[::\]:80 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen IP', - attr: 'ipv6_listen_ip', - value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', - match: %r{\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:80 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen port', - attr: 'ipv6_listen_port', - value: 45, - match: %r{\s+listen\s+\[::\]:45 default ipv6only=on;} - }, - { - title: 'should set the IPv6 listen options', - attr: 'ipv6_listen_options', - value: 'spdy', - match: %r{\s+listen\s+\[::\]:80 spdy;} - }, - { - title: 'should contain raw_prepend directives', - attr: 'raw_prepend', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - }, - { - title: 'should contain raw_append directives', - attr: 'raw_append', - value: [ - 'if (a) {', - ' b;', - '}' - ], - match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + describe 'server_header template content' do + [ + { + title: 'should set the IPv4 listen IP', + attr: 'listen_ip', + value: '127.0.0.1', + match: %r{\s+listen\s+127.0.0.1:80;} + }, + { + title: 'should set the IPv4 listen port', + attr: 'listen_port', + value: 45, + match: %r{\s+listen\s+\*:45;} + }, + { + title: 'should set the IPv4 listen options', + attr: 'listen_options', + value: 'spdy default', + match: %r{\s+listen\s+\*:80 spdy default;} + }, + { + title: 'should enable IPv6', + attr: 'ipv6_enable', + value: true, + match: %r{\s+listen\s+\[::\]:80 default ipv6only=on;} + }, + { + title: 'should not enable IPv6', + attr: 'ipv6_enable', + value: false, + notmatch: %r{\slisten \[::\]:80 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen IP', + attr: 'ipv6_listen_ip', + value: '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + match: %r{\s+listen\s+\[2001:0db8:85a3:0000:0000:8a2e:0370:7334\]:80 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen port', + attr: 'ipv6_listen_port', + value: 45, + match: %r{\s+listen\s+\[::\]:45 default ipv6only=on;} + }, + { + title: 'should set the IPv6 listen options', + attr: 'ipv6_listen_options', + value: 'spdy', + match: %r{\s+listen\s+\[::\]:80 spdy;} + }, + { + title: 'should contain raw_prepend directives', + attr: 'raw_prepend', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + }, + { + title: 'should contain raw_append directives', + attr: 'raw_append', + value: [ + 'if (a) {', + ' b;', + '}' + ], + match: %r{^\s+if \(a\) \{\n\s++b;\n\s+\}} + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - it { is_expected.to contain_concat__fragment("#{title}-header") } - it param[:title] do - matches = Array(param[:match]) + it { is_expected.to contain_concat__fragment("#{title}-header") } + it param[:title] do + matches = Array(param[:match]) - if matches.all? { |m| m.is_a? Regexp } - matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } - else - lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - end - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment("#{title}-header").without_content(item) + if matches.all? { |m| m.is_a? Regexp } + matches.each { |item| is_expected.to contain_concat__fragment("#{title}-header").with_content(item) } + else + lines = catalogue.resource('concat::fragment', "#{title}-header").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + end + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment("#{title}-header").without_content(item) + end + end end end end diff --git a/spec/defines/resource_upstream_spec.rb b/spec/defines/resource_upstream_spec.rb index af28b1d32..50ff66f98 100644 --- a/spec/defines/resource_upstream_spec.rb +++ b/spec/defines/resource_upstream_spec.rb @@ -1,134 +1,141 @@ require 'spec_helper' describe 'nginx::resource::upstream' do - let :title do - 'upstream-test' - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts + end + let :title do + 'upstream-test' + end - let :default_params do - { - members: ['test'] - } - end + let :default_params do + { + members: ['test'] + } + end - let :pre_condition do - [ - 'include ::nginx' - ] - end + let :pre_condition do + [ + 'include ::nginx' + ] + end - describe 'os-independent items' do - describe 'basic assumptions' do - let(:params) { default_params } + describe 'os-independent items' do + describe 'basic assumptions' do + let(:params) { default_params } - it { is_expected.to contain_concat("/etc/nginx/conf.d/#{title}-upstream.conf").that_requires('File[/etc/nginx/conf.d]') } - it { is_expected.to contain_concat__fragment("#{title}_upstream_header").with_content(%r{upstream #{title}}) } + it { is_expected.to contain_concat("/etc/nginx/conf.d/#{title}-upstream.conf").that_requires('File[/etc/nginx/conf.d]') } + it { is_expected.to contain_concat__fragment("#{title}_upstream_header").with_content(%r{upstream #{title}}) } - it do - is_expected.to contain_concat__fragment("#{title}_upstream_header").with( - 'target' => "/etc/nginx/conf.d/#{title}-upstream.conf", - 'order' => 10 - ) - end + it do + is_expected.to contain_concat__fragment("#{title}_upstream_header").with( + 'target' => "/etc/nginx/conf.d/#{title}-upstream.conf", + 'order' => 10 + ) + end - it do - is_expected.to contain_concat__fragment("#{title}_upstream_members").with( - 'target' => "/etc/nginx/conf.d/#{title}-upstream.conf", - 'order' => 50 - ) - end + it do + is_expected.to contain_concat__fragment("#{title}_upstream_members").with( + 'target' => "/etc/nginx/conf.d/#{title}-upstream.conf", + 'order' => 50 + ) + end - it do - is_expected.to contain_concat__fragment("#{title}_upstream_footer").with( - 'target' => "/etc/nginx/conf.d/#{title}-upstream.conf", - 'order' => 90 - ).with_content("}\n") - end - end + it do + is_expected.to contain_concat__fragment("#{title}_upstream_footer").with( + 'target' => "/etc/nginx/conf.d/#{title}-upstream.conf", + 'order' => 90 + ).with_content("}\n") + end + end - describe 'upstream.conf template content' do - [ - { - title: 'should contain ordered prepended directives', - attr: 'upstream_cfg_prepend', - fragment: 'header', - value: { - 'test3' => 'test value 3', - 'test6' => { 'subkey1' => %w(subvalue1 subvalue2) }, - 'keepalive' => 'keepalive 1', - 'test2' => 'test value 2', - 'test5' => { 'subkey1' => 'subvalue1' }, - 'test4' => ['test value 1', 'test value 2'] - }, - match: [ - ' test2 test value 2;', - ' test3 test value 3;', - ' test4 test value 1;', - ' test4 test value 2;', - ' test5 subkey1 subvalue1;', - ' test6 subkey1 subvalue1;', - ' test6 subkey1 subvalue2;', - ' keepalive keepalive 1;' - ] - }, - { - title: 'should set server', - attr: 'members', - fragment: 'members', - value: %w(test3 test1 test2), - match: [ - ' server test3 fail_timeout=10s;', - ' server test1 fail_timeout=10s;', - ' server test2 fail_timeout=10s;' - ] - }, - { - title: 'should contain ordered appended directives', - attr: 'upstream_cfg_append', - fragment: 'footer', - value: { - 'test3' => 'test value 3', - 'test6' => { 'subkey1' => %w(subvalue1 subvalue2) }, - 'keepalive' => 'keepalive 1', - 'test2' => 'test value 2', - 'test5' => { 'subkey1' => 'subvalue1' }, - 'test4' => ['test value 1', 'test value 2'] - }, - match: [ - ' test2 test value 2;', - ' test3 test value 3;', - ' test4 test value 1;', - ' test4 test value 2;', - ' test5 subkey1 subvalue1;', - ' test6 subkey1 subvalue1;', - ' test6 subkey1 subvalue2;', - ' keepalive keepalive 1;' - ] - } - ].each do |param| - context "when #{param[:attr]} is #{param[:value]}" do - let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } + describe 'upstream.conf template content' do + [ + { + title: 'should contain ordered prepended directives', + attr: 'upstream_cfg_prepend', + fragment: 'header', + value: { + 'test3' => 'test value 3', + 'test6' => { 'subkey1' => %w(subvalue1 subvalue2) }, + 'keepalive' => 'keepalive 1', + 'test2' => 'test value 2', + 'test5' => { 'subkey1' => 'subvalue1' }, + 'test4' => ['test value 1', 'test value 2'] + }, + match: [ + ' test2 test value 2;', + ' test3 test value 3;', + ' test4 test value 1;', + ' test4 test value 2;', + ' test5 subkey1 subvalue1;', + ' test6 subkey1 subvalue1;', + ' test6 subkey1 subvalue2;', + ' keepalive keepalive 1;' + ] + }, + { + title: 'should set server', + attr: 'members', + fragment: 'members', + value: %w(test3 test1 test2), + match: [ + ' server test3 fail_timeout=10s;', + ' server test1 fail_timeout=10s;', + ' server test2 fail_timeout=10s;' + ] + }, + { + title: 'should contain ordered appended directives', + attr: 'upstream_cfg_append', + fragment: 'footer', + value: { + 'test3' => 'test value 3', + 'test6' => { 'subkey1' => %w(subvalue1 subvalue2) }, + 'keepalive' => 'keepalive 1', + 'test2' => 'test value 2', + 'test5' => { 'subkey1' => 'subvalue1' }, + 'test4' => ['test value 1', 'test value 2'] + }, + match: [ + ' test2 test value 2;', + ' test3 test value 3;', + ' test4 test value 1;', + ' test4 test value 2;', + ' test5 subkey1 subvalue1;', + ' test6 subkey1 subvalue1;', + ' test6 subkey1 subvalue2;', + ' keepalive keepalive 1;' + ] + } + ].each do |param| + context "when #{param[:attr]} is #{param[:value]}" do + let(:params) { default_params.merge(param[:attr].to_sym => param[:value]) } - it { is_expected.to contain_concat("/etc/nginx/conf.d/#{title}-upstream.conf").with_mode('0644') } - it { is_expected.to contain_concat__fragment("#{title}_upstream_#{param[:fragment]}") } - it param[:title] do - lines = catalogue.resource('concat::fragment', "#{title}_upstream_#{param[:fragment]}").send(:parameters)[:content].split("\n") - expect(lines & Array(param[:match])).to eq(Array(param[:match])) - Array(param[:notmatch]).each do |item| - is_expected.to contain_concat__fragment("#{title}_upstream_#{param[:fragment]}").without_content(item) + it { is_expected.to contain_concat("/etc/nginx/conf.d/#{title}-upstream.conf").with_mode('0644') } + it { is_expected.to contain_concat__fragment("#{title}_upstream_#{param[:fragment]}") } + it param[:title] do + lines = catalogue.resource('concat::fragment', "#{title}_upstream_#{param[:fragment]}").send(:parameters)[:content].split("\n") + expect(lines & Array(param[:match])).to eq(Array(param[:match])) + Array(param[:notmatch]).each do |item| + is_expected.to contain_concat__fragment("#{title}_upstream_#{param[:fragment]}").without_content(item) + end + end end end - end - end - context 'when ensure => absent' do - let :params do - default_params.merge( - ensure: 'absent' - ) - end + context 'when ensure => absent' do + let :params do + default_params.merge( + ensure: 'absent' + ) + end - it { is_expected.to contain_concat("/etc/nginx/conf.d/#{title}-upstream.conf").with_ensure('absent') } + it { is_expected.to contain_concat("/etc/nginx/conf.d/#{title}-upstream.conf").with_ensure('absent') } + end + end end end end