Skip to content

Commit 7c25ad6

Browse files
authored
Merge pull request #597 from gautierdelorme/fix-panic-missing-inventory
fix: do not panic when the inventory object is missing
2 parents 4dd01e9 + 38ab8f9 commit 7c25ad6

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

cmd/status/cmdstatus_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func TestCommand(t *testing.T) {
6464
expectedErrMsg string
6565
expectedOutput string
6666
}{
67+
"no inventory template": {
68+
input: "",
69+
expectedErrMsg: "Package uninitialized. Please run \"init\" command.",
70+
},
6771
"no inventory in live state": {
6872
input: inventoryTemplate,
6973
expectedOutput: "no resources found in the inventory\n",

pkg/inventory/inventory_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,29 @@ func TestSplitUnstructureds(t *testing.T) {
256256
expectedObjs []*unstructured.Unstructured
257257
isError bool
258258
}{
259-
"No objects is returns nil and no objects": {
259+
"No objects returns error": {
260260
allObjs: []*unstructured.Unstructured{},
261261
expectedInv: nil,
262262
expectedObjs: []*unstructured.Unstructured{},
263-
isError: false,
263+
isError: true,
264264
},
265265
"Only inventory object returns inv and no objects": {
266266
allObjs: []*unstructured.Unstructured{inventoryObj},
267267
expectedInv: inventoryObj,
268268
expectedObjs: []*unstructured.Unstructured{},
269269
isError: false,
270270
},
271-
"Single object returns nil inventory and object": {
272-
allObjs: []*unstructured.Unstructured{pod1},
273-
expectedInv: nil,
271+
"Inventory object with single object returns inventory and object": {
272+
allObjs: []*unstructured.Unstructured{inventoryObj, pod1},
273+
expectedInv: inventoryObj,
274274
expectedObjs: []*unstructured.Unstructured{pod1},
275275
isError: false,
276276
},
277-
"Multiple non-inventory objects returns nil inventory and objs": {
277+
"Multiple non-inventory objects returns error": {
278278
allObjs: []*unstructured.Unstructured{pod1, pod2, pod3},
279279
expectedInv: nil,
280280
expectedObjs: []*unstructured.Unstructured{pod1, pod2, pod3},
281-
isError: false,
281+
isError: true,
282282
},
283283
"Inventory object with multiple others splits correctly": {
284284
allObjs: []*unstructured.Unstructured{pod1, pod2, inventoryObj, pod3},

pkg/inventory/storage.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ func ValidateNoInventory(objs object.UnstructuredSet) error {
110110

111111
// splitUnstructureds takes a set of unstructured.Unstructured objects and
112112
// splits it into one set that contains the inventory object templates and
113-
// another one that contains the remaining resources. If there is no inventory
114-
// object the first return value is nil. Returns an error if there are
115-
// more than one inventory objects.
113+
// another one that contains the remaining resources. Returns an error if there
114+
// there is no inventory object or more than one inventory objects.
116115
func SplitUnstructureds(objs object.UnstructuredSet) (*unstructured.Unstructured, object.UnstructuredSet, error) {
117116
invs := make(object.UnstructuredSet, 0)
118117
resources := make(object.UnstructuredSet, 0)
@@ -125,12 +124,13 @@ func SplitUnstructureds(objs object.UnstructuredSet) (*unstructured.Unstructured
125124
}
126125
var inv *unstructured.Unstructured
127126
var err error
128-
if len(invs) == 1 {
127+
switch len(invs) {
128+
case 0:
129+
err = &NoInventoryObjError{}
130+
case 1:
129131
inv = invs[0]
130-
} else if len(invs) > 1 {
131-
err = &MultipleInventoryObjError{
132-
InventoryObjectTemplates: invs,
133-
}
132+
default:
133+
err = &MultipleInventoryObjError{InventoryObjectTemplates: invs}
134134
}
135135
return inv, resources, err
136136
}

0 commit comments

Comments
 (0)