Improve error handling during var resolution.

This commit is contained in:
jregan
2019-02-24 10:17:11 -08:00
committed by Jeffrey Regan
parent f8c80b7335
commit 6bfd7cff72
8 changed files with 150 additions and 44 deletions

View File

@@ -46,15 +46,6 @@ func (m ResMap) GetMatchingIds(matches IdMatcher) []resid.ResId {
return result
}
// 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
}
return nil, false
}
// EncodeAsYaml encodes a ResMap to YAML; encoded objects separated by `---`.
func (m ResMap) EncodeAsYaml() ([]byte, error) {
var ids []resid.ResId

View File

@@ -92,35 +92,34 @@ func TestDemandOneGvknMatchForId(t *testing.T) {
}),
}
_, ok := rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1"))
if !ok {
t.Fatal("Expected single map entry but got none")
result := rm1.GetMatchingIds(
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix1", "ns1").GvknEquals)
if len(result) != 1 {
t.Fatalf("Expected single map entry but got %v", result)
}
// confirm that ns and prefix are not included in match
_, ok = rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix", "ns"))
if !ok {
t.Fatal("Expected single map entry but got none")
result = rm1.GetMatchingIds(
resid.NewResIdWithPrefixNamespace(cmap, "cm2", "prefix", "ns").GvknEquals)
if len(result) != 1 {
t.Fatalf("Expected single map entry but got %v", result)
}
// confirm that name is matched correctly
result, ok := rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns1"))
if ok {
result = rm1.GetMatchingIds(
resid.NewResIdWithPrefixNamespace(cmap, "cm3", "prefix1", "ns1").GvknEquals)
if len(result) > 0 {
t.Fatalf("Expected no map entries but got %v", result)
}
cmap2 := gvk.Gvk{Version: "v2", Kind: "ConfigMap"}
// confirm that gvk is matched correctly
result, ok = rm1.DemandOneGvknMatchForId(
resid.NewResIdWithPrefixNamespace(cmap2, "cm2", "prefix1", "ns1"))
if ok {
result = rm1.GetMatchingIds(
resid.NewResIdWithPrefixNamespace(cmap2, "cm2", "prefix1", "ns1").GvknEquals)
if len(result) > 0 {
t.Fatalf("Expected no map entries but got %v", result)
}
}
func TestFilterBy(t *testing.T) {