| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package kusttest_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"sigs.k8s.io/kustomize/api/ifc" |
| 14 |
fLdr "sigs.k8s.io/kustomize/api/internal/loader" |
| 15 |
pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader" |
| 16 |
"sigs.k8s.io/kustomize/api/konfig" |
| 17 |
"sigs.k8s.io/kustomize/api/provider" |
| 18 |
"sigs.k8s.io/kustomize/api/resmap" |
| 19 |
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest" |
| 20 |
"sigs.k8s.io/kustomize/api/types" |
| 21 |
"sigs.k8s.io/kustomize/kyaml/filesys" |
| 22 |
) |
| 23 |
|
| 24 |
// HarnessEnhanced manages a full plugin environment for tests. |
| 25 |
type HarnessEnhanced struct { |
| 26 |
// An instance of *testing.T, and a filesystem (likely in-memory) |
| 27 |
// for loading test data - plugin config, resources to transform, etc. |
| 28 |
Harness |
| 29 |
|
| 30 |
// plugintestEnv holds the plugin compiler and data needed to |
| 31 |
// create compilation sub-processes. |
| 32 |
pte *pluginTestEnv |
| 33 |
|
| 34 |
// rf creates Resources from byte streams. |
| 35 |
rf *resmap.Factory |
| 36 |
|
| 37 |
// A file loader using the Harness.fSys to read test data. |
| 38 |
ldr ifc.Loader |
| 39 |
|
| 40 |
// If true, wipe the ifc.loader root (not the plugin loader root) |
| 41 |
// as part of cleanup. |
| 42 |
shouldWipeLdrRoot bool |
| 43 |
|
| 44 |
// A plugin loader that loads plugins from a (real) file system. |
| 45 |
pl *pLdr.Loader |
| 46 |
} |
| 47 |
|
| 48 |
func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced { |
| 49 |
t.Helper() |
| 50 |
r := makeBaseEnhancedHarness(t) |
| 51 |
r.Harness = MakeHarnessWithFs(t, filesys.MakeFsInMemory()) |
| 52 |
// Point the Harness's file loader to the root ('/') |
| 53 |
// of the in-memory file system. |
| 54 |
r.ResetLoaderRoot(filesys.Separator) |
| 55 |
return r |
| 56 |
} |
| 57 |
|
| 58 |
func MakeEnhancedHarnessWithTmpRoot(t *testing.T) *HarnessEnhanced { |
| 59 |
t.Helper() |
| 60 |
r := makeBaseEnhancedHarness(t) |
| 61 |
fSys := filesys.MakeFsOnDisk() |
| 62 |
r.Harness = MakeHarnessWithFs(t, fSys) |
| 63 |
tmpDir, err := os.MkdirTemp("", "kust-testing-") |
| 64 |
if err != nil { |
| 65 |
panic("test harness cannot make tmp dir: " + err.Error()) |
| 66 |
} |
| 67 |
r.ldr, err = fLdr.NewLoader(fLdr.RestrictionRootOnly, tmpDir, fSys) |
| 68 |
if err != nil { |
| 69 |
panic("test harness cannot make ldr at tmp dir: " + err.Error()) |
| 70 |
} |
| 71 |
r.shouldWipeLdrRoot = true |
| 72 |
return r |
| 73 |
} |
| 74 |
|
| 75 |
func makeBaseEnhancedHarness(t *testing.T) *HarnessEnhanced { |
| 76 |
t.Helper() |
| 77 |
rf := resmap.NewFactory( |
| 78 |
provider.NewDefaultDepProvider().GetResourceFactory()) |
| 79 |
return &HarnessEnhanced{ |
| 80 |
pte: newPluginTestEnv(t).set(), |
| 81 |
rf: rf, |
| 82 |
pl: pLdr.NewLoader( |
| 83 |
types.EnabledPluginConfig(types.BploLoadFromFileSys), |
| 84 |
rf, |
| 85 |
// Plugin configs are always located on disk, |
| 86 |
// regardless of the test harness's FS |
| 87 |
filesys.MakeFsOnDisk())} |
| 88 |
} |
| 89 |
|
| 90 |
func (th *HarnessEnhanced) ErrIfNoHelm() error { |
| 91 |
_, err := exec.LookPath(th.GetPluginConfig().HelmConfig.Command) |
| 92 |
return err |
| 93 |
} |
| 94 |
|
| 95 |
func (th *HarnessEnhanced) GetRoot() string { |
| 96 |
return th.ldr.Root() |
| 97 |
} |
| 98 |
|
| 99 |
func (th *HarnessEnhanced) MkDir(path string) string { |
| 100 |
dir := filepath.Join(th.ldr.Root(), path) |
| 101 |
th.GetFSys().Mkdir(dir) |
| 102 |
return dir |
| 103 |
} |
| 104 |
|
| 105 |
func (th *HarnessEnhanced) Reset() { |
| 106 |
if th.shouldWipeLdrRoot { |
| 107 |
root, _ := filepath.EvalSymlinks(th.ldr.Root()) |
| 108 |
tmpdir, _ := filepath.EvalSymlinks(os.TempDir()) |
| 109 |
|
| 110 |
if !strings.HasPrefix(root, tmpdir) { |
| 111 |
// sanity check. |
| 112 |
panic("something strange about th.ldr.Root() = " + th.ldr.Root()) |
| 113 |
} |
| 114 |
os.RemoveAll(th.ldr.Root()) |
| 115 |
} |
| 116 |
th.pte.reset() |
| 117 |
} |
| 118 |
|
| 119 |
func (th *HarnessEnhanced) GetPluginConfig() *types.PluginConfig { |
| 120 |
return th.pl.Config() |
| 121 |
} |
| 122 |
|
| 123 |
func (th *HarnessEnhanced) PrepBuiltin(k string) *HarnessEnhanced { |
| 124 |
return th.BuildGoPlugin(konfig.BuiltinPluginPackage, "", k) |
| 125 |
} |
| 126 |
|
| 127 |
func (th *HarnessEnhanced) BuildGoPlugin(g, v, k string) *HarnessEnhanced { |
| 128 |
th.pte.prepareGoPlugin(g, v, k) |
| 129 |
return th |
| 130 |
} |
| 131 |
|
| 132 |
func (th *HarnessEnhanced) PrepExecPlugin(g, v, k string) *HarnessEnhanced { |
| 133 |
th.pte.prepareExecPlugin(g, v, k) |
| 134 |
return th |
| 135 |
} |
| 136 |
|
| 137 |
// ResetLoaderRoot interprets its argument as an absolute directory path. |
| 138 |
// It creates the directory, and creates the harness's file loader |
| 139 |
// rooted in that directory. |
| 140 |
func (th *HarnessEnhanced) ResetLoaderRoot(root string) { |
| 141 |
if err := th.fSys.Mkdir(root); err != nil { |
| 142 |
th.t.Fatal(err) |
| 143 |
} |
| 144 |
ldr, err := fLdr.NewLoader( |
| 145 |
fLdr.RestrictionRootOnly, root, th.fSys) |
| 146 |
if err != nil { |
| 147 |
th.t.Fatalf("Unable to make loader: %v", err) |
| 148 |
} |
| 149 |
th.ldr = ldr |
| 150 |
} |
| 151 |
|
| 152 |
func (th *HarnessEnhanced) LoadAndRunGenerator( |
| 153 |
config string) resmap.ResMap { |
| 154 |
rm := th.LoadAndRunGeneratorWithBuildAnnotations(config) |
| 155 |
rm.RemoveBuildAnnotations() |
| 156 |
return rm |
| 157 |
} |
| 158 |
|
| 159 |
func (th *HarnessEnhanced) LoadAndRunGeneratorWithBuildAnnotations( |
| 160 |
config string) resmap.ResMap { |
| 161 |
res, err := th.rf.RF().FromBytes([]byte(config)) |
| 162 |
if err != nil { |
| 163 |
th.t.Fatalf("Err: %v", err) |
| 164 |
} |
| 165 |
g, err := th.pl.LoadGenerator( |
| 166 |
th.ldr, valtest_test.MakeFakeValidator(), res) |
| 167 |
if err != nil { |
| 168 |
th.t.Fatalf("Err: %v", err) |
| 169 |
} |
| 170 |
rm, err := g.Generate() |
| 171 |
if err != nil { |
| 172 |
th.t.Fatalf("generate err: %v", err) |
| 173 |
} |
| 174 |
return rm |
| 175 |
} |
| 176 |
|
| 177 |
func (th *HarnessEnhanced) LoadAndRunTransformer( |
| 178 |
config, input string) resmap.ResMap { |
| 179 |
resMap, err := th.RunTransformer(config, input) |
| 180 |
if err != nil { |
| 181 |
th.t.Fatalf("Err: %v", err) |
| 182 |
} |
| 183 |
return resMap |
| 184 |
} |
| 185 |
|
| 186 |
func (th *HarnessEnhanced) RunTransformerAndCheckResult( |
| 187 |
config, input, expected string) { |
| 188 |
resMap := th.LoadAndRunTransformer(config, input) |
| 189 |
th.AssertActualEqualsExpectedNoIdAnnotations(resMap, expected) |
| 190 |
} |
| 191 |
|
| 192 |
func (th *HarnessEnhanced) ErrorFromLoadAndRunTransformer( |
| 193 |
config, input string) error { |
| 194 |
_, err := th.RunTransformer(config, input) |
| 195 |
return err |
| 196 |
} |
| 197 |
|
| 198 |
type AssertFunc func(t *testing.T, err error) |
| 199 |
|
| 200 |
func (th *HarnessEnhanced) RunTransformerAndCheckError( |
| 201 |
config, input string, assertFn AssertFunc) { |
| 202 |
_, err := th.RunTransformer(config, input) |
| 203 |
assertFn(th.t, err) |
| 204 |
} |
| 205 |
|
| 206 |
func (th *HarnessEnhanced) RunTransformer( |
| 207 |
config, input string) (resmap.ResMap, error) { |
| 208 |
resMap, err := th.rf.NewResMapFromBytes([]byte(input)) |
| 209 |
if err != nil { |
| 210 |
th.t.Fatalf("Err: %v", err) |
| 211 |
} |
| 212 |
return th.RunTransformerFromResMap(config, resMap) |
| 213 |
} |
| 214 |
|
| 215 |
func (th *HarnessEnhanced) RunTransformerFromResMap( |
| 216 |
config string, resMap resmap.ResMap) (resmap.ResMap, error) { |
| 217 |
transConfig, err := th.rf.RF().FromBytes([]byte(config)) |
| 218 |
if err != nil { |
| 219 |
th.t.Logf("config: '%s'", config) |
| 220 |
th.t.Fatalf("Err: %v", err) |
| 221 |
} |
| 222 |
g, err := th.pl.LoadTransformer( |
| 223 |
th.ldr, valtest_test.MakeFakeValidator(), transConfig) |
| 224 |
if err != nil { |
| 225 |
return nil, err |
| 226 |
} |
| 227 |
err = g.Transform(resMap) |
| 228 |
return resMap, err |
| 229 |
} |