diff --git a/bin/pre-commit.sh b/bin/pre-commit.sh index 10a33da53..c1d828799 100755 --- a/bin/pre-commit.sh +++ b/bin/pre-commit.sh @@ -11,7 +11,7 @@ cd "$base_dir" || { rc=0 -function runTest { +function runFunc { local name=$1 local result="SUCCESS" printf "============== begin %s\n" "$name" @@ -85,18 +85,18 @@ echo "GOPATH=$GOPATH" echo "GO111MODULE=$GO111MODULE" echo pwd=`pwd` echo " " -echo "Beginning tests..." +echo "Working..." -runTest testGoLangCILint -runTest testGoTest +runFunc testGoLangCILint +runFunc testGoTest if [ -z ${TRAVIS+x} ]; then echo Not on travis, so running the notravis tests - runTest testNoTravisGoTest + runFunc testNoTravisGoTest fi PATH=$HOME/go/bin:$PATH -runTest testExamples +runFunc testExamples if [ $rc -eq 0 ]; then echo "SUCCESS!" diff --git a/internal/plugintest/plugintestenv.go b/internal/plugintest/plugintestenv.go index 70eac0a42..7daac18bc 100644 --- a/internal/plugintest/plugintestenv.go +++ b/internal/plugintest/plugintestenv.go @@ -23,7 +23,6 @@ import ( "path/filepath" "testing" - "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" "sigs.k8s.io/kustomize/pkg/pgmconfig" "sigs.k8s.io/kustomize/pkg/plugins" ) @@ -63,7 +62,7 @@ func (x *PluginTestEnv) BuildGoPlugin(g, v, k string) { func (x *PluginTestEnv) BuildExecPlugin(name ...string) { obj := filepath.Join( - append([]string{x.workDir, pgmconfig.ProgramName, plugin.PluginRoot}, name...)...) + append([]string{x.workDir, pgmconfig.ProgramName, pgmconfig.PluginRoot}, name...)...) srcRoot, err := plugins.DefaultSrcRoot() if err != nil { @@ -90,7 +89,7 @@ func (x *PluginTestEnv) makeCompiler() *plugins.Compiler { // $objRoot // so set things up accordingly. objRoot := filepath.Join( - x.workDir, pgmconfig.ProgramName, plugin.PluginRoot) + x.workDir, pgmconfig.ProgramName, pgmconfig.PluginRoot) err := os.MkdirAll(objRoot, os.ModePerm) if err != nil { x.t.Error(err) diff --git a/k8sdeps/kv/plugin/registry.go b/k8sdeps/kv/plugin/registry.go index 3a5cf6e56..adadedcd4 100644 --- a/k8sdeps/kv/plugin/registry.go +++ b/k8sdeps/kv/plugin/registry.go @@ -32,7 +32,6 @@ type Registry struct { } const ( - PluginRoot = "plugin" pluginTypeGo = types.PluginType("go") pluginTypeBuiltIn = types.PluginType("builtin") ) @@ -47,7 +46,7 @@ func DefaultPluginConfig() *types.PluginConfig { return &types.PluginConfig{ GoEnabled: false, DirectoryPath: filepath.Join( - pgmconfig.ConfigRoot(), PluginRoot), + pgmconfig.ConfigRoot(), pgmconfig.PluginRoot), } } diff --git a/pkg/kusttest/kusttestharness.go b/pkg/kusttest/kusttestharness.go index 72defaea8..e4ff488de 100644 --- a/pkg/kusttest/kusttestharness.go +++ b/pkg/kusttest/kusttestharness.go @@ -128,6 +128,27 @@ func (th *KustTestHarness) LoadAndRunGenerator( return rm } +func (th *KustTestHarness) LoadAndRunTransformer( + config, input string) resmap.ResMap { + transConfig, err := th.rf.RF().FromBytes([]byte(config)) + if err != nil { + th.t.Fatalf("Err: %v", err) + } + resMap, err := th.rf.NewResMapFromBytes([]byte(input)) + if err != nil { + th.t.Fatalf("Err: %v", err) + } + g, err := th.pl.LoadTransformer(th.ldr, transConfig) + if err != nil { + th.t.Fatalf("Err: %v", err) + } + err = g.Transform(resMap) + if err != nil { + th.t.Fatalf("Err: %v", err) + } + return resMap +} + func tabToSpace(input string) string { var result []string for _, i := range input { diff --git a/pkg/pgmconfig/config.go b/pkg/pgmconfig/config.go index 5185c50cd..5dc5eb8ad 100644 --- a/pkg/pgmconfig/config.go +++ b/pkg/pgmconfig/config.go @@ -27,6 +27,7 @@ import ( const ( XDG_CONFIG_HOME = "XDG_CONFIG_HOME" defaultConfigSubdir = ".config" + PluginRoot = "plugin" ) // Use https://github.com/kirsle/configdir instead? diff --git a/pkg/plugins/compiler.go b/pkg/plugins/compiler.go index 125861403..f8b030e74 100644 --- a/pkg/plugins/compiler.go +++ b/pkg/plugins/compiler.go @@ -50,7 +50,7 @@ func DefaultSrcRoot() (string, error) { root = filepath.Join( os.Getenv("GOPATH"), "src", pgmconfig.DomainName, - pgmconfig.ProgramName, plugin.PluginRoot) + pgmconfig.ProgramName, pgmconfig.PluginRoot) if FileExists(root) { return root, nil } @@ -59,7 +59,7 @@ func DefaultSrcRoot() (string, error) { // get the root kustomize source directory when // GOPATH is not set _, filename, _, _ := runtime.Caller(1) - root = path.Join(path.Dir(filename), "../..", plugin.PluginRoot) + root = path.Join(path.Dir(filename), "../..", pgmconfig.PluginRoot) if FileExists(root) { return root, nil } @@ -73,7 +73,7 @@ func DefaultSrcRoot() (string, error) { root = filepath.Join( pgmconfig.HomeDir(), - pgmconfig.ProgramName, plugin.PluginRoot) + pgmconfig.ProgramName, pgmconfig.PluginRoot) if FileExists(root) { return root, nil } diff --git a/pkg/plugins/config.go b/pkg/plugins/config.go index 8081a4d07..24c94ba65 100644 --- a/pkg/plugins/config.go +++ b/pkg/plugins/config.go @@ -16,4 +16,4 @@ limitations under the License. package plugins -const pluginSymbol = "KustomizePlugin" +const PluginSymbol = "KustomizePlugin" diff --git a/pkg/plugins/loader.go b/pkg/plugins/loader.go index b40c37bb6..e5dfe5905 100644 --- a/pkg/plugins/loader.go +++ b/pkg/plugins/loader.go @@ -141,11 +141,11 @@ func (l *Loader) loadGoPlugin(id resid.ResId) (c Configurable, err error) { if err != nil { return nil, errors.Wrapf(err, "plugin %s fails to load", name) } - symbol, err := p.Lookup(pluginSymbol) + symbol, err := p.Lookup(PluginSymbol) if err != nil { return nil, errors.Wrapf( err, "plugin %s doesn't have symbol %s", - name, pluginSymbol) + name, PluginSymbol) } c, ok = symbol.(Configurable) if !ok { diff --git a/pkg/target/baseandoverlaysmall_test.go b/pkg/target/baseandoverlaysmall_test.go index fbb406ec9..3bbf12996 100644 --- a/pkg/target/baseandoverlaysmall_test.go +++ b/pkg/target/baseandoverlaysmall_test.go @@ -25,6 +25,42 @@ import ( "sigs.k8s.io/kustomize/pkg/loader" ) +func TestBaseInResourceList(t *testing.T) { + th := kusttest_test.NewKustTestHarness(t, "/app/prod") + th.WriteK("/app/prod", ` +namePrefix: b- +resources: +- ../base +`) + th.WriteK("/app/base", ` +namePrefix: a- +resources: +- service.yaml +`) + th.WriteF("/app/base/service.yaml", ` +apiVersion: v1 +kind: Service +metadata: + name: myService +spec: + selector: + backend: bungie +`) + m, err := th.MakeKustTarget().MakeCustomizedResMap() + if err != nil { + t.Fatalf("Err: %v", err) + } + th.AssertActualEqualsExpected(m, ` +apiVersion: v1 +kind: Service +metadata: + name: b-a-myService +spec: + selector: + backend: bungie +`) +} + func writeSmallBase(th *kusttest_test.KustTestHarness) { th.WriteK("/app/base", ` namePrefix: a- diff --git a/plugin/builtin/ConfigMapGenerator_test.go b/plugin/builtin/ConfigMapGenerator_test.go index 7e3a0c488..7790368f5 100644 --- a/plugin/builtin/ConfigMapGenerator_test.go +++ b/plugin/builtin/ConfigMapGenerator_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package builtin +package main_test import ( "testing" diff --git a/plugin/builtin/SecretGenerator_test.go b/plugin/builtin/SecretGenerator_test.go index 59a9eb13e..cd108989d 100644 --- a/plugin/builtin/SecretGenerator_test.go +++ b/plugin/builtin/SecretGenerator_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package builtin +package main_test import ( "testing" diff --git a/plugin/someteam.example.com/v1/ChartInflatorExec b/plugin/someteam.example.com/v1/ChartInflatorExec index 97f37da97..dbffd47cf 100755 --- a/plugin/someteam.example.com/v1/ChartInflatorExec +++ b/plugin/someteam.example.com/v1/ChartInflatorExec @@ -19,10 +19,9 @@ set -e # and inflates it to stdout, using the given values file. # # chartDir default: $TMP_DIR/charts -# chartDir default: - +# # Example execution: -# ./plugins/kustomize.config.k8s.io/v1/ChartInflatorExec configFile.yaml +# ./plugin/someteam.example.com/v1/ChartInflatorExec configFile.yaml # Yaml parsing is a ridiculous thing to do in bash, diff --git a/plugin/someteam.example.com/v1/SedTransformer_test.go b/plugin/someteam.example.com/v1/SedTransformer_test.go new file mode 100644 index 000000000..e54ccac3c --- /dev/null +++ b/plugin/someteam.example.com/v1/SedTransformer_test.go @@ -0,0 +1,65 @@ +/* +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 main_test + +import ( + "testing" + + "sigs.k8s.io/kustomize/internal/plugintest" + "sigs.k8s.io/kustomize/k8sdeps/kv/plugin" + "sigs.k8s.io/kustomize/pkg/kusttest" + "sigs.k8s.io/kustomize/pkg/loader" +) + +func TestSedTransformer(t *testing.T) { + tc := plugintest_test.NewPluginTestEnv(t).Set() + defer tc.Reset() + + tc.BuildExecPlugin("someteam.example.com", "v1", "SedTransformer") + th := kusttest_test.NewKustTestHarnessFull( + t, "/app", loader.RestrictionRootOnly, plugin.ActivePluginConfig()) + + th.WriteF("/app/sed-input.txt", ` +s/$FRUIT/orange/g +s/$VEGGIE/tomato/g +`) + + rm := th.LoadAndRunTransformer(` +apiVersion: someteam.example.com/v1 +kind: SedTransformer +metadata: + name: notImportantHere +argsOneLiner: s/one/two/g +argsFromFile: sed-input.txt +`, + `apiVersion: apps/v1 +kind: MeatBall +metadata: + name: notImportantHere +beans: one one one one +fruit: $FRUIT +vegetable: $VEGGIE +`) + + th.AssertActualEqualsExpected(rm, ` +apiVersion: apps/v1 +beans: two two two two +fruit: orange +kind: MeatBall +metadata: + annotations: {} + name: notImportantHere +vegetable: tomato +`) +}