/
fake_buildpack.go
101 lines (83 loc) · 2.74 KB
/
fake_buildpack.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
package fakes
import (
"bytes"
"fmt"
"io"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/buildpacks/pack/pkg/archive"
"github.com/buildpacks/pack/pkg/buildpack"
"github.com/buildpacks/pack/pkg/dist"
)
type fakeBuildpack struct {
descriptor dist.BuildpackDescriptor
chmod int64
options []FakeBuildpackOption
}
type fakeBuildpackConfig struct {
// maping of extrafilename to stringified contents
ExtraFiles map[string]string
OpenError error
}
func newFakeBuildpackConfig() *fakeBuildpackConfig {
return &fakeBuildpackConfig{ExtraFiles: map[string]string{}}
}
type FakeBuildpackOption func(*fakeBuildpackConfig)
func WithExtraBuildpackContents(filename, contents string) FakeBuildpackOption {
return func(f *fakeBuildpackConfig) {
f.ExtraFiles[filename] = contents
}
}
func WithBpOpenError(err error) FakeBuildpackOption {
return func(f *fakeBuildpackConfig) {
f.OpenError = err
}
}
// NewFakeBuildpack creates a fake buildpack with contents:
//
// \_ /cnb/buildpacks/{ID}
// \_ /cnb/buildpacks/{ID}/{version}
// \_ /cnb/buildpacks/{ID}/{version}/buildpack.toml
// \_ /cnb/buildpacks/{ID}/{version}/bin
// \_ /cnb/buildpacks/{ID}/{version}/bin/build
// build-contents
// \_ /cnb/buildpacks/{ID}/{version}/bin/detect
// detect-contents
func NewFakeBuildpack(descriptor dist.BuildpackDescriptor, chmod int64, options ...FakeBuildpackOption) (buildpack.BuildModule, error) {
return &fakeBuildpack{
descriptor: descriptor,
chmod: chmod,
options: options,
}, nil
}
func (b *fakeBuildpack) Descriptor() buildpack.Descriptor {
return &b.descriptor
}
func (b *fakeBuildpack) Open() (io.ReadCloser, error) {
fConfig := newFakeBuildpackConfig()
for _, option := range b.options {
option(fConfig)
}
if fConfig.OpenError != nil {
return nil, fConfig.OpenError
}
buf := &bytes.Buffer{}
if err := toml.NewEncoder(buf).Encode(b.descriptor); err != nil {
return nil, err
}
tarBuilder := archive.TarBuilder{}
ts := archive.NormalizedDateTime
tarBuilder.AddDir(fmt.Sprintf("/cnb/buildpacks/%s", b.descriptor.EscapedID()), b.chmod, ts)
bpDir := fmt.Sprintf("/cnb/buildpacks/%s/%s", b.descriptor.EscapedID(), b.descriptor.Info().Version)
tarBuilder.AddDir(bpDir, b.chmod, ts)
tarBuilder.AddFile(bpDir+"/buildpack.toml", b.chmod, ts, buf.Bytes())
if len(b.descriptor.Order()) == 0 {
tarBuilder.AddDir(bpDir+"/bin", b.chmod, ts)
tarBuilder.AddFile(bpDir+"/bin/build", b.chmod, ts, []byte("build-contents"))
tarBuilder.AddFile(bpDir+"/bin/detect", b.chmod, ts, []byte("detect-contents"))
}
for extraFilename, extraContents := range fConfig.ExtraFiles {
tarBuilder.AddFile(filepath.Join(bpDir, extraFilename), b.chmod, ts, []byte(extraContents))
}
return tarBuilder.Reader(archive.DefaultTarWriterFactory()), nil
}