Skip to content

Commit

Permalink
Add debug command to get filesystem metadata
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Jul 30, 2024
1 parent 7bb92cf commit 5136af3
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/stratis_cli/_actions/_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,51 @@ def get_object_path(namespace):
)
print(fs_object_path)

@staticmethod
def get_metadata(namespace):
"""
Get filesystem medatada. If a specific filesystem is not specified,
get metadata for all filesystems in the pool.
:raises StratisCliEngineError:
"""

# pylint: disable=import-outside-toplevel
from ._data import ObjectManager, Pool, pools

proxy = get_object(TOP_OBJECT)
managed_objects = ObjectManager.Methods.GetManagedObjects(proxy, {})

(pool_object_path, _) = next(
pools(props={"Name": namespace.pool_name})
.require_unique_match(True)
.search(managed_objects)
)

(metadata, return_code, message) = Pool.Methods.FilesystemMetadata(
get_object(pool_object_path),
{
"fs_name": (
(False, "")
if namespace.fs_name is None
else (True, namespace.fs_name)
),
"current": not namespace.written,
},
)

if return_code != StratisdErrors.OK: # pragma: no cover
raise StratisCliEngineError(return_code, message)

json_format_data = [json.loads(chunk) for chunk in metadata.split("\n")]

if namespace.pretty:
for js in json_format_data:
print(json.dumps(js, sort_keys=True, indent=4))
else:
for js in json_format_data:
print(json.dumps(js))


class BlockdevDebugActions: # pylint: disable=too-few-public-methods
"""
Expand Down
28 changes: 28 additions & 0 deletions src/stratis_cli/_parser/_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@
"func": FilesystemDebugActions.get_object_path,
},
),
(
"get-metadata",
{
"help": (
"Get the filesystem metadata for all filesystems belonging to "
"the specified pool"
),
"args": [
("pool_name", {"help": "Pool name"}),
(
"--pretty",
{
"action": "store_true",
"help": "Format output string prettily",
},
),
(
"--written",
{
"action": "store_true",
"help": "Read the metadata most recently written",
},
),
("--fs-name", {"help": "Optional filesystem name"}),
],
"func": FilesystemDebugActions.get_metadata,
},
),
]

BLOCKDEV_DEBUG_SUBCMDS = [
Expand Down
43 changes: 43 additions & 0 deletions tests/whitebox/integration/logical/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,46 @@ def test_lookup_name(self):
self._FSNAME,
]
TEST_RUNNER(command_line)

def test_metadata_name(self):
"""
Test getting filesystem metadata.
"""
command_line = ["filesystem", "create", self._POOLNAME, "newname"]
RUNNER(command_line)

command_line = self._MENU + [
"get-metadata",
self._POOLNAME,
]
TEST_RUNNER(command_line)

def test_metadata_name_pretty(self):
"""
Test getting filesystem metadata and pretty printing it.
"""
command_line = ["filesystem", "create", self._POOLNAME, "newname"]
RUNNER(command_line)

command_line = self._MENU + [
"get-metadata",
self._POOLNAME,
"--pretty",
]
TEST_RUNNER(command_line)

def test_metadata_name_pretty_fs_name(self):
"""
Test getting filesystem metadata and pretty printing it, specifying
the filesystem name.
"""
command_line = ["filesystem", "create", self._POOLNAME, "newname"]
RUNNER(command_line)

command_line = self._MENU + [
"get-metadata",
self._POOLNAME,
"--pretty",
"--fs-name=newname",
]
TEST_RUNNER(command_line)

0 comments on commit 5136af3

Please sign in to comment.