def test_bad_directory():
    dirs = FileListing(context_wrap(BAD_DIRECTORY_ENTRIES))

    # None of those entries should parse.  So we should have the raw lines,
    # but no parsed entries
    bad_listing = [
        s.strip() for s in BAD_DIRECTORY_ENTRIES.split('\n')[5:] if s
    ]
    assert dirs.raw_directory('/badness') == bad_listing
    assert dirs.listing_of('/badness') == {}
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', 'raw_list', '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.raw_directory('/etc/sysconfig') == MULTIPLE_DIRECTORIES.split(
        '\n')[3:10]

    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