-
Notifications
You must be signed in to change notification settings - Fork 5
/
local_fetcher_test.go
149 lines (114 loc) · 4.12 KB
/
local_fetcher_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
package freezer_test
import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/ForestEckhardt/freezer"
"github.com/ForestEckhardt/freezer/fakes"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testLocalFetcher(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
cacheDir string
buildpackCache *fakes.BuildpackCache
packager *fakes.Packager
namer *fakes.Namer
localBuildpack freezer.LocalBuildpack
localFetcher freezer.LocalFetcher
)
it.Before(func() {
var err error
cacheDir, err = os.MkdirTemp("", "cache")
Expect(err).NotTo(HaveOccurred())
packager = &fakes.Packager{}
buildpackCache = &fakes.BuildpackCache{}
buildpackCache.DirCall.Stub = func() string {
return cacheDir
}
buildpackCache.GetCall.Returns.Bool = true
namer = &fakes.Namer{}
namer.RandomNameCall.Stub = func(name string) (string, error) {
return fmt.Sprintf("%s-random-string", name), nil
}
localBuildpack = freezer.NewLocalBuildpack("path/to/buildpack", "some-buildpack")
localBuildpack.Offline = false
localBuildpack.Version = "some-version"
localFetcher = freezer.NewLocalFetcher(buildpackCache, packager, namer)
})
it.After(func() {
Expect(os.RemoveAll(cacheDir)).To(Succeed())
})
context("Get", func() {
context("when there is not already an existing file", func() {
it("builds an uncached version of the buildpack and puts it in the cache", func() {
uri, err := localFetcher.Get(localBuildpack)
Expect(err).ToNot(HaveOccurred())
Expect(buildpackCache.GetCall.CallCount).To(Equal(1))
Expect(namer.RandomNameCall.Receives.Name).To(Equal("some-buildpack"))
Expect(packager.ExecuteCall.Receives.BuildpackDir).To(Equal("path/to/buildpack"))
Expect(packager.ExecuteCall.Receives.Output).To(Equal(filepath.Join(cacheDir, "some-buildpack", "some-buildpack-random-string.cnb")))
Expect(packager.ExecuteCall.Receives.Version).To(Equal("some-version"))
Expect(packager.ExecuteCall.Receives.Cached).To(BeFalse())
Expect(buildpackCache.SetCall.CallCount).To(Equal(1))
Expect(uri).To(Equal(filepath.Join(cacheDir, "some-buildpack", "some-buildpack-random-string.cnb")))
})
})
context("failure cases", func() {
context("when the namer fails to generate a random name", func() {
it.Before(func() {
namer.RandomNameCall.Stub = nil
namer.RandomNameCall.Returns.Error = fmt.Errorf("namer failed")
})
it("returns an error", func() {
_, err := localFetcher.Get(localBuildpack)
Expect(err).To(MatchError("random name generation failed: namer failed"))
})
})
context("cache get fails", func() {
it.Before(func() {
buildpackCache.GetCall.Returns.Error = errors.New("failed get")
})
it("returns an error", func() {
_, err := localFetcher.Get(localBuildpack)
Expect(err).To(MatchError("failed get"))
})
})
context("unable to create new directory in cache directory", func() {
it.Before(func() {
buildpackCache.GetCall.Returns.Bool = false
Expect(os.Chmod(cacheDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(cacheDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := localFetcher.Get(localBuildpack)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("the packager fails to package the buildpack", func() {
it.Before(func() {
packager.ExecuteCall.Returns.Error = errors.New("execution failed")
})
it("returns an error", func() {
_, err := localFetcher.Get(localBuildpack)
Expect(err).To(MatchError("failed to package buildpack: execution failed"))
})
})
context("when setting the new buildpack information failes", func() {
it.Before(func() {
buildpackCache.SetCall.Returns.Error = errors.New("failed to set new cache entry")
Expect(os.MkdirAll(filepath.Join(cacheDir, "some-buildpack"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := localFetcher.Get(localBuildpack)
Expect(err).To(MatchError("failed to set new cache entry"))
})
})
})
})
}