diff --git a/Makefile b/Makefile index ff6ae677a..884a02271 100644 --- a/Makefile +++ b/Makefile @@ -231,6 +231,7 @@ $(MYGOBIN)/helm: .PHONY: clean clean: + go clean --cache rm -f $(builtinplugins) rm -f $(MYGOBIN)/pluginator rm -f $(MYGOBIN)/kustomize diff --git a/api/internal/target/chartinflatorplugin_test.go b/api/internal/target/chartinflatorplugin_test.go index 91016f2f1..d2a7b3173 100644 --- a/api/internal/target/chartinflatorplugin_test.go +++ b/api/internal/target/chartinflatorplugin_test.go @@ -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") diff --git a/api/internal/target/plugindir_test.go b/api/internal/target/plugindir_test.go index 0ed73862f..463f9fd24 100644 --- a/api/internal/target/plugindir_test.go +++ b/api/internal/target/plugindir_test.go @@ -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() diff --git a/api/internal/target/transformerplugin_test.go b/api/internal/target/transformerplugin_test.go index 421fe0fe0..1376def49 100644 --- a/api/internal/target/transformerplugin_test.go +++ b/api/internal/target/transformerplugin_test.go @@ -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") diff --git a/api/krusty/testingharness_test.go b/api/krusty/testingharness_test.go index 424005e31..edda22743 100644 --- a/api/krusty/testingharness_test.go +++ b/api/krusty/testingharness_test.go @@ -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. diff --git a/api/testutils/kusttest/plugintestenv.go b/api/testutils/kusttest/plugintestenv.go index d7d2d8434..9d8a9b1ac 100644 --- a/api/testutils/kusttest/plugintestenv.go +++ b/api/testutils/kusttest/plugintestenv.go @@ -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) } } diff --git a/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap_test.go b/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap_test.go index 24b09afe6..097fda8f3 100644 --- a/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap_test.go +++ b/plugin/someteam.example.com/v1/bashedconfigmap/BashedConfigMap_test.go @@ -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") diff --git a/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go b/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go index c3abbb84a..efb136a18 100644 --- a/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go +++ b/plugin/someteam.example.com/v1/chartinflator/ChartInflator_test.go @@ -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") diff --git a/plugin/someteam.example.com/v1/gogetter/GoGetter_test.go b/plugin/someteam.example.com/v1/gogetter/GoGetter_test.go index e5c659d89..1171f5b4d 100644 --- a/plugin/someteam.example.com/v1/gogetter/GoGetter_test.go +++ b/plugin/someteam.example.com/v1/gogetter/GoGetter_test.go @@ -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") diff --git a/plugin/someteam.example.com/v1/printworkdir/PrintWorkDir_test.go b/plugin/someteam.example.com/v1/printworkdir/PrintWorkDir_test.go index 5410ab079..353915969 100644 --- a/plugin/someteam.example.com/v1/printworkdir/PrintWorkDir_test.go +++ b/plugin/someteam.example.com/v1/printworkdir/PrintWorkDir_test.go @@ -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") diff --git a/plugin/someteam.example.com/v1/sedtransformer/SedTransformer_test.go b/plugin/someteam.example.com/v1/sedtransformer/SedTransformer_test.go index 60bba0965..eaed5dfc1 100644 --- a/plugin/someteam.example.com/v1/sedtransformer/SedTransformer_test.go +++ b/plugin/someteam.example.com/v1/sedtransformer/SedTransformer_test.go @@ -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", ` diff --git a/plugin/someteam.example.com/v1/validator/validator_test.go b/plugin/someteam.example.com/v1/validator/validator_test.go index e1fbe120d..6a580f1b3 100644 --- a/plugin/someteam.example.com/v1/validator/validator_test.go +++ b/plugin/someteam.example.com/v1/validator/validator_test.go @@ -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(`