def test_single_directory(): # Mainly just testing that we can read a single directory and # recognise it via the path rather than the directory in the output. # Testing of the main functionality is mostly done below. ctx = context_wrap(SINGLE_DIRECTORY, path='ls_-la_.etc.pki.tls') dirs = FileListing(ctx) assert '/etc/pki/tls' in dirs assert '/etc/pki/tls' in dirs.listings assert '/etc/pki/tls/certs' not in dirs assert '/etc/pki' not in dirs assert '/etc' not in dirs assert dirs.listings['/etc/pki/tls']['name'] == '/etc/pki/tls' assert dirs.files_of('/etc/pki/tls') == ['cert.pem', 'openssl.cnf'] assert dirs.dirs_of('/etc/pki/tls') == ['.', '..', 'certs', 'misc', 'private'] assert dirs.total_of('/etc/pki/tls') == 32
def test_multiple_directories(): dirs = FileListing(context_wrap(MULTIPLE_DIRECTORIES, path='ls_-la_.etc')) assert '/etc/sysconfig' in dirs assert '/etc/sysconfig' in dirs.listings assert '/etc/rc.d/rc3.d' in dirs assert '/etc/rc.d/rc4.d' not in dirs esc = dirs.listings['/etc/sysconfig'] assert sorted(esc.keys()) == sorted(['entries', 'files', 'dirs', 'specials', 'total', 'name']) assert dirs.files_of('/etc/sysconfig') == ['ebtables-config', 'firewalld', 'grub'] assert dirs.dirs_of('/etc/sysconfig') == ['.', '..', 'cbq', 'console'] assert dirs.specials_of('/etc/sysconfig') == [] # Testing the main features listing = dirs.listing_of('/etc/sysconfig') assert listing['..'] == { 'type': 'd', 'perms': 'rwxr-xr-x.', 'links': 77, 'owner': '0', 'group': '0', 'size': 8192, 'date': 'Jul 13 03:55', 'name': '..', 'raw_entry': 'drwxr-xr-x. 77 0 0 8192 Jul 13 03:55 ..', 'dir': '/etc/sysconfig' } assert listing['cbq'] == { 'type': 'd', 'perms': 'rwxr-xr-x.', 'links': 2, 'owner': '0', 'group': '0', 'size': 41, 'date': 'Jul 6 23:32', 'name': 'cbq', 'raw_entry': 'drwxr-xr-x. 2 0 0 41 Jul 6 23:32 cbq', 'dir': '/etc/sysconfig' } assert listing['firewalld'] == { 'type': '-', 'perms': 'rw-r--r--.', 'links': 1, 'owner': '0', 'group': '0', 'size': 72, 'date': 'Sep 15 2015', 'name': 'firewalld', 'raw_entry': '-rw-r--r--. 1 0 0 72 Sep 15 2015 firewalld', 'dir': '/etc/sysconfig' } assert listing['grub'] == { 'type': 'l', 'perms': 'rwxrwxrwx.', 'links': 1, 'owner': '0', 'group': '0', 'size': 17, 'date': 'Jul 6 23:32', 'name': 'grub', 'link': '/etc/default/grub', 'raw_entry': 'lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 grub -> /etc/default/grub', 'dir': '/etc/sysconfig' } listing = dirs.listing_of('/etc/rc.d/rc3.d') assert listing['..'] == { 'type': 'd', 'perms': 'rwxr-xr-x.', 'links': 10, 'owner': '0', 'group': '0', 'size': 4096, 'date': 'Sep 16 2015', 'name': '..', 'raw_entry': 'drwxr-xr-x. 10 0 0 4096 Sep 16 2015 ..', 'dir': '/etc/rc.d/rc3.d' } assert listing['K50netconsole'] == { 'type': 'l', 'perms': 'rwxrwxrwx.', 'links': 1, 'owner': '0', 'group': '0', 'size': 20, 'date': 'Jul 6 23:32', 'name': 'K50netconsole', 'link': '../init.d/netconsole', 'raw_entry': 'lrwxrwxrwx. 1 0 0 20 Jul 6 23:32 K50netconsole -> ../init.d/netconsole', 'dir': '/etc/rc.d/rc3.d' } assert dirs.total_of('/etc/sysconfig') == 96 assert dirs.total_of('/etc/rc.d/rc3.d') == 4 assert dirs.dir_contains('/etc/sysconfig', 'firewalld') assert dirs.dir_entry('/etc/sysconfig', 'grub') == { 'type': 'l', 'perms': 'rwxrwxrwx.', 'links': 1, 'owner': '0', 'group': '0', 'size': 17, 'date': 'Jul 6 23:32', 'name': 'grub', 'link': '/etc/default/grub', 'raw_entry': 'lrwxrwxrwx. 1 0 0 17 Jul 6 23:32 grub -> /etc/default/grub', 'dir': '/etc/sysconfig' } assert dirs.path_entry('/etc/sysconfig/cbq') == { 'type': 'd', 'perms': 'rwxr-xr-x.', 'links': 2, 'owner': '0', 'group': '0', 'size': 41, 'date': 'Jul 6 23:32', 'name': 'cbq', 'raw_entry': 'drwxr-xr-x. 2 0 0 41 Jul 6 23:32 cbq', 'dir': '/etc/sysconfig' } assert dirs.path_entry('no_slash') is None assert dirs.path_entry('/') is None assert dirs.path_entry('/foo') is None assert dirs.path_entry('/etc/sysconfig/notfound') is None