Skip to content

Commit

Permalink
Run a python-based test to revert a filesystem
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Sep 4, 2024
1 parent b081b65 commit 3879e96
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/support.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
python3-dbus-client-gen
python3-dbus-python-client-gen
python3-justbytes
python3-libmount
python3-psutil
python3-pyudev
python3-semantic_version
Expand Down
6 changes: 4 additions & 2 deletions tests-fmf/python.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require:
- python3-dbus
- python3-dbus-client-gen
- python3-dbus-python-client-gen
- python3-justbytes
- python3-libmount
- python3-psutil
- python3-pyudev

Expand All @@ -28,12 +30,12 @@ environment:

/legacy/loop:
summary: Run Python tests that use loopbacked device framework
test: make -f Makefile tang-tests dump-metadata-tests startup-tests
test: make -f Makefile tang-tests dump-metadata-tests startup-tests revert-tests

/v2/udev:
summary: Run Python udev tests
test: make -f Makefile udev-tests

/v2/loop:
summary: Run Python tests that use loopbacked device framework
test: make -f Makefile tang-tests dump-metadata-tests startup-tests
test: make -f Makefile tang-tests dump-metadata-tests startup-tests revert-tests
4 changes: 4 additions & 0 deletions tests/client-dbus/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ filesystem-predict-tests:
.PHONY: dump-metadata-tests
dump-metadata-tests:
python3 -m unittest ${UNITTEST_OPTS} tests.udev.test_dump

.PHONY: revert-tests
revert-tests:
python3 -m unittest ${UNITTEST_OPTS} tests.udev.test_revert
120 changes: 120 additions & 0 deletions tests/client-dbus/tests/udev/test_revert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright 2021 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Test reverting a filesystem.
"""

# isort: STDLIB
import os
import tempfile

# isort: THIRDPARTY
import libmount
from justbytes import Range

# isort: LOCAL
from stratisd_client_dbus import Filesystem

from ._utils import (
Pool,
ServiceContextManager,
UdevTest,
create_pool,
get_object,
random_string,
)


class TestRevert(UdevTest):
"""
Test reverting a filesystem.
"""

def test_revert(self): # pylint: disable=too-many-locals
"""
Schedule a revert and verify that it has succeeded when the pool is
restarted.
"""
mountdir = tempfile.mkdtemp("_stratis_mnt")

with ServiceContextManager():
device_tokens = self._lb_mgr.create_devices(2)

pool_name = random_string(5)

(_, (pool_object_path, _)) = create_pool(
pool_name, self._lb_mgr.device_files(device_tokens)
)

fs_name = "fs1"
fs_size = Range(1024**3)
((_, fs_object_paths), return_code, message) = (
Pool.Methods.CreateFilesystems(
get_object(pool_object_path),
{"specs": [(fs_name, str(fs_size.magnitude), (False, ""))]},
)
)

if return_code != 0:
raise RuntimeError(
f"Failed to create a requested filesystem: {message}"
)

filepath = f"/dev/stratis/{pool_name}/{fs_name}"

mountcxt = libmount.Context( # pylint: disable=no-member
target=mountdir, source=filepath
)
mountcxt.mount()

file1 = "file1.txt"
with open(os.path.join(mountdir, file1), encoding="utf-8", mode="w") as fd:
print(file1, file=fd)

snap_name = "snap1"
((_, snap_object_path), return_code, message) = (
Pool.Methods.SnapshotFilesystem(
get_object(pool_object_path),
{"origin": fs_object_paths[0], "snapshot_name": snap_name},
)
)

file2 = "file2.txt"
with open(os.path.join(mountdir, file2), encoding="utf-8", mode="w") as fd:
print(file2, file=fd)

Filesystem.Properties.MergeScheduled.set(get_object(snap_object_path), True)
mountcxt.umount()

self.assertTrue(os.path.exists(f"/dev/stratis/{pool_name}/{snap_name}"))

with ServiceContextManager():
self.wait_for_pools(1)

mountcxt = libmount.Context( # pylint: disable=no-member
target=mountdir, source=filepath
)
mountcxt.mount()

with open(os.path.join(mountdir, file1), encoding="utf-8") as fd:
self.assertEqual(fd.read(), file1)

with self.assertRaises(FileNotFoundError):
# pylint: disable=consider-using-with
open(os.path.join(mountdir, file2), encoding="utf-8")

mountcxt.umount()

self.assertFalse(os.path.exists(f"/dev/stratis/{pool_name}/{snap_name}"))

0 comments on commit 3879e96

Please sign in to comment.