Enhance test harness.

This commit is contained in:
monopole
2021-04-11 18:00:50 -07:00
parent 5100568b0c
commit 3af1ae4159
4 changed files with 57 additions and 22 deletions

View File

@@ -319,7 +319,6 @@ func (fl *fileLoader) Load(path string) ([]byte, error) {
} }
return body, nil return body, nil
} }
if !filepath.IsAbs(path) { if !filepath.IsAbs(path) {
path = fl.root.Join(path) path = fl.root.Join(path)
} }

View File

@@ -54,7 +54,6 @@ func MakeFakeFs(td []testData) filesys.FileSystem {
func makeLoader() *fileLoader { func makeLoader() *fileLoader {
return NewFileLoaderAtRoot(MakeFakeFs(testCases)) return NewFileLoaderAtRoot(MakeFakeFs(testCases))
} }
func TestLoaderLoad(t *testing.T) { func TestLoaderLoad(t *testing.T) {

View File

@@ -4,6 +4,9 @@
package kusttest_test package kusttest_test
import ( import (
"io/ioutil"
"os"
"strings"
"testing" "testing"
"sigs.k8s.io/kustomize/api/filesys" "sigs.k8s.io/kustomize/api/filesys"
@@ -33,36 +36,68 @@ type HarnessEnhanced struct {
// A file loader using the Harness.fSys to read test data. // A file loader using the Harness.fSys to read test data.
ldr ifc.Loader ldr ifc.Loader
// If true, wipe the ifc.loader root (not the plugin loader root)
// as part of cleanup.
shouldWipeLdrRoot bool
// A plugin loader that loads plugins from a (real) file system. // A plugin loader that loads plugins from a (real) file system.
pl *pLdr.Loader pl *pLdr.Loader
} }
func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced { func MakeEnhancedHarness(t *testing.T) *HarnessEnhanced {
pte := newPluginTestEnv(t).set() r := makeBaseEnhancedHarness(t)
r.Harness = MakeHarnessWithFs(t, filesys.MakeFsInMemory())
// Point the Harness's file loader to the root ('/')
// of the in-memory file system.
r.ResetLoaderRoot(filesys.Separator)
return r
}
pc := types.EnabledPluginConfig(types.BploLoadFromFileSys) func MakeEnhancedHarnessWithTmpRoot(t *testing.T) *HarnessEnhanced {
pc.FnpLoadingOptions.EnableStar = true r := makeBaseEnhancedHarness(t)
p := provider.NewDefaultDepProvider() fSys := filesys.MakeFsOnDisk()
resourceFactory := p.GetResourceFactory() r.Harness = MakeHarnessWithFs(t, fSys)
resmapFactory := resmap.NewFactory(resourceFactory) tmpDir, err := ioutil.TempDir("", "kust-testing-")
if err != nil {
panic("test harness cannot make tmp dir: " + err.Error())
}
r.ldr, err = fLdr.NewLoader(fLdr.RestrictionRootOnly, tmpDir, fSys)
if err != nil {
panic("test harness cannot make ldr at tmp dir: " + err.Error())
}
r.shouldWipeLdrRoot = true
return r
}
result := &HarnessEnhanced{ func makeBaseEnhancedHarness(t *testing.T) *HarnessEnhanced {
Harness: MakeHarness(t), rf := resmap.NewFactory(
pte: pte, provider.NewDefaultDepProvider().GetResourceFactory())
rf: resmapFactory, return &HarnessEnhanced{
// The plugin configs are always located on disk, regardless of the test harness's FS pte: newPluginTestEnv(t).set(),
pl: pLdr.NewLoader(pc, resmapFactory, filesys.MakeFsOnDisk())} rf: rf,
pl: pLdr.NewLoader(
// Point the file loader to the root ('/') of the in-memory file system. types.EnabledPluginConfig(types.BploLoadFromFileSys),
result.ResetLoaderRoot(filesys.Separator) rf,
// Plugin configs are always located on disk,
return result // regardless of the test harness's FS
filesys.MakeFsOnDisk())}
} }
func (th *HarnessEnhanced) Reset() { func (th *HarnessEnhanced) Reset() {
if th.shouldWipeLdrRoot {
if !strings.HasPrefix(th.ldr.Root(), os.TempDir()) {
// sanity check.
panic("something strange about th.ldr.Root() = " + th.ldr.Root())
}
os.RemoveAll(th.ldr.Root())
}
th.pte.reset() th.pte.reset()
} }
func (th *HarnessEnhanced) GetPluginConfig() *types.PluginConfig {
return th.pl.Config()
}
func (th *HarnessEnhanced) PrepBuiltin(k string) *HarnessEnhanced { func (th *HarnessEnhanced) PrepBuiltin(k string) *HarnessEnhanced {
return th.BuildGoPlugin(konfig.BuiltinPluginPackage, "", k) return th.BuildGoPlugin(konfig.BuiltinPluginPackage, "", k)
} }
@@ -105,7 +140,7 @@ func (th *HarnessEnhanced) LoadAndRunGenerator(
} }
rm, err := g.Generate() rm, err := g.Generate()
if err != nil { if err != nil {
th.t.Fatalf("Err: %v", err) th.t.Fatalf("generate err: %v", err)
} }
rm.RemoveBuildAnnotations() rm.RemoveBuildAnnotations()
return rm return rm

View File

@@ -15,8 +15,10 @@ type PluginConfig struct {
FnpLoadingOptions FnPluginLoadingOptions FnpLoadingOptions FnPluginLoadingOptions
} }
func EnabledPluginConfig(b BuiltinPluginLoadingOptions) *PluginConfig { func EnabledPluginConfig(b BuiltinPluginLoadingOptions) (pc *PluginConfig) {
return MakePluginConfig(PluginRestrictionsNone, b) pc = MakePluginConfig(PluginRestrictionsNone, b)
pc.FnpLoadingOptions.EnableStar = true
return
} }
func DisabledPluginConfig() *PluginConfig { func DisabledPluginConfig() *PluginConfig {