From 38ab8f92b6adba86f491244201250c212d1c8570 Mon Sep 17 00:00:00 2001 From: Gautier Delorme Date: Tue, 26 Jul 2022 15:49:25 +0200 Subject: [PATCH] do not panic when the inventory object is missing Signed-off-by: Gautier Delorme --- cmd/status/cmdstatus_test.go | 4 ++++ pkg/inventory/inventory_test.go | 14 +++++++------- pkg/inventory/storage.go | 16 ++++++++-------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/status/cmdstatus_test.go b/cmd/status/cmdstatus_test.go index a017212d..a1375296 100644 --- a/cmd/status/cmdstatus_test.go +++ b/cmd/status/cmdstatus_test.go @@ -64,6 +64,10 @@ func TestCommand(t *testing.T) { expectedErrMsg string expectedOutput string }{ + "no inventory template": { + input: "", + expectedErrMsg: "Package uninitialized. Please run \"init\" command.", + }, "no inventory in live state": { input: inventoryTemplate, expectedOutput: "no resources found in the inventory\n", diff --git a/pkg/inventory/inventory_test.go b/pkg/inventory/inventory_test.go index 8b687490..eb98bdd0 100644 --- a/pkg/inventory/inventory_test.go +++ b/pkg/inventory/inventory_test.go @@ -256,11 +256,11 @@ func TestSplitUnstructureds(t *testing.T) { expectedObjs []*unstructured.Unstructured isError bool }{ - "No objects is returns nil and no objects": { + "No objects returns error": { allObjs: []*unstructured.Unstructured{}, expectedInv: nil, expectedObjs: []*unstructured.Unstructured{}, - isError: false, + isError: true, }, "Only inventory object returns inv and no objects": { allObjs: []*unstructured.Unstructured{inventoryObj}, @@ -268,17 +268,17 @@ func TestSplitUnstructureds(t *testing.T) { expectedObjs: []*unstructured.Unstructured{}, isError: false, }, - "Single object returns nil inventory and object": { - allObjs: []*unstructured.Unstructured{pod1}, - expectedInv: nil, + "Inventory object with single object returns inventory and object": { + allObjs: []*unstructured.Unstructured{inventoryObj, pod1}, + expectedInv: inventoryObj, expectedObjs: []*unstructured.Unstructured{pod1}, isError: false, }, - "Multiple non-inventory objects returns nil inventory and objs": { + "Multiple non-inventory objects returns error": { allObjs: []*unstructured.Unstructured{pod1, pod2, pod3}, expectedInv: nil, expectedObjs: []*unstructured.Unstructured{pod1, pod2, pod3}, - isError: false, + isError: true, }, "Inventory object with multiple others splits correctly": { allObjs: []*unstructured.Unstructured{pod1, pod2, inventoryObj, pod3}, diff --git a/pkg/inventory/storage.go b/pkg/inventory/storage.go index 7dfb5526..36d5f9a5 100644 --- a/pkg/inventory/storage.go +++ b/pkg/inventory/storage.go @@ -110,9 +110,8 @@ func ValidateNoInventory(objs object.UnstructuredSet) error { // splitUnstructureds takes a set of unstructured.Unstructured objects and // splits it into one set that contains the inventory object templates and -// another one that contains the remaining resources. If there is no inventory -// object the first return value is nil. Returns an error if there are -// more than one inventory objects. +// another one that contains the remaining resources. Returns an error if there +// there is no inventory object or more than one inventory objects. func SplitUnstructureds(objs object.UnstructuredSet) (*unstructured.Unstructured, object.UnstructuredSet, error) { invs := make(object.UnstructuredSet, 0) resources := make(object.UnstructuredSet, 0) @@ -125,12 +124,13 @@ func SplitUnstructureds(objs object.UnstructuredSet) (*unstructured.Unstructured } var inv *unstructured.Unstructured var err error - if len(invs) == 1 { + switch len(invs) { + case 0: + err = &NoInventoryObjError{} + case 1: inv = invs[0] - } else if len(invs) > 1 { - err = &MultipleInventoryObjError{ - InventoryObjectTemplates: invs, - } + default: + err = &MultipleInventoryObjError{InventoryObjectTemplates: invs} } return inv, resources, err }