-
Notifications
You must be signed in to change notification settings - Fork 31
/
installer_test.go
1013 lines (862 loc) · 36.9 KB
/
installer_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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package libbuildpack_test
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
"github.com/cloudfoundry/libbuildpack"
"github.com/cloudfoundry/libbuildpack/ansicleaner"
httpmock "github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Installer", func() {
var (
oldCfStack string
installer *libbuildpack.Installer
manifestDir string
err error
currentTime time.Time
buffer *bytes.Buffer
logger *libbuildpack.Logger
)
BeforeEach(func() {
oldCfStack = os.Getenv("CF_STACK")
os.Setenv("CF_STACK", "cflinuxfs2")
err = nil
manifestDir = "fixtures/manifest/standard"
currentTime = time.Now()
httpmock.Reset()
buffer = new(bytes.Buffer)
logger = libbuildpack.NewLogger(ansicleaner.New(buffer))
})
AfterEach(func() { err = os.Setenv("CF_STACK", oldCfStack); Expect(err).To(BeNil()) })
JustBeforeEach(func() {
manifest, err := libbuildpack.NewManifest(manifestDir, logger, currentTime)
Expect(err).To(BeNil())
installer = libbuildpack.NewInstaller(manifest)
installer.SetRetryTimeLimit(10 * time.Millisecond)
installer.SetRetryTimeInitialInterval(1 * time.Millisecond)
})
Describe("FetchDependency", func() {
type ExpectedEntry struct {
entry libbuildpack.ManifestEntry
content []byte
appCachePath string
buildpackCache string
}
var (
tmpdir, outputFile, appCacheDir string
entryToFetch ExpectedEntry
allEntries []libbuildpack.ManifestEntry
)
BeforeEach(func() {
tmpdir, err = ioutil.TempDir("", "downloads")
Expect(err).To(BeNil())
outputFile = filepath.Join(tmpdir, "out.tgz")
manifestDir, err = ioutil.TempDir("", "buildpack")
Expect(err).To(BeNil())
appCacheDir, err = ioutil.TempDir("", "appCache")
Expect(err).To(BeNil())
entryToFetch = ExpectedEntry{
entry: libbuildpack.ManifestEntry{
Dependency: libbuildpack.Dependency{
Name: "thing",
Version: "1"},
URI: "https://example.com/dependencies/thing-1-linux-x64.tgz",
File: "",
SHA256: "fdf72806b9bc1a1bc78be1bfc21978d03591dea5042304211b81235dbf87bd77",
CFStacks: []string{"cflinuxfs2"},
},
content: []byte("exciting binary data"),
}
shaURI := sha256.Sum256([]byte(entryToFetch.entry.URI))
entryToFetch.appCachePath = filepath.Join(appCacheDir, "dependencies", hex.EncodeToString(shaURI[:]), "thing-1-linux-x64.tgz")
allEntries = []libbuildpack.ManifestEntry{entryToFetch.entry}
for _, name := range []string{"thing", "some-dependency-name", "mysql"} {
for _, version := range []string{"5", "6", "7.5.0", "albumin"} {
entry := libbuildpack.ManifestEntry{
Dependency: libbuildpack.Dependency{
Name: name,
Version: version,
},
}
allEntries = append(allEntries, entry)
}
}
})
AfterEach(func() {
Expect(os.RemoveAll(tmpdir)).To(Succeed())
Expect(os.RemoveAll(appCacheDir)).To(Succeed())
})
type CachedTestInputs struct {
pathToCachedFile string
dependency libbuildpack.Dependency
expectedContents []byte
}
usingCachedFileTests := func(inputs *CachedTestInputs) {
Context("dependency exists cached on disk and matches checksum", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Dir(inputs.pathToCachedFile), 0755)
Expect(ioutil.WriteFile(inputs.pathToCachedFile, inputs.expectedContents, 0644)).To(Succeed())
})
It("copies the cached file to outputFile", func() {
err = installer.FetchDependency(inputs.dependency, outputFile)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(outputFile)).To(Equal(inputs.expectedContents))
})
It("makes intermediate directories", func() {
outputFile = filepath.Join(tmpdir, "notexist", "out.tgz")
err = installer.FetchDependency(inputs.dependency, outputFile)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(outputFile)).To(Equal(inputs.expectedContents))
})
})
Context("dependency exists cached on disk and does not match checksum", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Dir(inputs.pathToCachedFile), 0755)
Expect(ioutil.WriteFile(inputs.pathToCachedFile, append(inputs.expectedContents, []byte(" except not")...), 0644)).To(Succeed())
})
It("raises error", func() {
err = installer.FetchDependency(inputs.dependency, outputFile)
Expect(err).ToNot(BeNil())
})
It("outputfile does not exist", func() {
err = installer.FetchDependency(inputs.dependency, outputFile)
Expect(outputFile).ToNot(BeAnExistingFile())
})
})
Context("dependency is not cached on disk", func() {
It("raises error", func() {
err = installer.FetchDependency(inputs.dependency, outputFile)
Expect(err).ToNot(BeNil())
})
})
}
type DownloadingTestInputs struct {
Dependency libbuildpack.Dependency
DependencyURI string
ExpectedContent []byte
PathToCachedFile string
CheckOnSuccess func()
CheckOnError func()
}
BehaviorWhenDownloading := func(inputs *DownloadingTestInputs) {
Context("url exists and matches checksum", func() {
BeforeEach(func() {
httpmock.RegisterResponder("GET", inputs.DependencyURI,
httpmock.NewStringResponder(200, string(inputs.ExpectedContent)))
})
It("downloads the file to the requested location", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(outputFile)).To(Equal(inputs.ExpectedContent))
})
inputs.CheckOnSuccess()
It("makes intermediate directories", func() {
outputFile = filepath.Join(tmpdir, "notexist", "out.tgz")
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(outputFile)).To(Equal(inputs.ExpectedContent))
})
})
Context("url returns error then success and matches checksum", func() {
BeforeEach(func() {
httpmock.RegisterResponder("GET", inputs.DependencyURI,
httpmock.ResponderFromMultipleResponses(
[]*http.Response{
httpmock.NewStringResponse(404, string(inputs.ExpectedContent)),
httpmock.NewStringResponse(200, string(inputs.ExpectedContent)),
},
))
})
It("downloads the file to the requested location", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(outputFile)).To(Equal(inputs.ExpectedContent))
})
inputs.CheckOnSuccess()
It("makes intermediate directories", func() {
outputFile = filepath.Join(tmpdir, "notexist", "out.tgz")
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).To(BeNil())
Expect(ioutil.ReadFile(outputFile)).To(Equal(inputs.ExpectedContent))
})
It("retries to get success", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).To(BeNil())
Expect(httpmock.GetTotalCallCount()).To(BeNumerically(">", 1))
})
})
Context("url returns 404", func() {
BeforeEach(func() {
httpmock.RegisterResponder("GET", inputs.DependencyURI,
httpmock.NewStringResponder(404, string(inputs.ExpectedContent)))
})
It("raises error", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).ToNot(BeNil())
})
It("alerts the user that the url could not be downloaded", func() {
Expect(inputs.Dependency.Name).To(Equal("thing"))
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).To(MatchError(ContainSubstring("could not download: 404")))
Expect(buffer.String()).ToNot(ContainSubstring("to ["))
})
It("outputfile does not exist", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(outputFile).ToNot(BeAnExistingFile())
})
It("retries", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(httpmock.GetTotalCallCount()).To(BeNumerically(">", 1))
})
inputs.CheckOnError()
})
Context("connection reset by peer", func() {
BeforeEach(func() {
httpmock.RegisterNoResponder(httpmock.NewErrorResponder(errors.New("connection reset by peer")))
})
It("retries a failure", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(httpmock.GetTotalCallCount()).To(BeNumerically(">", 1))
})
It("logs the error", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(buffer.String()).To(MatchRegexp("error.*connection reset by peer, retrying in .*"))
})
inputs.CheckOnError()
})
Context("url exists but does not match checksum", func() {
BeforeEach(func() {
httpmock.RegisterResponder("GET", inputs.DependencyURI,
httpmock.NewStringResponder(200, string(append(inputs.ExpectedContent, []byte("other data")...))))
})
It("raises error", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(err).ToNot(BeNil())
})
It("outputfile does not exist", func() {
err = installer.FetchDependency(inputs.Dependency, outputFile)
Expect(outputFile).ToNot(BeAnExistingFile())
})
inputs.CheckOnError()
})
}
Context("uncached", func() {
inputs := DownloadingTestInputs{}
inputs.CheckOnSuccess = func() {}
inputs.CheckOnError = func() {}
BeforeEach(func() {
entryToFetch.entry.File = "" // not cached in buildpack
manifestForTest := libbuildpack.Manifest{
LanguageString: "sample",
ManifestEntries: allEntries,
}
y := libbuildpack.NewYAML()
Expect(y.Write(filepath.Join(manifestDir, "manifest.yml"), manifestForTest)).To(Succeed())
inputs.Dependency = entryToFetch.entry.Dependency
inputs.DependencyURI = entryToFetch.entry.URI
inputs.ExpectedContent = entryToFetch.content
})
BehaviorWhenDownloading(&inputs)
})
Context("app cached", func() {
var (
manifestForTest libbuildpack.Manifest
)
inputs := DownloadingTestInputs{}
BeforeEach(func() {
entryToFetch.entry.File = "" // not cached in buildpack
manifestForTest = libbuildpack.Manifest{
LanguageString: "sample",
ManifestEntries: []libbuildpack.ManifestEntry{entryToFetch.entry},
}
y := libbuildpack.NewYAML()
Expect(y.Write(filepath.Join(manifestDir, "manifest.yml"), manifestForTest)).To(Succeed())
inputs.Dependency = entryToFetch.entry.Dependency
inputs.DependencyURI = entryToFetch.entry.URI
inputs.ExpectedContent = entryToFetch.content
})
JustBeforeEach(func() {
Expect(installer.SetAppCacheDir(appCacheDir)).To(Succeed())
})
Context("when there is no cached file", func() {
checkOnSuccess := func() {
It("downloads the file to the cache location", func() {
Expect(installer.FetchDependency(entryToFetch.entry.Dependency, outputFile)).To(Succeed())
Expect(ioutil.ReadFile(entryToFetch.appCachePath)).To(Equal(entryToFetch.content))
})
}
checkOnError := func() {
It("cached file does not exist", func() {
err = installer.FetchDependency(entryToFetch.entry.Dependency, outputFile)
Expect(err).ToNot(BeNil())
Expect(entryToFetch.appCachePath).ToNot(BeAnExistingFile())
})
}
inputs.CheckOnSuccess = checkOnSuccess
inputs.CheckOnError = checkOnError
BehaviorWhenDownloading(&inputs)
})
Context("when there are other files in the app cache", func() {
var extraFilePaths []string
BeforeEach(func() {
extraFilePaths = []string{}
// create file in app cache dir
extraFile := filepath.Join(appCacheDir, "dependencies", "abcdef0123456789", "decoyFile")
Expect(os.MkdirAll(filepath.Dir(extraFile), 0755)).To(Succeed())
Expect(ioutil.WriteFile(extraFile, []byte("decoy content"), 0644)).To(Succeed())
extraFilePaths = append(extraFilePaths, extraFile)
// create file for real dependency in manifest
extraOtherDepFile := filepath.Join(appCacheDir, "dependencies", "662eacac1df6ae7eee9ccd1ac1eb1d0d8777c403e5375fd64d14907f875f50c0", "some-dependency-name-5.tgz")
os.MkdirAll(filepath.Dir(extraOtherDepFile), 0755)
Expect(ioutil.WriteFile(extraOtherDepFile, []byte("some super legit dependency content"), 0644)).To(Succeed())
extraFilePaths = append(extraFilePaths, extraOtherDepFile)
// create extra file for the fetched dependency
extraDepFile := filepath.Join(filepath.Dir(entryToFetch.appCachePath), "decoyDep.zip")
os.MkdirAll(filepath.Dir(extraDepFile), 0755)
Expect(ioutil.WriteFile(extraDepFile, []byte("some more decoy content"), 0644)).To(Succeed())
extraFilePaths = append(extraFilePaths, extraDepFile)
// Add extra dependency to manifest & rewrite that file
manifestForTest.ManifestEntries = append(manifestForTest.ManifestEntries,
libbuildpack.ManifestEntry{
Dependency: libbuildpack.Dependency{
Name: "some-dependency-name",
Version: "5"},
URI: "http://www.example.com/some/dependency/uri/some-dependency-name-5.tgz",
SHA256: "shaofcontent",
CFStacks: []string{"cflinuxfs2"},
})
y := libbuildpack.NewYAML()
Expect(y.Write(filepath.Join(manifestDir, "manifest.yml"), manifestForTest)).To(Succeed())
inputs.Dependency = entryToFetch.entry.Dependency
inputs.DependencyURI = entryToFetch.entry.URI
inputs.ExpectedContent = entryToFetch.content
})
checkOnSuccess := func() {
It("downloads the file to the cache location", func() {
Expect(installer.FetchDependency(entryToFetch.entry.Dependency, outputFile)).To(Succeed())
Expect(ioutil.ReadFile(entryToFetch.appCachePath)).To(Equal(entryToFetch.content))
})
It("everything else is deleted", func() {
Expect(installer.FetchDependency(entryToFetch.entry.Dependency, outputFile)).To(Succeed())
Expect(installer.CleanupAppCache()).To(Succeed())
for _, extraFilePath := range extraFilePaths {
Expect(extraFilePath).ToNot(BeAnExistingFile())
}
})
}
checkOnError := func() {
It("cached file does not exist", func() {
Expect(installer.FetchDependency(entryToFetch.entry.Dependency, outputFile)).ToNot(Succeed())
Expect(entryToFetch.appCachePath).ToNot(BeAnExistingFile())
})
It("other files remain", func() {
Expect(installer.FetchDependency(entryToFetch.entry.Dependency, outputFile)).ToNot(Succeed())
for _, extraFilePath := range extraFilePaths {
Expect(extraFilePath).To(BeAnExistingFile())
}
})
}
inputs.CheckOnError = checkOnError
inputs.CheckOnSuccess = checkOnSuccess
BehaviorWhenDownloading(&inputs)
})
Context("when file is in the app cache", func() {
cachedInputs := CachedTestInputs{}
BeforeEach(func() {
cachedInputs.pathToCachedFile = entryToFetch.appCachePath
cachedInputs.dependency = entryToFetch.entry.Dependency
cachedInputs.expectedContents = entryToFetch.content
})
usingCachedFileTests(&cachedInputs)
})
})
Context("buildpack cached", func() {
cachedInputs := CachedTestInputs{}
BeforeEach(func() {
dependenciesDir := filepath.Join(manifestDir, "dependencies")
os.MkdirAll(dependenciesDir, 0755)
entryToFetch.entry.File = "dependencies/c4fef5682adf1c19c7f9b76fde9d0ecb/thing-1-linux-x64.tgz"
manifestForTest := libbuildpack.Manifest{
LanguageString: "sample",
ManifestEntries: []libbuildpack.ManifestEntry{entryToFetch.entry},
}
y := libbuildpack.NewYAML()
Expect(y.Write(filepath.Join(manifestDir, "manifest.yml"), manifestForTest)).To(Succeed())
outputFile = filepath.Join(tmpdir, "out.tgz")
cachedInputs.pathToCachedFile = filepath.Join(manifestDir, entryToFetch.entry.File)
cachedInputs.dependency = entryToFetch.entry.Dependency
cachedInputs.expectedContents = entryToFetch.content
})
usingCachedFileTests(&cachedInputs)
})
})
Describe("CleanupAppCache", func() {
var (
appCacheDir string
)
BeforeEach(func() {
appCacheDir, err = ioutil.TempDir("", "appCache")
Expect(err).To(BeNil())
})
JustBeforeEach(func() {
Expect(installer.SetAppCacheDir(appCacheDir)).To(Succeed())
})
Context("no dependencies were cached", func() {
BeforeEach(func() {
Expect(filepath.Join(appCacheDir, "dependencies")).ToNot(BeADirectory())
})
It("does nothing and succeeds", func() {
Expect(installer.CleanupAppCache()).To(Succeed())
})
})
Context("dependencies were cached", func() {
BeforeEach(func() {
Expect(os.Mkdir(filepath.Join(appCacheDir, "dependencies"), 0755)).To(Succeed())
Expect(os.Mkdir(filepath.Join(appCacheDir, "dependencies", "abcd"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(appCacheDir, "dependencies", "abcd", "file.tgz"), []byte("contents"), 0644)).To(Succeed())
})
It("deletes old files", func() {
Expect(filepath.Join(appCacheDir, "dependencies", "abcd", "file.tgz")).To(BeARegularFile())
Expect(installer.CleanupAppCache()).To(Succeed())
Expect(filepath.Join(appCacheDir, "dependencies", "abcd", "file.tgz")).ToNot(BeARegularFile())
})
})
})
Describe("InstallDependency", func() {
var outputDir string
BeforeEach(func() {
outputDir, err = ioutil.TempDir("", "downloads")
Expect(err).To(BeNil())
})
AfterEach(func() {
err = os.RemoveAll(outputDir)
Expect(err).To(BeNil())
})
Context("uncached", func() {
BeforeEach(func() {
manifestDir = "fixtures/manifest/fetch"
})
Context("url exists and matches sha256", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/real_tar_file-3-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("logs the name and version of the dependency", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_tar_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("-----> Installing real_tar_file 3"))
})
It("extracts a file at the root", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_tar_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "root.txt")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "root.txt"))).To(Equal([]byte("root\n")))
})
It("extracts a nested file", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_tar_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "thing", "bin", "file2.exe")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "thing", "bin", "file2.exe"))).To(Equal([]byte("progam2\n")))
})
It("makes intermediate directories", func() {
outputDir = filepath.Join(outputDir, "notexist")
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_tar_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "thing", "bin", "file2.exe")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "thing", "bin", "file2.exe"))).To(Equal([]byte("progam2\n")))
})
Context("file does not need to be unpackaged", func() {
BeforeEach(func() {
someContents, err := ioutil.ReadFile("fixtures/source.txt")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://github.com/wp-cli/wp-cli/releases/download/v2.2.0/wp-cli-2.2.0.phar",
httpmock.NewStringResponder(200, string(someContents)))
})
It("downloads a file at the root", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "wp-cli", Version: "2.2.0"}, outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "wp-cli-2.2.0.phar")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "wp-cli-2.2.0.phar"))).To(Equal([]byte("a file\n")))
})
})
Context("by default, the version is NOT latest patch in version line", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-6.2.2-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("warns the user", func() {
patchWarning := "**WARNING** A newer version of thing is available in this buildpack. " +
"Please adjust your app to use version 6.2.3 instead of version 6.2.2 as soon as possible. " +
"Old versions of thing are only provided to assist in migrating to newer versions.\n"
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "6.2.2"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(patchWarning))
})
})
Context("when there is a greater minor version and a greater patch version", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-8.1.2-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("warns about greater minor version over greater patch version", func() {
installer.SetVersionLine("thing", "minor")
patchWarning := "**WARNING** A newer version of thing is available in this buildpack. " +
"Please adjust your app to use version 8.2.2 instead of version 8.1.2 as soon as possible. " +
"Old versions of thing are only provided to assist in migrating to newer versions.\n"
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "8.1.2"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(patchWarning))
})
})
Context("when the version line in a manifest is by patch line and the version installed is not the latest patch in that line", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-8.1.2-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("warns about greater patch version and not the greater minor version", func() {
installer.SetVersionLine("thing", "patch")
patchWarning := "**WARNING** A newer version of thing is available in this buildpack. " +
"Please adjust your app to use version 8.1.3 instead of version 8.1.2 as soon as possible. " +
"Old versions of thing are only provided to assist in migrating to newer versions.\n"
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "8.1.2"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(patchWarning))
})
})
Context("version is latest in version line", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-6.2.3-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("does not warn the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "6.2.3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).NotTo(ContainSubstring("newer version"))
})
})
Context("version is not semver", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://buildpacks.cloudfoundry.org/dependencies/godep/godep-v79-linux-x64-9e37ce0f.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("does not warn the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "godep", Version: "v79"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).NotTo(ContainSubstring("newer version"))
})
})
Context("version has an EOL, version line is major", func() {
const warning = "**WARNING** thing 4.x will no longer be available in new buildpacks released after 2017-03-01."
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-4.6.1-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
Context("less than 30 days in the future", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2017-02-15")
Expect(err).To(BeNil())
})
It("warns the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "4.6.1"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(warning))
})
Context("dependency EOL has a link associated with it", func() {
It("includes the link in the warning", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "4.6.1"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("See: http://example.com/eol-policy"))
})
})
Context("dependency EOL does not have a link associated with it", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-5.2.3-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("does not include the word 'See:' in the warning", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "5.2.3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).ToNot(ContainSubstring("See:"))
})
})
})
Context("in the past", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2017-12-15")
Expect(err).To(BeNil())
})
It("warns the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "4.6.1"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(warning))
})
})
Context("more than 30 days in the future", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2016-10-15")
Expect(err).To(BeNil())
})
It("does not warn the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "4.6.1"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).ToNot(ContainSubstring(warning))
})
})
})
Context("version has an EOL, version line is major + minor", func() {
const warning = "**WARNING** thing 6.2.x will no longer be available in new buildpacks released after 2018-04-01"
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-6.2.3-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
Context("less than 30 days in the future", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2018-03-29")
Expect(err).To(BeNil())
})
It("warns the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "6.2.3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(warning))
})
})
Context("in the past", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2019-12-30")
Expect(err).To(BeNil())
})
It("warns the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "6.2.3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(warning))
})
})
Context("more than 30 days in the future", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2018-01-15")
Expect(err).To(BeNil())
})
It("does not warn the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "6.2.3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).ToNot(ContainSubstring(warning))
})
})
})
Context("version has an EOL, version line non semver", func() {
const warning = "**WARNING** nonsemver abc-1.2.3-def-4.5.6 will no longer be available in new buildpacks released after 2018-04-01"
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/nonsemver-abc-1.2.3-def-4.5.6-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
Context("less than 30 days in the future", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2018-03-29")
Expect(err).To(BeNil())
})
It("warns the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "nonsemver", Version: "abc-1.2.3-def-4.5.6"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(warning))
})
})
Context("in the past", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2019-12-30")
Expect(err).To(BeNil())
})
It("warns the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "nonsemver", Version: "abc-1.2.3-def-4.5.6"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring(warning))
})
})
Context("more than 30 days in the future", func() {
BeforeEach(func() {
currentTime, err = time.Parse("2006-01-02", "2018-01-15")
Expect(err).To(BeNil())
})
It("does not warn the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "nonsemver", Version: "abc-1.2.3-def-4.5.6"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).ToNot(ContainSubstring(warning))
})
})
})
Context("version does not have an EOL", func() {
const warning = "**WARNING** real_tar_file 3 will no longer be available in new buildpacks released after"
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/real_tar_file-3-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("does not warn the user", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_tar_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).ToNot(ContainSubstring(warning))
})
})
})
Context("url exists but does not match sha256", func() {
BeforeEach(func() {
httpmock.RegisterResponder("GET", "https://example.com/dependencies/thing-1-linux-x64.tgz",
httpmock.NewStringResponder(200, "other data"))
})
It("logs the name and version of the dependency", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "1"}, outputDir)
Expect(err).ToNot(BeNil())
Expect(buffer.String()).To(ContainSubstring("-----> Installing thing 1"))
})
It("outputfile does not exist", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "thing", Version: "1"}, outputDir)
Expect(err).ToNot(BeNil())
Expect(filepath.Join(outputDir, "root.txt")).ToNot(BeAnExistingFile())
})
})
})
Context("cached", func() {
var (
dependenciesDir string
outputDir string
)
BeforeEach(func() {
manifestDir, err = ioutil.TempDir("", "cached")
Expect(err).To(BeNil())
dependenciesDir = filepath.Join(manifestDir, "dependencies")
os.MkdirAll(dependenciesDir, 0755)
data, err := ioutil.ReadFile("fixtures/manifest/fetch_cached/manifest.yml")
Expect(err).To(BeNil())
err = ioutil.WriteFile(filepath.Join(manifestDir, "manifest.yml"), data, 0644)
Expect(err).To(BeNil())
outputDir, err = ioutil.TempDir("", "downloads")
Expect(err).To(BeNil())
})
Context("url exists cached on disk and matches sha256", func() {
BeforeEach(func() {
libbuildpack.CopyFile("fixtures/thing.zip", filepath.Join(dependenciesDir, "f666296d630cce4c94c62afcc6680b44", "real_zip_file-3-linux-x64.zip"))
})
It("logs the name and version of the dependency", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_zip_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(buffer.String()).To(ContainSubstring("-----> Installing real_zip_file 3"))
})
It("extracts a file at the root", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_zip_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "root.txt")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "root.txt"))).To(Equal([]byte("root\n")))
})
It("extracts a nested file", func() {
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_zip_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "thing", "bin", "file2.exe")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "thing", "bin", "file2.exe"))).To(Equal([]byte("progam2\n")))
})
It("makes intermediate directories", func() {
outputDir = filepath.Join(outputDir, "notexist")
err = installer.InstallDependency(libbuildpack.Dependency{Name: "real_zip_file", Version: "3"}, outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "thing", "bin", "file2.exe")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "thing", "bin", "file2.exe"))).To(Equal([]byte("progam2\n")))
})
})
})
})
Describe("InstallOnlyVersion", func() {
var outputDir string
BeforeEach(func() {
manifestDir = "fixtures/manifest/fetch"
outputDir, err = ioutil.TempDir("", "downloads")
Expect(err).To(BeNil())
})
AfterEach(func() { err = os.RemoveAll(outputDir); Expect(err).To(BeNil()) })
Context("there is only one version of the dependency", func() {
BeforeEach(func() {
tgzContents, err := ioutil.ReadFile("fixtures/thing.tgz")
Expect(err).To(BeNil())
httpmock.RegisterResponder("GET", "https://example.com/dependencies/real_tar_file-3-linux-x64.tgz",
httpmock.NewStringResponder(200, string(tgzContents)))
})
It("installs", func() {
outputDir = filepath.Join(outputDir, "notexist")
err = installer.InstallOnlyVersion("real_tar_file", outputDir)
Expect(err).To(BeNil())
Expect(filepath.Join(outputDir, "thing", "bin", "file2.exe")).To(BeAnExistingFile())
Expect(ioutil.ReadFile(filepath.Join(outputDir, "thing", "bin", "file2.exe"))).To(Equal([]byte("progam2\n")))
})
})
Context("there is more than one version of the dependency", func() {
It("fails", func() {
outputDir = filepath.Join(outputDir, "notexist")
err = installer.InstallOnlyVersion("thing", outputDir)
Expect(err).To(MatchError("more than one version of thing found"))
})
})
Context("there are no versions of the dependency", func() {
It("fails", func() {
outputDir = filepath.Join(outputDir, "notexist")
err = installer.InstallOnlyVersion("not_a_dependency", outputDir)
Expect(err).To(MatchError("no versions of not_a_dependency found"))
})
})
})
Describe("SetVersionLine", func() {
var i *libbuildpack.Installer
var versionLine map[string]string
BeforeEach(func() {
i = libbuildpack.NewInstaller(nil)
versionLine = *i.GetVersionLine()
})
It("is an empty map by default", func() {
Expect(len(versionLine)).To(BeZero())
})
It("returns the version line that was previously set", func() {
i.SetVersionLine("thing", "minor")
line := versionLine["thing"]
Expect(line).To(Equal("minor"))
})
It("sets more than one line", func() {
i.SetVersionLine("thing", "minor")
i.SetVersionLine("thing2", "patch")
line := versionLine["thing"]