Source code for test_FILESYSTEM

#!/usr/bin/env python2
#   Author(s): Milan Falesnik <mfalesni@redhat.com>
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   Copyright (c) 2012 Red Hat, Inc. All rights reserved.
#
#   This copyrighted material is made available to anyone wishing
#   to use, modify, copy, or redistribute it subject to the terms
#   and conditions of the GNU General Public License version 2.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE. See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import os
import stat
import sys
import pytest
import common.shell
import fnmatch

# Python 2.4 compatibility
try:
    any
except NameError:
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False

""" Filesystem checking tests """
[docs]class TestFileSystem(object): @pytest.fixture
[docs] def world_writable_whitelist(self): """ Reads a whitelist of files that can be world_writable. :returns: Whitelist of world-writable files """ try: f = open("parametrized/world_writable_whitelist", "r") except IOError: f = open("../parametrized/world_writable_whitelist", "r") # For testing purposes data = [line.strip() for line in f.readlines()] f.close() return data
@pytest.fixture
[docs] def ignore_patterns(self): """ Reads a list of ignore patterns which won't be tested. :returns: List of ignore patterns """ try: f = open("parametrized/fs_ignore_patterns", "r") except IOError: f = open("../parametrized/fs_ignore_patterns", "r") # For testing purposes data = [line.strip() for line in f.readlines()] f.close() return data
[docs] def fmt_mode_str(self, st_mode): """ ??? Does the formatting of props. """ # Type ftype_alias = dict(REG='-', FIFO='p', UID='s', GID='s', VTX='t') mode_str = '-' for ftype in ['DIR', 'CHR', 'BLK', 'FIFO', 'LNK', 'SOCK',]: func = getattr(stat, 'S_IS%s' % ftype) if func(st_mode): mode_str = ftype_alias.get(ftype, ftype[:1].lower()) break for level in "USR", "GRP", "OTH": for perm in "R", "W", "X": if st_mode & getattr(stat,"S_I"+perm+level): mode_str += perm.lower() else: mode_str += '-' # sticky bit for (count, fsticky) in enumerate(['UID', 'GID', 'VTX'], 1): if st_mode & getattr(stat,"S_IS"+fsticky): pos = count * 3 fchar = ftype_alias.get(fsticky, fsticky[:1].lower()) mode_str = mode_str[:pos] + fchar + mode_str[pos+1:] return mode_str + '.'