diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index 9f4172898..8ab2b49d7 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -232,7 +232,9 @@ if ($autoindex != undef) { validate_string($autoindex) } - validate_array($index_files) + if ($index_files != undef) { + validate_array($index_files) + } if ($proxy != undef) { validate_string($proxy) } diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index 43ec31146..cdc364517 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -284,35 +284,71 @@ describe 'vhost_location_alias template content' do let :default_params do - { location: 'location', vhost: 'vhost1', location_alias: 'value' } + { + location: 'location', + vhost: 'vhost1', + location_alias: 'value' + } end - context "when location_alias is 'value'" do + context 'location_alias template with default params' do let(:params) { default_params } it { is_expected.to contain_concat__fragment('vhost1-500-' + Digest::MD5.hexdigest('location')) } it 'sets alias' do is_expected.to contain_concat__fragment('vhost1-500-' + Digest::MD5.hexdigest('location')). - with_content(%r{^[ ]+alias\s+value;}) + with_content(%r{^\s+alias\s+value;}) end - end - - context "when autoindex is 'on'" do - let(:params) { default_params.merge(autoindex: 'on') } - it { is_expected.to contain_concat__fragment('vhost1-500-' + Digest::MD5.hexdigest('location')) } - it 'sets autoindex' do + it "doesn't set try_files" do is_expected.to contain_concat__fragment('vhost1-500-' + Digest::MD5.hexdigest('location')). - with_content(%r{^[ ]+autoindex\s+on;}) + without_content(%r{^\s+try_files[^;]+;}) end - end - - context 'when autoindex is not set' do - let(:params) { default_params } - it { is_expected.to contain_concat__fragment('vhost1-500-' + Digest::MD5.hexdigest('location')) } - it 'does not set autoindex' do + it "doesn't set autoindex" do is_expected.to contain_concat__fragment('vhost1-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('vhost1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it param[:title] do + fragment = 'vhost1-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('vhost1-500-' + Digest::MD5.hexdigest(params[:location].to_s)).without_content(item) + end + end + end + end end describe 'vhost_location_directory template content' do diff --git a/templates/vhost/locations/alias.erb b/templates/vhost/locations/alias.erb index a588efd2e..eded012ac 100644 --- a/templates/vhost/locations/alias.erb +++ b/templates/vhost/locations/alias.erb @@ -2,3 +2,9 @@ <% if defined? @autoindex -%> autoindex <%= @autoindex %>; <% end -%> +<% if @index_files and @index_files.count > 0 -%> + index <% Array(@index_files).each do |i| %> <%= i %><% end %>; +<% end -%> +<% if @try_files -%> + try_files<% @try_files.each do |try| -%> <%= try %><% end -%>; +<% end -%> diff --git a/templates/vhost/locations/directory.erb b/templates/vhost/locations/directory.erb index 60850a0c1..778ce9052 100644 --- a/templates/vhost/locations/directory.erb +++ b/templates/vhost/locations/directory.erb @@ -5,7 +5,7 @@ autoindex <%= @autoindex %>; <% end -%> -<% if @index_files.count > 0 -%> +<% if @index_files and @index_files.count > 0 -%> index <% Array(@index_files).each do |i| %> <%= i %><% end %>; <% end -%> <% if @try_files -%>