Merge pull request #1066 from monopole/addSedTransformerTest

Add SedTransformerTest
This commit is contained in:
Jeff Regan
2019-05-12 11:34:57 -07:00
committed by GitHub
13 changed files with 142 additions and 22 deletions

View File

@@ -11,7 +11,7 @@ cd "$base_dir" || {
rc=0 rc=0
function runTest { function runFunc {
local name=$1 local name=$1
local result="SUCCESS" local result="SUCCESS"
printf "============== begin %s\n" "$name" printf "============== begin %s\n" "$name"
@@ -85,18 +85,18 @@ echo "GOPATH=$GOPATH"
echo "GO111MODULE=$GO111MODULE" echo "GO111MODULE=$GO111MODULE"
echo pwd=`pwd` echo pwd=`pwd`
echo " " echo " "
echo "Beginning tests..." echo "Working..."
runTest testGoLangCILint runFunc testGoLangCILint
runTest testGoTest runFunc testGoTest
if [ -z ${TRAVIS+x} ]; then if [ -z ${TRAVIS+x} ]; then
echo Not on travis, so running the notravis tests echo Not on travis, so running the notravis tests
runTest testNoTravisGoTest runFunc testNoTravisGoTest
fi fi
PATH=$HOME/go/bin:$PATH PATH=$HOME/go/bin:$PATH
runTest testExamples runFunc testExamples
if [ $rc -eq 0 ]; then if [ $rc -eq 0 ]; then
echo "SUCCESS!" echo "SUCCESS!"

View File

@@ -23,7 +23,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"sigs.k8s.io/kustomize/k8sdeps/kv/plugin"
"sigs.k8s.io/kustomize/pkg/pgmconfig" "sigs.k8s.io/kustomize/pkg/pgmconfig"
"sigs.k8s.io/kustomize/pkg/plugins" "sigs.k8s.io/kustomize/pkg/plugins"
) )
@@ -63,7 +62,7 @@ func (x *PluginTestEnv) BuildGoPlugin(g, v, k string) {
func (x *PluginTestEnv) BuildExecPlugin(name ...string) { func (x *PluginTestEnv) BuildExecPlugin(name ...string) {
obj := filepath.Join( 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() srcRoot, err := plugins.DefaultSrcRoot()
if err != nil { if err != nil {
@@ -90,7 +89,7 @@ func (x *PluginTestEnv) makeCompiler() *plugins.Compiler {
// $objRoot // $objRoot
// so set things up accordingly. // so set things up accordingly.
objRoot := filepath.Join( objRoot := filepath.Join(
x.workDir, pgmconfig.ProgramName, plugin.PluginRoot) x.workDir, pgmconfig.ProgramName, pgmconfig.PluginRoot)
err := os.MkdirAll(objRoot, os.ModePerm) err := os.MkdirAll(objRoot, os.ModePerm)
if err != nil { if err != nil {
x.t.Error(err) x.t.Error(err)

View File

@@ -32,7 +32,6 @@ type Registry struct {
} }
const ( const (
PluginRoot = "plugin"
pluginTypeGo = types.PluginType("go") pluginTypeGo = types.PluginType("go")
pluginTypeBuiltIn = types.PluginType("builtin") pluginTypeBuiltIn = types.PluginType("builtin")
) )
@@ -47,7 +46,7 @@ func DefaultPluginConfig() *types.PluginConfig {
return &types.PluginConfig{ return &types.PluginConfig{
GoEnabled: false, GoEnabled: false,
DirectoryPath: filepath.Join( DirectoryPath: filepath.Join(
pgmconfig.ConfigRoot(), PluginRoot), pgmconfig.ConfigRoot(), pgmconfig.PluginRoot),
} }
} }

View File

@@ -128,6 +128,27 @@ func (th *KustTestHarness) LoadAndRunGenerator(
return rm 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 { func tabToSpace(input string) string {
var result []string var result []string
for _, i := range input { for _, i := range input {

View File

@@ -27,6 +27,7 @@ import (
const ( const (
XDG_CONFIG_HOME = "XDG_CONFIG_HOME" XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
defaultConfigSubdir = ".config" defaultConfigSubdir = ".config"
PluginRoot = "plugin"
) )
// Use https://github.com/kirsle/configdir instead? // Use https://github.com/kirsle/configdir instead?

View File

@@ -50,7 +50,7 @@ func DefaultSrcRoot() (string, error) {
root = filepath.Join( root = filepath.Join(
os.Getenv("GOPATH"), "src", os.Getenv("GOPATH"), "src",
pgmconfig.DomainName, pgmconfig.DomainName,
pgmconfig.ProgramName, plugin.PluginRoot) pgmconfig.ProgramName, pgmconfig.PluginRoot)
if FileExists(root) { if FileExists(root) {
return root, nil return root, nil
} }
@@ -59,7 +59,7 @@ func DefaultSrcRoot() (string, error) {
// get the root kustomize source directory when // get the root kustomize source directory when
// GOPATH is not set // GOPATH is not set
_, filename, _, _ := runtime.Caller(1) _, filename, _, _ := runtime.Caller(1)
root = path.Join(path.Dir(filename), "../..", plugin.PluginRoot) root = path.Join(path.Dir(filename), "../..", pgmconfig.PluginRoot)
if FileExists(root) { if FileExists(root) {
return root, nil return root, nil
} }
@@ -73,7 +73,7 @@ func DefaultSrcRoot() (string, error) {
root = filepath.Join( root = filepath.Join(
pgmconfig.HomeDir(), pgmconfig.HomeDir(),
pgmconfig.ProgramName, plugin.PluginRoot) pgmconfig.ProgramName, pgmconfig.PluginRoot)
if FileExists(root) { if FileExists(root) {
return root, nil return root, nil
} }

View File

@@ -16,4 +16,4 @@ limitations under the License.
package plugins package plugins
const pluginSymbol = "KustomizePlugin" const PluginSymbol = "KustomizePlugin"

View File

@@ -141,11 +141,11 @@ func (l *Loader) loadGoPlugin(id resid.ResId) (c Configurable, err error) {
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "plugin %s fails to load", name) return nil, errors.Wrapf(err, "plugin %s fails to load", name)
} }
symbol, err := p.Lookup(pluginSymbol) symbol, err := p.Lookup(PluginSymbol)
if err != nil { if err != nil {
return nil, errors.Wrapf( return nil, errors.Wrapf(
err, "plugin %s doesn't have symbol %s", err, "plugin %s doesn't have symbol %s",
name, pluginSymbol) name, PluginSymbol)
} }
c, ok = symbol.(Configurable) c, ok = symbol.(Configurable)
if !ok { if !ok {

View File

@@ -25,6 +25,42 @@ import (
"sigs.k8s.io/kustomize/pkg/loader" "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) { func writeSmallBase(th *kusttest_test.KustTestHarness) {
th.WriteK("/app/base", ` th.WriteK("/app/base", `
namePrefix: a- namePrefix: a-

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package builtin package main_test
import ( import (
"testing" "testing"

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package builtin package main_test
import ( import (
"testing" "testing"

View File

@@ -19,10 +19,9 @@ set -e
# and inflates it to stdout, using the given values file. # and inflates it to stdout, using the given values file.
# #
# chartDir default: $TMP_DIR/charts # chartDir default: $TMP_DIR/charts
# chartDir default: #
# Example execution: # 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, # Yaml parsing is a ridiculous thing to do in bash,

View File

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