...
1
2
3
4 package kusttest_test
5
6 import (
7 "path/filepath"
8 "testing"
9
10 "sigs.k8s.io/kustomize/api/internal/konfig/builtinpluginconsts"
11 "sigs.k8s.io/kustomize/api/konfig"
12 "sigs.k8s.io/kustomize/api/krusty"
13 "sigs.k8s.io/kustomize/api/resmap"
14 "sigs.k8s.io/kustomize/api/types"
15 "sigs.k8s.io/kustomize/kyaml/filesys"
16 )
17
18
19 type Harness struct {
20 t *testing.T
21 fSys filesys.FileSystem
22 }
23
24 func MakeHarness(t *testing.T) Harness {
25 t.Helper()
26 return MakeHarnessWithFs(t, filesys.MakeFsInMemory())
27 }
28
29 func MakeHarnessWithFs(
30 t *testing.T, fSys filesys.FileSystem) Harness {
31 t.Helper()
32 return Harness{
33 t: t,
34 fSys: fSys,
35 }
36 }
37
38 func (th Harness) GetT() *testing.T {
39 return th.t
40 }
41
42 func (th Harness) GetFSys() filesys.FileSystem {
43 return th.fSys
44 }
45
46 func (th Harness) WriteK(path string, content string) {
47 err := th.fSys.WriteFile(
48 filepath.Join(
49 path,
50 konfig.DefaultKustomizationFileName()), []byte(`
51 apiVersion: kustomize.config.k8s.io/v1beta1
52 kind: Kustomization
53 `+content))
54 if err != nil {
55 th.t.Fatalf("unexpected error while writing Kustomization to %s: %v", path, err)
56 }
57 }
58
59 func (th Harness) WriteC(path string, content string) {
60 err := th.fSys.WriteFile(
61 filepath.Join(
62 path,
63 konfig.DefaultKustomizationFileName()), []byte(`
64 apiVersion: kustomize.config.k8s.io/v1alpha1
65 kind: Component
66 `+content))
67 if err != nil {
68 th.t.Fatalf("unexpected error while writing Component to %s: %v", path, err)
69 }
70 }
71
72 func (th Harness) WriteF(path string, content string) {
73 err := th.fSys.WriteFile(path, []byte(content))
74 if err != nil {
75 th.t.Fatalf("unexpected error while writing file to %s: %v", path, err)
76 }
77 }
78
79 func (th Harness) MakeDefaultOptions() krusty.Options {
80 return th.MakeOptionsPluginsDisabled()
81 }
82
83
84 func (th Harness) MakeOptionsPluginsDisabled() krusty.Options {
85 return *krusty.MakeDefaultOptions()
86 }
87
88
89 func (th Harness) MakeOptionsPluginsEnabled() krusty.Options {
90 pc := types.EnabledPluginConfig(types.BploLoadFromFileSys)
91 o := *krusty.MakeDefaultOptions()
92 o.PluginConfig = pc
93 return o
94 }
95
96
97 func (th Harness) Run(path string, o krusty.Options) resmap.ResMap {
98 m, err := krusty.MakeKustomizer(&o).Run(th.fSys, path)
99 if err != nil {
100 th.t.Fatal(err)
101 }
102 return m
103 }
104
105
106 func (th Harness) RunWithErr(path string, o krusty.Options) error {
107 _, err := krusty.MakeKustomizer(&o).Run(th.fSys, path)
108 if err == nil {
109 th.t.Fatalf("expected error")
110 }
111 return err
112 }
113
114 func (th Harness) WriteLegacyConfigs(fName string) {
115 m := builtinpluginconsts.GetDefaultFieldSpecsAsMap()
116 var content []byte
117 for _, tCfg := range m {
118 content = append(content, []byte(tCfg)...)
119 }
120 err := th.fSys.WriteFile(fName, content)
121 if err != nil {
122 th.t.Fatalf("unable to add file %s", fName)
123 }
124 }
125
126 func (th Harness) AssertActualEqualsExpected(
127 m resmap.ResMap, expected string) {
128 th.AssertActualEqualsExpectedWithTweak(m, nil, expected)
129 }
130
131 func (th Harness) AssertActualEqualsExpectedNoIdAnnotations(m resmap.ResMap, expected string) {
132 m.RemoveBuildAnnotations()
133 th.AssertActualEqualsExpectedWithTweak(m, nil, expected)
134 }
135
136 func (th Harness) AssertActualEqualsExpectedWithTweak(
137 m resmap.ResMap, tweaker func([]byte) []byte, expected string) {
138 assertActualEqualsExpectedWithTweak(th, m, tweaker, expected)
139 }
140
View as plain text