mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 10:15:22 +00:00
Remove static cling in plugin development flow.
This commit is contained in:
@@ -70,8 +70,7 @@ func (th Harness) MakeOptionsPluginsDisabled() krusty.Options {
|
||||
|
||||
// Enables use of non-builtin plugins.
|
||||
func (th Harness) MakeOptionsPluginsEnabled() krusty.Options {
|
||||
// TODO: Change to types.BploLoadFromFileSys to enable debugging.
|
||||
c, err := konfig.EnabledPluginConfig(types.BploUseStaticallyLinked)
|
||||
c, err := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "unable to find plugin root") {
|
||||
th.t.Log(
|
||||
|
||||
@@ -47,8 +47,7 @@ type HarnessEnhanced struct {
|
||||
func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
|
||||
pte := newPluginTestEnv(t).set()
|
||||
|
||||
// TODO: Change to types.BploLoadFromFileSys to enable debugging.
|
||||
pc, err := konfig.EnabledPluginConfig(types.BploUseStaticallyLinked)
|
||||
pc, err := konfig.EnabledPluginConfig(types.BploLoadFromFileSys)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -4,11 +4,7 @@
|
||||
package kusttest_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filesys"
|
||||
@@ -17,16 +13,13 @@ import (
|
||||
)
|
||||
|
||||
// pluginTestEnv manages compiling plugins for tests.
|
||||
// It manages a Go plugin compiler,
|
||||
// maybe makes and removes a temporary working directory,
|
||||
// maybe sets/resets shell env vars as needed.
|
||||
// It manages a Go plugin compiler, 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
|
||||
t *testing.T
|
||||
compiler *compiler.Compiler
|
||||
pluginRoot string
|
||||
oldXdg string
|
||||
wasSet bool
|
||||
}
|
||||
|
||||
// newPluginTestEnv returns a new instance of pluginTestEnv.
|
||||
@@ -39,76 +32,44 @@ func newPluginTestEnv(t *testing.T) *pluginTestEnv {
|
||||
// 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())
|
||||
x.pluginRoot, err = compiler.DeterminePluginSrcRoot(filesys.MakeFsOnDisk())
|
||||
if err != nil {
|
||||
x.t.Error(err)
|
||||
}
|
||||
x.compiler = compiler.NewCompiler(x.srcRoot, x.workDir)
|
||||
x.compiler = compiler.NewCompiler(x.pluginRoot)
|
||||
x.setEnv()
|
||||
return x
|
||||
}
|
||||
|
||||
// reset restores the environment to pre-test state.
|
||||
func (x *pluginTestEnv) reset() {
|
||||
// Calling Cleanup forces recompilation in a test file with multiple
|
||||
// calls to MakeEnhancedHarness - so leaving it out. Your .gitignore
|
||||
// should ignore .so files anyway.
|
||||
// x.compiler.Cleanup()
|
||||
x.resetEnv()
|
||||
x.removeWorkDir()
|
||||
}
|
||||
|
||||
// prepareGoPlugin 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.
|
||||
// created object code alongside the src code.
|
||||
func (x *pluginTestEnv) prepareGoPlugin(g, v, k string) {
|
||||
err := x.compiler.Compile(g, v, k)
|
||||
x.compiler.SetGVK(g, v, k)
|
||||
err := x.compiler.Compile()
|
||||
if err != nil {
|
||||
x.t.Errorf("compile failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// prepareExecPlugin 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) prepareExecPlugin(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)
|
||||
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, tmp)
|
||||
cmd.Env = os.Environ()
|
||||
if err := cmd.Run(); err != nil {
|
||||
x.t.Errorf("error copying %s to %s: %v", src, tmp, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *pluginTestEnv) createWorkDir() {
|
||||
var err error
|
||||
x.workDir, err = ioutil.TempDir("", "kustomize-plugin-tests")
|
||||
if err != nil {
|
||||
x.t.Errorf("failed to make work dir: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *pluginTestEnv) removeWorkDir() {
|
||||
err := os.RemoveAll(x.workDir)
|
||||
if err != nil {
|
||||
x.t.Errorf(
|
||||
"removing work dir: %s %v", x.workDir, err)
|
||||
}
|
||||
func (x *pluginTestEnv) prepareExecPlugin(_, _, _ string) {
|
||||
// Do nothing. At one point this method
|
||||
// copied the exec plugin directory to a temp dir
|
||||
// and ran it from there. Left as a hook.
|
||||
}
|
||||
|
||||
func (x *pluginTestEnv) setEnv() {
|
||||
x.oldXdg, x.wasSet = os.LookupEnv(konfig.KustomizePluginHomeEnv)
|
||||
os.Setenv(konfig.KustomizePluginHomeEnv, x.workDir)
|
||||
os.Setenv(konfig.KustomizePluginHomeEnv, x.pluginRoot)
|
||||
}
|
||||
|
||||
func (x *pluginTestEnv) resetEnv() {
|
||||
|
||||
Reference in New Issue
Block a user