Ejemplo n.º 1
0
    def test_statfs(self, cmdbuilder_mock_avail_cmds):
        xrosfs = XrosFS(Mock(), cmdbuilder_mock_avail_cmds(), '/mnt')

        # Prepare dummy results
        xrosfs.shell.entry.side_effect = [
            EntryResult('{"f_bavail": 1, "f_bfree": 2, "f_blocks": 3,'
                        '"f_bsize": 4, "f_favail": 5, "f_ffree": 6,'
                        '"f_files": 7, "f_frsize": 8, "f_namemax": 9}\n'),
            EntryResult('overlay / overlay rw,relatime,lowerdir=/var/... 0 0\n'
                        'proc /proc proc ... 0 0\n'
                        'tmpfs /dev tmpfs ... 0 0\n')
        ]

        # Test
        assert xrosfs.statfs('/home/pdu/xrosfs/bench_test/mnt/xrosfs') == \
            {"f_bavail": 1, "f_bfree": 2, "f_blocks": 3,
             "f_bsize": 4, "f_favail": 5, "f_ffree": 6,
             "f_files": 7, "f_frsize": 8, "f_namemax": 9,
             "f_flag": 32 | os.ST_RELATIME}
        calls = [
            mock.call(('/usr/bin/stat', '-f', '-c',
                       '{"f_bavail":%a,"f_bfree":%f,"f_blocks":%b,'
                       '"f_bsize":%s,"f_favail":%d,"f_ffree":%d,'
                       '"f_files":%c,"f_frsize":%S,"f_namemax":%l}', '--',
                       '/mnt/home/pdu/xrosfs/bench_test/mnt/xrosfs')),
            mock.call(['cat', '/proc/mounts'])
        ]
        xrosfs.shell.entry.assert_has_calls(calls)

        # Reset mock
        xrosfs.shell.entry.side_effect = None
        xrosfs.shell.entry.reset_mock()

        # Test not exist path
        xrosfs.shell.entry.return_value =\
            EntryResult('', 1, 'stat: cannot stat ‘/not_extist’:'
                        'No such file or directory\n')
        with pytest.raises(OSError) as excinfo:
            assert xrosfs.statfs('/home/pdu/xrosfs/bench_test/mnt/xrosfs') == \
                ''
        assert 'fuse.FuseOSError: [Errno 2] No such file or directory'\
               in str(excinfo)

        # Reset mock
        xrosfs.shell.entry.reset_mock()

        # Test json parse error
        xrosfs.shell.entry.return_value =\
            EntryResult('{')
        with pytest.raises(RuntimeError) as excinfo:
            assert xrosfs.getattr('/usr/bin/stat') == ''
        assert 'RuntimeError: stat:' in str(excinfo)
Ejemplo n.º 2
0
    def test_getattr(self, cmdbuilder_mock_avail_cmds):
        xrosfs = XrosFS(Mock(), cmdbuilder_mock_avail_cmds(), '/mnt')

        # Prepare dummy results
        xrosfs.shell.entry.return_value = EntryResult(
            '{"st_mode":"0x81ed","st_ino":547312,"st_dev":46,'
            '"st_nlink":1,"st_uid":0,"st_gid":0,"st_size":76584,'
            '"st_atime":1502264616,"st_mtime":1426348024,'
            '"st_ctime":1501780227}')

        # Test
        assert xrosfs.getattr('/usr/bin/stat') ==\
            {"st_mode": 33261, "st_ino": 547312, "st_dev": 46,
             "st_nlink": 1, "st_uid": 0, "st_gid": 0, "st_size": 76584,
             "st_atime": 1502264616, "st_mtime": 1426348024,
             "st_ctime": 1501780227}
        xrosfs.shell.entry.assert_called_once_with(
            ('/usr/bin/stat', '-c', xrosfs.cmd_builder.stat2json, '--',
             '/mnt/usr/bin/stat'))

        # Reset mock
        xrosfs.shell.entry.reset_mock()

        # Test not exist path
        xrosfs.shell.entry.return_value =\
            EntryResult('', 1, 'stat: cannot stat ‘/not_extist’:'
                        'No such file or directory\n')
        with pytest.raises(OSError) as excinfo:
            assert xrosfs.getattr('/usr/bin/stat') == ''
        assert 'fuse.FuseOSError: [Errno 2] No such file or directory'\
               in str(excinfo)

        # Reset mock
        xrosfs.shell.entry.reset_mock()

        # Test json parse error
        xrosfs.shell.entry.return_value =\
            EntryResult('{')
        with pytest.raises(RuntimeError) as excinfo:
            assert xrosfs.getattr('/usr/bin/stat') == ''
        assert 'RuntimeError: stat:' in str(excinfo)

        # Reset mock
        xrosfs.shell.entry.reset_mock()

        # Check recover from getattr failed
        xrosfs.shell.entry.side_effect = (
            EntryResult('', 1, ''),
            EntryResult('{"st_mode":"0x81ed","st_ino":547312,"st_dev":46,'
                        '"st_nlink":1,"st_uid":0,"st_gid":0,"st_size":76584,'
                        '"st_atime":1502264616,"st_mtime":1426348024,'
                        '"st_ctime":1501780227}'))
        assert xrosfs.getattr('/usr/bin/stat') ==\
            {"st_mode": 33261, "st_ino": 547312, "st_dev": 46,
             "st_nlink": 1, "st_uid": 0, "st_gid": 0, "st_size": 76584,
             "st_atime": 1502264616, "st_mtime": 1426348024,
             "st_ctime": 1501780227}
        assert xrosfs.shell.entry.call_count == 2

        # Reset mock
        xrosfs.shell.entry.reset_mock()
        xrosfs.shell.entry.side_effect = \
            [EntryResult('', 1, '') for i in range(1, 20)]

        # Check fatal error
        xrosfs.shell.entry.return_value = EntryResult('', 1, ''),
        with pytest.raises(RuntimeError) as excinfo:
            assert xrosfs.getattr('/usr/bin/stat') == ''
        assert 'RuntimeError: Fatale error getattr failed 10 times' in \
            str(excinfo)
        assert xrosfs.shell.entry.call_count == 10

        # Reset mock
        xrosfs.shell.entry.reset_mock()
        xrosfs.shell.entry.side_effect = None

        # Check to raise unknown
        xrosfs.shell.entry.side_effect = [EntryResult('', 1, 'check')]

        with pytest.raises(RuntimeError) as excinfo:
            assert xrosfs.getattr('/usr/bin/stat') == ''
        assert 'RuntimeError: cmd' in str(excinfo)