-
Notifications
You must be signed in to change notification settings - Fork 26
/
service_test.go
1492 lines (1286 loc) · 43.5 KB
/
service_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 postal_test
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/postal/fakes"
"github.com/sclevine/spec"
//nolint Ignore SA1019, usage of deprecated package within a deprecated test case
"github.com/paketo-buildpacks/packit/v2/paketosbom"
. "github.com/onsi/gomega"
)
func testService(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
path string
transport *fakes.Transport
mappingResolver *fakes.MappingResolver
mirrorResolver *fakes.MirrorResolver
service postal.Service
)
it.Before(func() {
file, err := os.CreateTemp("", "buildpack.toml")
Expect(err).NotTo(HaveOccurred())
path = file.Name()
_, err = file.WriteString(`
[[metadata.dependencies]]
deprecation_date = 2022-04-01T00:00:00Z
cpe = "some-cpe"
cpes = ["some-cpe", "other-cpe"]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack"]
uri = "some-uri"
version = "1.2.3"
[[metadata.dependencies]]
id = "some-other-entry"
cpes = ["some-cpe", "other-cpe"]
sha256 = "some-other-sha"
stacks = ["some-stack"]
uri = "some-uri"
version = "1.2.4"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["other-stack"]
uri = "some-uri"
version = "1.2.5"
[[metadata.dependencies]]
id = "some-random-entry"
cpe = "some-cpe"
cpes = ["some-cpe", "other-cpe"]
sha256 = "some-random-sha"
stacks = ["other-random-stack"]
uri = "some-uri"
version = "1.3.0"
[[metadata.dependencies]]
id = "some-random-other-entry"
sha256 = "some-random-other-sha"
stacks = ["some-other-random-stack"]
uri = "some-uri"
version = "2.0.0"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack"]
uri = "some-uri"
version = "4.5.6"
strip-components = 1
[[metadata.dependencies]]
id = "some-other-entry"
sha256 = "some-sha"
stacks = ["*"]
uri = "some-uri"
version = "4.5.6"
strip-components = 1
`)
Expect(err).NotTo(HaveOccurred())
Expect(file.Close()).To(Succeed())
transport = &fakes.Transport{}
mappingResolver = &fakes.MappingResolver{}
mirrorResolver = &fakes.MirrorResolver{}
service = postal.NewService(transport).
WithDependencyMappingResolver(mappingResolver).
WithDependencyMirrorResolver(mirrorResolver)
})
context("Resolve", func() {
it("finds the best matching dependency given a plan entry", func() {
deprecationDate, err := time.Parse(time.RFC3339, "2022-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
dependency, err := service.Resolve(path, "some-entry", "1.2.*", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
CPE: "some-cpe",
CPEs: []string{"some-cpe", "other-cpe"},
DeprecationDate: deprecationDate,
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "1.2.3",
}))
})
context("when the dependency has a wildcard stack", func() {
it("is compatible with all stack ids", func() {
dependency, err := service.Resolve(path, "some-other-entry", "", "random-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
ID: "some-other-entry",
Stacks: []string{"*"},
URI: "some-uri",
SHA256: "some-sha",
Version: "4.5.6",
StripComponents: 1,
}))
})
})
context("when there is NOT a default version", func() {
context("when the entry version is empty", func() {
it("picks the dependency with the highest semantic version number", func() {
dependency, err := service.Resolve(path, "some-entry", "", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "4.5.6",
StripComponents: 1,
}))
})
})
context("when the entry version is default", func() {
it("picks the dependency with the highest semantic version number", func() {
dependency, err := service.Resolve(path, "some-entry", "default", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "4.5.6",
StripComponents: 1,
}))
})
})
context("when there is a version with a major, minor, patch, and pessimistic operator (~>)", func() {
it("picks the dependency >= version and < major.minor+1", func() {
deprecationDate, err := time.Parse(time.RFC3339, "2022-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
dependency, err := service.Resolve(path, "some-entry", "~> 1.2.0", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
DeprecationDate: deprecationDate,
CPE: "some-cpe",
CPEs: []string{"some-cpe", "other-cpe"},
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "1.2.3",
}))
})
})
context("when there is a version with a major, minor, and pessimistic operator (~>)", func() {
it("picks the dependency >= version and < major+1", func() {
deprecationDate, err := time.Parse(time.RFC3339, "2022-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
dependency, err := service.Resolve(path, "some-entry", "~> 1.1", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
CPE: "some-cpe",
CPEs: []string{"some-cpe", "other-cpe"},
DeprecationDate: deprecationDate,
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "1.2.3",
}))
})
})
context("when there is a version with a major line only and pessimistic operator (~>)", func() {
it("picks the dependency >= version.0.0 and < major+1.0.0", func() {
deprecationDate, err := time.Parse(time.RFC3339, "2022-04-01T00:00:00Z")
Expect(err).NotTo(HaveOccurred())
dependency, err := service.Resolve(path, "some-entry", "~> 1", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
CPE: "some-cpe",
CPEs: []string{"some-cpe", "other-cpe"},
DeprecationDate: deprecationDate,
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "1.2.3",
}))
})
})
})
context("when there is a default version", func() {
it.Before(func() {
err := os.WriteFile(path, []byte(`
[metadata]
[metadata.default-versions]
some-entry = "1.2.x"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack"]
uri = "some-uri"
version = "1.2.3"
[[metadata.dependencies]]
id = "some-other-entry"
sha256 = "some-other-sha"
stacks = ["some-stack"]
uri = "some-uri"
version = "1.2.4"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["other-stack"]
uri = "some-uri"
version = "1.2.5"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack"]
uri = "some-uri"
version = "4.5.6"
`), 0600)
Expect(err).NotTo(HaveOccurred())
})
context("when the entry version is empty", func() {
it("picks the dependency that best matches the default version", func() {
dependency, err := service.Resolve(path, "some-entry", "", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "1.2.3",
}))
})
})
context("when the entry version is default", func() {
it("picks the dependency that best matches the default version", func() {
dependency, err := service.Resolve(path, "some-entry", "default", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri",
SHA256: "some-sha",
Version: "1.2.3",
}))
})
})
})
context("when both a wildcard stack constraint and a specific stack constraint exist for the same dependency version", func() {
it.Before(func() {
err := os.WriteFile(path, []byte(`
[metadata]
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack"]
uri = "some-uri-specific-stack"
version = "1.2.1"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["*"]
uri = "some-uri-only-wildcard"
version = "1.2.1"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack","*"]
uri = "some-uri-only-wildcard"
version = "1.2.3"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack"]
uri = "some-uri-specific-stack"
version = "1.2.3"
`), 0600)
Expect(err).NotTo(HaveOccurred())
})
it("selects the more specific stack constraint", func() {
dependency, err := service.Resolve(path, "some-entry", "*", "some-stack")
Expect(err).NotTo(HaveOccurred())
Expect(dependency).To(Equal(postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-uri-specific-stack",
SHA256: "some-sha",
Version: "1.2.3",
}))
})
})
context("failure cases", func() {
context("when the buildpack.toml is malformed", func() {
it.Before(func() {
err := os.WriteFile(path, []byte("this is not toml"), 0600)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := service.Resolve(path, "some-entry", "1.2.3", "some-stack")
Expect(err).To(MatchError(ContainSubstring("failed to parse buildpack.toml")))
})
})
context("when the entry version constraint is not valid", func() {
it("returns an error", func() {
_, err := service.Resolve(path, "some-entry", "this-is-not-semver", "some-stack")
Expect(err).To(MatchError(ContainSubstring("improper constraint")))
})
})
context("when the dependency version is not valid", func() {
it.Before(func() {
err := os.WriteFile(path, []byte(`
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha"
stacks = ["some-stack"]
uri = "some-uri"
version = "this is super not semver"
`), 0600)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := service.Resolve(path, "some-entry", "1.2.3", "some-stack")
Expect(err).To(MatchError(ContainSubstring("Invalid Semantic Version")))
})
})
context("when multiple dependencies have a wildcard stack for the same version", func() {
it.Before(func() {
err := os.WriteFile(path, []byte(`
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha-A"
stacks = ["some-stack","*"]
uri = "some-uri-A"
version = "1.2.3"
[[metadata.dependencies]]
id = "some-entry"
sha256 = "some-sha-B"
stacks = ["some-stack","some-other-stack","*"]
uri = "some-uri-B"
version = "1.2.3"
`), 0600)
Expect(err).NotTo(HaveOccurred())
})
it("returns an error", func() {
_, err := service.Resolve(path, "some-entry", "1.2.3", "some-stack")
Expect(err).To(MatchError(ContainSubstring(`multiple dependencies support wildcard stack for version: "1.2.3"`)))
})
})
context("when the entry version constraint cannot be satisfied", func() {
it("returns a typed error with all the supported versions listed", func() {
expectedErr := &postal.ErrNoDeps{}
_, err := service.Resolve(path, "some-entry", "9.9.9", "some-stack")
Expect(errors.As(err, &expectedErr)).To(BeTrue())
Expect(err).To(MatchError(ContainSubstring("failed to satisfy \"some-entry\" dependency version constraint \"9.9.9\": no compatible versions on \"some-stack\" stack. Supported versions are: [1.2.3, 4.5.6]")))
})
})
})
})
context("Deliver", func() {
var (
dependencyHash string
hash512 string
layerPath string
deliver func() error
)
it.Before(func() {
var err error
layerPath, err = os.MkdirTemp("", "layer")
Expect(err).NotTo(HaveOccurred())
buffer := bytes.NewBuffer(nil)
zw := gzip.NewWriter(buffer)
tw := tar.NewWriter(zw)
Expect(tw.WriteHeader(&tar.Header{Name: "./some-dir", Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
nestedFile := "./some-dir/some-file"
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
for _, file := range []string{"./first", "./second", "./third"} {
Expect(tw.WriteHeader(&tar.Header{Name: file, Mode: 0755, Size: int64(len(file))})).To(Succeed())
_, err = tw.Write([]byte(file))
Expect(err).NotTo(HaveOccurred())
}
linkName := "./symlink"
linkDest := "./first"
Expect(tw.WriteHeader(&tar.Header{Name: linkName, Mode: 0777, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: linkDest})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
Expect(zw.Close()).To(Succeed())
sum := sha256.Sum256(buffer.Bytes())
dependencyHash = hex.EncodeToString(sum[:])
sum512 := sha512.Sum512(buffer.Bytes())
hash512 = hex.EncodeToString(sum512[:])
transport.DropCall.Returns.ReadCloser = io.NopCloser(buffer)
deliver = func() error {
return service.Deliver(
postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-entry.tgz",
SHA256: dependencyHash,
Version: "1.2.3",
},
"some-cnb-path",
layerPath,
"some-platform-dir",
)
}
})
it.After(func() {
Expect(os.RemoveAll(layerPath)).To(Succeed())
})
it("downloads the dependency and unpackages it into the path", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("some-entry.tgz"))
Expect(mappingResolver.FindDependencyMappingCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(layerPath, "first"),
filepath.Join(layerPath, "second"),
filepath.Join(layerPath, "third"),
filepath.Join(layerPath, "some-dir"),
filepath.Join(layerPath, "symlink"),
}))
info, err := os.Stat(filepath.Join(layerPath, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
})
context("when using the checksum field", func() {
it.Before(func() {
deliver = func() error {
return service.Deliver(
postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-entry.tgz",
Checksum: fmt.Sprintf("sha512:%s", hash512),
Version: "1.2.3",
},
"some-cnb-path",
layerPath,
"some-platform-dir",
)
}
})
it("downloads the dependency and unpackages it into the path", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("some-entry.tgz"))
Expect(mappingResolver.FindDependencyMappingCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(layerPath, "first"),
filepath.Join(layerPath, "second"),
filepath.Join(layerPath, "third"),
filepath.Join(layerPath, "some-dir"),
filepath.Join(layerPath, "symlink"),
}))
info, err := os.Stat(filepath.Join(layerPath, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
})
})
context("when the dependency has a strip-components value set", func() {
it.Before(func() {
var err error
layerPath, err = os.MkdirTemp("", "path")
Expect(err).NotTo(HaveOccurred())
buffer := bytes.NewBuffer(nil)
zw := gzip.NewWriter(buffer)
tw := tar.NewWriter(zw)
Expect(tw.WriteHeader(&tar.Header{Name: "some-dir", Mode: 0755, Typeflag: tar.TypeDir})).To(Succeed())
_, err = tw.Write(nil)
Expect(err).NotTo(HaveOccurred())
nestedFile := "some-dir/some-file"
Expect(tw.WriteHeader(&tar.Header{Name: nestedFile, Mode: 0755, Size: int64(len(nestedFile))})).To(Succeed())
_, err = tw.Write([]byte(nestedFile))
Expect(err).NotTo(HaveOccurred())
for _, file := range []string{"some-dir/first", "some-dir/second", "some-dir/third"} {
Expect(tw.WriteHeader(&tar.Header{Name: file, Mode: 0755, Size: int64(len(file))})).To(Succeed())
_, err = tw.Write([]byte(file))
Expect(err).NotTo(HaveOccurred())
}
linkName := "some-dir/symlink"
linkDest := "./first"
Expect(tw.WriteHeader(&tar.Header{Name: linkName, Mode: 0777, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: linkDest})).To(Succeed())
_, err = tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
Expect(zw.Close()).To(Succeed())
sum := sha256.Sum256(buffer.Bytes())
dependencyHash = hex.EncodeToString(sum[:])
transport.DropCall.Returns.ReadCloser = io.NopCloser(buffer)
deliver = func() error {
return service.Deliver(
postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-entry.tgz",
SHA256: dependencyHash,
Version: "1.2.3",
StripComponents: 1,
},
"some-cnb-path",
layerPath,
"",
)
}
})
it.After(func() {
Expect(os.RemoveAll(layerPath)).To(Succeed())
})
it("downloads the dependency, strips given number of componenets and unpackages it into the path", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("some-entry.tgz"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(layerPath, "first"),
filepath.Join(layerPath, "second"),
filepath.Join(layerPath, "third"),
filepath.Join(layerPath, "symlink"),
filepath.Join(layerPath, "some-file"),
}))
info, err := os.Stat(filepath.Join(layerPath, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
})
})
context("when the dependency should be a named file", func() {
it.Before(func() {
var err error
layerPath, err = os.MkdirTemp("", "path")
Expect(err).NotTo(HaveOccurred())
buffer := bytes.NewBuffer(nil)
buffer.WriteString("some-file-contents")
sum := sha256.Sum256(buffer.Bytes())
dependencyHash = hex.EncodeToString(sum[:])
transport.DropCall.Returns.ReadCloser = io.NopCloser(buffer)
deliver = func() error {
return service.Deliver(
postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "https://dependencies.example.com/dependencies/some-file-name.txt",
SHA256: dependencyHash,
Version: "1.2.3",
},
"some-cnb-path",
layerPath,
"some-platform-dir",
)
}
})
it.After(func() {
Expect(os.RemoveAll(layerPath)).To(Succeed())
})
it("downloads the dependency and copies it into the path with the given name", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("https://dependencies.example.com/dependencies/some-file-name.txt"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{filepath.Join(layerPath, "some-file-name.txt")}))
content, err := os.ReadFile(filepath.Join(layerPath, "some-file-name.txt"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some-file-contents"))
})
})
context("when there is a dependency mapping via binding", func() {
it.Before(func() {
mappingResolver.FindDependencyMappingCall.Returns.String = "dependency-mapping-entry.tgz"
})
context("the dependency has a checksum field", func() {
it("looks up the dependency from the platform binding and downloads that instead", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(mappingResolver.FindDependencyMappingCall.Receives.Checksum).To(Equal("sha256:" + dependencyHash))
Expect(mappingResolver.FindDependencyMappingCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("dependency-mapping-entry.tgz"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(layerPath, "first"),
filepath.Join(layerPath, "second"),
filepath.Join(layerPath, "third"),
filepath.Join(layerPath, "some-dir"),
filepath.Join(layerPath, "symlink"),
}))
info, err := os.Stat(filepath.Join(layerPath, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
})
})
context("the dependency has a SHA256 field", func() {
it("looks up the dependency from the platform binding and downloads that instead", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(mappingResolver.FindDependencyMappingCall.Receives.Checksum).To(Equal(fmt.Sprintf("sha256:%s", dependencyHash)))
Expect(mappingResolver.FindDependencyMappingCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("dependency-mapping-entry.tgz"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(layerPath, "first"),
filepath.Join(layerPath, "second"),
filepath.Join(layerPath, "third"),
filepath.Join(layerPath, "some-dir"),
filepath.Join(layerPath, "symlink"),
}))
info, err := os.Stat(filepath.Join(layerPath, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
})
})
})
context("when there is a dependency mapping via binding", func() {
it.Before(func() {
mappingResolver.FindDependencyMappingCall.Returns.String = "dependency-mapping-entry.tgz"
})
it("looks up the dependency from the platform binding and downloads that instead", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(mappingResolver.FindDependencyMappingCall.Receives.Checksum).To(Equal(fmt.Sprintf("sha256:%s", dependencyHash)))
Expect(mappingResolver.FindDependencyMappingCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("dependency-mapping-entry.tgz"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(layerPath, "first"),
filepath.Join(layerPath, "second"),
filepath.Join(layerPath, "third"),
filepath.Join(layerPath, "some-dir"),
filepath.Join(layerPath, "symlink"),
}))
info, err := os.Stat(filepath.Join(layerPath, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
})
})
context("when there is a dependency mirror", func() {
it.Before(func() {
mirrorResolver.FindDependencyMirrorCall.Returns.String = "dependency-mirror-url"
})
it("downloads dependency from mirror", func() {
err := deliver()
Expect(err).NotTo(HaveOccurred())
Expect(mirrorResolver.FindDependencyMirrorCall.Receives.Uri).To(Equal("some-entry.tgz"))
Expect(mirrorResolver.FindDependencyMirrorCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
Expect(transport.DropCall.Receives.Root).To(Equal("some-cnb-path"))
Expect(transport.DropCall.Receives.Uri).To(Equal("dependency-mirror-url"))
files, err := filepath.Glob(fmt.Sprintf("%s/*", layerPath))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(layerPath, "first"),
filepath.Join(layerPath, "second"),
filepath.Join(layerPath, "third"),
filepath.Join(layerPath, "some-dir"),
filepath.Join(layerPath, "symlink"),
}))
info, err := os.Stat(filepath.Join(layerPath, "first"))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
})
})
context("failure cases", func() {
context("when dependency mapping resolver fails", func() {
it.Before(func() {
mappingResolver.FindDependencyMappingCall.Returns.Error = fmt.Errorf("some dependency mapping error")
})
it("fails to find dependency mappings", func() {
err := deliver()
Expect(err).To(MatchError(ContainSubstring("some dependency mapping error")))
})
})
context("when dependency mirror resolver fails", func() {
it.Before(func() {
mirrorResolver.FindDependencyMirrorCall.Returns.Error = fmt.Errorf("some dependency mirror error")
})
it("fails to find dependency mirror", func() {
err := deliver()
Expect(err).To(MatchError(ContainSubstring("some dependency mirror error")))
})
})
context("when the transport cannot fetch a dependency", func() {
it.Before(func() {
transport.DropCall.Returns.Error = errors.New("there was an error")
})
it("returns an error", func() {
err := deliver()
Expect(err).To(MatchError("failed to fetch dependency: there was an error"))
})
})
context("when there is a problem with the checksum", func() {
it.Before(func() {
deliver = func() error {
return service.Deliver(
postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-entry.tgz",
Checksum: fmt.Sprintf("magic:%s", hash512),
Version: "1.2.3",
},
"some-cnb-path",
layerPath,
"some-platform-dir",
)
}
})
it("fails to create a validated reader", func() {
err := deliver()
Expect(err).To(MatchError(ContainSubstring(`unsupported algorithm "magic"`)))
})
})
context("when the file contents are empty", func() {
it.Before(func() {
// This is a FLAC header
buffer := bytes.NewBuffer([]byte("\x66\x4C\x61\x43\x00\x00\x00\x22"))
transport.DropCall.Returns.ReadCloser = io.NopCloser(buffer)
sum := sha256.Sum256(buffer.Bytes())
dependencyHash = hex.EncodeToString(sum[:])
})
it("fails to create a gzip reader", func() {
err := deliver()
Expect(err).To(MatchError(ContainSubstring("unsupported archive type")))
})
})
context("when the file checksum does not match", func() {
it("fails to create a tar reader", func() {
err := service.Deliver(
postal.Dependency{
ID: "some-entry",
Stacks: []string{"some-stack"},
URI: "some-entry.tgz",
SHA256: "this is not a valid checksum",
Version: "1.2.3",
},
"some-cnb-path",
layerPath,
"",
)
Expect(err).To(MatchError("validation error: checksum does not match"))
})
})
context("when it does not have permission to write into directory on container", func() {
it.Before(func() {
Expect(os.Chmod(layerPath, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(layerPath, 0755)).To(Succeed())
})
it("fails to make a dir", func() {
err := deliver()
Expect(err).To(MatchError(ContainSubstring("failed to create archived directory")))
})
})
context("when it does not have permission to write into directory that it decompressed", func() {
var testDir string
it.Before(func() {
testDir = filepath.Join(layerPath, "some-dir")
Expect(os.MkdirAll(testDir, os.ModePerm)).To(Succeed())
Expect(os.Chmod(testDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(testDir, 0755)).To(Succeed())
})
it("fails to make a file", func() {
err := deliver()
Expect(err).To(MatchError(ContainSubstring("failed to create archived file")))
})
})
context("when it is given a broken symlink", func() {
it.Before(func() {
buffer := bytes.NewBuffer(nil)
zw := gzip.NewWriter(buffer)
tw := tar.NewWriter(zw)
linkName := "symlink"
Expect(tw.WriteHeader(&tar.Header{Name: linkName, Mode: 0777, Size: int64(0), Typeflag: tar.TypeSymlink, Linkname: "some-file"})).To(Succeed())
_, err := tw.Write([]byte{})
Expect(err).NotTo(HaveOccurred())
Expect(tw.Close()).To(Succeed())
Expect(zw.Close()).To(Succeed())
Expect(os.WriteFile(filepath.Join(layerPath, "some-file"), nil, 0644)).To(Succeed())
Expect(os.Symlink("some-file", filepath.Join(layerPath, "symlink"))).To(Succeed())
sum := sha256.Sum256(buffer.Bytes())
dependencyHash = hex.EncodeToString(sum[:])
transport.DropCall.Returns.ReadCloser = io.NopCloser(buffer)
})
it("fails to extract the symlink", func() {
err := deliver()
Expect(err).To(MatchError(ContainSubstring("failed to extract symlink")))
})
})
context("when the has additional data in the byte stream", func() {
it.Before(func() {
var err error
layerPath, err = os.MkdirTemp("", "path")
Expect(err).NotTo(HaveOccurred())