mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 10:00:56 +00:00
Simplify plugin tests.
This commit is contained in:
@@ -75,7 +75,7 @@ func (th Harness) MakeOptionsPluginsEnabled() krusty.Options {
|
||||
if strings.Contains(err.Error(), "unable to find plugin root") {
|
||||
th.t.Log(
|
||||
"Tests that want to run with plugins enabled must be " +
|
||||
"bookended by calls to NewPluginTestEnv.Set(), Reset().")
|
||||
"bookended by calls to newPluginTestEnv.Set(), Reset().")
|
||||
}
|
||||
th.t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -20,30 +20,60 @@ import (
|
||||
)
|
||||
|
||||
// HarnessEnhanced manages a full plugin environment for tests.
|
||||
// TODO: get rid of this. Combine Harness and PluginTestEnv.
|
||||
type HarnessEnhanced struct {
|
||||
Harness
|
||||
pte *pluginTestEnv
|
||||
rf *resmap.Factory
|
||||
ldr loadertest.FakeLoader
|
||||
pl *pLdr.Loader
|
||||
}
|
||||
|
||||
func MakeHarnessEnhanced(
|
||||
t *testing.T, path string) *HarnessEnhanced {
|
||||
func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
||||
pte := newPluginTestEnv(t).set()
|
||||
|
||||
pc, err := konfig.EnabledPluginConfig()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fSys := filesys.MakeFsInMemory()
|
||||
|
||||
rf := resmap.NewFactory(
|
||||
resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()),
|
||||
transformer.NewFactoryImpl())
|
||||
return &HarnessEnhanced{
|
||||
|
||||
result := &HarnessEnhanced{
|
||||
Harness: Harness{t: t, fSys: fSys},
|
||||
pte: pte,
|
||||
rf: rf,
|
||||
ldr: loadertest.NewFakeLoaderWithRestrictor(
|
||||
fLdr.RestrictionRootOnly, fSys, path),
|
||||
pl: pLdr.NewLoader(pc, rf)}
|
||||
pl: pLdr.NewLoader(pc, rf)}
|
||||
result.ResetLoaderRoot(filesys.Separator)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (th *HarnessEnhanced) Reset() {
|
||||
th.pte.reset()
|
||||
}
|
||||
|
||||
func (th *HarnessEnhanced) BuildGoPlugin(g, v, k string) *HarnessEnhanced {
|
||||
th.pte.buildGoPlugin(g, v, k)
|
||||
return th
|
||||
}
|
||||
|
||||
func (th *HarnessEnhanced) PrepExecPlugin(g, v, k string) *HarnessEnhanced {
|
||||
th.pte.prepExecPlugin(g, v, k)
|
||||
return th
|
||||
}
|
||||
|
||||
func (th *HarnessEnhanced) PrepBuiltin(k string) *HarnessEnhanced {
|
||||
th.pte.buildGoPlugin(konfig.BuiltinPluginPackage, "", k)
|
||||
return th
|
||||
}
|
||||
|
||||
func (th *HarnessEnhanced) ResetLoaderRoot(root string) {
|
||||
th.ldr = loadertest.NewFakeLoaderWithRestrictor(
|
||||
fLdr.RestrictionRootOnly, th.fSys, root)
|
||||
}
|
||||
|
||||
func (th *HarnessEnhanced) LoadAndRunGenerator(
|
||||
|
||||
@@ -16,11 +16,11 @@ import (
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
)
|
||||
|
||||
// PluginTestEnv manages the plugin test environment.
|
||||
// pluginTestEnv manages plugins for tests.
|
||||
// It manages a Go plugin compiler,
|
||||
// makes and removes a temporary working directory,
|
||||
// and sets/resets shell env vars as needed.
|
||||
type PluginTestEnv struct {
|
||||
type pluginTestEnv struct {
|
||||
t *testing.T
|
||||
compiler *compiler.Compiler
|
||||
srcRoot string
|
||||
@@ -29,13 +29,16 @@ type PluginTestEnv struct {
|
||||
wasSet bool
|
||||
}
|
||||
|
||||
// NewPluginTestEnv returns a new instance of PluginTestEnv.
|
||||
func NewPluginTestEnv(t *testing.T) *PluginTestEnv {
|
||||
return &PluginTestEnv{t: t}
|
||||
// newPluginTestEnv returns a new instance of pluginTestEnv.
|
||||
func newPluginTestEnv(t *testing.T) *pluginTestEnv {
|
||||
return &pluginTestEnv{t: t}
|
||||
}
|
||||
|
||||
// Set creates a test environment.
|
||||
func (x *PluginTestEnv) Set() *PluginTestEnv {
|
||||
// set creates a test environment.
|
||||
// Uses a filesystem on disk for compilation (or copying) of
|
||||
// plugin code - this FileSystem has nothing to do with
|
||||
// the FileSystem used for loading config yaml in the tests.
|
||||
func (x *pluginTestEnv) set() *pluginTestEnv {
|
||||
x.createWorkDir()
|
||||
var err error
|
||||
x.srcRoot, err = compiler.DeterminePluginSrcRoot(filesys.MakeFsOnDisk())
|
||||
@@ -47,30 +50,25 @@ func (x *PluginTestEnv) Set() *PluginTestEnv {
|
||||
return x
|
||||
}
|
||||
|
||||
// Reset restores the environment to pre-test state.
|
||||
func (x *PluginTestEnv) Reset() {
|
||||
// reset restores the environment to pre-test state.
|
||||
func (x *pluginTestEnv) reset() {
|
||||
x.resetEnv()
|
||||
x.removeWorkDir()
|
||||
}
|
||||
|
||||
// WorkDir allows inspection of the temp working directory.
|
||||
func (x *PluginTestEnv) WorkDir() string {
|
||||
return x.workDir
|
||||
}
|
||||
|
||||
// BuildGoPlugin compiles a Go plugin, leaving the newly
|
||||
// buildGoPlugin compiles a Go plugin, leaving the newly
|
||||
// created object code in the right place - a temporary
|
||||
// working directory pointed to by KustomizePluginHomeEnv.
|
||||
// This avoids overwriting anything the user/developer has
|
||||
// otherwise created.
|
||||
func (x *PluginTestEnv) BuildGoPlugin(g, v, k string) {
|
||||
func (x *pluginTestEnv) buildGoPlugin(g, v, k string) {
|
||||
err := x.compiler.Compile(g, v, k)
|
||||
if err != nil {
|
||||
x.t.Errorf("compile failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// PrepExecPlugin copies an exec plugin from it's
|
||||
// prepExecPlugin copies an exec plugin from it's
|
||||
// home in the discovered srcRoot to the same temp
|
||||
// directory where Go plugin object code is placed.
|
||||
// Kustomize (and its tests) expect to find plugins
|
||||
@@ -78,7 +76,7 @@ func (x *PluginTestEnv) BuildGoPlugin(g, v, k string) {
|
||||
// framework is compiling Go plugins to a temp dir,
|
||||
// it must likewise copy Exec plugins to that same
|
||||
// temp dir.
|
||||
func (x *PluginTestEnv) PrepExecPlugin(g, v, k string) {
|
||||
func (x *pluginTestEnv) prepExecPlugin(g, v, k string) {
|
||||
lowK := strings.ToLower(k)
|
||||
src := filepath.Join(x.srcRoot, g, v, lowK, k)
|
||||
tmp := filepath.Join(x.workDir, g, v, lowK, k)
|
||||
@@ -92,7 +90,7 @@ func (x *PluginTestEnv) PrepExecPlugin(g, v, k string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) createWorkDir() {
|
||||
func (x *pluginTestEnv) createWorkDir() {
|
||||
var err error
|
||||
x.workDir, err = ioutil.TempDir("", "kustomize-plugin-tests")
|
||||
if err != nil {
|
||||
@@ -100,7 +98,7 @@ func (x *PluginTestEnv) createWorkDir() {
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) removeWorkDir() {
|
||||
func (x *pluginTestEnv) removeWorkDir() {
|
||||
err := os.RemoveAll(x.workDir)
|
||||
if err != nil {
|
||||
x.t.Errorf(
|
||||
@@ -108,12 +106,12 @@ func (x *PluginTestEnv) removeWorkDir() {
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) setEnv() {
|
||||
func (x *pluginTestEnv) setEnv() {
|
||||
x.oldXdg, x.wasSet = os.LookupEnv(konfig.KustomizePluginHomeEnv)
|
||||
os.Setenv(konfig.KustomizePluginHomeEnv, x.workDir)
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) resetEnv() {
|
||||
func (x *pluginTestEnv) resetEnv() {
|
||||
if x.wasSet {
|
||||
os.Setenv(konfig.KustomizePluginHomeEnv, x.oldXdg)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user