forked from buildpacks/lifecycle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer_test.go
199 lines (165 loc) · 5.82 KB
/
analyzer_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
package lifecycle_test
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/apex/log"
"github.com/apex/log/handlers/discard"
"github.com/buildpacks/imgutil/fakes"
"github.com/buildpacks/imgutil/local"
"github.com/golang/mock/gomock"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/cache"
"github.com/buildpacks/lifecycle/cmd"
"github.com/buildpacks/lifecycle/platform"
h "github.com/buildpacks/lifecycle/testhelpers"
"github.com/buildpacks/lifecycle/testmock"
)
func TestAnalyzer(t *testing.T) {
for _, api := range api.Platform.Supported {
spec.Run(t, "unit-analyzer/"+api.String(), testAnalyzerBuilder(api.String()), spec.Parallel(), spec.Report(report.Terminal{}))
}
}
func testAnalyzerBuilder(platformAPI string) func(t *testing.T, when spec.G, it spec.S) {
return func(t *testing.T, when spec.G, it spec.S) {
var (
cacheDir string
layersDir string
tmpDir string
analyzer *lifecycle.Analyzer
image *fakes.Image
metadataRestorer *testmock.MockLayerMetadataRestorer
mockCtrl *gomock.Controller
testCache lifecycle.Cache
)
it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "analyzer-tests")
h.AssertNil(t, err)
layersDir, err = ioutil.TempDir("", "lifecycle-layer-dir")
h.AssertNil(t, err)
cacheDir, err = ioutil.TempDir("", "some-cache-dir")
h.AssertNil(t, err)
testCache, err = cache.NewVolumeCache(cacheDir)
h.AssertNil(t, err)
image = fakes.NewImage("image-repo-name", "", local.IDIdentifier{
ImageID: "s0m3D1g3sT",
})
discardLogger := log.Logger{Handler: &discard.Handler{}}
mockCtrl = gomock.NewController(t)
metadataRestorer = testmock.NewMockLayerMetadataRestorer(mockCtrl)
p, err := platform.NewPlatform(platformAPI)
h.AssertNil(t, err)
analyzer = &lifecycle.Analyzer{
Image: image,
Logger: &discardLogger,
Platform: p,
Buildpacks: []buildpack.GroupBuildpack{
{ID: "metadata.buildpack", API: api.Buildpack.Latest().String()},
{ID: "no.cache.buildpack", API: api.Buildpack.Latest().String()},
{ID: "no.metadata.buildpack", API: api.Buildpack.Latest().String()},
},
Cache: testCache,
LayerMetadataRestorer: metadataRestorer,
}
if testing.Verbose() {
analyzer.Logger = cmd.DefaultLogger
h.AssertNil(t, cmd.SetLogLevel("debug"))
}
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(tmpDir))
h.AssertNil(t, os.RemoveAll(layersDir))
h.AssertNil(t, os.RemoveAll(cacheDir))
h.AssertNil(t, image.Cleanup())
mockCtrl.Finish()
})
when("#Analyze", func() {
var (
expectedAppMetadata platform.LayersMetadata
expectedCacheMetadata platform.CacheMetadata
ref *testmock.MockReference
)
expectRestoresLayerMetadataIfSupported := func() {
if api.MustParse(analyzer.Platform.API()).LessThan("0.7") {
useShaFiles := true
layerSHAStore := lifecycle.NewLayerSHAStore(useShaFiles)
metadataRestorer.EXPECT().Restore(analyzer.Buildpacks, expectedAppMetadata, expectedCacheMetadata, layerSHAStore)
}
}
it.Before(func() {
ref = testmock.NewMockReference(mockCtrl)
ref.EXPECT().Name().AnyTimes()
})
when("image exists", func() {
it.Before(func() {
metadata := h.MustReadFile(t, filepath.Join("testdata", "analyzer", "app_metadata.json"))
h.AssertNil(t, image.SetLabel("io.buildpacks.lifecycle.metadata", string(metadata)))
h.AssertNil(t, json.Unmarshal(metadata, &expectedAppMetadata))
})
it("returns the analyzed metadata", func() {
expectRestoresLayerMetadataIfSupported()
md, err := analyzer.Analyze()
h.AssertNil(t, err)
h.AssertEq(t, md.Image.Reference, "s0m3D1g3sT")
h.AssertEq(t, md.Metadata, expectedAppMetadata)
})
when("cache exists", func() {
it.Before(func() {
metadata := h.MustReadFile(t, filepath.Join("testdata", "analyzer", "cache_metadata.json"))
h.AssertNil(t, json.Unmarshal(metadata, &expectedCacheMetadata))
h.AssertNil(t, testCache.SetMetadata(expectedCacheMetadata))
h.AssertNil(t, testCache.Commit())
analyzer.Buildpacks = append(analyzer.Buildpacks, buildpack.GroupBuildpack{ID: "escaped/buildpack/id", API: api.Buildpack.Latest().String()})
expectRestoresLayerMetadataIfSupported()
})
it("returns the analyzed metadata", func() {
md, err := analyzer.Analyze()
h.AssertNil(t, err)
h.AssertEq(t, md.Metadata, expectedAppMetadata)
})
})
})
when("image not found", func() {
it.Before(func() {
h.AssertNil(t, image.Delete())
expectRestoresLayerMetadataIfSupported()
})
it("returns a nil image in the analyzed metadata", func() {
md, err := analyzer.Analyze()
h.AssertNil(t, err)
h.AssertNil(t, md.Image)
h.AssertEq(t, md.Metadata, platform.LayersMetadata{})
})
})
when("image does not have metadata label", func() {
it.Before(func() {
h.AssertNil(t, image.SetLabel("io.buildpacks.lifecycle.metadata", ""))
expectRestoresLayerMetadataIfSupported()
})
it("returns empty analyzed metadata", func() {
md, err := analyzer.Analyze()
h.AssertNil(t, err)
h.AssertEq(t, md.Metadata, platform.LayersMetadata{})
})
})
when("image has incompatible metadata", func() {
it.Before(func() {
h.AssertNil(t, image.SetLabel("io.buildpacks.lifecycle.metadata", `{["bad", "metadata"]}`))
expectRestoresLayerMetadataIfSupported()
})
it("returns empty analyzed metadata", func() {
md, err := analyzer.Analyze()
h.AssertNil(t, err)
h.AssertEq(t, md.Metadata, platform.LayersMetadata{})
})
})
})
}
}