2013-11-22 04:10:53 +01:00
require 'spec_helper'
2014-06-10 23:10:00 +02:00
require 'digest/md5'
2013-11-22 04:10:53 +01:00
describe 'nginx::resource::location' do
let :title do
'rspec-test'
end
let :pre_condition do
[
'include ::nginx::config' ,
]
end
describe 'os-independent items' do
describe 'basic assumptions' do
let :params do {
:www_root = > " /var/www/rspec " ,
:vhost = > 'vhost1' ,
} end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_class ( " nginx::config " ) }
it { is_expected . to contain_concat__fragment ( " f25e14942fb58942ee13b1465a4e1719 " ) . with_content ( / location rspec-test / ) }
it { is_expected . not_to contain_file ( '/etc/nginx/fastcgi_params' ) }
it { is_expected . not_to contain_concat__fragment ( " vhost1-800-rspec-test-ssl " ) }
it { is_expected . not_to contain_file ( " /etc/nginx/rspec-test_htpasswd " ) }
2013-11-22 04:10:53 +01:00
end
2014-06-26 05:40:22 +02:00
describe " vhost/location_header template content " do
2013-11-22 04:10:53 +01:00
[
{
:title = > 'should set the location' ,
:attr = > 'location' ,
:value = > 'my_location' ,
:match = > ' location my_location {' ,
} ,
2014-06-23 22:31:32 +02:00
{
:title = > 'should not set internal' ,
:attr = > 'internal' ,
:value = > false ,
:notmatch = > / internal; /
} ,
{
:title = > 'should set internal' ,
:attr = > 'internal' ,
:value = > true ,
:match = > ' internal;'
} ,
2014-08-19 03:07:22 +02:00
{
:title = > 'should not set mp4' ,
:attr = > 'mp4' ,
:value = > false ,
:notmatch = > / mp4; /
} ,
{
:title = > 'should set mp4' ,
:attr = > 'mp4' ,
:value = > true ,
:match = > ' mp4;'
} ,
{
:title = > 'should not set flv' ,
:attr = > 'flv' ,
:value = > false ,
:notmatch = > / flv; /
} ,
{
:title = > 'should set flv' ,
:attr = > 'flv' ,
:value = > true ,
:match = > ' flv;'
} ,
2014-06-26 05:40:22 +02:00
{
: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;' ,
] ,
} ,
2013-11-22 04:10:53 +01:00
{
:title = > 'should contain ordered prepended directives' ,
:attr = > 'location_cfg_prepend' ,
2014-06-26 05:40:22 +02:00
:value = > { 'test1' = > 'test value 1' , 'test2' = > [ 'test value 2a' , 'test value 2b' ] ,
'test3' = > { 'subtest1' = > [ '"sub test value1a"' , '"sub test value1b"' ] ,
2014-03-12 08:21:15 +01:00
'subtest2' = > '"sub test value2"' } } ,
2013-11-22 04:10:53 +01:00
:match = > [
2014-03-07 13:53:32 +01:00
' test1 test value 1;' ,
' test2 test value 2a;' ,
' test2 test value 2b;' ,
2014-06-26 05:40:22 +02:00
' test3 subtest1 "sub test value1a";' ,
' test3 subtest1 "sub test value1b";' ,
' test3 subtest2 "sub test value2";' ,
2013-11-22 04:10:53 +01:00
] ,
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should contain custom prepended directives' ,
:attr = > 'location_custom_cfg_prepend' ,
:value = > { 'test1' = > 'bar' , 'test2' = > [ 'foobar' , 'barbaz' ] ,
'test3' = > { 'subtest1' = > [ '"sub test value1a"' , '"sub test value1b"' ] ,
'subtest2' = > '"sub test value2"' } } ,
:match = > [
/ ^[ ]+test1 \ s+bar / ,
/ ^[ ]+test2 \ s+foobar / ,
/ ^[ ]+test2 \ s+barbaz / ,
/ ^[ ]+test3 \ s+subtest1 "sub test value1a" / ,
/ ^[ ]+test3 \ s+subtest1 "sub test value1b" / ,
/ ^[ ]+test3 \ s+subtest2 "sub test value2" / ,
] ,
2013-11-22 04:10:53 +01:00
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should contain raw_prepend directives' ,
:attr = > 'raw_prepend' ,
:value = > [
'if (a) {' ,
' b;' ,
'}'
] ,
:match = > / ^ \ s+if \ (a \ ) { \ n \ s++b; \ n \ s+ \ } / ,
2013-11-22 04:10:53 +01:00
} ,
2014-06-26 05:40:22 +02:00
] . each do | param |
context " when #{ param [ :attr ] } is #{ param [ :value ] } " do
let :default_params do { :location = > 'location' , :proxy = > 'proxy_value' , :vhost = > 'vhost1' } end
let :params do default_params . merge ( { param [ :attr ] . to_sym = > param [ :value ] } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2014-06-26 05:40:22 +02:00
it param [ :title ] do
fragment = Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " )
matches = Array ( param [ :match ] )
if matches . all? { | m | m . is_a? Regexp }
2014-07-18 04:28:38 +02:00
matches . each { | item | is_expected . to contain_concat__fragment ( fragment ) . with_content ( item ) }
2014-06-26 05:40:22 +02:00
else
lines = subject . resource ( 'concat::fragment' , fragment ) . send ( :parameters ) [ :content ] . split ( " \n " )
2014-07-18 04:28:38 +02:00
expect ( lines & matches ) . to eq ( matches )
2014-06-26 05:40:22 +02:00
end
Array ( param [ :notmatch ] ) . each do | item |
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) . without_content ( item )
2014-06-26 05:40:22 +02:00
end
end
end
end
end
describe " vhost/location_footer template content " do
[
2013-11-22 04:10:53 +01:00
{
:title = > 'should contain ordered appended directives' ,
:attr = > 'location_cfg_append' ,
2014-06-26 05:40:22 +02:00
:value = > { 'test1' = > 'test value 1' , 'test2' = > [ 'test value 2a' , 'test value 2b' ] ,
'test3' = > { 'subtest1' = > [ '"sub test value1a"' , '"sub test value1b"' ] ,
2014-03-12 08:21:15 +01:00
'subtest2' = > '"sub test value2"' } } ,
2013-11-22 04:10:53 +01:00
:match = > [
2014-06-26 05:40:22 +02:00
' 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";' ,
2013-11-22 04:10:53 +01:00
] ,
} ,
2014-07-31 19:59:37 +02:00
{
:title = > 'should contain include directives' ,
:attr = > 'include' ,
:value = > [ '/file1' , '/file2' ] ,
:match = > [
%r'^\s+include\s+/file1;' ,
%r'^\s+include\s+/file2;' ,
] ,
} ,
2014-01-13 19:20:02 +01:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should contain custom appended directives' ,
:attr = > 'location_custom_cfg_append' ,
:value = > { 'test1' = > 'bar' , 'test2' = > [ 'foobar' , 'barbaz' ] ,
'test3' = > { 'subtest1' = > [ '"sub test value1a"' , '"sub test value1b"' ] ,
'subtest2' = > '"sub test value2"' } } ,
2014-01-13 19:20:02 +01:00
:match = > [
2014-06-26 05:40:22 +02:00
/ ^[ ]+test1 \ s+bar / ,
/ ^[ ]+test2 \ s+foobar / ,
/ ^[ ]+test2 \ s+barbaz / ,
/ ^[ ]+test3 \ s+subtest1 "sub test value1a" / ,
/ ^[ ]+test3 \ s+subtest1 "sub test value1b" / ,
/ ^[ ]+test3 \ s+subtest2 "sub test value2" / ,
2014-01-13 19:20:02 +01:00
] ,
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should contain raw_append directives' ,
:attr = > 'raw_append' ,
:value = > [
'if (a) {' ,
' b;' ,
'}'
] ,
:match = > / ^ \ s+if \ (a \ ) { \ n \ s++b; \ n \ s+ \ } / ,
2014-01-13 19:20:02 +01:00
} ,
2013-11-22 04:10:53 +01:00
] . each do | param |
context " when #{ param [ :attr ] } is #{ param [ :value ] } " do
let :default_params do { :location = > 'location' , :proxy = > 'proxy_value' , :vhost = > 'vhost1' } end
let :params do default_params . merge ( { param [ :attr ] . to_sym = > param [ :value ] } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2014-06-26 05:40:22 +02:00
2013-11-22 04:10:53 +01:00
it param [ :title ] do
2014-06-26 05:40:22 +02:00
fragment = Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " )
matches = Array ( param [ :match ] )
if matches . all? { | m | m . is_a? Regexp }
2014-07-18 04:28:38 +02:00
matches . each { | item | is_expected . to contain_concat__fragment ( fragment ) . with_content ( item ) }
2014-06-26 05:40:22 +02:00
else
lines = subject . resource ( 'concat::fragment' , fragment ) . send ( :parameters ) [ :content ] . split ( " \n " )
2014-07-18 04:28:38 +02:00
expect ( lines & matches ) . to eq ( matches )
2014-06-26 05:40:22 +02:00
end
2013-12-06 17:36:28 +01:00
Array ( param [ :notmatch ] ) . each do | item |
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) . without_content ( item )
2013-12-06 17:36:28 +01:00
end
2013-11-22 04:10:53 +01:00
end
2014-06-26 05:40:22 +02:00
it " should end with a closing brace " do
fragment = Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " )
content = subject . resource ( 'concat::fragment' , fragment ) . send ( :parameters ) [ :content ]
2014-07-18 04:28:38 +02:00
expect ( ( content . split ( " \n " ) . reject { | l | l =~ / ^( \ s* # |$) / } . last ) . strip ) . to eq ( '}' )
2014-06-26 05:40:22 +02:00
end
2013-11-22 04:10:53 +01:00
end
end
2014-06-26 05:40:22 +02:00
end
2013-11-22 04:10:53 +01:00
2014-06-26 05:40:22 +02:00
describe " vhost_location_alias template content " do
let :default_params do
{ :location = > 'location' , :vhost = > 'vhost1' , :location_alias = > 'value' }
end
2013-11-22 04:10:53 +01:00
2014-06-26 05:40:22 +02:00
context " when location_alias is 'value' " do
let :params do default_params end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) }
2014-06-26 05:40:22 +02:00
it " should set alias " do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) .
2014-06-26 05:40:22 +02:00
with_content ( / ^[ ]+alias \ s+value; / )
end
end
context " when autoindex is 'on' " do
let :params do default_params . merge ( { :autoindex = > 'on' } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) }
2014-06-26 05:40:22 +02:00
it " should set autoindex " do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) .
2014-06-26 05:40:22 +02:00
with_content ( / ^[ ]+autoindex \ s+on; / )
end
end
context " when autoindex is not set " do
let :params do default_params end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) }
2014-06-26 05:40:22 +02:00
it " should not set autoindex " do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) .
2014-06-26 05:40:22 +02:00
without_content ( / ^[ ]+autoindex[^;]+; / )
end
2013-11-22 04:10:53 +01:00
end
end
2014-06-26 05:40:22 +02:00
describe " vhost_location_directory template content " do
let :default_params do
{
:location = > 'location' ,
:www_root = > '/var/www/root' ,
2014-09-12 19:42:51 +02:00
:vhost = > 'vhost1' ,
2014-06-26 05:40:22 +02:00
}
end
2013-11-22 04:10:53 +01:00
[
{
2014-06-26 05:40:22 +02:00
:title = > 'should set www_root' ,
:attr = > 'www_root' ,
:value = > '/' ,
2014-09-12 19:42:51 +02:00
:match = > ' root /;'
2013-11-22 04:10:53 +01:00
} ,
2014-06-23 22:31:32 +02:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should set try_file(s)' ,
:attr = > 'try_files' ,
:value = > [ 'name1' , 'name2' ] ,
:match = > ' try_files name1 name2;' ,
2014-06-23 22:31:32 +02:00
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should set index_file(s)' ,
:attr = > 'index_files' ,
:value = > [ 'name1' , 'name2' ] ,
2014-09-12 19:42:51 +02:00
:match = > ' index name1 name2;' ,
2014-06-23 22:31:32 +02:00
} ,
2013-11-22 05:04:39 +01:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should contain rewrite rules' ,
:attr = > 'rewrite_rules' ,
:value = > [
'^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last' ,
'^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last' ,
'^/users/(.*)$ /show?user=$1? last' ,
] ,
2013-11-22 05:04:39 +01:00
:match = > [
2014-06-26 05:40:22 +02:00
' rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;' ,
' rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;' ,
' rewrite ^/users/(.*)$ /show?user=$1? last;' ,
2013-11-22 05:04:39 +01:00
] ,
} ,
2013-11-22 04:10:53 +01:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should not set rewrite_rules' ,
:attr = > 'rewrite_rules' ,
:value = > [ ] ,
:notmatch = > / rewrite /
} ,
{
:title = > 'should set auth_basic' ,
:attr = > 'auth_basic' ,
2013-11-22 04:10:53 +01:00
:value = > 'value' ,
2014-06-26 05:40:22 +02:00
:match = > ' auth_basic "value";' ,
2013-11-22 04:10:53 +01:00
} ,
2013-11-22 05:04:39 +01:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should set auth_basic_user_file' ,
:attr = > 'auth_basic_user_file' ,
:value = > 'value' ,
:match = > ' auth_basic_user_file value;' ,
2013-11-22 05:04:39 +01:00
} ,
2013-11-22 04:10:53 +01:00
] . each do | param |
context " when #{ param [ :attr ] } is #{ param [ :value ] } " do
let :params do default_params . merge ( { param [ :attr ] . to_sym = > param [ :value ] } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2013-11-22 04:10:53 +01:00
it param [ :title ] do
2014-06-26 05:40:22 +02:00
fragment = Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " )
matches = Array ( param [ :match ] )
if matches . all? { | m | m . is_a? Regexp }
2014-07-18 04:28:38 +02:00
matches . each { | item | is_expected . to contain_concat__fragment ( fragment ) . with_content ( item ) }
2014-06-26 05:40:22 +02:00
else
lines = subject . resource ( 'concat::fragment' , fragment ) . send ( :parameters ) [ :content ] . split ( " \n " )
2014-07-18 04:28:38 +02:00
expect ( lines & matches ) . to eq ( matches )
2014-06-26 05:40:22 +02:00
end
2013-12-06 17:36:28 +01:00
Array ( param [ :notmatch ] ) . each do | item |
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) . without_content ( item )
2013-12-06 17:36:28 +01:00
end
2013-11-22 04:10:53 +01:00
end
end
end
2014-06-26 05:40:22 +02:00
context " when autoindex is 'on' " do
let :params do default_params . merge ( { :autoindex = > 'on' } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) }
2014-06-26 05:40:22 +02:00
it " should set autoindex " do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) .
2014-06-26 05:40:22 +02:00
with_content ( / ^[ ]+autoindex \ s+on; / )
end
end
context " when autoindex is not set " do
let :params do default_params end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) }
2014-06-26 05:40:22 +02:00
it " should not set autoindex " do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) .
2014-06-26 05:40:22 +02:00
without_content ( / ^[ ]+autoindex[^;]+; / )
end
end
2013-11-22 04:10:53 +01:00
end
2014-06-26 05:40:22 +02:00
describe " vhost_location_empty template content " do
2013-11-22 04:10:53 +01:00
[
{
2014-06-26 05:40:22 +02:00
: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' ,
2014-03-13 11:01:40 +01:00
'test4' = > { 'subtest1' = > [ '"sub test value1a"' , '"sub test value1b"' ] ,
2014-03-12 08:21:15 +01:00
'subtest2' = > '"sub test value2"' } } ,
2013-11-22 04:10:53 +01:00
:match = > [
2014-03-07 15:20:58 +01:00
' allow test value 3;' ,
2014-06-26 05:40:22 +02:00
' test1 test value 1a;' ,
' test1 test value 1b;' ,
' test2 test value 2;' ,
2014-03-07 15:20:58 +01:00
' test4 subtest1 "sub test value1a";' ,
' test4 subtest1 "sub test value1b";' ,
2014-03-12 08:21:15 +01:00
' test4 subtest2 "sub test value2";' ,
2013-11-22 04:10:53 +01:00
] ,
} ,
] . each do | param |
context " when #{ param [ :attr ] } is #{ param [ :value ] } " do
2014-06-26 05:40:22 +02:00
let :default_params do { :location = > 'location' , :location_custom_cfg = > { 'test1' = > 'value1' } , :vhost = > 'vhost1' } end
2013-11-22 04:10:53 +01:00
let :params do default_params . merge ( { param [ :attr ] . to_sym = > param [ :value ] } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2013-11-22 04:10:53 +01:00
it param [ :title ] do
2014-06-26 05:40:22 +02:00
fragment = Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " )
matches = Array ( param [ :match ] )
if matches . all? { | m | m . is_a? Regexp }
2014-07-18 04:28:38 +02:00
matches . each { | item | is_expected . to contain_concat__fragment ( fragment ) . with_content ( item ) }
2014-06-26 05:40:22 +02:00
else
lines = subject . resource ( 'concat::fragment' , fragment ) . send ( :parameters ) [ :content ] . split ( " \n " )
2014-07-18 04:28:38 +02:00
expect ( lines & matches ) . to eq ( matches )
2014-06-26 05:40:22 +02:00
end
2013-12-06 17:36:28 +01:00
Array ( param [ :notmatch ] ) . each do | item |
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) . without_content ( item )
2013-12-06 17:36:28 +01:00
end
2013-11-22 04:10:53 +01:00
end
end
end
end
describe " vhost_location_fastcgi template content " do
2014-06-26 05:40:22 +02:00
let :default_params do
2014-06-23 22:31:32 +02:00
{
2014-06-26 05:40:22 +02:00
:location = > 'location' ,
:fastcgi = > 'localhost:9000' ,
:vhost = > 'vhost1'
}
end
[
2013-11-22 04:10:53 +01:00
{
:title = > 'should set www_root' ,
:attr = > 'www_root' ,
:value = > '/' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+root\s+/;'
2013-11-22 04:10:53 +01:00
} ,
{
:title = > 'should set fastcgi_split_path' ,
:attr = > 'fastcgi_split_path' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+fastcgi_split_path_info\s+value;'
2013-11-22 04:10:53 +01:00
} ,
{
:title = > 'should set try_file(s)' ,
:attr = > 'try_files' ,
:value = > [ 'name1' , 'name2' ] ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+try_files\s+name1 name2;' ,
2013-11-22 04:10:53 +01:00
} ,
{
:title = > 'should set fastcgi_params' ,
:attr = > 'fastcgi_params' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+include\s+value;'
2013-11-22 04:10:53 +01:00
} ,
{
:title = > 'should set fastcgi_pass' ,
:attr = > 'fastcgi' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+fastcgi_pass\s+value;'
2013-11-22 04:10:53 +01:00
} ,
] . each do | param |
context " when #{ param [ :attr ] } is #{ param [ :value ] } " do
let :params do default_params . merge ( { param [ :attr ] . to_sym = > param [ :value ] } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2013-11-22 04:10:53 +01:00
it param [ :title ] do
2014-06-26 05:40:22 +02:00
fragment = Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " )
matches = Array ( param [ :match ] )
if matches . all? { | m | m . is_a? Regexp }
2014-07-18 04:28:38 +02:00
matches . each { | item | is_expected . to contain_concat__fragment ( fragment ) . with_content ( item ) }
2014-06-26 05:40:22 +02:00
else
lines = subject . resource ( 'concat::fragment' , fragment ) . send ( :parameters ) [ :content ] . split ( " \n " )
2014-07-18 04:28:38 +02:00
expect ( lines & matches ) . to eq ( matches )
2014-06-26 05:40:22 +02:00
end
2013-12-06 17:36:28 +01:00
Array ( param [ :notmatch ] ) . each do | item |
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) . without_content ( item )
2013-12-06 17:36:28 +01:00
end
2013-11-22 04:10:53 +01:00
end
end
end
2014-06-26 05:40:22 +02:00
context " when fastcgi_script is 'value' " do
let :params do default_params . merge ( { :fastcgi_script = > 'value' } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2014-06-26 05:40:22 +02:00
it " should set fastcgi_script " do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) .
2014-06-26 05:40:22 +02:00
with_content ( %r|^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+value;| )
end
end
context " when fastcgi_script is not set " do
let :params do default_params end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2014-06-26 05:40:22 +02:00
it " should not set fastcgi_script " do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) .
2014-06-26 05:40:22 +02:00
without_content ( / ^[ ]+fastcgi_param \ s+SCRIPT_FILENAME \ s+.+?; / )
end
end
2014-08-11 10:04:21 +02:00
2014-08-13 12:24:59 +02:00
context " when fastcgi_param is {'CUSTOM_PARAM' => 'value'} " do
let :params do default_params . merge ( { :fastcgi_param = > { 'CUSTOM_PARAM' = > 'value' , 'CUSTOM_PARAM2' = > 'value2' } } ) end
it " should set fastcgi_param " do
should contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) .
with_content ( %r|fastcgi_param\s+CUSTOM_PARAM\s+value;| ) .
with_content ( %r|fastcgi_param\s+CUSTOM_PARAM2\s+value2;| )
2014-08-11 10:04:21 +02:00
end
end
2014-08-13 12:24:59 +02:00
context " when fastcgi_param is not set " do
2014-08-11 10:04:21 +02:00
let :params do default_params end
2014-08-13 12:24:59 +02:00
it " should not set fastcgi_param " do
should contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) .
without_content ( / fastcgi_param \ s+CUSTOM_PARAM \ s+.+?; / ) .
without_content ( / fastcgi_param \ s+CUSTOM_PARAM2 \ s+.+?; / )
2014-08-11 10:04:21 +02:00
end
it " should not add comment # Enable custom fastcgi_params " do
2014-08-13 12:24:59 +02:00
should contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) .
2014-08-11 10:04:21 +02:00
without_content ( / # Enable custom fastcgi_params \ s+ / )
end
end
2013-11-22 04:10:53 +01:00
end
2014-06-26 05:40:22 +02:00
describe " vhost_location_proxy template content " do
2013-11-22 04:10:53 +01:00
[
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy_cache' ,
:attr = > 'proxy_cache' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > / ^ \ s+proxy_cache \ s+value; / ,
2013-11-22 04:10:53 +01:00
} ,
2014-09-03 20:50:37 +02:00
{
:title = > 'should not set proxy_cache_valid' ,
:attr = > 'proxy_cache_valid' ,
:value = > false ,
:notmatch = > / proxy_cache_valid \ b /
} ,
{
:title = > 'should set proxy_cache_valid' ,
:attr = > 'proxy_cache_valid' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > / ^ \ s+proxy_cache_valid \ s+value; / ,
2014-09-03 20:50:37 +02:00
} ,
2014-06-23 22:31:32 +02:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should not set proxy_cache' ,
:attr = > 'proxy_cache' ,
2014-06-23 22:31:32 +02:00
:value = > false ,
2014-06-26 05:40:22 +02:00
:notmatch = > / proxy_cache \ b /
2014-06-23 22:31:32 +02:00
} ,
2014-09-03 20:50:37 +02:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy_pass' ,
:attr = > 'proxy' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > / ^ \ s+proxy_pass \ s+value; / ,
2013-11-22 04:10:53 +01:00
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy_read_timeout' ,
:attr = > 'proxy_read_timeout' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+proxy_read_timeout\s+value;' ,
2013-11-22 04:10:53 +01:00
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy_connect_timeout' ,
:attr = > 'proxy_connect_timeout' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+proxy_connect_timeout\s+value;' ,
2013-11-22 04:10:53 +01:00
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy_read_timeout' ,
:attr = > 'proxy_read_timeout' ,
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+proxy_read_timeout\s+value;' ,
2013-11-22 04:10:53 +01:00
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy headers' ,
:attr = > 'proxy_set_header' ,
:value = > [ 'X-TestHeader1 value1' , 'X-TestHeader2 value2' ] ,
:match = > [
2014-09-12 19:42:51 +02:00
/ ^ \ s+proxy_set_header \ s+X-TestHeader1 value1; / ,
/ ^ \ s+proxy_set_header \ s+X-TestHeader2 value2; / ,
2014-06-26 05:40:22 +02:00
]
2013-11-22 04:10:53 +01:00
} ,
2013-11-22 05:22:01 +01:00
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy_method' ,
:attr = > 'proxy_method' ,
2013-11-22 05:22:01 +01:00
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+proxy_method\s+value;' ,
2013-11-22 05:22:01 +01:00
} ,
{
2014-06-26 05:40:22 +02:00
:title = > 'should set proxy_set_body' ,
:attr = > 'proxy_set_body' ,
2013-11-22 05:22:01 +01:00
:value = > 'value' ,
2014-09-12 19:42:51 +02:00
:match = > %r'\s+proxy_set_body\s+value;' ,
2013-11-22 04:10:53 +01:00
} ,
2014-01-13 19:20:02 +01:00
{
:title = > 'should contain rewrite rules' ,
:attr = > 'rewrite_rules' ,
:value = > [
'^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last' ,
'^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last' ,
'^/users/(.*)$ /show?user=$1? last' ,
] ,
:match = > [
' rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;' ,
' rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;' ,
' rewrite ^/users/(.*)$ /show?user=$1? last;' ,
] ,
} ,
{
:title = > 'should not set rewrite_rules' ,
:attr = > 'rewrite_rules' ,
:value = > [ ] ,
:notmatch = > / rewrite /
} ,
2013-11-22 04:10:53 +01:00
] . each do | param |
context " when #{ param [ :attr ] } is #{ param [ :value ] } " do
2014-06-26 05:40:22 +02:00
let :default_params do { :location = > 'location' , :proxy = > 'proxy_value' , :vhost = > 'vhost1' } end
2013-11-22 04:10:53 +01:00
let :params do default_params . merge ( { param [ :attr ] . to_sym = > param [ :value ] } ) end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) }
2013-11-22 04:10:53 +01:00
it param [ :title ] do
2014-06-26 05:40:22 +02:00
fragment = Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " )
matches = Array ( param [ :match ] )
if matches . all? { | m | m . is_a? Regexp }
2014-07-18 04:28:38 +02:00
matches . each { | item | is_expected . to contain_concat__fragment ( fragment ) . with_content ( item ) }
2014-06-26 05:40:22 +02:00
else
lines = subject . resource ( 'concat::fragment' , fragment ) . send ( :parameters ) [ :content ] . split ( " \n " )
2014-07-18 04:28:38 +02:00
expect ( lines & matches ) . to eq ( matches )
2014-06-26 05:40:22 +02:00
end
2013-12-06 17:36:28 +01:00
Array ( param [ :notmatch ] ) . each do | item |
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) . without_content ( item )
2013-12-06 17:36:28 +01:00
end
2013-11-22 04:10:53 +01:00
end
end
end
2014-06-26 05:40:22 +02:00
context " when proxy_cache_valid is 10m " do
let :params do {
:location = > 'location' ,
:proxy = > 'proxy_value' ,
:vhost = > 'vhost1' ,
:proxy_cache = > 'true' ,
:proxy_cache_valid = > '10m' ,
} end
2013-11-22 04:10:53 +01:00
2014-09-12 19:42:51 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-location " ) ) . with_content ( / proxy_cache_valid \ s+10m; / ) }
2014-06-26 05:40:22 +02:00
end
end
describe " vhost_location_stub_status template content " do
let :params do { :location = > 'location' , :stub_status = > true , :vhost = > 'vhost1' } end
it do
2014-07-18 04:28:38 +02:00
is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500- #{ params [ :location ] } " ) ) .
2014-06-26 05:40:22 +02:00
with_content ( / stub_status \ s+on / )
2013-11-22 04:10:53 +01:00
end
end
context 'attribute resources' do
2014-01-06 18:41:47 +01:00
context 'when fastcgi => "localhost:9000"' do
let :params do { :fastcgi = > 'localhost:9000' , :vhost = > 'vhost1' } end
2013-11-22 04:10:53 +01:00
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_file ( '/etc/nginx/fastcgi_params' ) . with_mode ( '0770' ) }
2013-11-22 04:10:53 +01:00
end
context 'when ssl_only => true' do
let :params do { :ssl_only = > true , :vhost = > 'vhost1' , :www_root = > '/' , } end
2014-07-18 04:28:38 +02:00
it { is_expected . not_to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-rspec-test " ) ) }
2013-11-22 04:10:53 +01:00
end
context 'when ssl_only => false' do
let :params do { :ssl_only = > false , :vhost = > 'vhost1' , :www_root = > '/' , } end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-500-rspec-test " ) ) }
2013-11-22 04:10:53 +01:00
end
context 'when ssl => true' do
let :params do { :ssl = > true , :vhost = > 'vhost1' , :www_root = > '/' , } end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-800-rspec-test-ssl " ) ) }
2013-11-22 04:10:53 +01:00
end
context 'when ssl => false' do
let :params do { :ssl = > false , :vhost = > 'vhost1' , :www_root = > '/' , } end
2014-07-18 04:28:38 +02:00
it { is_expected . not_to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " vhost1-800-rspec-test-ssl " ) ) }
2013-11-22 04:10:53 +01:00
end
context 'when auth_basic_user_file => true' do
2014-01-06 18:41:47 +01:00
let :params do { :auth_basic_user_file = > '/path/to/file' , :vhost = > 'vhost1' , :www_root = > '/' , } end
2013-11-22 04:10:53 +01:00
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_file ( " /etc/nginx/rspec-test_htpasswd " ) }
2013-11-22 04:10:53 +01:00
end
context 'when ensure => absent' do
let :params do {
2014-01-06 18:41:47 +01:00
:www_root = > '/' ,
2013-11-22 04:10:53 +01:00
:vhost = > 'vhost1' ,
:ensure = > 'absent' ,
:ssl = > true ,
2014-01-06 18:41:47 +01:00
:auth_basic_user_file = > '/path/to/file' ,
2013-11-22 04:10:53 +01:00
} end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_file ( " /etc/nginx/rspec-test_htpasswd " ) . with_ensure ( 'absent' ) }
2013-11-22 04:10:53 +01:00
end
context " vhost missing " do
let :params do {
:www_root = > '/' ,
} end
2014-07-18 04:28:38 +02:00
it { expect { is_expected . to contain_class ( 'nginx::resource::location' ) } . to raise_error ( Puppet :: Error , / Cannot create a location reference without attaching to a virtual host / ) }
2013-11-22 04:10:53 +01:00
end
context " location type missing " do
let :params do {
:vhost = > 'vhost1' ,
} end
2014-09-17 00:00:03 +02:00
it { expect { is_expected . to contain_class ( 'nginx::resource::location' ) } . to raise_error ( Puppet :: Error , / Cannot create a location reference without a www_root, proxy, location_alias, fastcgi, stub_status, internal, or location_custom_cfg defined / ) }
2013-11-22 04:10:53 +01:00
end
context " www_root and proxy are set " do
let :params do {
:vhost = > 'vhost1' ,
:www_root = > '/' ,
2014-01-06 18:41:47 +01:00
:proxy = > 'http://localhost:8000/uri/' ,
2013-11-22 04:10:53 +01:00
} end
2014-07-18 04:28:38 +02:00
it { expect { is_expected . to contain_class ( 'nginx::resource::location' ) } . to raise_error ( Puppet :: Error , / Cannot define both directory and proxy in a virtual host / ) }
2013-11-22 04:10:53 +01:00
end
2014-01-18 19:31:05 +01:00
context 'when vhost name is sanitized' do
let :title do 'www.rspec-location.com' end
let :params do {
:vhost = > 'www rspec-vhost com' ,
:www_root = > '/' ,
:ssl = > true ,
} end
2014-07-18 04:28:38 +02:00
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " www_rspec-vhost_com-500-www.rspec-location.com " ) ) . with_target ( '/etc/nginx/sites-available/www_rspec-vhost_com.conf' ) }
it { is_expected . to contain_concat__fragment ( Digest :: MD5 . hexdigest ( " www_rspec-vhost_com-800-www.rspec-location.com-ssl " ) ) . with_target ( '/etc/nginx/sites-available/www_rspec-vhost_com.conf' ) }
2014-01-18 19:31:05 +01:00
end
2013-11-22 04:10:53 +01:00
end
end
end