Add more resid test coverage.

This commit is contained in:
jregan
2019-02-16 11:22:32 -08:00
parent ef3b0672c5
commit d4d993a53c
10 changed files with 411 additions and 121 deletions

View File

@@ -50,7 +50,7 @@ func (rmF *Factory) FromFiles(
if err != nil {
return nil, errors.Wrap(err, "Load from path "+path+" failed")
}
res, err := rmF.newResMapFromBytes(content)
res, err := rmF.NewResMapFromBytes(content)
if err != nil {
return nil, internal.Handler(err, path)
}
@@ -60,7 +60,7 @@ func (rmF *Factory) FromFiles(
}
// newResMapFromBytes decodes a list of objects in byte array format.
func (rmF *Factory) newResMapFromBytes(b []byte) (ResMap, error) {
func (rmF *Factory) NewResMapFromBytes(b []byte) (ResMap, error) {
resources, err := rmF.resF.SliceFromBytes(b)
if err != nil {
return nil, err

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package resmap
package resmap_test
import (
"encoding/base64"
@@ -28,6 +28,7 @@ import (
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/resid"
. "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/types"
)
@@ -124,7 +125,7 @@ metadata:
},
}),
}
m, err := rmF.newResMapFromBytes(encoded)
m, err := rmF.NewResMapFromBytes(encoded)
fmt.Printf("%v\n", m)
if err != nil {
t.Fatalf("unexpected error: %v", err)

View File

@@ -32,20 +32,23 @@ import (
// ResMap is a map from ResId to Resource.
type ResMap map[resid.ResId]*resource.Resource
// FindByGVKN find the matched ResIds by Group/Version/Kind and Name
func (m ResMap) FindByGVKN(inputId resid.ResId) []resid.ResId {
type IdMatcher func(resid.ResId) bool
// GetMatchingIds returns a slice of ResId keys from the map
// that all satisfy the given matcher function.
func (m ResMap) GetMatchingIds(matches IdMatcher) []resid.ResId {
var result []resid.ResId
for id := range m {
if id.GvknEquals(inputId) {
if matches(id) {
result = append(result, id)
}
}
return result
}
// DemandOneMatchForId find the matched resource by Group/Version/Kind and Name
func (m ResMap) DemandOneMatchForId(inputId resid.ResId) (*resource.Resource, bool) {
result := m.FindByGVKN(inputId)
// DemandOneGvknMatchForId find the matched resource by Group/Version/Kind and Name
func (m ResMap) DemandOneGvknMatchForId(inputId resid.ResId) (*resource.Resource, bool) {
result := m.GetMatchingIds(inputId.GvknEquals)
if len(result) == 1 {
return m[result[0]], true
}
@@ -177,7 +180,7 @@ func MergeWithOverride(maps ...ResMap) (ResMap, error) {
continue
}
for id, r := range m {
matchedId := result.FindByGVKN(id)
matchedId := result.GetMatchingIds(id.GvknEquals)
if len(matchedId) == 1 {
id = matchedId[0]
switch r.Behavior() {

View File

@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package resmap
package resmap_test
import (
"reflect"
@@ -23,6 +23,7 @@ import (
"sigs.k8s.io/kustomize/k8sdeps/kunstruct"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resid"
. "sigs.k8s.io/kustomize/pkg/resmap"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/types"
)
@@ -71,7 +72,7 @@ metadata:
}
}
func TestDemandOneMatchForId(t *testing.T) {
func TestDemandOneGvknMatchForId(t *testing.T) {
rm1 := ResMap{
resid.NewResIdWithPrefixNamespace(cmap, "cm1", "prefix1", "ns1"): rf.FromMap(
map[string]interface{}{
@@ -91,19 +92,22 @@ func TestDemandOneMatchForId(t *testing.T) {
}),
}
_, ok := rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"))
_, ok := rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"))
if !ok {
t.Fatal("Expected single map entry but got none")
}
// confirm that ns and prefix are not included in match
_, ok = rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix", "ns"))
_, ok = rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix", "ns"))
if !ok {
t.Fatal("Expected single map entry but got none")
}
// confirm that name is matched correctly
result, ok := rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns1"))
result, ok := rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns1"))
if ok {
t.Fatalf("Expected no map entries but got %v", result)
}
@@ -111,7 +115,8 @@ func TestDemandOneMatchForId(t *testing.T) {
cmap2 := gvk.Gvk{Version: "v2", Kind: "ConfigMap"}
// confirm that gvk is matched correctly
result, ok = rm1.DemandOneMatchForId(resid.NewResIdWithPrefixNamespace(cmap2, "cm2", "prefix1", "ns1"))
result, ok = rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap2, "cm2", "prefix1", "ns1"))
if ok {
t.Fatalf("Expected no map entries but got %v", result)
}
@@ -291,8 +296,73 @@ func TestDeepCopy(t *testing.T) {
}
}
func TestErrorIfNotEqual(t *testing.T) {
func TestGetMatchingIds(t *testing.T) {
// These ids used as map keys.
// They must be different to avoid overwriting
// map entries during construction.
ids := []resid.ResId{
resid.NewResId(
gvk.Gvk{Kind: "vegetable"},
"bedlam"),
resid.NewResId(
gvk.Gvk{Group: "g1", Version: "v1", Kind: "vegetable"},
"domino"),
resid.NewResIdWithPrefixNamespace(
gvk.Gvk{Kind: "vegetable"},
"peter", "p", "happy"),
resid.NewResIdWithPrefixNamespace(
gvk.Gvk{Version: "v1", Kind: "fruit"},
"shatterstar", "p", "happy"),
}
m := ResMap{}
for _, id := range ids {
// Resources values don't matter in this test.
m[id] = nil
}
if len(m) != len(ids) {
t.Fatalf("unexpected map len %d presumably due to duplicate keys", len(m))
}
tests := []struct {
name string
matcher IdMatcher
count int
}{
{
"match everything",
func(resid.ResId) bool { return true },
4,
},
{
"match nothing",
func(resid.ResId) bool { return false },
0,
},
{
"name is peter",
func(x resid.ResId) bool { return x.Name() == "peter" },
1,
},
{
"happy vegetable",
func(x resid.ResId) bool {
return x.Namespace() == "happy" &&
x.Gvk().Kind == "vegetable"
},
1,
},
}
for _, tst := range tests {
result := m.GetMatchingIds(tst.matcher)
if len(result) != tst.count {
t.Fatalf("test '%s'; actual: %d, expected: %d",
tst.name, len(result), tst.count)
}
}
}
func TestErrorIfNotEqual(t *testing.T) {
rm1 := ResMap{
resid.NewResId(cmap, "cm1"): rf.FromMap(
map[string]interface{}{
@@ -436,7 +506,6 @@ func TestMergeWithoutOverride(t *testing.T) {
}
func generateMergeFixtures(b types.GenerationBehavior) []ResMap {
input1 := ResMap{
resid.NewResId(cmap, "cmap"): rf.FromMapAndOption(
map[string]interface{}{