Add tests in support of Issue 3489.

This commit is contained in:
monopole
2021-02-02 18:22:33 -08:00
parent 3892e3c910
commit aae2be1a79
8 changed files with 130 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ package resource
import (
"fmt"
"log"
"reflect"
"strings"
@@ -293,7 +294,7 @@ func (r *Resource) GetOutermostNameSuffix() string {
return nameSuffixes[len(nameSuffixes)-1]
}
func sameEndingSubarray(a, b []string) bool {
func SameEndingSubarray(a, b []string) bool {
compareLen := len(b)
if len(a) < len(b) {
compareLen = len(a)
@@ -340,16 +341,8 @@ func (r *Resource) OutermostPrefixSuffixEquals(o ResCtx) bool {
// PrefixesSuffixesEquals is conceptually doing the same task
// as OutermostPrefixSuffix but performs a deeper comparison
// of the suffix and prefix slices.
//
// Important note: The PrefixSuffixTransformer is stacking the
// prefix values in the reverse order of appearance in
// the transformed name. For this reason the sameEndingSubarray
// method is used (as opposed to the sameBeginningSubarray)
// to compare the prefix slice. In the same spirit, the
// GetOutermostNamePrefix is using the last element of the
// nameprefix slice and not the first.
func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
return sameEndingSubarray(r.GetNamePrefixes(), o.GetNamePrefixes()) && sameEndingSubarray(r.GetNameSuffixes(), o.GetNameSuffixes())
return SameEndingSubarray(r.GetNamePrefixes(), o.GetNamePrefixes()) && SameEndingSubarray(r.GetNameSuffixes(), o.GetNameSuffixes())
}
// RemoveBuildAnnotations removes annotations created by the build process.
@@ -433,6 +426,15 @@ func (r *Resource) AsYAML() ([]byte, error) {
return yaml.JSONToYAML(json)
}
// MustYaml returns YAML or panics.
func (r *Resource) MustYaml() string {
yml, err := r.AsYAML()
if err != nil {
log.Fatal(err)
}
return string(yml)
}
// SetOptions updates the generator options for the resource.
func (r *Resource) SetOptions(o *types.GenArgs) {
r.options = o

View File

@@ -1177,3 +1177,55 @@ spec:
name: nginx
`, imagename)
}
func TestSameEndingSubarray(t *testing.T) {
testCases := map[string]struct {
a []string
b []string
expected bool
}{
"both nil": {
expected: true,
},
"one nil": {
b: []string{},
expected: true,
},
"both empty": {
a: []string{},
b: []string{},
expected: true,
},
"no1 - TODO(3489) this should be false": {
a: []string{"a"},
b: []string{},
expected: true,
},
"no2": {
a: []string{"b", "a"},
b: []string{"b"},
expected: false,
},
"yes1": {
a: []string{"a", "b"},
b: []string{"b"},
expected: true,
},
"yes2": {
a: []string{"a", "b", "c"},
b: []string{"b", "c"},
expected: true,
},
"yes3": {
a: []string{"a", "b", "c", "d", "e", "f"},
b: []string{"f"},
expected: true,
},
}
for n := range testCases {
tc := testCases[n]
t.Run(n, func(t *testing.T) {
assert.Equal(t, tc.expected, SameEndingSubarray(tc.a, tc.b))
})
}
}