mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-25 16:18:37 +00:00
Merge pull request #1865 from monopole/simplifyPluginTestHarness
Simplify and document plugin test harness.
This commit is contained in:
1
Makefile
1
Makefile
@@ -231,6 +231,7 @@ $(MYGOBIN)/helm:
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
go clean --cache
|
||||
rm -f $(builtinplugins)
|
||||
rm -f $(MYGOBIN)/pluginator
|
||||
rm -f $(MYGOBIN)/kustomize
|
||||
|
||||
@@ -31,7 +31,7 @@ func TestChartInflatorPlugin(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "ChartInflator")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestPluginDir(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "PrintWorkDir")
|
||||
|
||||
base, err := os.Getwd()
|
||||
|
||||
@@ -121,7 +121,7 @@ func TestSedTransformer(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "SedTransformer")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
@@ -43,20 +43,25 @@ func (th testingHarness) WriteF(path string, content string) {
|
||||
}
|
||||
|
||||
func (th testingHarness) MakeDefaultOptions() Options {
|
||||
return th.MakeOptionsPluginsDisabled()
|
||||
}
|
||||
|
||||
func (th testingHarness) MakeOptionsPluginsDisabled() Options {
|
||||
return Options{
|
||||
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
||||
PluginConfig: konfig.DisabledPluginConfig(),
|
||||
}
|
||||
}
|
||||
|
||||
func (th testingHarness) MakeEnabledPluginConfig() *types.PluginConfig {
|
||||
// TODO: this doesn't work yet - need to set an env var.
|
||||
// TODO: steal from kusttest_test.NewPluginTestEnv
|
||||
func (th testingHarness) MakeOptionsPluginsEnabled() Options {
|
||||
c, err := konfig.EnabledPluginConfig()
|
||||
if err != nil {
|
||||
th.t.Fatal(err)
|
||||
}
|
||||
return c
|
||||
return Options{
|
||||
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
||||
PluginConfig: c,
|
||||
}
|
||||
}
|
||||
|
||||
// Run, failing on error.
|
||||
|
||||
@@ -17,32 +17,47 @@ import (
|
||||
)
|
||||
|
||||
// PluginTestEnv manages the plugin test environment.
|
||||
// It sets/resets XDG_CONFIG_HOME, makes/removes a temp objRoot,
|
||||
// manages a plugin compiler, etc.
|
||||
// It manages a Go plugin compiler,
|
||||
// makes and removes a temporary working directory,
|
||||
// and sets/resets shell env vars as needed.
|
||||
type PluginTestEnv struct {
|
||||
t *testing.T
|
||||
compiler *compiler.Compiler
|
||||
srcRoot string
|
||||
workDir string
|
||||
oldXdg string
|
||||
wasSet bool
|
||||
}
|
||||
|
||||
// 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 {
|
||||
x.createWorkDir()
|
||||
x.compiler = x.makeCompiler()
|
||||
var err error
|
||||
x.srcRoot, err = compiler.DeterminePluginSrcRoot(filesys.MakeFsOnDisk())
|
||||
if err != nil {
|
||||
x.t.Error(err)
|
||||
}
|
||||
x.compiler = compiler.NewCompiler(x.srcRoot, x.workDir)
|
||||
x.setEnv()
|
||||
return x
|
||||
}
|
||||
|
||||
// Reset restores the environment to pre-test state.
|
||||
func (x *PluginTestEnv) Reset() {
|
||||
x.resetEnv()
|
||||
x.removeWorkDir()
|
||||
}
|
||||
|
||||
// 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) {
|
||||
err := x.compiler.Compile(g, v, k)
|
||||
if err != nil {
|
||||
@@ -50,39 +65,28 @@ func (x *PluginTestEnv) BuildGoPlugin(g, v, k string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) BuildExecPlugin(g, v, k string) {
|
||||
// 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
|
||||
// (Go or Exec) in the same spot, and since the test
|
||||
// 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) {
|
||||
lowK := strings.ToLower(k)
|
||||
obj := filepath.Join(x.compiler.ObjRoot(), g, v, lowK, k)
|
||||
src := filepath.Join(x.compiler.SrcRoot(), g, v, lowK, k)
|
||||
if err := os.MkdirAll(filepath.Dir(obj), 0755); err != nil {
|
||||
x.t.Errorf("error making directory: %s", filepath.Dir(obj))
|
||||
src := filepath.Join(x.srcRoot, g, v, lowK, k)
|
||||
tmp := filepath.Join(x.workDir, g, v, lowK, k)
|
||||
if err := os.MkdirAll(filepath.Dir(tmp), 0755); err != nil {
|
||||
x.t.Errorf("error making directory: %s", filepath.Dir(tmp))
|
||||
}
|
||||
cmd := exec.Command("cp", src, obj)
|
||||
cmd := exec.Command("cp", src, tmp)
|
||||
cmd.Env = os.Environ()
|
||||
if err := cmd.Run(); err != nil {
|
||||
x.t.Errorf("error copying %s to %s: %v", src, obj, err)
|
||||
x.t.Errorf("error copying %s to %s: %v", src, tmp, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) makeCompiler() *compiler.Compiler {
|
||||
// The plugin loader wants to find object code under
|
||||
// $XDG_CONFIG_HOME/kustomize/plugins
|
||||
// and the compiler writes object code to
|
||||
// $objRoot
|
||||
// so set things up accordingly.
|
||||
objRoot := filepath.Join(
|
||||
x.workDir, konfig.ProgramName, konfig.RelPluginHome)
|
||||
err := os.MkdirAll(objRoot, os.ModePerm)
|
||||
if err != nil {
|
||||
x.t.Error(err)
|
||||
}
|
||||
srcRoot, err := compiler.DeterminePluginSrcRoot(filesys.MakeFsOnDisk())
|
||||
if err != nil {
|
||||
x.t.Error(err)
|
||||
}
|
||||
return compiler.NewCompiler(srcRoot, objRoot)
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) createWorkDir() {
|
||||
var err error
|
||||
x.workDir, err = ioutil.TempDir("", "kustomize-plugin-tests")
|
||||
@@ -100,14 +104,14 @@ func (x *PluginTestEnv) removeWorkDir() {
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) setEnv() {
|
||||
x.oldXdg, x.wasSet = os.LookupEnv(konfig.XdgConfigHomeEnv)
|
||||
os.Setenv(konfig.XdgConfigHomeEnv, x.workDir)
|
||||
x.oldXdg, x.wasSet = os.LookupEnv(konfig.KustomizePluginHomeEnv)
|
||||
os.Setenv(konfig.KustomizePluginHomeEnv, x.workDir)
|
||||
}
|
||||
|
||||
func (x *PluginTestEnv) resetEnv() {
|
||||
if x.wasSet {
|
||||
os.Setenv(konfig.XdgConfigHomeEnv, x.oldXdg)
|
||||
os.Setenv(konfig.KustomizePluginHomeEnv, x.oldXdg)
|
||||
} else {
|
||||
os.Unsetenv(konfig.XdgConfigHomeEnv)
|
||||
os.Unsetenv(konfig.KustomizePluginHomeEnv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ func TestBashedConfigMapPlugin(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "BashedConfigMap")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestChartInflator(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "ChartInflator")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestGoGetter(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "GoGetter")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
@@ -47,7 +47,7 @@ func TestGoGetterUrl(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "GoGetter")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
@@ -76,7 +76,7 @@ func TestGoGetterCommand(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "GoGetter")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
@@ -105,7 +105,7 @@ func TestGoGetterSubPath(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "GoGetter")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestPrintWorkDirPlugin(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin(
|
||||
tc.PrepExecPlugin(
|
||||
"someteam.example.com", "v1", "PrintWorkDir")
|
||||
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/theAppRoot")
|
||||
|
||||
@@ -13,7 +13,7 @@ func TestSedTransformer(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin("someteam.example.com", "v1", "SedTransformer")
|
||||
tc.PrepExecPlugin("someteam.example.com", "v1", "SedTransformer")
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
th.WriteF("/app/sed-input.txt", `
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestValidatorHappy(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin("someteam.example.com", "v1", "Validator")
|
||||
tc.PrepExecPlugin("someteam.example.com", "v1", "Validator")
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
rm := th.LoadAndRunTransformer(`
|
||||
@@ -51,7 +51,7 @@ func TestValidatorUnHappy(t *testing.T) {
|
||||
tc := kusttest_test.NewPluginTestEnv(t).Set()
|
||||
defer tc.Reset()
|
||||
|
||||
tc.BuildExecPlugin("someteam.example.com", "v1", "Validator")
|
||||
tc.PrepExecPlugin("someteam.example.com", "v1", "Validator")
|
||||
th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app")
|
||||
|
||||
err := th.ErrorFromLoadAndRunTransformer(`
|
||||
|
||||
Reference in New Issue
Block a user