forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinspect_builder_test.go
159 lines (141 loc) · 4.28 KB
/
inspect_builder_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
package pack_test
import (
"errors"
"fmt"
"testing"
imgtest "github.com/buildpack/lifecycle/testhelpers"
"github.com/fatih/color"
"github.com/golang/mock/gomock"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
h "github.com/buildpack/pack/testhelpers"
"github.com/buildpack/pack"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/mocks"
"github.com/buildpack/pack/testhelpers"
)
func TestInspectBuilder(t *testing.T) {
color.NoColor = true
spec.Run(t, "InspectBuilder", testInspectBuilder, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testInspectBuilder(t *testing.T, when spec.G, it spec.S) {
var (
client *pack.Client
mockFetcher *mocks.MockFetcher
mockController *gomock.Controller
builderImage *imgtest.FakeImage
)
it.Before(func() {
mockController = gomock.NewController(t)
mockFetcher = mocks.NewMockFetcher(mockController)
client = pack.NewClient(&config.Config{
RunImages: []config.RunImage{
{Image: "some/run-image", Mirrors: []string{"some/local-mirror"}},
},
}, mockFetcher)
builderImage = imgtest.NewFakeImage(t, "some/builder", "", "")
})
it.After(func() {
mockController.Finish()
})
when("the image exists", func() {
for _, useDaemon := range []bool{true, false} {
when(fmt.Sprintf("daemon is %t", useDaemon), func() {
it.Before(func() {
if useDaemon {
mockFetcher.EXPECT().FetchLocalImage("some/builder").Return(builderImage, nil)
} else {
mockFetcher.EXPECT().FetchRemoteImage("some/builder").Return(builderImage, nil)
}
})
when("the builder image has a metadata label", func() {
it.Before(func() {
testhelpers.AssertNil(t, builderImage.SetLabel("io.buildpacks.stack.id", "test.stack.id"))
testhelpers.AssertNil(t, builderImage.SetLabel("io.buildpacks.builder.metadata", `{
"stack": {
"runImage": {
"image": "some/run-image",
"mirrors": [
"gcr.io/some/default"
]
}
},
"buildpacks": [
{
"id": "test.bp.one",
"version": "1.0.0",
"latest": true
}
],
"groups": [
{
"buildpacks": [
{
"id": "test.bp.one",
"version": "1.0.0",
"latest": true
}
]
}
]
}`))
})
it("returns the builder with the given name", func() {
builderInfo, err := client.InspectBuilder("some/builder", useDaemon)
h.AssertNil(t, err)
h.AssertEq(t, builderInfo.RunImage, "some/run-image")
})
it("set the local run image mirrors", func() {
builderInfo, err := client.InspectBuilder("some/builder", useDaemon)
h.AssertNil(t, err)
h.AssertEq(t, builderInfo.LocalRunImageMirrors, []string{"some/local-mirror"})
})
it("set the defaults run image mirrors", func() {
builderInfo, err := client.InspectBuilder("some/builder", useDaemon)
h.AssertNil(t, err)
h.AssertEq(t, builderInfo.RunImageMirrors, []string{"gcr.io/some/default"})
})
it("sets the buildpacks", func() {
builderInfo, err := client.InspectBuilder("some/builder", useDaemon)
h.AssertNil(t, err)
h.AssertEq(t, builderInfo.Buildpacks[0], pack.BuildpackInfo{
ID: "test.bp.one",
Version: "1.0.0",
Latest: true,
})
})
it("sets the groups", func() {
builderInfo, err := client.InspectBuilder("some/builder", useDaemon)
h.AssertNil(t, err)
h.AssertEq(t, builderInfo.Groups[0], []pack.BuildpackInfo{{
ID: "test.bp.one",
Version: "1.0.0",
Latest: true,
}})
})
})
})
}
})
when("fetcher fails to fetch the image", func() {
it.Before(func() {
mockFetcher.EXPECT().FetchRemoteImage("some/builder").Return(nil, errors.New("some-error"))
})
it("returns an error", func() {
_, err := client.InspectBuilder("some/builder", false)
h.AssertError(t, err, "failed to get builder image 'some/builder': some-error")
})
})
when("the image does not exist", func() {
it.Before(func() {
notFoundImage := imgtest.NewFakeImage(t, "", "", "")
notFoundImage.Delete()
mockFetcher.EXPECT().FetchLocalImage("some/builder").Return(notFoundImage, nil)
})
it("return nil metadata", func() {
metadata, err := client.InspectBuilder("some/builder", true)
h.AssertNil(t, err)
h.AssertNil(t, metadata)
})
})
}