Simplify some of the plugin testing code.

This commit is contained in:
Jeffrey Regan
2019-05-17 14:51:00 -07:00
parent f9c631e9ee
commit 3a85fcd365
23 changed files with 110 additions and 362 deletions

View File

@@ -1,20 +1,7 @@
// +build plugin
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
//go:generate go run sigs.k8s.io/kustomize/cmd/pluginator
package main
@@ -27,8 +14,8 @@ import (
)
type plugin struct {
ldr ifc.Loader
rf *resmap.Factory
ldr ifc.Loader
rf *resmap.Factory
types.GeneratorOptions
types.ConfigMapArgs
}

View File

@@ -1,39 +1,23 @@
/*
Copyright 2018 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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
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"
"sigs.k8s.io/kustomize/plugin"
)
func TestConfigMapGenerator(t *testing.T) {
tc := plugintest_test.NewPluginTestEnv(t).Set()
tc := plugin.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "ConfigMapGenerator")
th := kusttest_test.NewKustTestHarnessFull(
t, "/app", loader.RestrictionRootOnly, plugin.ActivePluginConfig())
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
th.WriteF("/app/devops.env", `
SERVICE_PORT=32

View File

@@ -1,20 +1,7 @@
// +build plugin
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
//go:generate go run sigs.k8s.io/kustomize/cmd/pluginator
package main
@@ -46,11 +33,10 @@ func (p *plugin) Config(
func (p *plugin) Transform(m resmap.ResMap) error {
argsList := make([]image.Image, 1)
argsList[0] = p.ImageTag
t, err := transformers.NewImageTransformer(argsList, p.FieldSpecs)
if err != nil {
return err
argsList[0] = p.ImageTag
t, err := transformers.NewImageTransformer(argsList, p.FieldSpecs)
if err != nil {
return err
}
return t.Transform(m)
return t.Transform(m)
}

View File

@@ -1,39 +1,23 @@
/*
Copyright 2018 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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
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"
"sigs.k8s.io/kustomize/plugin"
)
func TestImageTagTransformer(t *testing.T) {
tc := plugintest_test.NewPluginTestEnv(t).Set()
tc := plugin.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "ImageTagTransformer")
th := kusttest_test.NewKustTestHarnessFull(
t, "/app", loader.RestrictionRootOnly, plugin.ActivePluginConfig())
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
rm := th.LoadAndRunTransformer(`
apiVersion: builtin
@@ -43,7 +27,7 @@ metadata:
imageTag:
name: nginx
newTag: v2
`,`
`, `
group: apps
apiVersion: v1
kind: Deployment

View File

@@ -6,23 +6,18 @@ 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"
"sigs.k8s.io/kustomize/plugin"
)
func TestNameTransformer(t *testing.T) {
tc := plugintest_test.NewPluginTestEnv(t).Set()
tc := plugin.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "NameTransformer")
th := kusttest_test.NewKustTestHarnessFull(
t, "/app", loader.RestrictionRootOnly,
plugin.ActivePluginConfig())
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
rm := th.LoadAndRunTransformer(`
apiVersion: builtin
kind: NameTransformer

View File

@@ -1,20 +1,7 @@
// +build plugin
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
//go:generate go run sigs.k8s.io/kustomize/cmd/pluginator
package main

View File

@@ -1,39 +1,23 @@
/*
Copyright 2018 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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
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"
"sigs.k8s.io/kustomize/plugin"
)
func TestSecretGenerator(t *testing.T) {
tc := plugintest_test.NewPluginTestEnv(t).Set()
tc := plugin.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"builtin", "", "SecretGenerator")
th := kusttest_test.NewKustTestHarnessFull(
t, "/app", loader.RestrictionRootOnly, plugin.ActivePluginConfig())
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
th.WriteF("/app/a.env", `
ROUTER_PASSWORD=admin

View File

@@ -9,14 +9,14 @@ import (
)
type ConfigMapGeneratorPlugin struct {
ldr ifc.Loader
rf *resmap.Factory
ldr ifc.Loader
rf *resmap.Factory
types.GeneratorOptions
types.ConfigMapArgs
}
func NewConfigMapGeneratorPlugin() *ConfigMapGeneratorPlugin {
return &ConfigMapGeneratorPlugin{}
return &ConfigMapGeneratorPlugin{}
}
func (p *ConfigMapGeneratorPlugin) Config(

View File

@@ -18,7 +18,7 @@ type ImageTagTransformerPlugin struct {
}
func NewImageTagTransformerPlugin() *ImageTagTransformerPlugin {
return &ImageTagTransformerPlugin{}
return &ImageTagTransformerPlugin{}
}
func (p *ImageTagTransformerPlugin) Config(
@@ -30,11 +30,10 @@ func (p *ImageTagTransformerPlugin) Config(
func (p *ImageTagTransformerPlugin) Transform(m resmap.ResMap) error {
argsList := make([]image.Image, 1)
argsList[0] = p.ImageTag
t, err := transformers.NewImageTransformer(argsList, p.FieldSpecs)
if err != nil {
return err
argsList[0] = p.ImageTag
t, err := transformers.NewImageTransformer(argsList, p.FieldSpecs)
if err != nil {
return err
}
return t.Transform(m)
return t.Transform(m)
}

View File

@@ -17,7 +17,7 @@ type NameTransformerPlugin struct {
}
func NewNameTransformerPlugin() *NameTransformerPlugin {
return &NameTransformerPlugin{}
return &NameTransformerPlugin{}
}
func (p *NameTransformerPlugin) Config(

View File

@@ -16,7 +16,7 @@ type SecretGeneratorPlugin struct {
}
func NewSecretGeneratorPlugin() *SecretGeneratorPlugin {
return &SecretGeneratorPlugin{}
return &SecretGeneratorPlugin{}
}
func (p *SecretGeneratorPlugin) Config(

111
plugin/plugintestenv.go Normal file
View File

@@ -0,0 +1,111 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package plugin
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"sigs.k8s.io/kustomize/pkg/pgmconfig"
"sigs.k8s.io/kustomize/pkg/plugins"
)
// PluginTestEnv manages the plugin test environment.
// It sets/resets XDG_CONFIG_HOME, makes/removes a temp objRoot.
type PluginTestEnv struct {
t *testing.T
compiler *plugins.Compiler
workDir string
oldXdg string
wasSet bool
}
func NewPluginTestEnv(t *testing.T) *PluginTestEnv {
return &PluginTestEnv{t: t}
}
func (x *PluginTestEnv) Set() *PluginTestEnv {
x.createWorkDir()
x.compiler = x.makeCompiler()
x.setEnv()
return x
}
func (x *PluginTestEnv) Reset() {
x.resetEnv()
x.removeWorkDir()
}
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 *PluginTestEnv) BuildExecPlugin(name ...string) {
obj := filepath.Join(
append([]string{x.compiler.ObjRoot()}, name...)...)
src := filepath.Join(
append([]string{x.compiler.SrcRoot()}, name...)...)
if err := os.MkdirAll(filepath.Dir(obj), 0755); err != nil {
x.t.Errorf("error making directory: %s", filepath.Dir(obj))
}
cmd := exec.Command("cp", src, obj)
cmd.Env = os.Environ()
if err := cmd.Run(); err != nil {
x.t.Errorf("error copying %s to %s: %v", src, obj, err)
}
}
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
// $objRoot
// so set things up accordingly.
objRoot := filepath.Join(
x.workDir, pgmconfig.ProgramName, pgmconfig.PluginRoot)
err := os.MkdirAll(objRoot, os.ModePerm)
if err != nil {
x.t.Error(err)
}
srcRoot, err := plugins.DefaultSrcRoot()
if err != nil {
x.t.Error(err)
}
return plugins.NewCompiler(srcRoot, objRoot)
}
func (x *PluginTestEnv) createWorkDir() {
var err error
x.workDir, err = ioutil.TempDir("", "kustomize-plugin-tests")
if err != nil {
x.t.Errorf("failed to make work dir: %v", err)
}
}
func (x *PluginTestEnv) removeWorkDir() {
err := os.RemoveAll(x.workDir)
if err != nil {
x.t.Errorf(
"removing work dir: %s %v", x.workDir, err)
}
}
func (x *PluginTestEnv) setEnv() {
x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XDG_CONFIG_HOME)
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.workDir)
}
func (x *PluginTestEnv) resetEnv() {
if x.wasSet {
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.oldXdg)
} else {
os.Unsetenv(pgmconfig.XDG_CONFIG_HOME)
}
}

View File

@@ -1,20 +1,7 @@
// +build notravis
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Disabled on travis, because don't want to install helm on travis.
@@ -23,9 +10,8 @@ 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/plugin"
)
// This test requires having the helm binary on the PATH.
@@ -33,14 +19,13 @@ import (
// TODO: Download and inflate the chart, and check that
// in for the test.
func TestChartInflator(t *testing.T) {
tc := plugintest_test.NewPluginTestEnv(t).Set()
tc := plugin.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildExecPlugin(
"someteam.example.com", "v1", "ChartInflator")
th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig())
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
m := th.LoadAndRunGenerator(`
apiVersion: someteam.example.com/v1

View File

@@ -1,34 +1,21 @@
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
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"
"sigs.k8s.io/kustomize/plugin"
)
func TestSedTransformer(t *testing.T) {
tc := plugintest_test.NewPluginTestEnv(t).Set()
tc := plugin.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildExecPlugin("someteam.example.com", "v1", "SedTransformer")
th := kusttest_test.NewKustTestHarnessFull(
t, "/app", loader.RestrictionRootOnly, plugin.ActivePluginConfig())
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
th.WriteF("/app/sed-input.txt", `
s/$FRUIT/orange/g

View File

@@ -1,20 +1,7 @@
// +build plugin
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package main

View File

@@ -1,38 +1,23 @@
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
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/plugin"
)
func TestSomeServiceGeneratorPlugin(t *testing.T) {
tc := plugintest_test.NewPluginTestEnv(t).Set()
tc := plugin.NewPluginTestEnv(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(
"someteam.example.com", "v1", "SomeServiceGenerator")
th := kusttest_test.NewKustTestHarnessWithPluginConfig(
t, "/app", plugin.ActivePluginConfig())
th := kusttest_test.NewKustTestPluginHarness(t, "/app")
m := th.LoadAndRunGenerator(`
apiVersion: someteam.example.com/v1

View File

@@ -1,20 +1,7 @@
// +build plugin
/*
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.
*/
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package main
@@ -43,7 +30,7 @@ func (p *plugin) Config(
func (p *plugin) Transform(m resmap.ResMap) error {
tr, err := transformers.NewNamePrefixSuffixTransformer(
p.Metadata.Name + "-", "",
p.Metadata.Name+"-", "",
config.MakeDefaultConfig().NamePrefix)
if err != nil {
return err