def statfs ( self ): logging.debug("%s" % ('statfs' ) ) maxspace, spaceused = self.provider.statfs() if maxspace == -1: maxspace = (1024*1024*1024*1024*1024) # 1pb blocksize=1048576 # 1mb blocks fragment=1 blockstotal=maxspace/blocksize blocksfree=blockstotal-(spaceused/blocksize) nodestotal=blockstotal nodesfree=blocksfree flag=1024 filenamelength=255 result = posix.statvfs_result((blocksize, fragment, blockstotal, blocksfree, blocksfree, nodestotal, nodesfree, nodesfree, flag, filenamelength)) return result
def test_get_df_data_should_return_proper_data_when_data_dev_and_xlog_dev_are_different( self, mocked_os_statvfs, mocked_get_mounted_device, mocked_get_mount_point): mocked_os_statvfs.side_effect = [ posix.statvfs_result(sequence=(4096, 4096, 10312784, 9823692, 9389714, 2621440, 2537942, 2537942, 4096, 255)), posix.statvfs_result(sequence=(1024, 1024, 103127, 9823, 9389, 2621, 2537, 2537, 1024, 255)) ] detached_disk = DetachedDiskStatCollector( mock.Mock(), ['/var/lib/postgresql/9.3/main']) df_data = detached_disk.get_df_data('/var/lib/postgresql/9.3/main') expected_df_data = { 'data': ('/dev/sda1', 41251136, 37558856), 'xlog': ('/dev/sda2', 103127, 9389) } self.assertEqual(expected_df_data, df_data)
def test_diskusage(mock_factory): with patch('os.statvfs') as statvfs: statvfs.return_value = statvfs_result( (4096, 4096, 100000, 48000, 48000, 0, 0, 0, 0, 255)) with DiskUsage() as disk: assert repr(disk).startswith('<gpiozero.DiskUsage object') assert disk.filesystem == '/' assert disk.usage == 52.0 assert disk.is_active == False assert disk.value == 0.52 with DiskUsage(threshold=50.0) as disk: assert disk.is_active == True with warnings.catch_warnings(record=True) as w: warnings.resetwarnings() with DiskUsage(threshold=125) as disk: assert disk.threshold == 125 assert not disk.is_active assert len(w) == 1 assert w[0].category == ThresholdOutOfRange assert disk.usage == 52.0
def MockStatFS(unused_path): return posix.statvfs_result((f_bsize, f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_flag, f_namemax))
def fake_statvfs(self, *args, **kwargs): self.statvfs_calls.append((args, kwargs)) return statvfs_result(self.statvfs_output)
def statvfs(self): seq = (4096, 4096, 10047582, 7332259, 6820195, 2564096, 2271310, 2271310, 1024, 255) return posix.statvfs_result(sequence=seq)
def MockStatFS(unused_path): return posix.statvfs_result( (f_bsize, f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_flag, f_namemax))
def test_get_disk_stats(): with mock.patch('os.statvfs') as statvfs: statvfs.return_value = statvfs_result( (4096, 4096, 100000, 48000, 48000, 0, 0, 0, 0, 255)) assert get_disk_stats('/home/pi') == (100000 * 4096, 48000 * 4096)
class TestDiskDrivers(BaseTestCase): fake_statvfs = statvfs_result(( 4096, # bsize 4096, # frsize 236216421, # blocks 231166945, # bfree 219162081, # bavail 60006400, # files 59315784, # ffree 59315784, # favail 4096, # flag 255, # namemax )) scenarios = ( ("disk.DiskTotalSpacePuller", { "puller_factory": disk.DiskTotalSpacePuller, "disk_patch": patch.object( os, "statvfs", Mock(return_value=fake_statvfs)), "expected_data": Measurement( name="compute.node.disk.total", timestamp="2015-08-04T15:15:45.703542+00:00", unit="bytes", type_="gauge", value=4096 * 236216421, resource_id="test_node", host="test_node", resource_metadata={ "host": "test_node", "title": "disk_total" } ) }), ("disk.DiskFreeSpacePuller", { "puller_factory": disk.DiskFreeSpacePuller, "disk_patch": patch.object( os, "statvfs", Mock(return_value=fake_statvfs)), "expected_data": Measurement( name="compute.node.disk.free", timestamp="2015-08-04T15:15:45.703542+00:00", unit="bytes", type_="gauge", value=4096 * 219162081, resource_id="test_node", host="test_node", resource_metadata={ "host": "test_node", "title": "disk_free" } ) }), ("disk.DiskUsedSpacePuller", { "puller_factory": disk.DiskUsedSpacePuller, "disk_patch": patch.object( os, "statvfs", Mock(return_value=fake_statvfs)), "expected_data": Measurement( name="compute.node.disk.used", timestamp="2015-08-04T15:15:45.703542+00:00", unit="bytes", type_="gauge", value=(236216421 - 231166945) * 4096, resource_id="test_node", host="test_node", resource_metadata={ "host": "test_node", "title": "disk_used" } ) }), ) @freeze_time("2015-08-04T15:15:45.703542+00:00") @patch.object(platform, "node", Mock(return_value="test_node")) def test_disk(self): data_puller = self.puller_factory( self.puller_factory.get_name(), self.puller_factory.get_default_probe_id(), self.puller_factory.get_default_interval(), ) with self.disk_patch: pulled_data = data_puller.do_pull() self.assertEqual( [measurement.as_dict() for measurement in pulled_data], [self.expected_data.as_dict()] )
def statfs(self): print 'statfs' stats = self._request(self._server.remote_statfs) return posix.statvfs_result(stats)