@@ -6,106 +6,81 @@ import (
6
6
"testing"
7
7
8
8
"github.com/paketo-buildpacks/packit/postal/internal"
9
+ "github.com/paketo-buildpacks/packit/postal/internal/fakes"
10
+ "github.com/paketo-buildpacks/packit/servicebindings"
9
11
"github.com/sclevine/spec"
10
12
11
13
. "github.com/onsi/gomega"
12
14
)
13
15
14
16
func testDependencyMappings (t * testing.T , context spec.G , it spec.S ) {
15
17
var (
16
- Expect = NewWithT (t ).Expect
17
- path string
18
- resolver internal.DependencyMappingResolver
19
- bindingPath string
20
- err error
18
+ Expect = NewWithT (t ).Expect
19
+ tmpDir string
20
+ resolver internal.DependencyMappingResolver
21
+ bindingResolver * fakes. BindingResolver
22
+ err error
21
23
)
22
24
23
25
it .Before (func () {
24
- resolver = internal .NewDependencyMappingResolver ()
25
- bindingPath , err = os .MkdirTemp ("" , "bindings" )
26
+ tmpDir , err = os .MkdirTemp ("" , "dependency-mappings" )
27
+ Expect (err ).NotTo (HaveOccurred ())
28
+ Expect (os .WriteFile (filepath .Join (tmpDir , "entry-data" ), []byte ("dependency-mapping-entry.tgz" ), os .ModePerm ))
29
+
30
+ bindingResolver = & fakes.BindingResolver {}
31
+ resolver = internal .NewDependencyMappingResolver (bindingResolver )
26
32
Expect (err ).NotTo (HaveOccurred ())
27
33
})
28
34
29
35
it .After (func () {
30
- Expect (os .RemoveAll (path )).To (Succeed ())
36
+ Expect (os .RemoveAll (tmpDir )).To (Succeed ())
31
37
})
32
38
33
39
context ("FindDependencyMapping" , func () {
34
40
it .Before (func () {
35
- Expect (os .MkdirAll (filepath .Join (bindingPath , "some-binding" ), 0700 )).To (Succeed ())
36
- Expect (os .WriteFile (filepath .Join (bindingPath , "some-binding" , "type" ), []byte ("dependency-mapping" ), 0600 )).To (Succeed ())
37
- Expect (os .WriteFile (filepath .Join (bindingPath , "some-binding" , "some-sha" ), []byte ("dependency-mapping-entry.tgz" ), 0600 )).To (Succeed ())
38
-
39
- Expect (os .MkdirAll (filepath .Join (bindingPath , "other-binding" ), 0700 )).To (Succeed ())
40
- Expect (os .WriteFile (filepath .Join (bindingPath , "other-binding" , "type" ), []byte ("dependency-mapping" ), 0600 )).To (Succeed ())
41
- Expect (os .WriteFile (filepath .Join (bindingPath , "other-binding" , "other-sha" ), []byte ("dependency-mapping-entry.tgz" ), 0600 )).To (Succeed ())
42
-
43
- Expect (os .MkdirAll (filepath .Join (bindingPath , "another-binding" ), 0700 )).To (Succeed ())
44
- Expect (os .WriteFile (filepath .Join (bindingPath , "another-binding" , "type" ), []byte ("another type" ), 0600 )).To (Succeed ())
45
- Expect (os .WriteFile (filepath .Join (bindingPath , "another-binding" , "some-sha" ), []byte ("entry.tgz" ), 0600 )).To (Succeed ())
41
+ bindingResolver .ResolveCall .Returns .BindingSlice = []servicebindings.Binding {
42
+ {
43
+ Name : "some-binding" ,
44
+ Path : "some-path" ,
45
+ Type : "dependency-mapping" ,
46
+ Entries : map [string ]* servicebindings.Entry {
47
+ "some-sha" : servicebindings .NewEntry (filepath .Join (tmpDir , "entry-data" )),
48
+ },
49
+ },
50
+ {
51
+ Name : "other-binding" ,
52
+ Path : "other-path" ,
53
+ Type : "dependency-mapping" ,
54
+ Entries : map [string ]* servicebindings.Entry {
55
+ "other-sha" : servicebindings .NewEntry ("some-entry-path" ),
56
+ },
57
+ },
58
+ {
59
+ Name : "another-binding" ,
60
+ Path : "another-path" ,
61
+ Type : "another-type" ,
62
+ Entries : map [string ]* servicebindings.Entry {},
63
+ },
64
+ }
46
65
})
47
66
48
67
context ("given a set of bindings and a dependency" , func () {
49
68
it ("finds a matching dependency mappings in the platform bindings if there is one" , func () {
50
- boundDependency , err := resolver .FindDependencyMapping ("some-sha" , bindingPath )
69
+ boundDependency , err := resolver .FindDependencyMapping ("some-sha" , "some-platform-dir" )
51
70
Expect (err ).ToNot (HaveOccurred ())
71
+ Expect (bindingResolver .ResolveCall .Receives .Typ ).To (Equal ("dependency-mapping" ))
72
+ Expect (bindingResolver .ResolveCall .Receives .Provider ).To (BeEmpty ())
73
+ Expect (bindingResolver .ResolveCall .Receives .PlatformDir ).To (Equal ("some-platform-dir" ))
52
74
Expect (boundDependency ).To (Equal ("dependency-mapping-entry.tgz" ))
53
75
})
54
76
})
55
77
56
78
context ("given a set of bindings and a dependency" , func () {
57
79
it ("returns an empty DependencyMapping if there is no match" , func () {
58
- boundDependency , err := resolver .FindDependencyMapping ("unmatched-sha" , bindingPath )
80
+ boundDependency , err := resolver .FindDependencyMapping ("unmatched-sha" , "" )
59
81
Expect (err ).ToNot (HaveOccurred ())
60
82
Expect (boundDependency ).To (Equal ("" ))
61
83
})
62
84
})
63
85
})
64
-
65
- context ("failure cases" , func () {
66
- context ("when the binding path is a bad pattern" , func () {
67
- it ("errors" , func () {
68
- _ , err := resolver .FindDependencyMapping ("some-sha" , "///" )
69
- Expect (err ).To (HaveOccurred ())
70
- })
71
- })
72
-
73
- context ("when type file cannot be opened" , func () {
74
- it .Before (func () {
75
- Expect (os .MkdirAll (filepath .Join (bindingPath , "some-binding" ), 0700 )).To (Succeed ())
76
- Expect (os .WriteFile (filepath .Join (bindingPath , "some-binding" , "type" ), []byte ("dependency-mapping" ), 0000 )).To (Succeed ())
77
- Expect (os .WriteFile (filepath .Join (bindingPath , "some-binding" , "some-sha" ), []byte ("dependency-mapping-entry.tgz" ), 0600 )).To (Succeed ())
78
- })
79
- it ("errors" , func () {
80
- _ , err := resolver .FindDependencyMapping ("some-sha" , bindingPath )
81
- Expect (err ).To (HaveOccurred ())
82
- Expect (err ).To (MatchError (ContainSubstring ("couldn't read binding type" )))
83
- })
84
- })
85
-
86
- context ("when SHA256 file cannot be stat" , func () {
87
- it .Before (func () {
88
- Expect (os .MkdirAll (filepath .Join (bindingPath , "new-binding" ), 0700 )).To (Succeed ())
89
- Expect (os .WriteFile (filepath .Join (bindingPath , "new-binding" , "type" ), []byte ("dependency-mapping" ), 0644 )).To (Succeed ())
90
- Expect (os .WriteFile (filepath .Join (bindingPath , "new-binding" , "some-sha" ), []byte ("dependency-mapping-entry.tgz" ), 0644 )).To (Succeed ())
91
- Expect (os .Chmod (filepath .Join (bindingPath , "new-binding" , "some-sha" ), 0000 )).To (Succeed ())
92
- })
93
- it ("errors" , func () {
94
- _ , err := resolver .FindDependencyMapping ("some-sha" , bindingPath )
95
- Expect (err ).To (HaveOccurred ())
96
- })
97
- })
98
-
99
- context ("when SHA256 contents cannot be opened" , func () {
100
- it .Before (func () {
101
- Expect (os .MkdirAll (filepath .Join (bindingPath , "some-binding" ), 0700 )).To (Succeed ())
102
- Expect (os .WriteFile (filepath .Join (bindingPath , "some-binding" , "type" ), []byte ("dependency-mapping" ), 0600 )).To (Succeed ())
103
- Expect (os .WriteFile (filepath .Join (bindingPath , "some-binding" , "some-sha" ), []byte ("dependency-mapping-entry.tgz" ), 0000 )).To (Succeed ())
104
- })
105
- it ("errors" , func () {
106
- _ , err := resolver .FindDependencyMapping ("some-sha" , bindingPath )
107
- Expect (err ).To (HaveOccurred ())
108
- })
109
- })
110
- })
111
86
}
0 commit comments