mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Add SedTransformerTest
This commit is contained in:
@@ -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!"
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
const (
|
||||
XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
|
||||
defaultConfigSubdir = ".config"
|
||||
PluginRoot = "plugin"
|
||||
)
|
||||
|
||||
// Use https://github.com/kirsle/configdir instead?
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -16,4 +16,4 @@ limitations under the License.
|
||||
|
||||
package plugins
|
||||
|
||||
const pluginSymbol = "KustomizePlugin"
|
||||
const PluginSymbol = "KustomizePlugin"
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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-
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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,
|
||||
|
||||
65
plugin/someteam.example.com/v1/SedTransformer_test.go
Normal file
65
plugin/someteam.example.com/v1/SedTransformer_test.go
Normal 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
|
||||
`)
|
||||
}
|
||||
Reference in New Issue
Block a user