Skip to content

Commit

Permalink
Merge pull request #597 from gautierdelorme/fix-panic-missing-inventory
Browse files Browse the repository at this point in the history
fix: do not panic when the inventory object is missing
  • Loading branch information
k8s-ci-robot committed Jul 26, 2022
2 parents 4dd01e9 + 38ab8f9 commit 7c25ad6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
4 changes: 4 additions & 0 deletions cmd/status/cmdstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions pkg/inventory/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,29 +256,29 @@ 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},
expectedInv: inventoryObj,
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},
Expand Down
16 changes: 8 additions & 8 deletions pkg/inventory/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down

0 comments on commit 7c25ad6

Please sign in to comment.