/
finalize_test.go
935 lines (814 loc) · 31.2 KB
/
finalize_test.go
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
package finalize_test
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"staticfile/finalize"
"syscall"
"bytes"
"github.com/cloudfoundry/libbuildpack"
"github.com/cloudfoundry/libbuildpack/ansicleaner"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
//go:generate mockgen -source=finalize.go --destination=mocks_test.go --package=finalize_test
var _ = Describe("Compile", func() {
var (
staticfile finalize.Staticfile
err error
buildDir string
depDir string
finalizer *finalize.Finalizer
logger *libbuildpack.Logger
mockCtrl *gomock.Controller
mockYaml *MockYAML
buffer *bytes.Buffer
data []byte
)
BeforeEach(func() {
buildDir, err = ioutil.TempDir("", "staticfile-buildpack.build.")
Expect(err).To(BeNil())
depDir, err = ioutil.TempDir("", "staticfile-buildpack.depDir.")
Expect(err).To(BeNil())
buffer = new(bytes.Buffer)
logger = libbuildpack.NewLogger(ansicleaner.New(buffer))
mockCtrl = gomock.NewController(GinkgoT())
mockYaml = NewMockYAML(mockCtrl)
})
JustBeforeEach(func() {
finalizer = &finalize.Finalizer{
BuildDir: buildDir,
DepDir: depDir,
Config: staticfile,
YAML: mockYaml,
Log: logger,
}
})
AfterEach(func() {
mockCtrl.Finish()
err = os.RemoveAll(buildDir)
Expect(err).To(BeNil())
err = os.RemoveAll(depDir)
Expect(err).To(BeNil())
})
Describe("WriteStartupFiles", func() {
It("writes staticfile.sh to the profile.d directory", func() {
err = finalizer.WriteStartupFiles()
Expect(err).To(BeNil())
contents, err := ioutil.ReadFile(filepath.Join(depDir, "profile.d", "staticfile.sh"))
Expect(err).To(BeNil())
Expect(string(contents)).To(ContainSubstring("export LD_LIBRARY_PATH=$APP_ROOT/nginx/lib:$LD_LIBRARY_PATH"))
})
It("writes start_logging.sh in appdir", func() {
err = finalizer.WriteStartupFiles()
Expect(err).To(BeNil())
contents, err := ioutil.ReadFile(filepath.Join(buildDir, "start_logging.sh"))
Expect(err).To(BeNil())
Expect(string(contents)).To(Equal("\ncat < $APP_ROOT/nginx/logs/access.log &\n(>&2 cat) < $APP_ROOT/nginx/logs/error.log &\n"))
})
It("start_logging.sh is an executable file", func() {
err = finalizer.WriteStartupFiles()
Expect(err).To(BeNil())
fi, err := os.Stat(filepath.Join(buildDir, "start_logging.sh"))
Expect(err).To(BeNil())
Expect(fi.Mode().Perm() & 0111).NotTo(Equal(os.FileMode(0000)))
})
It("writes boot.sh in appdir", func() {
err = finalizer.WriteStartupFiles()
Expect(err).To(BeNil())
contents, err := ioutil.ReadFile(filepath.Join(buildDir, "boot.sh"))
Expect(err).To(BeNil())
Expect(string(contents)).To(Equal("#!/bin/sh\nset -ex\n$APP_ROOT/start_logging.sh\nnginx -p $APP_ROOT/nginx -c $APP_ROOT/nginx/conf/nginx.conf\n"))
})
It("boot.sh is an executable file", func() {
err = finalizer.WriteStartupFiles()
Expect(err).To(BeNil())
fi, err := os.Stat(filepath.Join(buildDir, "boot.sh"))
Expect(err).To(BeNil())
Expect(fi.Mode().Perm() & 0111).NotTo(Equal(os.FileMode(0000)))
})
})
Describe("LoadStaticfile", func() {
Context("the staticfile does not exist", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Return(os.ErrNotExist)
})
It("does not return an error", func() {
err = finalizer.LoadStaticfile()
Expect(err).To(BeNil())
})
It("has default values", func() {
err = finalizer.LoadStaticfile()
Expect(err).To(BeNil())
Expect(finalizer.Config.RootDir).To(Equal(""))
Expect(finalizer.Config.HostDotFiles).To(Equal(false))
Expect(finalizer.Config.LocationInclude).To(Equal(""))
Expect(finalizer.Config.DirectoryIndex).To(Equal(false))
Expect(finalizer.Config.SSI).To(Equal(false))
Expect(finalizer.Config.PushState).To(Equal(false))
Expect(finalizer.Config.HSTS).To(Equal(false))
Expect(finalizer.Config.HSTSIncludeSubDomains).To(Equal(false))
Expect(finalizer.Config.HSTSPreload).To(Equal(false))
Expect(finalizer.Config.ForceHTTPS).To(Equal(false))
Expect(finalizer.Config.BasicAuth).To(Equal(false))
})
It("does not log enabling statements", func() {
err = finalizer.LoadStaticfile()
Expect(buffer.String()).To(Equal(""))
})
})
Context("the staticfile exists", func() {
JustBeforeEach(func() {
err = finalizer.LoadStaticfile()
Expect(err).To(BeNil())
})
Context("and sets root", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["root"] = "root_test"
})
})
It("sets RootDir", func() {
Expect(finalizer.Config.RootDir).To(Equal("root_test"))
})
})
Context("and sets host_dot_files", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["host_dot_files"] = "true"
})
})
It("sets HostDotFiles", func() {
Expect(finalizer.Config.HostDotFiles).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(Equal("-----> Enabling hosting of dotfiles\n"))
})
})
Context("and sets location_include", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["location_include"] = "a/b/c"
})
})
It("sets location_include", func() {
Expect(finalizer.Config.LocationInclude).To(Equal("a/b/c"))
})
It("Logs", func() {
Expect(buffer.String()).To(Equal("-----> Enabling location include file a/b/c\n"))
})
})
Context("and sets directory", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["directory"] = "any_string"
})
})
It("sets location_include", func() {
Expect(finalizer.Config.DirectoryIndex).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(Equal("-----> Enabling directory index for folders without index.html files\n"))
})
})
Context("and sets ssi", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["ssi"] = "enabled"
})
})
It("sets ssi", func() {
Expect(finalizer.Config.SSI).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(Equal("-----> Enabling SSI\n"))
})
})
Context("and sets pushstate", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["pushstate"] = "enabled"
})
})
It("sets pushstate", func() {
Expect(finalizer.Config.PushState).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(Equal("-----> Enabling pushstate\n"))
})
})
Context("and sets http_strict_transport_security", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["http_strict_transport_security"] = "true"
})
})
It("sets http_strict_transport_security", func() {
Expect(finalizer.Config.HSTS).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(Equal("-----> Enabling HSTS\n"))
})
})
Context("and sets http_strict_transport_security_include_subdomains", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["http_strict_transport_security_include_subdomains"] = "true"
})
})
It("sets http_strict_transport_security_include_subdomains", func() {
Expect(finalizer.Config.HSTSIncludeSubDomains).To(Equal(true))
Expect(finalizer.Config.HSTS).To(Equal(false))
})
It("Logs", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Enabling HSTS includeSubDomains\n"))
})
})
Context("and sets http_strict_transport_security_preload", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["http_strict_transport_security_preload"] = "true"
})
})
It("sets http_strict_transport_security_preload", func() {
Expect(finalizer.Config.HSTSPreload).To(Equal(true))
Expect(finalizer.Config.HSTS).To(Equal(false))
})
It("Logs", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Enabling HSTS Preload\n"))
})
})
Context("and sets force_https", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Do(func(_ string, hash *map[string]string) {
(*hash)["force_https"] = "true"
})
})
It("sets force_https", func() {
Expect(finalizer.Config.ForceHTTPS).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(Equal("-----> Enabling HTTPS redirect\n"))
})
})
})
Context("Staticfile.auth is present", func() {
BeforeEach(func() {
err = ioutil.WriteFile(filepath.Join(buildDir, "Staticfile.auth"), []byte("some credentials"), 0644)
Expect(err).To(BeNil())
})
JustBeforeEach(func() {
err = finalizer.LoadStaticfile()
Expect(err).To(BeNil())
})
Context("the staticfile exists", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(gomock.Any(), gomock.Any())
})
It("sets BasicAuth", func() {
Expect(finalizer.Config.BasicAuth).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Enabling basic authentication using Staticfile.auth\n"))
})
})
Context("the staticfile does not exist", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(gomock.Any(), gomock.Any()).Return(syscall.ENOENT)
})
It("sets BasicAuth", func() {
Expect(finalizer.Config.BasicAuth).To(Equal(true))
})
It("Logs", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Enabling basic authentication using Staticfile.auth\n"))
})
})
})
Context("the staticfile exists and is not valid", func() {
BeforeEach(func() {
mockYaml.EXPECT().Load(filepath.Join(buildDir, "Staticfile"), gomock.Any()).Return(errors.New("a yaml parsing error"))
})
It("returns an error", func() {
err = finalizer.LoadStaticfile()
Expect(err).NotTo(BeNil())
})
})
})
Describe("GetAppRootDir", func() {
var (
returnDir string
)
JustBeforeEach(func() {
returnDir, err = finalizer.GetAppRootDir()
})
Context("the staticfile has a root directory specified", func() {
Context("the directory does not exist", func() {
BeforeEach(func() {
staticfile.RootDir = "not_exist"
})
It("logs the staticfile's root directory", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Root folder"))
Expect(buffer.String()).To(ContainSubstring("not_exist"))
})
It("returns an error", func() {
Expect(returnDir).To(Equal(""))
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("the application Staticfile specifies a root directory"))
Expect(err.Error()).To(ContainSubstring("that does not exist"))
})
})
Context("the directory exists but is actually a file", func() {
BeforeEach(func() {
ioutil.WriteFile(filepath.Join(buildDir, "actually_a_file"), []byte("xxx"), 0644)
staticfile.RootDir = "actually_a_file"
})
It("logs the staticfile's root directory", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Root folder"))
Expect(buffer.String()).To(ContainSubstring("actually_a_file"))
})
It("returns an error", func() {
Expect(returnDir).To(Equal(""))
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("the application Staticfile specifies a root directory"))
Expect(err.Error()).To(ContainSubstring("that is a plain file"))
})
})
Context("the directory exists", func() {
BeforeEach(func() {
os.Mkdir(filepath.Join(buildDir, "a_directory"), 0755)
staticfile.RootDir = "a_directory"
})
It("logs the staticfile's root directory", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Root folder"))
Expect(buffer.String()).To(ContainSubstring("a_directory"))
})
It("returns the full directory path", func() {
Expect(err).To(BeNil())
Expect(returnDir).To(Equal(filepath.Join(buildDir, "a_directory")))
})
})
})
Context("the staticfile does not have an root directory", func() {
BeforeEach(func() {
staticfile.RootDir = ""
})
It("logs the build directory as the root directory", func() {
Expect(buffer.String()).To(ContainSubstring("-----> Root folder"))
Expect(buffer.String()).To(ContainSubstring(buildDir))
})
It("returns the build directory", func() {
Expect(err).To(BeNil())
Expect(returnDir).To(Equal(buildDir))
})
})
})
Describe("Warnings", func() {
JustBeforeEach(func() {
finalizer.Warnings()
})
Context("location_include is set", func() {
BeforeEach(func() {
staticfile.LocationInclude = "includes/*.conf"
})
Context("root is NOT set", func() {
BeforeEach(func() {
staticfile.RootDir = ""
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("**WARNING** The location_include directive only works in conjunction with root."))
Expect(buffer.String()).To(ContainSubstring("Please specify root to use location_include"))
})
})
Context("root IS set", func() {
BeforeEach(func() {
staticfile.RootDir = "public"
})
It("does not warn the user", func() {
Expect(buffer.String()).ToNot(ContainSubstring("location_include directive only works in conjunction with root."))
Expect(buffer.String()).ToNot(ContainSubstring("Please specify root to use location_include"))
})
})
})
Context("app dir has a nginx/conf directory", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "nginx", "conf"), 0755)
})
Context("root is NOT set", func() {
BeforeEach(func() {
staticfile.RootDir = ""
})
It("warns the user", func() {
Expect(buffer.String()).To(ContainSubstring("**WARNING** You have an nginx/conf directory, but have not set *root*."))
Expect(buffer.String()).To(ContainSubstring("If you are using the nginx/conf directory for nginx configuration, you probably need to also set the *root* directive."))
})
})
Context("root IS set", func() {
BeforeEach(func() {
staticfile.RootDir = "somedir"
})
It("does not warn the user", func() {
Expect(buffer.String()).ToNot(ContainSubstring("**WARNING** You have an nginx/conf directory, but have not set *root*."))
Expect(buffer.String()).ToNot(ContainSubstring("If you are using the nginx/conf directory for nginx configuration, you probably need to also set the *root* directive."))
})
})
})
})
Describe("ConfigureNginx", func() {
JustBeforeEach(func() {
err = finalizer.ConfigureNginx()
Expect(err).To(BeNil())
})
Context("custom nginx.conf exists", func() {
BeforeEach(func() {
err = os.MkdirAll(filepath.Join(buildDir, "public"), 0755)
Expect(err).To(BeNil())
err = ioutil.WriteFile(filepath.Join(buildDir, "public", "nginx.conf"), []byte("nginx configuration"), 0644)
Expect(err).To(BeNil())
})
It("uses the custom configuration", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(data).To(Equal([]byte("nginx configuration")))
})
})
Context("custom nginx.conf does NOT exist", func() {
hostDotConf := `
location ~ /\. {
deny all;
return 404;
}
`
pushStateConf := `
if (!-e $request_filename) {
rewrite ^(.*)$ / break;
}
`
forceHTTPSConf := `
if ($http_x_forwarded_proto != "https") {
return 301 https://$host$request_uri;
}
`
forceHTTPSErb := `
<% if ENV["FORCE_HTTPS"] %>
if ($http_x_forwarded_proto != "https") {
return 301 https://$host$request_uri;
}
<% end %>
`
basicAuthConf := `
auth_basic "Restricted"; #For Basic Auth
auth_basic_user_file <%= ENV["APP_ROOT"] %>/nginx/conf/.htpasswd;
`
Context("host_dot_files is set in staticfile", func() {
BeforeEach(func() {
staticfile.HostDotFiles = true
})
It("allows dotfiles to be hosted", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring(hostDotConf))
})
})
Context("host_dot_files is NOT set in staticfile", func() {
BeforeEach(func() {
staticfile.HostDotFiles = false
})
It("allows dotfiles to be hosted", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(hostDotConf))
})
})
Context("location_include is set in staticfile", func() {
BeforeEach(func() {
staticfile.LocationInclude = "a/b/c"
})
It("includes the file", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring("include a/b/c;"))
})
})
Context("location_include is NOT set in staticfile", func() {
BeforeEach(func() {
staticfile.LocationInclude = ""
})
It("does not include the file", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring("include ;"))
})
})
Context("directory is set in staticfile", func() {
BeforeEach(func() {
staticfile.DirectoryIndex = true
})
It("sets autoindex on", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring("autoindex on;"))
})
})
Context("directory is NOT set in staticfile", func() {
BeforeEach(func() {
staticfile.DirectoryIndex = false
})
It("does not set autoindex on", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring("autoindex on;"))
})
})
Context("ssi is set in staticfile", func() {
BeforeEach(func() {
staticfile.SSI = true
})
It("enables SSI", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring("ssi on;"))
})
})
Context("ssi is NOT set in staticfile", func() {
BeforeEach(func() {
staticfile.SSI = false
})
It("does not enable SSI", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring("ssi on;"))
})
})
Context("pushstate is set in staticfile", func() {
BeforeEach(func() {
staticfile.PushState = true
})
It("it adds the configuration", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(pushStateConf))
})
})
Context("pushstate is NOT set in staticfile", func() {
BeforeEach(func() {
staticfile.PushState = false
})
It("it does not add the configuration", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring(pushStateConf))
})
})
Context("http_strict_transport_security is set in staticfile", func() {
BeforeEach(func() {
staticfile.HSTS = true
})
It("it adds the HSTS header", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(`add_header Strict-Transport-Security "max-age=31536000";`))
})
})
Context("http_strict_transport_security and http_strict_transport_security_include_subdomain is set in staticfile", func() {
BeforeEach(func() {
staticfile.HSTS = true
staticfile.HSTSIncludeSubDomains = true
})
It("it adds the HSTS header", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(`add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";`))
})
})
Context("http_strict_transport_security, http_strict_transport_security_include_subdomain, and http_strict_transport_security_preload is set in staticfile", func() {
BeforeEach(func() {
staticfile.HSTS = true
staticfile.HSTSIncludeSubDomains = true
staticfile.HSTSPreload = true
})
It("it adds the HSTS header", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(`add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";`))
})
})
Context("http_strict_transport_security is NOT set in staticfile", func() {
BeforeEach(func() {
staticfile.HSTS = false
})
It("it does not add the HSTS header", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring(`add_header Strict-Transport-Security "max-age=31536000";`))
})
})
Context("http_strict_transport_security is NOT set in staticfile, but http_strict_transport_security_preload or http_strict_transport_security_include_subdomain are set in staticfile", func() {
BeforeEach(func() {
staticfile.HSTS = false
staticfile.HSTSIncludeSubDomains = true
staticfile.HSTSPreload = true
})
It("it does not add the HSTS header", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring(`add_header Strict-Transport-Security "max-age=31536000";`))
})
})
Context("force_https is set in staticfile", func() {
BeforeEach(func() {
staticfile.ForceHTTPS = true
})
It("the 301 redirect does not depend on ENV['FORCE_HTTPS']", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(forceHTTPSConf))
Expect(string(data)).NotTo(ContainSubstring(`<% if ENV["FORCE_HTTPS"] %>`))
Expect(string(data)).NotTo(ContainSubstring(`<% end %>`))
})
})
Context("force_https is NOT set in staticfile", func() {
BeforeEach(func() {
staticfile.ForceHTTPS = false
})
It("the 301 redirect does depend on ENV['FORCE_HTTPS']", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(forceHTTPSErb))
})
})
Context("there is a Staticfile.auth", func() {
BeforeEach(func() {
staticfile.BasicAuth = true
err = ioutil.WriteFile(filepath.Join(buildDir, "Staticfile.auth"), []byte("authentication info"), 0644)
Expect(err).To(BeNil())
})
It("it enables basic authentication", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).To(ContainSubstring(basicAuthConf))
})
It("copies the Staticfile.auth to .htpasswd", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", ".htpasswd"))
Expect(err).To(BeNil())
Expect(string(data)).To(Equal("authentication info"))
})
})
Context("there is not a Staticfile.auth", func() {
BeforeEach(func() {
staticfile.BasicAuth = false
})
It("it does not enable basic authenticaiont", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "nginx.conf"))
Expect(err).To(BeNil())
Expect(string(data)).NotTo(ContainSubstring(basicAuthConf))
})
It("does not create an .htpasswd", func() {
Expect(filepath.Join(buildDir, "nginx", "conf", ".htpasswd")).NotTo(BeAnExistingFile())
})
})
})
Context("custom mime.types exists", func() {
BeforeEach(func() {
err = os.MkdirAll(filepath.Join(buildDir, "public"), 0755)
Expect(err).To(BeNil())
err = ioutil.WriteFile(filepath.Join(buildDir, "public", "mime.types"), []byte("mime types info"), 0644)
Expect(err).To(BeNil())
})
It("uses the custom configuration", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "mime.types"))
Expect(err).To(BeNil())
Expect(data).To(Equal([]byte("mime types info")))
})
})
Context("custom mime.types does NOT exist", func() {
It("uses the provided mime.types", func() {
data, err = ioutil.ReadFile(filepath.Join(buildDir, "nginx", "conf", "mime.types"))
Expect(err).To(BeNil())
Expect(string(data)).To(Equal(finalize.MimeTypes))
})
})
})
Describe("CopyFilesToPublic", func() {
var (
appRootDir string
appRootFiles []string
buildDirFiles []string
buildDirDirectories []string
)
JustBeforeEach(func() {
buildDirFiles = []string{"Staticfile", "Staticfile.auth", "manifest.yml", ".profile", "stackato.yml"}
for _, file := range buildDirFiles {
err = ioutil.WriteFile(filepath.Join(buildDir, file), []byte(file+"contents"), 0644)
Expect(err).To(BeNil())
}
appRootFiles = []string{".hidden.html", "index.html"}
for _, file := range appRootFiles {
err = ioutil.WriteFile(filepath.Join(appRootDir, file), []byte(file+"contents"), 0644)
Expect(err).To(BeNil())
}
buildDirDirectories = []string{".profile.d", ".cloudfoundry"}
for _, dir := range buildDirDirectories {
err = os.MkdirAll(filepath.Join(buildDir, dir), 0755)
Expect(err).To(BeNil())
}
err = finalizer.CopyFilesToPublic(appRootDir)
Expect(err).To(BeNil())
})
AfterEach(func() {
err = os.RemoveAll(appRootDir)
Expect(err).To(BeNil())
})
Context("The appRootDir is <buildDir>/public", func() {
BeforeEach(func() {
appRootDir = filepath.Join(buildDir, "public")
err = os.MkdirAll(appRootDir, 0755)
Expect(err).To(BeNil())
err = ioutil.WriteFile(filepath.Join(appRootDir, "index2.html"), []byte("html contents"), 0644)
})
It("doesn't copy any files", func() {
for _, file := range buildDirFiles {
Expect(filepath.Join(buildDir, file)).To(BeAnExistingFile())
}
for _, dir := range buildDirDirectories {
Expect(filepath.Join(buildDir, dir)).To(BeADirectory())
}
for _, file := range appRootFiles {
Expect(filepath.Join(appRootDir, file)).To(BeAnExistingFile())
}
Expect(filepath.Join(appRootDir, "index2.html")).To(BeAnExistingFile())
})
})
Context("The appRootDir is NOT <buildDir>/public", func() {
Context("host dotfiles is set", func() {
BeforeEach(func() {
staticfile.HostDotFiles = true
appRootDir, err = ioutil.TempDir("", "staticfile-buildpack.app_root.")
Expect(err).To(BeNil())
})
It("Moves the dot files to public/", func() {
Expect(filepath.Join(buildDir, "public", ".hidden.html")).To(BeAnExistingFile())
})
It("Moves the regular files to public/", func() {
Expect(filepath.Join(buildDir, "public", "index.html")).To(BeAnExistingFile())
})
It("Does not move the blacklisted files to public/", func() {
Expect(filepath.Join(buildDir, "Staticfile")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, "Staticfile.auth")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, "manifest.yml")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, ".profile")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, "stackato.yml")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, ".profile.d")).To(BeADirectory())
Expect(filepath.Join(buildDir, ".cloudfoundry")).To(BeADirectory())
Expect(filepath.Join(buildDir, "public", "Staticfile")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", "Staticfile.auth")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", "manifest.yml")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", ".profile")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", "stackato.yml")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", ".profile.d")).ToNot(BeADirectory())
Expect(filepath.Join(buildDir, "public", ".cloudfoundry")).ToNot(BeADirectory())
})
Context("and <buildDir>/public exists", func() {
BeforeEach(func() {
Expect(os.Mkdir(filepath.Join(buildDir, "public"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(buildDir, "public", "orig.html"), []byte("html contents"), 0644)).To(Succeed())
})
It("overrides <buildDir>/public", func() {
Expect(filepath.Join(buildDir, "public", "orig.html")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", "index.html")).To(BeAnExistingFile())
})
})
})
Context("host dotfiles is NOT set", func() {
BeforeEach(func() {
staticfile.HostDotFiles = false
appRootDir = buildDir
})
It("does NOT move the dot files to public/", func() {
Expect(filepath.Join(buildDir, ".hidden.html")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", ".hidden.html")).NotTo(BeAnExistingFile())
})
It("Moves the regular files to public/", func() {
Expect(filepath.Join(buildDir, "public", "index.html")).To(BeAnExistingFile())
})
It("Does not move the blacklisted files to public/", func() {
Expect(filepath.Join(buildDir, "Staticfile")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, "Staticfile.auth")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, "manifest.yml")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, ".profile")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, "stackato.yml")).To(BeAnExistingFile())
Expect(filepath.Join(buildDir, ".profile.d")).To(BeADirectory())
Expect(filepath.Join(buildDir, ".cloudfoundry")).To(BeADirectory())
Expect(filepath.Join(buildDir, "public", "Staticfile")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", "Staticfile.auth")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", "manifest.yml")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", ".profile")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", "stackato.yml")).ToNot(BeAnExistingFile())
Expect(filepath.Join(buildDir, "public", ".profile.d")).ToNot(BeADirectory())
Expect(filepath.Join(buildDir, "public", ".cloudfoundry")).ToNot(BeADirectory())
})
})
})
})
})