/
fakesyncer.go
94 lines (81 loc) · 2.28 KB
/
fakesyncer.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
package syncer
import (
"os"
"path"
"path/filepath"
"testing"
"github.com/bitnami/charts-syncer/api"
"github.com/bitnami/charts-syncer/pkg/client/repo/local"
)
// FakeSyncerOpts allows to configure a Fake syncer.
type FakeSyncerOpts struct {
Destination string
skipCharts []string
}
// FakeSyncerOption is an option value used to create a new fake syncer instance.
type FakeSyncerOption func(*FakeSyncerOpts)
// WithFakeSyncerDestination configures a destination directory
func WithFakeSyncerDestination(dir string) FakeSyncerOption {
return func(s *FakeSyncerOpts) {
s.Destination = dir
}
}
func WithFakeSkipCharts(charts []string) FakeSyncerOption {
return func(s *FakeSyncerOpts) {
s.skipCharts = charts
}
}
// NewFake returns a fake Syncer
func NewFake(t *testing.T, opts ...FakeSyncerOption) *Syncer {
sopts := &FakeSyncerOpts{}
for _, o := range opts {
o(sopts)
}
srcTmp, err := os.MkdirTemp("", "charts-syncer-tests-src-fake")
if err != nil {
t.Fatalf("error creating temporary folder: %v", err)
}
t.Cleanup(func() { os.RemoveAll(srcTmp) })
if sopts.Destination == "" {
dstTmp, err := os.MkdirTemp("", "charts-syncer-tests-dst-fake")
if err != nil {
t.Fatalf("error creating temporary folder: %v", err)
}
t.Cleanup(func() { os.RemoveAll(dstTmp) })
sopts.Destination = dstTmp
}
// Copy all testdata tgz files to the source temporary folder
// We are not adding charts in the entries only to avoid specifying
// the dependencies
matches, err := filepath.Glob("../../testdata/*.tgz")
if err != nil {
t.Fatalf("error listing tgz files: %v", err)
}
for _, sourceFile := range matches {
input, err := os.ReadFile(sourceFile)
if err != nil {
t.Fatalf("error reading %q chart: %v", sourceFile, err)
}
dstFile := path.Join(srcTmp, filepath.Base(sourceFile))
if err = os.WriteFile(dstFile, input, 0644); err != nil {
t.Fatalf("error copying chart to %q: %v", dstFile, err)
}
}
srcCli, err := local.New(srcTmp)
if err != nil {
t.Fatalf("error creating source client: %v", err)
}
dstCli, err := local.New(sopts.Destination)
if err != nil {
t.Fatalf("error creating target client: %v", err)
}
return &Syncer{
source: &api.Source{},
target: &api.Target{},
cli: &Clients{
src: srcCli,
dst: dstCli,
},
skipCharts: sopts.skipCharts,
}
}