From 74ddb086a64dd9c0c8c588aff293ef3d4c2274a4 Mon Sep 17 00:00:00 2001 From: Guillaume Boutry Date: Fri, 31 May 2024 00:06:55 +0200 Subject: [PATCH] snap.py: fix snap.get optional key (#125) Fix snap.get method to handle the case where the key is falsy. Treat it as no key provided, which results in the expected behavior to get all values from snap configs. --- lib/charms/operator_libs_linux/v2/snap.py | 7 +++++-- tests/integration/test_snap.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/charms/operator_libs_linux/v2/snap.py b/lib/charms/operator_libs_linux/v2/snap.py index 6d4dc385..9d09a78d 100644 --- a/lib/charms/operator_libs_linux/v2/snap.py +++ b/lib/charms/operator_libs_linux/v2/snap.py @@ -83,7 +83,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 6 +LIBPATCH = 7 # Regex to locate 7-bit C1 ANSI sequences @@ -319,7 +319,10 @@ def get(self, key: Optional[str], *, typed: bool = False) -> Any: Default is to return a string. """ if typed: - config = json.loads(self._snap("get", ["-d", key])) + args = ["-d"] + if key: + args.append(key) + config = json.loads(self._snap("get", args)) if key: return config.get(key) return config diff --git a/tests/integration/test_snap.py b/tests/integration/test_snap.py index 640b5681..c3597aef 100644 --- a/tests/integration/test_snap.py +++ b/tests/integration/test_snap.py @@ -116,6 +116,22 @@ def test_snap_set_and_get_with_typed(): assert lxd.get("criu.enable", typed=True) == "true" assert lxd.get("ceph.external", typed=True) == "false" + assert lxd.get(None, typed=True) == { + "true": True, + "false": False, + "integer": 1, + "float": 2.0, + "list": [1, 2.0, True, False, None], + "dict": { + "true": True, + "false": False, + "integer": 1, + "float": 2.0, + "list": [1, 2.0, True, False, None], + }, + "criu": {"enable": "true"}, + "ceph": {"external": "false"}, + } def test_snap_set_and_get_untyped():