diff --git a/pkg/target/testenvcontroller_test.go b/internal/plugintest/plugintestenv.go similarity index 80% rename from pkg/target/testenvcontroller_test.go rename to internal/plugintest/plugintestenv.go index 58ed53d3c..0a9cfc86d 100644 --- a/pkg/target/testenvcontroller_test.go +++ b/internal/plugintest/plugintestenv.go @@ -11,23 +11,23 @@ See the License for the specific language governing permissions and limitations under the License. */ -package target_test +package plugintest_test import ( "io/ioutil" "os" "os/exec" "path/filepath" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "testing" + "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/pgmconfig" "sigs.k8s.io/kustomize/pkg/plugins" ) -// TestEnvController manages the KustTarget test environment. +// PluginTestEnv manages the plugin test environment. // It sets/resets XDG_CONFIG_HOME, makes/removes a temp objRoot. -type TestEnvController struct { +type PluginTestEnv struct { t *testing.T compiler *plugins.Compiler workDir string @@ -35,30 +35,30 @@ type TestEnvController struct { wasSet bool } -func NewTestEnvController(t *testing.T) *TestEnvController { - return &TestEnvController{t: t} +func NewPluginTestEnv(t *testing.T) *PluginTestEnv { + return &PluginTestEnv{t: t} } -func (x *TestEnvController) Set() *TestEnvController { +func (x *PluginTestEnv) Set() *PluginTestEnv { x.createWorkDir() x.compiler = x.makeCompiler() x.setEnv() return x } -func (x *TestEnvController) Reset() { +func (x *PluginTestEnv) Reset() { x.resetEnv() x.removeWorkDir() } -func (x *TestEnvController) 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) } } -func (x *TestEnvController) BuildExecPlugin(name ...string) { +func (x *PluginTestEnv) BuildExecPlugin(name ...string) { obj := filepath.Join( append([]string{x.workDir, pgmconfig.ProgramName, plugin.PluginRoot}, name...)...) @@ -80,7 +80,7 @@ func (x *TestEnvController) BuildExecPlugin(name ...string) { } } -func (x *TestEnvController) makeCompiler() *plugins.Compiler { +func (x *PluginTestEnv) makeCompiler() *plugins.Compiler { // The plugin loader wants to find object code under // $XDG_CONFIG_HOME/kustomize/plugins // and the compiler writes object code to @@ -99,7 +99,7 @@ func (x *TestEnvController) makeCompiler() *plugins.Compiler { return plugins.NewCompiler(srcRoot, objRoot) } -func (x *TestEnvController) createWorkDir() { +func (x *PluginTestEnv) createWorkDir() { var err error x.workDir, err = ioutil.TempDir("", "kustomize-plugin-tests") if err != nil { @@ -107,7 +107,7 @@ func (x *TestEnvController) createWorkDir() { } } -func (x *TestEnvController) removeWorkDir() { +func (x *PluginTestEnv) removeWorkDir() { err := os.RemoveAll(x.workDir) if err != nil { x.t.Errorf( @@ -115,12 +115,12 @@ func (x *TestEnvController) removeWorkDir() { } } -func (x *TestEnvController) setEnv() { +func (x *PluginTestEnv) setEnv() { x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XDG_CONFIG_HOME) os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.workDir) } -func (x *TestEnvController) resetEnv() { +func (x *PluginTestEnv) resetEnv() { if x.wasSet { os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.oldXdg) } else { diff --git a/pkg/plugins/generators.go b/pkg/plugins/generators.go deleted file mode 100644 index abc38b145..000000000 --- a/pkg/plugins/generators.go +++ /dev/null @@ -1,42 +0,0 @@ -/* -Copyright 2019 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package plugins - -import ( - "fmt" - - "sigs.k8s.io/kustomize/pkg/ifc" - "sigs.k8s.io/kustomize/pkg/resmap" - "sigs.k8s.io/kustomize/pkg/transformers" -) - -func (l *Loader) LoadGenerators( - ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Generator, error) { - var result []transformers.Generator - for _, res := range rm { - c, err := l.loadAndConfigurePlugin(ldr, res) - if err != nil { - return nil, err - } - g, ok := c.(transformers.Generator) - if !ok { - return nil, fmt.Errorf("plugin %s not a generator", res.Id()) - } - result = append(result, g) - } - return result, nil -} diff --git a/pkg/plugins/transformers.go b/pkg/plugins/loader.go similarity index 88% rename from pkg/plugins/transformers.go rename to pkg/plugins/loader.go index acb5ffeac..592e0524d 100644 --- a/pkg/plugins/transformers.go +++ b/pkg/plugins/loader.go @@ -45,6 +45,23 @@ func NewLoader( return &Loader{pc: pc, rf: rf} } +func (l *Loader) LoadGenerators( + ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Generator, error) { + var result []transformers.Generator + for _, res := range rm { + c, err := l.loadAndConfigurePlugin(ldr, res) + if err != nil { + return nil, err + } + g, ok := c.(transformers.Generator) + if !ok { + return nil, fmt.Errorf("plugin %s not a generator", res.Id()) + } + result = append(result, g) + } + return result, nil +} + func (l *Loader) LoadTransformers( ldr ifc.Loader, rm resmap.ResMap) ([]transformers.Transformer, error) { var result []transformers.Transformer diff --git a/pkg/plugins/loader_test.go b/pkg/plugins/loader_test.go new file mode 100644 index 000000000..d0e0177c8 --- /dev/null +++ b/pkg/plugins/loader_test.go @@ -0,0 +1,86 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package plugins_test + +import ( + "testing" + + "sigs.k8s.io/kustomize/internal/plugintest" + "sigs.k8s.io/kustomize/k8sdeps/kunstruct" + "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" + "sigs.k8s.io/kustomize/pkg/internal/loadertest" + "sigs.k8s.io/kustomize/pkg/plugins" + "sigs.k8s.io/kustomize/pkg/resmap" + "sigs.k8s.io/kustomize/pkg/resource" +) + +const ( + secretGenerator = `apiVersion: kustomize.config.k8s.io/v1 +kind: SecretGenerator +metadata: + name: secretGenerator +name: mySecret +behavior: merge +envFiles: +- a.env +- b.env +valueFiles: +- longsecret.txt +literals: +- FRUIT=apple +- VEGETABLE=carrot +` + serviceGenerator = ` +apiVersion: someteam.example.com/v1 +kind: ServiceGenerator +metadata: + name: myServiceGenerator +service: my-service +port: "12345" +` +) + +func TestLoader(t *testing.T) { + tc := plugintest_test.NewPluginTestEnv(t).Set() + defer tc.Reset() + + tc.BuildGoPlugin( + "kustomize.config.k8s.io", "v1", "SecretGenerator") + tc.BuildGoPlugin( + "someteam.example.com", "v1", "ServiceGenerator") + + rmF := resmap.NewFactory(resource.NewFactory( + kunstruct.NewKunstructuredFactoryImpl())) + + l := plugins.NewLoader(plugin.ActivePluginConfig(), rmF) + if l == nil { + t.Fatal("expect non-nil loader") + } + + ldr := loadertest.NewFakeLoader("/foo") + + m, err := rmF.NewResMapFromBytes([]byte( + serviceGenerator + "---\n" + secretGenerator)) + if err != nil { + t.Fatal(err) + } + + _, err = l.LoadGenerators(ldr, m) + if err != nil { + t.Fatal(err) + } +} diff --git a/pkg/target/chartinflatorexecplugin_test.go b/pkg/target/chartinflatorexecplugin_test.go index 7f80762d2..45441573c 100644 --- a/pkg/target/chartinflatorexecplugin_test.go +++ b/pkg/target/chartinflatorexecplugin_test.go @@ -20,6 +20,7 @@ package target_test import ( "testing" + "sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" ) @@ -33,7 +34,7 @@ import ( // This test requires having the helm binary on the PATH. // func TestChartInflatorExecPlugin(t *testing.T) { - tc := NewTestEnvController(t).Set() + tc := plugintest_test.NewPluginTestEnv(t).Set() defer tc.Reset() tc.BuildExecPlugin( diff --git a/pkg/target/generatorplugin_test.go b/pkg/target/generatorplugin_test.go index 02aa0afbd..b98796203 100644 --- a/pkg/target/generatorplugin_test.go +++ b/pkg/target/generatorplugin_test.go @@ -17,6 +17,7 @@ import ( "path/filepath" "testing" + "sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" ) @@ -32,7 +33,7 @@ port: "12345" } func TestServiceGeneratorPlugin(t *testing.T) { - tc := NewTestEnvController(t).Set() + tc := plugintest_test.NewPluginTestEnv(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -97,7 +98,7 @@ ut labore et dolore magna aliqua. // nolint:lll func TestSecretGenerator(t *testing.T) { - tc := NewTestEnvController(t).Set() + tc := plugintest_test.NewPluginTestEnv(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -129,7 +130,7 @@ type: Opaque } func TestConfigMapGenerator(t *testing.T) { - tc := NewTestEnvController(t).Set() + tc := plugintest_test.NewPluginTestEnv(t).Set() defer tc.Reset() tc.BuildExecPlugin( diff --git a/pkg/target/transformerplugin_test.go b/pkg/target/transformerplugin_test.go index be8020688..6f22323a1 100644 --- a/pkg/target/transformerplugin_test.go +++ b/pkg/target/transformerplugin_test.go @@ -16,6 +16,7 @@ package target_test import ( "testing" + "sigs.k8s.io/kustomize/internal/plugintest" "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" ) @@ -56,7 +57,7 @@ metadata: } func TestOrderedTransformers(t *testing.T) { - tc := NewTestEnvController(t).Set() + tc := plugintest_test.NewPluginTestEnv(t).Set() defer tc.Reset() tc.BuildGoPlugin( @@ -102,7 +103,7 @@ spec: } func TestSedTransformer(t *testing.T) { - tc := NewTestEnvController(t).Set() + tc := plugintest_test.NewPluginTestEnv(t).Set() defer tc.Reset() tc.BuildExecPlugin( @@ -148,7 +149,7 @@ metadata: } func TestTransformedTransformers(t *testing.T) { - tc := NewTestEnvController(t).Set() + tc := plugintest_test.NewPluginTestEnv(t).Set() defer tc.Reset() tc.BuildGoPlugin(