Skip to content

Commit

Permalink
qa/cephfs: improve caps_helper.CapTester
Browse files Browse the repository at this point in the history
Improvement #1:

CapTester.write_test_files() not only creates the test file but also
does the following for every mount object it receives in parameters -

* carefully produces the path for the test file as per parameters
  received
* generates the unique data for each test file on a CephFS mount
* creates a data structure -- list of lists -- that holds all this
  information along with mount object itself for each mount object so
  that tests can be conducted at a later point

Untangle this mess of code by splitting this method into 3 separate
methods -

1. To produce the path for test file (as per user's need).
2. To generate the data that will be written into the test file.
3. To actually create the test file on CephFS.

Improvement #2:

Remove the internal data structure used for testing -- self.test_set --
and use separate class attributes to store all the data required for
testing instead of a tuple. This serves two purpose -

One, it makes it easy to manipulate all this data from helper methods
and during debugging session, especially while using a PDB session.

And two, make it impossible to have multiple mounts/multiple "test sets"
within same CapTester instance for the sake of simplicity. Users can
instead create two instances of CapTester instances if needed.

Signed-off-by: Rishabh Dave <ridave@redhat.com>
  • Loading branch information
rishabh-d-dave committed Apr 18, 2023
1 parent e8bdf94 commit 008dbe9
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 117 deletions.
133 changes: 72 additions & 61 deletions qa/tasks/cephfs/caps_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ class CapTester:
run_mon_cap_tests() or run_mds_cap_tests() as per the need.
"""

def write_test_files(self, mounts, testpath=''):
def __init__(self, mount=None, path=''):
self._create_test_files(mount, path)

def _create_test_files(self, mount, path):
"""
Exercising 'r' and 'w' access levels on a file on CephFS mount is
pretty routine across all tests for caps. Adding to method to write
Expand All @@ -129,39 +132,47 @@ def write_test_files(self, mounts, testpath=''):
at the path passed in testpath for the given list of mounts. If
testpath is empty, the file is created at the root of the CephFS.
"""
dirname, filename = 'testdir', 'testfile'
self.test_set = []
# XXX: The reason behind testpath[1:] below is that the testpath is
# CephFS mount where read/write test will be conducted.
self.mount = mount
# Path where out test file located.
self.path = self._gen_test_file_path(path)
# Data that out test file will contain.
self.data = self._gen_test_file_data()

self.mount.write_file(self.path, self.data)
log.info(f'Test file has been created on FS '
f'"{self.mount.cephfs_name}" at path "{self.path}" with the '
f'following data -\n{self.data}')

def _gen_test_file_path(self, path=''):
# XXX: The reason behind path[1:] below is that the path is
# supposed to contain a path inside CephFS (which might be passed as
# an absolute path). os.path.join() deletes all previous path
# components when it encounters a path component starting with '/'.
# Deleting the first '/' from the string in testpath ensures that
# Deleting the first '/' from the string in path ensures that
# previous path components are not deleted by os.path.join().
if testpath:
testpath = testpath[1:] if testpath[0] == '/' else testpath
# XXX: passing just '/' screw up os.path.join() ahead.
if testpath == '/':
testpath = ''

for mount_x in mounts:
log.info(f'creating test file on FS {mount_x.cephfs_name} '
f'mounted at {mount_x.mountpoint}...')
dirpath = os_path_join(mount_x.hostfs_mntpt, testpath, dirname)
mount_x.run_shell(f'mkdir {dirpath}')
filepath = os_path_join(dirpath, filename)
# XXX: the reason behind adding filepathm, cephfs_name and both
# mntpts is to avoid a test bug where we mount cephfs1 but what
# ends up being mounted cephfs2. since filepath and filedata are
# identical, how would tests figure otherwise that they are
# accessing the right filename but on wrong CephFS.
filedata = (f'filepath = {filepath}\n'
f'cephfs_name = {mount_x.cephfs_name}\n'
f'cephfs_mntpt = {mount_x.cephfs_mntpt}\n'
f'hostfs_mntpt = {mount_x.hostfs_mntpt}')
mount_x.write_file(filepath, filedata)
self.test_set.append([mount_x, filepath, filedata])
log.info(f'Test file created at "{filepath}" with the following '
f'data -\n"{filedata}"')
if path:
path = path[1:] if path[0] == '/' else path
# XXX: passing just '/' messes up os.path.join() ahead.
if path == '/':
path = ''

dirname, filename = 'testdir', 'testfile'
dirpath = os_path_join(self.mount.hostfs_mntpt, path, dirname)
self.mount.run_shell(f'mkdir {dirpath}')
return os_path_join(dirpath, filename)

def _gen_test_file_data(self):
# XXX: the reason behind adding path, cephfs_name and both
# mntpts is to avoid a test bug where we mount cephfs1 but what
# ends up being mounted cephfs2. since self.path and self.data are
# identical, how would tests figure otherwise that they are
# accessing the right filename but on wrong CephFS.
return dedent(f'''\
self.path = {self.path}
cephfs_name = {self.mount.cephfs_name}
cephfs_mntpt = {self.mount.cephfs_mntpt}
hostfs_mntpt = {self.mount.hostfs_mntpt}''')

def run_cap_tests(self, perm, mntpt=None):
# TODO
Expand Down Expand Up @@ -235,15 +246,17 @@ def run_mds_cap_tests(self, perm, mntpt=None):
Run test for read perm and, for write perm, run positive test if it
is present and run negative test if not.
"""
# XXX: mntpt is path inside cephfs that serves as root for current
# mount. Therefore, this path must me deleted from self.filepaths.
# Example -
# orignal path: /mnt/cephfs_x/dir1/dir2/testdir
# cephfs dir serving as root for current mnt: /dir1/dir2
# therefore, final path: /mnt/cephfs_x/testdir
if mntpt:
self.test_set = [(x, y.replace(mntpt, ''), z) for x, y, z in \
self.test_set]
# beacaue we want to value of mntpt from test_set.path along with
# slash that precedes it.
mntpt = '/' + mntpt if mntpt[0] != '/' else mntpt
# XXX: mntpt is path inside cephfs that serves as root for current
# mount. Therefore, this path must me deleted from self.path.
# Example -
# orignal path: /mnt/cephfs_x/dir1/dir2/testdir
# cephfs dir serving as root for current mnt: /dir1/dir2
# therefore, final path: /mnt/cephfs_x/testdir
self.path = self.path.replace(mntpt, '')

self.conduct_pos_test_for_read_caps()

Expand All @@ -255,35 +268,33 @@ def run_mds_cap_tests(self, perm, mntpt=None):
raise RuntimeError(f'perm = {perm}\nIt should be "r" or "rw".')

def conduct_pos_test_for_read_caps(self):
for mount, path, data in self.test_set:
log.info(f'test read perm: read file {path} and expect data '
f'"{data}"')
contents = mount.read_file(path)
assert_equal(data, contents)
log.info(f'read perm was tested successfully: "{data}" was '
f'successfully read from path {path}')
log.info(f'test read perm: read file {self.path} and expect data '
f'"{self.data}"')
contents = self.mount.read_file(self.path)
assert_equal(self.data, contents)
log.info(f'read perm was tested successfully: "{self.data}" was '
f'successfully read from path {self.path}')

def conduct_pos_test_for_write_caps(self):
for mount, path, data in self.test_set:
log.info(f'test write perm: try writing data "{data}" to '
f'file {path}.')
mount.write_file(path=path, data=data)
contents = mount.read_file(path=path)
assert_equal(data, contents)
log.info(f'write perm was tested was successfully: data '
f'"{data}" was successfully written to file "{path}".')
log.info(f'test write perm: try writing data "{self.data}" to '
f'file {self.path}.')
self.mount.write_file(path=self.path, data=self.data)
contents = self.mount.read_file(path=self.path)
assert_equal(self.data, contents)
log.info(f'write perm was tested was successfully: self.data '
f'"{self.data}" was successfully written to file '
f'"{self.path}".')

def conduct_neg_test_for_write_caps(self, sudo_write=False):
possible_errmsgs = ('permission denied', 'operation not permitted')
cmdargs = ['echo', 'some random data', Raw('|')]
cmdargs += ['sudo', 'tee'] if sudo_write else ['tee']

# don't use data, cmd args to write are set already above.
for mount, path, data in self.test_set:
log.info('test absence of write perm: expect failure '
f'writing data to file {path}.')
cmdargs.append(path)
mount.negtestcmd(args=cmdargs, retval=1, errmsgs=possible_errmsgs)
cmdargs.pop(-1)
log.info('absence of write perm was tested successfully: '
f'failed to be write data to file {path}.')
log.info('test absence of write perm: expect failure '
f'writing data to file {self.path}.')
cmdargs.append(self.path)
self.mount.negtestcmd(args=cmdargs, retval=1, errmsgs=possible_errmsgs)
cmdargs.pop(-1)
log.info('absence of write perm was tested successfully: '
f'failed to be write data to file {self.path}.')
56 changes: 22 additions & 34 deletions qa/tasks/cephfs/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,27 +1206,30 @@ class TestFsAuthorize(CephFSTestCase):
def test_single_path_r(self):
PERM = 'r'
FS_AUTH_CAPS = (('/', PERM),)
self.captester = CapTester()
self.setup_test_env(FS_AUTH_CAPS)
self.captester = CapTester(self.mount_a, '/')
keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

self._remount(keyring)
self.captester.run_mon_cap_tests(self.fs, self.client_id)
self.captester.run_mds_cap_tests(PERM)

def test_single_path_rw(self):
PERM = 'rw'
FS_AUTH_CAPS = (('/', PERM),)
self.captester = CapTester()
self.setup_test_env(FS_AUTH_CAPS)
self.captester = CapTester(self.mount_a, '/')
keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

self._remount(keyring)
self.captester.run_mon_cap_tests(self.fs, self.client_id)
self.captester.run_mds_cap_tests(PERM)

def test_single_path_rootsquash(self):
PERM = 'rw'
FS_AUTH_CAPS = (('/', PERM, 'root_squash'),)
self.captester = CapTester()
self.setup_test_env(FS_AUTH_CAPS)
self.captester = CapTester(self.mount_a, '/')
keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

self._remount(keyring)
# testing MDS caps...
# Since root_squash is set in client caps, client can read but not
# write even thought access level is set to "rw".
Expand All @@ -1250,32 +1253,35 @@ def test_single_path_authorize_on_nonalphanumeric_fsname(self):
self.mount_a.remount(cephfs_name=self.fs.name)
PERM = 'rw'
FS_AUTH_CAPS = (('/', PERM),)
self.captester = CapTester()
self.setup_test_env(FS_AUTH_CAPS)
self.captester = CapTester(self.mount_a, '/')
keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

self._remount(keyring)
self.captester.run_mds_cap_tests(PERM)

def test_multiple_path_r(self):
PERM = 'r'
FS_AUTH_CAPS = (('/dir1/dir12', PERM), ('/dir2/dir22', PERM))
for c in FS_AUTH_CAPS:
self.mount_a.run_shell(f'mkdir -p .{c[0]}')
self.captesters = (CapTester(), CapTester())
self.setup_test_env(FS_AUTH_CAPS)
self.captesters = (CapTester(self.mount_a, '/dir1/dir12'),
CapTester(self.mount_a, '/dir2/dir22'))
keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

self.run_cap_test_one_by_one(FS_AUTH_CAPS)
self._remount_and_run_tests(FS_AUTH_CAPS, keyring)

def test_multiple_path_rw(self):
PERM = 'rw'
FS_AUTH_CAPS = (('/dir1/dir12', PERM), ('/dir2/dir22', PERM))
for c in FS_AUTH_CAPS:
self.mount_a.run_shell(f'mkdir -p .{c[0]}')
self.captesters = (CapTester(), CapTester())
self.setup_test_env(FS_AUTH_CAPS)
self.captesters = (CapTester(self.mount_a, '/dir1/dir12'),
CapTester(self.mount_a, '/dir2/dir22'))
keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

self.run_cap_test_one_by_one(FS_AUTH_CAPS)
self._remount_and_run_tests(FS_AUTH_CAPS, keyring)

def run_cap_test_one_by_one(self, fs_auth_caps):
keyring = self.run_cluster_cmd(f'auth get {self.client_name}')
def _remount_and_run_tests(self, fs_auth_caps, keyring):
for i, c in enumerate(fs_auth_caps):
self.assertIn(i, (0, 1))
PATH = c[0]
Expand All @@ -1297,24 +1303,6 @@ def _remount(self, keyring, path='/'):
client_keyring_path=keyring_path,
cephfs_mntpt=path)

def setup_for_single_path(self, fs_auth_caps):
self.captester.write_test_files((self.mount_a,), '/')
keyring = self.fs.authorize(self.client_id, fs_auth_caps)
self._remount(keyring)

def setup_for_multiple_paths(self, fs_auth_caps):
for i, c in enumerate(fs_auth_caps):
PATH = c[0]
self.captesters[i].write_test_files((self.mount_a,), PATH)

self.fs.authorize(self.client_id, fs_auth_caps)

def setup_test_env(self, fs_auth_caps):
if len(fs_auth_caps) == 1:
self.setup_for_single_path(fs_auth_caps[0])
else:
self.setup_for_multiple_paths(fs_auth_caps)


class TestAdminCommandIdempotency(CephFSTestCase):
"""
Expand Down
Loading

0 comments on commit 008dbe9

Please sign in to comment.