Add a plugin loader test.

This commit is contained in:
Jeffrey Regan
2019-04-22 14:26:00 -07:00
parent f123380917
commit 1545e07dd6
7 changed files with 128 additions and 64 deletions

View File

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

View File

@@ -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

View File

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