-
-
Notifications
You must be signed in to change notification settings - Fork 879
/
resource_location_spec.rb
753 lines (686 loc) · 28.5 KB
/
resource_location_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
require 'spec_helper'
require 'digest/md5'
describe 'nginx::resource::location' do
let :title do
'rspec-test'
end
let :facts do
{
:osfamily => 'Debian',
:operatingsystem => 'debian',
}
end
let :pre_condition do
[
'include ::nginx::params',
'include ::nginx::config',
]
end
describe 'os-independent items' do
describe 'basic assumptions' do
let :params do {
:www_root => "/var/www/rspec",
:vhost => 'vhost1',
} end
it { is_expected.to contain_class("nginx::params") }
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") }
end
describe "vhost/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 => /internal;/
},
{
:title => 'should set internal',
:attr => 'internal',
:value => true,
:match => ' internal;'
},
{
: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;'
},
{
: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' => ['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"/,
],
},
{
:title => 'should contain raw_prepend directives',
:attr => 'raw_prepend',
:value => [
'if (a) {',
' b;',
'}'
],
:match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
},
].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
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it param[:title] do
fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
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 = subject.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(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
end
end
end
end
end
describe "vhost/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' => ['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"/,
],
},
{
:title => 'should contain raw_append directives',
:attr => 'raw_append',
:value => [
'if (a) {',
' b;',
'}'
],
:match => /^\s+if \(a\) {\n\s++b;\n\s+\}/,
},
].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
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it param[:title] do
fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
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 = subject.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(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
end
end
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]
expect((content.split("\n").reject {|l| l =~ /^(\s*#|$)/ }.last).strip).to eq('}')
end
end
end
end
describe "vhost_location_alias template content" do
let :default_params do
{ :location => 'location', :vhost => 'vhost1', :location_alias => 'value' }
end
context "when location_alias is 'value'" do
let :params do default_params end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
it "should set alias" do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
with_content(/^[ ]+alias\s+value;/)
end
end
context "when autoindex is 'on'" do
let :params do default_params.merge({ :autoindex => 'on' }) end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
it "should set autoindex" do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
with_content(/^[ ]+autoindex\s+on;/)
end
end
context "when autoindex is not set" do
let :params do default_params end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
it "should not set autoindex" do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
without_content(/^[ ]+autoindex[^;]+;/)
end
end
end
describe "vhost_location_directory template content" do
let :default_params do
{
:location => 'location',
:www_root => '/var/www/root',
:vhost => 'vhost1',
}
end
[
{
:title => 'should set www_root',
:attr => 'www_root',
:value => '/',
:match => ' root /;'
},
{
:title => 'should set try_file(s)',
:attr => 'try_files',
:value => ['name1','name2'],
:match => ' try_files name1 name2;',
},
{
:title => 'should set index_file(s)',
:attr => 'index_files',
:value => ['name1','name2'],
:match => ' index name1 name2;',
},
{
: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/
},
{
: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;',
},
].each do |param|
context "when #{param[:attr]} is #{param[:value]}" do
let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it param[:title] do
fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
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 = subject.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(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
end
end
end
end
context "when autoindex is 'on'" do
let :params do default_params.merge({ :autoindex => 'on' }) end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
it "should set autoindex" do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
with_content(/^[ ]+autoindex\s+on;/)
end
end
context "when autoindex is not set" do
let :params do default_params end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")) }
it "should not set autoindex" do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).
without_content(/^[ ]+autoindex[^;]+;/)
end
end
end
describe "vhost_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 do { :location => 'location', :location_custom_cfg => {'test1'=>'value1'}, :vhost => 'vhost1' } end
let :params do default_params.merge({ param[:attr].to_sym => param[:value] }) end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it param[:title] do
fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
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 = subject.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(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
end
end
end
end
end
describe "vhost_location_fastcgi template content" do
let :default_params do
{
:location => 'location',
:fastcgi => 'localhost:9000',
:vhost => 'vhost1'
}
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 => ['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 do default_params.merge({ param[:attr].to_sym => param[:value] }) end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it param[:title] do
fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
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 = subject.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(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
end
end
end
end
context "when fastcgi_script is 'value'" do
let :params do default_params.merge({ :fastcgi_script => 'value' }) end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it "should set fastcgi_script" do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
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
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it "should not set fastcgi_script" do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
without_content(/^[ ]+fastcgi_param\s+SCRIPT_FILENAME\s+.+?;/)
end
end
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;|)
end
end
context "when fastcgi_param is not set" do
let :params do default_params end
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+.+?;/)
end
it "should not add comment # Enable custom fastcgi_params" do
should contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
without_content(/# Enable custom fastcgi_params\s+/)
end
end
end
describe "vhost_location_proxy template content" do
[
{
:title => 'should set proxy_cache',
:attr => 'proxy_cache',
:value => 'value',
:match => /^\s+proxy_cache\s+value;/,
},
{
: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',
:match => /^\s+proxy_cache_valid\s+value;/,
},
{
:title => 'should not set proxy_cache',
:attr => 'proxy_cache',
:value => false,
:notmatch => /proxy_cache\b/
},
{
:title => 'should set proxy_pass',
:attr => 'proxy',
:value => 'value',
:match => /^\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 => [
/^\s+proxy_set_header\s+X-TestHeader1 value1;/,
/^\s+proxy_set_header\s+X-TestHeader2 value2;/,
]
},
{
: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 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/
},
].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
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")) }
it param[:title] do
fragment = Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")
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 = subject.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(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).without_content(item)
end
end
end
end
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
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-location")).with_content(/proxy_cache_valid\s+10m;/) }
end
end
describe "vhost_location_stub_status template content" do
let :params do { :location => 'location', :stub_status => true, :vhost => 'vhost1' } end
it do
is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-#{params[:location]}")).
with_content(/stub_status\s+on/)
end
end
context 'attribute resources' do
context 'when fastcgi => "localhost:9000"' do
let :params do { :fastcgi => 'localhost:9000', :vhost => 'vhost1' } end
it { is_expected.to contain_file('/etc/nginx/fastcgi_params').with_mode('0770') }
end
context 'when ssl_only => true' do
let :params do { :ssl_only => true, :vhost => 'vhost1', :www_root => '/', } end
it { is_expected.not_to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-rspec-test")) }
end
context 'when ssl_only => false' do
let :params do { :ssl_only => false, :vhost => 'vhost1', :www_root => '/', } end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-500-rspec-test")) }
end
context 'when ssl => true' do
let :params do { :ssl => true, :vhost => 'vhost1', :www_root => '/', } end
it { is_expected.to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-800-rspec-test-ssl")) }
end
context 'when ssl => false' do
let :params do { :ssl => false, :vhost => 'vhost1', :www_root => '/', } end
it { is_expected.not_to contain_concat__fragment(Digest::MD5.hexdigest("vhost1-800-rspec-test-ssl")) }
end
context 'when auth_basic_user_file => true' do
let :params do { :auth_basic_user_file => '/path/to/file', :vhost => 'vhost1', :www_root => '/', } end
it { is_expected.to contain_file("/etc/nginx/rspec-test_htpasswd") }
end
context 'when ensure => absent' do
let :params do {
:www_root => '/',
:vhost => 'vhost1',
:ensure => 'absent',
:ssl => true,
:auth_basic_user_file => '/path/to/file',
} end
it { is_expected.to contain_file("/etc/nginx/rspec-test_htpasswd").with_ensure('absent') }
end
context "vhost missing" do
let :params do {
:www_root => '/',
} end
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/) }
end
context "location type missing" do
let :params do {
:vhost => 'vhost1',
} end
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, or location_custom_cfg defined/) }
end
context "www_root and proxy are set" do
let :params do {
:vhost => 'vhost1',
:www_root => '/',
:proxy => 'http://localhost:8000/uri/',
} end
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/) }
end
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
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') }
end
end
end
end