Module: check_mk
Branch: master
Commit: f28e8833dca96428c507626695840d219e16f02f
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f28e8833dca964…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Wed Jan 16 13:16:47 2019 +0100
6956 FIX docker_node_info: Inventory no longer crashes upon missing info
Change-Id: I8726f59ee878fc8a0d9ce2b6353ac665a7148939
---
.werks/6956 | 10 ++++
inventory/docker_node_info | 35 +++++++------
tests/unit/inventory/test_inv_docker_node_info.py | 62 +++++++++++++++++++++++
3 files changed, 89 insertions(+), 18 deletions(-)
diff --git a/.werks/6956 b/.werks/6956
new file mode 100644
index 0000000..a095990
--- /dev/null
+++ b/.werks/6956
@@ -0,0 +1,10 @@
+Title: docker_node_info: Inventory no longer crashes upon missing info
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.6.0i1
+Date: 1547640973
+Class: fix
+
+
diff --git a/inventory/docker_node_info b/inventory/docker_node_info
index f6de95f..89cf62d 100644
--- a/inventory/docker_node_info
+++ b/inventory/docker_node_info
@@ -25,37 +25,36 @@
# Boston, MA 02110-1301 USA.
+def _try_set(node, nkey, data, dkey):
+ if dkey in data:
+ node[nkey] = data[dkey]
+
+
def inv_docker_node_info(parsed, inventory_tree, status_data_tree):
if not parsed or not isinstance(parsed, dict):
# String with error message in case the daemon is not running
return
node = inventory_tree.get_dict("software.applications.docker.")
- node["version"] = parsed[u"ServerVersion"]
- node["registry"] = parsed["IndexServerAddress"]
+ _try_set(node, "version", parsed, u"ServerVersion")
+ _try_set(node, "registry", parsed, "IndexServerAddress")
#
{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null}
- if parsed["Swarm"]["LocalNodeState"] == "inactive":
- node["swarm_state"] =
parsed["Swarm"]["LocalNodeState"]
- else:
- node.update({
- "swarm_state":
parsed["Swarm"]["LocalNodeState"],
- "swarm_manager":
parsed["Swarm"]["RemoteManagers"],
- "swarm_node_id": parsed["Swarm"]["NodeID"],
- })
+ swarm_data = parsed.get("Swarm", {})
+ _try_set(node, "swarm_state", swarm_data, "LocalNodeState")
+ _try_set(node, "swarm_manager", swarm_data, "RemoteManagers")
+ _try_set(node, "swarm_node_id", swarm_data, "NodeID")
- labels = parsed[u"Labels"]
+ labels = parsed.get(u"Labels")
if labels:
node_labels =
inventory_tree.get_list("software.applications.docker.node_labels:")
node_labels += [{"label": label} for label in labels]
status_node = status_data_tree.get_dict("software.applications.docker.")
- status_node.update({
- "num_containers_total": parsed["Containers"],
- "num_containers_running": parsed["ContainersRunning"],
- "num_containers_paused": parsed["ContainersPaused"],
- "num_containers_stopped": parsed["ContainersStopped"],
- "num_images": parsed["Images"],
- })
+ _try_set(status_node, "num_containers_total", parsed,
"Containers")
+ _try_set(status_node, "num_containers_running", parsed,
"ContainersRunning")
+ _try_set(status_node, "num_containers_paused", parsed,
"ContainersPaused")
+ _try_set(status_node, "num_containers_stopped", parsed,
"ContainersStopped")
+ _try_set(status_node, "num_images", parsed, "Images")
inv_info['docker_node_info'] = {
diff --git a/tests/unit/inventory/test_inv_docker_node_info.py
b/tests/unit/inventory/test_inv_docker_node_info.py
new file mode 100644
index 0000000..5fa6e99
--- /dev/null
+++ b/tests/unit/inventory/test_inv_docker_node_info.py
@@ -0,0 +1,62 @@
+import os
+import pytest
+
+INV_FILE = os.path.join(os.path.dirname(__file__),
'../../../inventory/docker_node_info')
+
+
+class MockTree(object):
+ def __init__(self):
+ self.data = {}
+
+ def get_dict(self, path):
+ return self.data.setdefault(path, dict())
+
+ def get_list(self, path):
+ return self.data.setdefault(path, list())
+
+
+(a)pytest.mark.parametrize('parsed,inv_data,stat_data'ata', [
+ ({"nothing": "usable"},
+ {"software.applications.docker.": {}},
+ {"software.applications.docker.": {}}),
+ ({'ServerVersion': u'1.17',
+ 'IndexServerAddress':
u'https://registry.access.redhat.com/v1/'v1/',
+ u'Containers': 11,
+ u'ContainersPaused': 0,
+ u'ContainersRunning': 11,
+ u'ContainersStopped': 0,
+ u'Images': 22,
+ u'Swarm': {'LocalNodeState': u'active',
+ 'NodeID': u'Hier koennte ihre Werbung stehen.'},
+ },
+ {
+ "software.applications.docker.": {
+ "version": "1.17",
+ "registry":
u'https://registry.access.redhat.com/v1/'v1/',
+ "swarm_state": "active",
+ "swarm_node_id": u'Hier koennte ihre Werbung stehen.',
+ },
+ },
+ {
+ "software.applications.docker.": {
+ "num_containers_total": 11,
+ "num_containers_running": 11,
+ "num_containers_paused": 0,
+ "num_containers_stopped": 0,
+ "num_images": 22,
+ },
+ })
+])
+def test_inv_docker_node_info(parsed, inv_data, stat_data):
+ inventory_tree = MockTree()
+ status_data_tree = MockTree()
+
+ context = {'inv_info': {}}
+ execfile(INV_FILE, context)
+ inv_docker_node_info = context["inv_docker_node_info"]
+
+ inv_docker_node_info(parsed, inventory_tree, status_data_tree)
+ assert inventory_tree.data == inv_data
+ assert status_data_tree.data == stat_data
+
+