forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register_buildpack_test.go
138 lines (117 loc) · 4.57 KB
/
register_buildpack_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
package pack
import (
"bytes"
"context"
"testing"
"github.com/buildpacks/imgutil/fakes"
ifakes "github.com/buildpacks/pack/internal/fakes"
"github.com/buildpacks/pack/internal/logging"
"github.com/buildpacks/pack/internal/registry"
h "github.com/buildpacks/pack/testhelpers"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
func TestRegisterBuildpack(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "register_buildpack", testRegisterBuildpack, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testRegisterBuildpack(t *testing.T, when spec.G, it spec.S) {
when("#RegisterBuildpack", func() {
var (
fakeImageFetcher *ifakes.FakeImageFetcher
fakeAppImage *fakes.Image
subject *Client
out bytes.Buffer
)
it.Before(func() {
fakeImageFetcher = ifakes.NewFakeImageFetcher()
fakeAppImage = fakes.NewImage("buildpack/image", "", &fakeIdentifier{name: "buildpack-image"})
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.buildpackage.metadata",
`{"id":"heroku/java-function","version":"1.1.1","stacks":[{"id":"heroku-18"},{"id":"io.buildpacks.stacks.bionic"},{"id":"org.cloudfoundry.stacks.cflinuxfs3"}]}`))
fakeImageFetcher.RemoteImages["buildpack/image"] = fakeAppImage
fakeLogger := logging.NewLogWithWriters(&out, &out)
subject = &Client{
logger: fakeLogger,
imageFetcher: fakeImageFetcher,
}
})
it.After(func() {
_ = fakeAppImage.Cleanup()
})
it("should return error for an invalid image (github)", func() {
fakeAppImage = fakes.NewImage("invalid/image", "", &fakeIdentifier{name: "buildpack-image"})
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.buildpackage.metadata", `{}`))
h.AssertNotNil(t, subject.RegisterBuildpack(context.TODO(),
RegisterBuildpackOptions{
ImageName: "invalid/image",
Type: "github",
URL: registry.DefaultRegistryURL,
Name: registry.DefaultRegistryName,
}))
})
it("should return error for missing image label (github)", func() {
fakeAppImage = fakes.NewImage("missinglabel/image", "", &fakeIdentifier{name: "buildpack-image"})
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.buildpackage.metadata", `{}`))
fakeImageFetcher.RemoteImages["missinglabel/image"] = fakeAppImage
h.AssertNotNil(t, subject.RegisterBuildpack(context.TODO(),
RegisterBuildpackOptions{
ImageName: "missinglabel/image",
Type: "github",
URL: registry.DefaultRegistryURL,
Name: registry.DefaultRegistryName,
}))
})
it("should throw error if missing URL (github)", func() {
h.AssertError(t, subject.RegisterBuildpack(context.TODO(),
RegisterBuildpackOptions{
ImageName: "buildpack/image",
Type: "github",
URL: "",
Name: "official",
}), "missing github URL")
})
it("should throw error if missing URL (git)", func() {
h.AssertError(t, subject.RegisterBuildpack(context.TODO(),
RegisterBuildpackOptions{
ImageName: "buildpack/image",
Type: "git",
URL: "",
Name: "official",
}), "invalid url: cannot parse username from url")
})
it("should throw error if using malformed URL (git)", func() {
h.AssertError(t, subject.RegisterBuildpack(context.TODO(),
RegisterBuildpackOptions{
ImageName: "buildpack/image",
Type: "git",
URL: "https://github.com//buildpack-registry/",
Name: "official",
}), "invalid url: username is empty")
})
it("should return error for an invalid image (git)", func() {
fakeAppImage = fakes.NewImage("invalid/image", "", &fakeIdentifier{name: "buildpack-image"})
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.buildpackage.metadata", `{}`))
h.AssertNotNil(t, subject.RegisterBuildpack(context.TODO(),
RegisterBuildpackOptions{
ImageName: "invalid/image",
Type: "git",
URL: registry.DefaultRegistryURL,
Name: registry.DefaultRegistryName,
}))
})
it("should return error for missing image label (git)", func() {
fakeAppImage = fakes.NewImage("missinglabel/image", "", &fakeIdentifier{name: "buildpack-image"})
h.AssertNil(t, fakeAppImage.SetLabel("io.buildpacks.buildpackage.metadata", `{}`))
fakeImageFetcher.RemoteImages["missinglabel/image"] = fakeAppImage
h.AssertNotNil(t, subject.RegisterBuildpack(context.TODO(),
RegisterBuildpackOptions{
ImageName: "missinglabel/image",
Type: "git",
URL: registry.DefaultRegistryURL,
Name: registry.DefaultRegistryName,
}))
})
})
}