Skip to content

Commit

Permalink
Switch to black box testing of KustTarget and Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed Feb 12, 2019
1 parent 74d3e92 commit 48717f3
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 67 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/build/build.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (o *Options) RunBuild(
return err return err
} }
defer ldr.Cleanup() defer ldr.Cleanup()
kt, err := target.NewKustTarget(ldr, fSys, rf, ptf) kt, err := target.NewKustTarget(ldr, rf, ptf)
if err != nil { if err != nil {
return err return err
} }
Expand Down
3 changes: 2 additions & 1 deletion pkg/resource/factory_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package resource package resource_test


import ( import (
"reflect" "reflect"
"testing" "testing"


"sigs.k8s.io/kustomize/pkg/internal/loadertest" "sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/patch" "sigs.k8s.io/kustomize/pkg/patch"
. "sigs.k8s.io/kustomize/pkg/resource"
) )


func TestSliceFromPatches(t *testing.T) { func TestSliceFromPatches(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/resource/resource.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (r *Resource) String() string {
if err != nil { if err != nil {
return "<" + err.Error() + ">" return "<" + err.Error() + ">"
} }
return strings.TrimSpace(string(bs)) return strings.TrimSpace(string(bs)) + r.options.String()
} }


// DeepCopy returns a new copy of resource // DeepCopy returns a new copy of resource
Expand Down
13 changes: 8 additions & 5 deletions pkg/resource/resource_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package resource package resource_test


import ( import (
"testing" "testing"


"sigs.k8s.io/kustomize/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resid"
. "sigs.k8s.io/kustomize/pkg/resource"
) )


var factory = NewFactory( var factory = NewFactory(
Expand All @@ -37,7 +38,9 @@ var testConfigMap = factory.FromMap(
}, },
}) })


const testConfigMapString = `{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie","namespace":"hundred-acre-wood"}}` const genArgOptions = "{nsfx:false,beh:unspecified}"

const configMapAsString = `{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie","namespace":"hundred-acre-wood"}}`


var testDeployment = factory.FromMap( var testDeployment = factory.FromMap(
map[string]interface{}{ map[string]interface{}{
Expand All @@ -48,7 +51,7 @@ var testDeployment = factory.FromMap(
}, },
}) })


const testDeploymentString = `{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"pooh"}}` const deploymentAsString = `{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"pooh"}}`


func TestResourceString(t *testing.T) { func TestResourceString(t *testing.T) {
tests := []struct { tests := []struct {
Expand All @@ -57,11 +60,11 @@ func TestResourceString(t *testing.T) {
}{ }{
{ {
in: testConfigMap, in: testConfigMap,
s: testConfigMapString, s: configMapAsString + genArgOptions,
}, },
{ {
in: testDeployment, in: testDeployment,
s: testDeploymentString, s: deploymentAsString + genArgOptions,
}, },
} }
for _, test := range tests { for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/target/baseandoverlaymedium_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"testing" "testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/target/baseandoverlaysmall_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"testing" "testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/target/configmaps_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"testing" "testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/target/crd_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"testing" "testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/target/customconfig_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"testing" "testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/target/generatormergeandreplace_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"testing" "testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/target/generatoroptions_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"testing" "testing"
Expand Down
16 changes: 6 additions & 10 deletions pkg/target/kusttarget.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/pkg/errors" "github.com/pkg/errors"
"sigs.k8s.io/kustomize/pkg/constants" "sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/ifc/transformer" "sigs.k8s.io/kustomize/pkg/ifc/transformer"
interror "sigs.k8s.io/kustomize/pkg/internal/error" interror "sigs.k8s.io/kustomize/pkg/internal/error"
Expand All @@ -42,15 +41,13 @@ import (
type KustTarget struct { type KustTarget struct {
kustomization *types.Kustomization kustomization *types.Kustomization
ldr ifc.Loader ldr ifc.Loader
fSys fs.FileSystem
rFactory *resmap.Factory rFactory *resmap.Factory
tFactory transformer.Factory tFactory transformer.Factory
} }


// NewKustTarget returns a new instance of KustTarget primed with a Loader. // NewKustTarget returns a new instance of KustTarget primed with a Loader.
func NewKustTarget( func NewKustTarget(
ldr ifc.Loader, ldr ifc.Loader,
fSys fs.FileSystem,
rFactory *resmap.Factory, rFactory *resmap.Factory,
tFactory transformer.Factory) (*KustTarget, error) { tFactory transformer.Factory) (*KustTarget, error) {
content, err := loadKustFile(ldr) content, err := loadKustFile(ldr)
Expand All @@ -70,7 +67,6 @@ func NewKustTarget(
return &KustTarget{ return &KustTarget{
kustomization: &k, kustomization: &k,
ldr: ldr, ldr: ldr,
fSys: fSys,
rFactory: rFactory, rFactory: rFactory,
tFactory: tFactory, tFactory: tFactory,
}, nil }, nil
Expand Down Expand Up @@ -123,7 +119,7 @@ func unmarshal(y []byte, o interface{}) error {
// MakeCustomizedResMap creates a ResMap per kustomization instructions. // MakeCustomizedResMap creates a ResMap per kustomization instructions.
// The Resources in the returned ResMap are fully customized. // The Resources in the returned ResMap are fully customized.
func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) { func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
ra, err := kt.accumulateTarget() ra, err := kt.AccumulateTarget()
if err != nil { if err != nil {
return nil, err return nil, err
} }
Expand All @@ -147,11 +143,11 @@ func (kt *KustTarget) shouldAddHashSuffixesToGeneratedResources() bool {
!kt.kustomization.GeneratorOptions.DisableNameSuffixHash !kt.kustomization.GeneratorOptions.DisableNameSuffixHash
} }


// accumulateTarget returns a new ResAccumulator, // AccumulateTarget returns a new ResAccumulator,
// holding customized resources and the data/rules used // holding customized resources and the data/rules used
// to do so. The name back references and vars are // to do so. The name back references and vars are
// not yet fixed. // not yet fixed.
func (kt *KustTarget) accumulateTarget() ( func (kt *KustTarget) AccumulateTarget() (
ra *ResAccumulator, err error) { ra *ResAccumulator, err error) {
// TODO(monopole): Get rid of the KustomizationErrors accumulator. // TODO(monopole): Get rid of the KustomizationErrors accumulator.
// It's not consistently used, and complicates tests. // It's not consistently used, and complicates tests.
Expand Down Expand Up @@ -249,15 +245,15 @@ func (kt *KustTarget) accumulateBases() (
continue continue
} }
subKt, err := NewKustTarget( subKt, err := NewKustTarget(
ldr, kt.fSys, kt.rFactory, kt.tFactory) ldr, kt.rFactory, kt.tFactory)
if err != nil { if err != nil {
errs.Append(errors.Wrap(err, "couldn't make target for "+path)) errs.Append(errors.Wrap(err, "couldn't make target for "+path))
ldr.Cleanup() ldr.Cleanup()
continue continue
} }
subRa, err := subKt.accumulateTarget() subRa, err := subKt.AccumulateTarget()
if err != nil { if err != nil {
errs.Append(errors.Wrap(err, "accumulateTarget")) errs.Append(errors.Wrap(err, "AccumulateTarget"))
ldr.Cleanup() ldr.Cleanup()
continue continue
} }
Expand Down
48 changes: 27 additions & 21 deletions pkg/target/kusttarget_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */


package target package target_test


import ( import (
"encoding/base64" "encoding/base64"
Expand All @@ -23,18 +23,18 @@ import (
"strings" "strings"
"testing" "testing"


"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/gvk" "sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
"sigs.k8s.io/kustomize/pkg/internal/loadertest" "sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resid"
"sigs.k8s.io/kustomize/pkg/resmap" "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource" "sigs.k8s.io/kustomize/pkg/resource"
. "sigs.k8s.io/kustomize/pkg/target"
"sigs.k8s.io/kustomize/pkg/types" "sigs.k8s.io/kustomize/pkg/types"
) )


const ( const (
kustomizationContent1 = ` kustomizationContent = `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
namePrefix: foo- namePrefix: foo-
Expand All @@ -47,6 +47,8 @@ commonAnnotations:
resources: resources:
- deployment.yaml - deployment.yaml
- namespace.yaml - namespace.yaml
generatorOptions:
disableNameSuffixHash: false
configMapGenerator: configMapGenerator:
- name: literalConfigMap - name: literalConfigMap
literals: literals:
Expand Down Expand Up @@ -83,9 +85,9 @@ metadata:
]` ]`
) )


func TestResources1(t *testing.T) { func TestResources(t *testing.T) {
th := NewKustTestHarness(t, "/whatever") th := NewKustTestHarness(t, "/whatever")
th.writeK("/whatever/", kustomizationContent1) th.writeK("/whatever/", kustomizationContent)
th.writeF("/whatever/deployment.yaml", deploymentContent) th.writeF("/whatever/deployment.yaml", deploymentContent)
th.writeF("/whatever/namespace.yaml", namespaceContent) th.writeF("/whatever/namespace.yaml", namespaceContent)
th.writeF("/whatever/jsonpatch.json", jsonpatchContent) th.writeF("/whatever/jsonpatch.json", jsonpatchContent)
Expand Down Expand Up @@ -146,7 +148,9 @@ func TestResources1(t *testing.T) {
"DB_USERNAME": "admin", "DB_USERNAME": "admin",
"DB_PASSWORD": "somepw", "DB_PASSWORD": "somepw",
}, },
}, &types.GeneratorArgs{}, nil), },
&types.GeneratorArgs{},
&types.GeneratorOptions{}),
resid.NewResIdWithPrefixSuffixNamespace( resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Version: "v1", Kind: "Secret"}, gvk.Gvk{Version: "v1", Kind: "Secret"},
"secret", "foo-", "-bar", "ns1"): th.fromMapAndOption( "secret", "foo-", "-bar", "ns1"): th.fromMapAndOption(
Expand All @@ -168,7 +172,9 @@ func TestResources1(t *testing.T) {
"DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")), "DB_USERNAME": base64.StdEncoding.EncodeToString([]byte("admin")),
"DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")), "DB_PASSWORD": base64.StdEncoding.EncodeToString([]byte("somepw")),
}, },
}, &types.GeneratorArgs{}, nil), },
&types.GeneratorArgs{},
&types.GeneratorOptions{}),
resid.NewResIdWithPrefixSuffixNamespace( resid.NewResIdWithPrefixSuffixNamespace(
gvk.Gvk{Version: "v1", Kind: "Namespace"}, gvk.Gvk{Version: "v1", Kind: "Namespace"},
"ns1", "foo-", "-bar", ""): th.fromMap( "ns1", "foo-", "-bar", ""): th.fromMap(
Expand Down Expand Up @@ -198,8 +204,7 @@ func TestResources1(t *testing.T) {
} }


func TestKustomizationNotFound(t *testing.T) { func TestKustomizationNotFound(t *testing.T) {
_, err := NewKustTarget( _, err := NewKustTarget(loadertest.NewFakeLoader("/foo"), nil, nil)
loadertest.NewFakeLoader("/foo"), fs.MakeFakeFS(), nil, nil)
if err == nil { if err == nil {
t.Fatalf("expected an error") t.Fatalf("expected an error")
} }
Expand All @@ -211,7 +216,7 @@ func TestKustomizationNotFound(t *testing.T) {


func TestResourceNotFound(t *testing.T) { func TestResourceNotFound(t *testing.T) {
th := NewKustTestHarness(t, "/whatever") th := NewKustTestHarness(t, "/whatever")
th.writeK("/whatever", kustomizationContent1) th.writeK("/whatever", kustomizationContent)
_, err := th.makeKustTarget().MakeCustomizedResMap() _, err := th.makeKustTarget().MakeCustomizedResMap()
if err == nil { if err == nil {
t.Fatalf("Didn't get the expected error for an unknown resource") t.Fatalf("Didn't get the expected error for an unknown resource")
Expand All @@ -232,13 +237,12 @@ func findSecret(m resmap.ResMap) *resource.Resource {


func TestDisableNameSuffixHash(t *testing.T) { func TestDisableNameSuffixHash(t *testing.T) {
th := NewKustTestHarness(t, "/whatever") th := NewKustTestHarness(t, "/whatever")
th.writeK("/whatever/", kustomizationContent1) th.writeK("/whatever/", kustomizationContent)
th.writeF("/whatever/deployment.yaml", deploymentContent) th.writeF("/whatever/deployment.yaml", deploymentContent)
th.writeF("/whatever/namespace.yaml", namespaceContent) th.writeF("/whatever/namespace.yaml", namespaceContent)
th.writeF("/whatever/jsonpatch.json", jsonpatchContent) th.writeF("/whatever/jsonpatch.json", jsonpatchContent)


kt := th.makeKustTarget() m, err := th.makeKustTarget().MakeCustomizedResMap()
m, err := kt.MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("unexpected Resources error %v", err) t.Fatalf("unexpected Resources error %v", err)
} }
Expand All @@ -250,9 +254,11 @@ func TestDisableNameSuffixHash(t *testing.T) {
t.Errorf("unexpected secret resource name: %s", secret.GetName()) t.Errorf("unexpected secret resource name: %s", secret.GetName())
} }


kt.kustomization.GeneratorOptions = &types.GeneratorOptions{ th.writeK("/whatever/",
DisableNameSuffixHash: true} strings.Replace(kustomizationContent,
m, err = kt.MakeCustomizedResMap() "disableNameSuffixHash: false",
"disableNameSuffixHash: true", -1))
m, err = th.makeKustTarget().MakeCustomizedResMap()
if err != nil { if err != nil {
t.Fatalf("unexpected Resources error %v", err) t.Fatalf("unexpected Resources error %v", err)
} }
Expand Down Expand Up @@ -338,11 +344,11 @@ vars:
name: heron name: heron
apiVersion: v300 apiVersion: v300
`) `)
ra, err := th.makeKustTarget().accumulateTarget() ra, err := th.makeKustTarget().AccumulateTarget()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
vars := ra.varSet.Set() vars := ra.Vars()
if len(vars) != 2 { if len(vars) != 2 {
t.Fatalf("unexpected size %d", len(vars)) t.Fatalf("unexpected size %d", len(vars))
} }
Expand Down Expand Up @@ -388,11 +394,11 @@ vars:
bases: bases:
- ../o1 - ../o1
`) `)
ra, err := th.makeKustTarget().accumulateTarget() ra, err := th.makeKustTarget().AccumulateTarget()
if err != nil { if err != nil {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
vars := ra.varSet.Set() vars := ra.Vars()
if len(vars) != 4 { if len(vars) != 4 {
for i, v := range vars { for i, v := range vars {
fmt.Printf("%v: %v\n", i, v) fmt.Printf("%v: %v\n", i, v)
Expand Down Expand Up @@ -441,7 +447,7 @@ vars:
bases: bases:
- ../o1 - ../o1
`) `)
_, err := th.makeKustTarget().accumulateTarget() _, err := th.makeKustTarget().AccumulateTarget()
if err == nil { if err == nil {
t.Fatalf("expected var collision") t.Fatalf("expected var collision")
} }
Expand Down
Loading

0 comments on commit 48717f3

Please sign in to comment.