Simplify plugin tests.

This commit is contained in:
Jeffrey Regan
2019-12-02 13:17:52 -08:00
parent 0a19a5dbd9
commit 861c86a70a
37 changed files with 375 additions and 632 deletions

View File

@@ -20,10 +20,10 @@ type FakeLoader struct {
// NewFakeLoader returns a Loader that uses a fake filesystem.
// The loader will be restricted to root only.
// The initialDir argument should be an absolute file path.
func NewFakeLoader(initialDir string) FakeLoader {
// The root argument should be an absolute file path.
func NewFakeLoader(root string) FakeLoader {
return NewFakeLoaderWithRestrictor(
loader.RestrictionRootOnly, filesys.MakeFsInMemory(), initialDir)
loader.RestrictionRootOnly, filesys.MakeFsInMemory(), root)
}
// NewFakeLoaderWithRestrictor returns a Loader that

View File

@@ -45,13 +45,10 @@ port: "12345"
)
func TestLoader(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "SecretGenerator")
tc.BuildGoPlugin(
"someteam.example.com", "v1", "SomeServiceGenerator")
th := kusttest_test.MakeEnhancedHarness(t).
BuildGoPlugin("builtin", "", "SecretGenerator").
BuildGoPlugin("someteam.example.com", "v1", "SomeServiceGenerator")
defer th.Reset()
rmF := resmap.NewFactory(resource.NewFactory(
kunstruct.NewKunstructuredFactoryImpl()), nil)

View File

@@ -21,21 +21,23 @@ import (
func makeKustTarget(
t *testing.T,
fSys filesys.FileSystem, path string) *target.KustTarget {
fSys filesys.FileSystem,
root string) *target.KustTarget {
return makeKustTargetWithRf(
t, fSys, path,
t, fSys, root,
resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()))
}
func makeKustTargetWithRf(
t *testing.T,
fSys filesys.FileSystem, path string,
fSys filesys.FileSystem,
root string,
resFact *resource.Factory) *target.KustTarget {
rf := resmap.NewFactory(resFact, transformer.NewFactoryImpl())
pc := konfig.DisabledPluginConfig()
kt := target.NewKustTarget(
loadertest.NewFakeLoaderWithRestrictor(
loader.RestrictionRootOnly, fSys, path),
loader.RestrictionRootOnly, fSys, root),
valtest_test.MakeFakeValidator(),
rf,
transformer.NewFactoryImpl(),

View File

@@ -28,13 +28,10 @@ import (
// TODO: Download and inflate the chart, and check that
// in for the test.
func TestChartInflatorPlugin(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
th := kusttest_test.MakeEnhancedHarness(t).
PrepExecPlugin("someteam.example.com", "v1", "ChartInflator")
defer th.Reset()
tc.PrepExecPlugin(
"someteam.example.com", "v1", "ChartInflator")
th := kusttest_test.MakeHarness(t)
th.WriteK("/app", `
generators:
- chartInflator.yaml

View File

@@ -13,13 +13,10 @@ import (
// This is a NamePrefixer that touches Deployments
// and Services exclusively.
func TestCustomNamePrefixer(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PrefixSuffixTransformer")
defer th.Reset()
tc.BuildGoPlugin(
"builtin", "", "PrefixSuffixTransformer")
th := kusttest_test.MakeHarness(t)
th.WriteK("/app", `
resources:
- deployment.yaml

View File

@@ -14,17 +14,11 @@ import (
// in a base, allowing them to be reused in any number of
// kustomizations.
func TestReusableCustomTransformers(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "PrefixSuffixTransformer")
tc.BuildGoPlugin(
"builtin", "", "AnnotationsTransformer")
tc.BuildGoPlugin(
"builtin", "", "LabelTransformer")
th := kusttest_test.MakeHarness(t)
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PrefixSuffixTransformer").
PrepBuiltin("AnnotationsTransformer").
PrepBuiltin("LabelTransformer")
defer th.Reset()
// First write three custom configurations for builtin plugins.

View File

@@ -331,13 +331,10 @@ patchesStrategicMerge:
}
func TestIssue1251_Plugins_ProdVsDev(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchJson6902Transformer")
defer th.Reset()
tc.BuildGoPlugin(
"builtin", "", "PatchJson6902Transformer")
th := kusttest_test.MakeHarness(t)
defineTransformerDirStructure(th)
th.WriteK("/app/prod", `
resources:
@@ -364,14 +361,11 @@ transformers:
}
func TestIssue1251_Plugins_Local(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchJson6902Transformer")
defer th.Reset()
tc.BuildGoPlugin(
"builtin", "", "PatchJson6902Transformer")
th := kusttest_test.MakeHarness(t)
writeDeploymentBase(th)
writeDeploymentBase(th.Harness)
writeJsonTransformerPluginConfig(
th, "/app/composite", "addDnsPolicy", patchJsonDnsPolicy)
@@ -393,7 +387,7 @@ transformers:
}
func writeJsonTransformerPluginConfig(
th kusttest_test.Harness, path, name, patch string) {
th *kusttest_test.HarnessEnhanced, path, name, patch string) {
th.WriteF(filepath.Join(path, name+"Config.yaml"),
fmt.Sprintf(`
apiVersion: builtin
@@ -411,14 +405,10 @@ jsonOp: '%s'
// Remote in the sense that they are bundled in a different kustomization.
func TestIssue1251_Plugins_Bundled(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "PatchJson6902Transformer")
th := kusttest_test.MakeHarness(t)
writeDeploymentBase(th)
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchJson6902Transformer")
defer th.Reset()
writeDeploymentBase(th.Harness)
th.WriteK("/app/patches", `
resources:
@@ -443,8 +433,8 @@ transformers:
th.AssertActualEqualsExpected(m, expectedPatchedDeployment)
}
func defineTransformerDirStructure(th kusttest_test.Harness) {
writeDeploymentBase(th)
func defineTransformerDirStructure(th *kusttest_test.HarnessEnhanced) {
writeDeploymentBase(th.Harness)
th.WriteK("/app/patches/addDnsPolicy", `
resources:

View File

@@ -18,11 +18,10 @@ import (
// its working directory and some environment variables,
// to add regression protection to plugin loading logic.
func TestPluginEnvironment(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.PrepExecPlugin(
"someteam.example.com", "v1", "PrintPluginEnv")
th := kusttest_test.MakeEnhancedHarness(t).
PrepExecPlugin(
"someteam.example.com", "v1", "PrintPluginEnv")
defer th.Reset()
confirmBehavior(
kusttest_test.MakeHarnessWithFs(t, filesys.MakeFsInMemory()),

View File

@@ -90,7 +90,6 @@ resources:
}
func TestBase(t *testing.T) {
//th := kusttest_test.NewKustTestHarness(t, "/app/base")
th := kusttest_test.MakeHarness(t)
writeBase(th)
m := th.Run("/app/base", th.MakeDefaultOptions())
@@ -339,7 +338,6 @@ rules:
func TestMultibasesWithConflict(t *testing.T) {
th := kusttest_test.MakeHarness(t)
//th := kusttest_test.NewKustTestHarness(t, "/app/combined")
writeBase(th)
writeMidOverlays(th)
writeTopOverlay(th)

View File

@@ -10,7 +10,7 @@ import (
"sigs.k8s.io/kustomize/api/types"
)
func writeDeployment(th kusttest_test.Harness, path string) {
func writeDeployment(th *kusttest_test.HarnessEnhanced, path string) {
th.WriteF(path, `
apiVersion: apps/v1
kind: Deployment
@@ -28,7 +28,7 @@ spec:
`)
}
func writeStringPrefixer(th kusttest_test.Harness, path, name string) {
func writeStringPrefixer(th *kusttest_test.HarnessEnhanced, path, name string) {
th.WriteF(path, `
apiVersion: someteam.example.com/v1
kind: StringPrefixer
@@ -37,7 +37,7 @@ metadata:
`)
}
func writeDatePrefixer(th kusttest_test.Harness, path, name string) {
func writeDatePrefixer(th *kusttest_test.HarnessEnhanced, path, name string) {
th.WriteF(path, `
apiVersion: someteam.example.com/v1
kind: DatePrefixer
@@ -47,15 +47,11 @@ metadata:
}
func TestOrderedTransformers(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
th := kusttest_test.MakeEnhancedHarness(t).
BuildGoPlugin("someteam.example.com", "v1", "StringPrefixer").
BuildGoPlugin("someteam.example.com", "v1", "DatePrefixer")
defer th.Reset()
tc.BuildGoPlugin(
"someteam.example.com", "v1", "StringPrefixer")
tc.BuildGoPlugin(
"someteam.example.com", "v1", "DatePrefixer")
th := kusttest_test.MakeHarness(t)
th.WriteK("/app", `
resources:
- deployment.yaml
@@ -89,13 +85,10 @@ spec:
}
func TestPluginsNotEnabled(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
th := kusttest_test.MakeEnhancedHarness(t).
BuildGoPlugin("someteam.example.com", "v1", "StringPrefixer")
defer th.Reset()
tc.BuildGoPlugin(
"someteam.example.com", "v1", "StringPrefixer")
th := kusttest_test.MakeHarness(t)
th.WriteK("/app", `
transformers:
- stringPrefixer.yaml
@@ -111,13 +104,10 @@ transformers:
}
func TestSedTransformer(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
th := kusttest_test.MakeEnhancedHarness(t).
PrepExecPlugin("someteam.example.com", "v1", "SedTransformer")
defer th.Reset()
tc.PrepExecPlugin(
"someteam.example.com", "v1", "SedTransformer")
th := kusttest_test.MakeHarness(t)
th.WriteK("/app", `
resources:
- configmap.yaml
@@ -176,16 +166,10 @@ metadata:
}
func TestTransformedTransformers(t *testing.T) {
tc := kusttest_test.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"someteam.example.com", "v1", "StringPrefixer")
tc.BuildGoPlugin(
"someteam.example.com", "v1", "DatePrefixer")
th := kusttest_test.MakeHarness(t)
th := kusttest_test.MakeEnhancedHarness(t).
BuildGoPlugin("someteam.example.com", "v1", "StringPrefixer").
BuildGoPlugin("someteam.example.com", "v1", "DatePrefixer")
defer th.Reset()
th.WriteK("/app/base", `
resources:

View File

@@ -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)
}

View File

@@ -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(

View File

@@ -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 {