Switch to black box testing of KustTarget and Resource

This commit is contained in:
Jeffrey Regan
2019-02-11 15:23:26 -08:00
parent 74d3e92b55
commit 48717f3f30
24 changed files with 136 additions and 67 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -26,7 +26,6 @@ import (
"github.com/ghodss/yaml"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/ifc/transformer"
interror "sigs.k8s.io/kustomize/pkg/internal/error"
@@ -42,7 +41,6 @@ import (
type KustTarget struct {
kustomization *types.Kustomization
ldr ifc.Loader
fSys fs.FileSystem
rFactory *resmap.Factory
tFactory transformer.Factory
}
@@ -50,7 +48,6 @@ type KustTarget struct {
// NewKustTarget returns a new instance of KustTarget primed with a Loader.
func NewKustTarget(
ldr ifc.Loader,
fSys fs.FileSystem,
rFactory *resmap.Factory,
tFactory transformer.Factory) (*KustTarget, error) {
content, err := loadKustFile(ldr)
@@ -70,7 +67,6 @@ func NewKustTarget(
return &KustTarget{
kustomization: &k,
ldr: ldr,
fSys: fSys,
rFactory: rFactory,
tFactory: tFactory,
}, nil
@@ -123,7 +119,7 @@ func unmarshal(y []byte, o interface{}) error {
// MakeCustomizedResMap creates a ResMap per kustomization instructions.
// The Resources in the returned ResMap are fully customized.
func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
ra, err := kt.accumulateTarget()
ra, err := kt.AccumulateTarget()
if err != nil {
return nil, err
}
@@ -147,11 +143,11 @@ func (kt *KustTarget) shouldAddHashSuffixesToGeneratedResources() bool {
!kt.kustomization.GeneratorOptions.DisableNameSuffixHash
}
// accumulateTarget returns a new ResAccumulator,
// AccumulateTarget returns a new ResAccumulator,
// holding customized resources and the data/rules used
// to do so. The name back references and vars are
// not yet fixed.
func (kt *KustTarget) accumulateTarget() (
func (kt *KustTarget) AccumulateTarget() (
ra *ResAccumulator, err error) {
// TODO(monopole): Get rid of the KustomizationErrors accumulator.
// It's not consistently used, and complicates tests.
@@ -249,15 +245,15 @@ func (kt *KustTarget) accumulateBases() (
continue
}
subKt, err := NewKustTarget(
ldr, kt.fSys, kt.rFactory, kt.tFactory)
ldr, kt.rFactory, kt.tFactory)
if err != nil {
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
ldr.Cleanup()
continue
}
subRa, err := subKt.accumulateTarget()
subRa, err := subKt.AccumulateTarget()
if err != nil {
errs.Append(errors.Wrap(err, "accumulateTarget"))
errs.Append(errors.Wrap(err, "AccumulateTarget"))
ldr.Cleanup()
continue
}

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package target
package target_test
import (
"encoding/base64"
@@ -23,18 +23,18 @@ import (
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
. "sigs.k8s.io/kustomize/pkg/target"
"sigs.k8s.io/kustomize/pkg/types"
)
const (
kustomizationContent1 = `
kustomizationContent = `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namePrefix: foo-
@@ -47,6 +47,8 @@ commonAnnotations:
resources:
- deployment.yaml
- namespace.yaml
generatorOptions:
disableNameSuffixHash: false
configMapGenerator:
- name: literalConfigMap
literals:
@@ -83,9 +85,9 @@ metadata:
]`
)
func TestResources1(t *testing.T) {
func TestResources(t *testing.T) {
th := NewKustTestHarness(t, "/whatever")
th.writeK("/whatever/", kustomizationContent1)
th.writeK("/whatever/", kustomizationContent)
th.writeF("/whatever/deployment.yaml", deploymentContent)
th.writeF("/whatever/namespace.yaml", namespaceContent)
th.writeF("/whatever/jsonpatch.json", jsonpatchContent)
@@ -146,7 +148,9 @@ func TestResources1(t *testing.T) {
"DB_USERNAME": "admin",
"DB_PASSWORD": "somepw",
},
}, &types.GeneratorArgs{}, nil),
},
&types.GeneratorArgs{},
&types.GeneratorOptions{}),
resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Version: "v1", Kind: "Secret"},
"secret", "foo-", "-bar", "ns1"): th.fromMapAndOption(
@@ -168,7 +172,9 @@ func TestResources1(t *testing.T) {
"DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")),
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
},
}, &types.GeneratorArgs{}, nil),
},
&types.GeneratorArgs{},
&types.GeneratorOptions{}),
resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Version: "v1", Kind: "Namespace"},
"ns1", "foo-", "-bar", ""): th.fromMap(
@@ -198,8 +204,7 @@ func TestResources1(t *testing.T) {
}
func TestKustomizationNotFound(t *testing.T) {
_, err := NewKustTarget(
loadertest.NewFakeLoader("/foo"), fs.MakeFakeFS(), nil, nil)
_, err := NewKustTarget(loadertest.NewFakeLoader("/foo"), nil, nil)
if err == nil {
t.Fatalf("expected an error")
}
@@ -211,7 +216,7 @@ func TestKustomizationNotFound(t *testing.T) {
func TestResourceNotFound(t *testing.T) {
th := NewKustTestHarness(t, "/whatever")
th.writeK("/whatever", kustomizationContent1)
th.writeK("/whatever", kustomizationContent)
_, err := th.makeKustTarget().MakeCustomizedResMap()
if err == nil {
t.Fatalf("Didn't get the expected error for an unknown resource")
@@ -232,13 +237,12 @@ func findSecret(m resmap.ResMap) *resource.Resource {
func TestDisableNameSuffixHash(t *testing.T) {
th := NewKustTestHarness(t, "/whatever")
th.writeK("/whatever/", kustomizationContent1)
th.writeK("/whatever/", kustomizationContent)
th.writeF("/whatever/deployment.yaml", deploymentContent)
th.writeF("/whatever/namespace.yaml", namespaceContent)
th.writeF("/whatever/jsonpatch.json", jsonpatchContent)
kt := th.makeKustTarget()
m, err := kt.MakeCustomizedResMap()
m, err := th.makeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("unexpected Resources error %v", err)
}
@@ -250,9 +254,11 @@ func TestDisableNameSuffixHash(t *testing.T) {
t.Errorf("unexpected secret resource name: %s", secret.GetName())
}
kt.kustomization.GeneratorOptions = &types.GeneratorOptions{
DisableNameSuffixHash: true}
m, err = kt.MakeCustomizedResMap()
th.writeK("/whatever/",
strings.Replace(kustomizationContent,
"disableNameSuffixHash: false",
"disableNameSuffixHash: true", -1))
m, err = th.makeKustTarget().MakeCustomizedResMap()
if err != nil {
t.Fatalf("unexpected Resources error %v", err)
}
@@ -338,11 +344,11 @@ vars:
name: heron
apiVersion: v300
`)
ra, err := th.makeKustTarget().accumulateTarget()
ra, err := th.makeKustTarget().AccumulateTarget()
if err != nil {
t.Fatalf("Err: %v", err)
}
vars := ra.varSet.Set()
vars := ra.Vars()
if len(vars) != 2 {
t.Fatalf("unexpected size %d", len(vars))
}
@@ -388,11 +394,11 @@ vars:
bases:
- ../o1
`)
ra, err := th.makeKustTarget().accumulateTarget()
ra, err := th.makeKustTarget().AccumulateTarget()
if err != nil {
t.Fatalf("Err: %v", err)
}
vars := ra.varSet.Set()
vars := ra.Vars()
if len(vars) != 4 {
for i, v := range vars {
fmt.Printf("%v: %v\n", i, v)
@@ -441,7 +447,7 @@ vars:
bases:
- ../o1
`)
_, err := th.makeKustTarget().accumulateTarget()
_, err := th.makeKustTarget().AccumulateTarget()
if err == nil {
t.Fatalf("expected var collision")
}

View File

@@ -14,24 +14,24 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package target
package target_test
// A collection of utilities used in target tests.
import (
"fmt"
"path/filepath"
"sigs.k8s.io/kustomize/pkg/transformers/config/defaultconfig"
"strings"
"testing"
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/k8sdeps/transformer"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
. "sigs.k8s.io/kustomize/pkg/target"
"sigs.k8s.io/kustomize/pkg/transformers/config/defaultconfig"
"sigs.k8s.io/kustomize/pkg/types"
)
@@ -50,17 +50,8 @@ func NewKustTestHarness(t *testing.T, path string) *KustTestHarness {
}
func (th *KustTestHarness) makeKustTarget() *KustTarget {
// Warning: the following filesystem - a fake - must be rooted at /.
// This fs root is used as the working directory for the shell spawned by
// the secretgenerator, and has nothing to do with the filesystem used
// to load relative paths from the fake filesystem.
// This trick only works for secret generator commands that don't actually
// try to read the file system, because these tests don't write to the
// real "/" directory. See use of exec package in the secretfactory.
fakeFs := fs.MakeFakeFS()
fakeFs.Mkdir("/")
kt, err := NewKustTarget(
th.ldr, fakeFs, th.rf, transformer.NewFactoryImpl())
th.ldr, th.rf, transformer.NewFactoryImpl())
if err != nil {
th.t.Fatalf("Unexpected construction error %v", err)
}

View File

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

View File

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

View File

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

View File

@@ -55,6 +55,11 @@ func (ra *ResAccumulator) ResMap() resmap.ResMap {
return result
}
// Vars returns a copy of underlying vars.
func (ra *ResAccumulator) Vars() []types.Var {
return ra.varSet.Set()
}
func (ra *ResAccumulator) MergeResourcesWithErrorOnIdCollision(
resources resmap.ResMap) (err error) {
ra.resMap, err = resmap.MergeWithErrorOnIdCollision(

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package target
package target_test
import (
"strings"
@@ -25,6 +25,7 @@ import (
"sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
. "sigs.k8s.io/kustomize/pkg/target"
"sigs.k8s.io/kustomize/pkg/transformers/config"
"sigs.k8s.io/kustomize/pkg/types"
)

View File

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

View File

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