Fix check_diskspace to work at a granularity less than 1GB. This was breaking a check we have that there be 100MB free on / by making it always appear true as it could no longer measure a value that low.
The previous change inadvertently limited the granularity of the disk space to check to whole integer GB rather than fractional. This gives it a granularity of MB so that a test for gb=0.1 works properly again. Signed-off-by: Gregory Smith <[email protected]> --- autotest/client/common_lib/hosts/base_classes.py 2011-02-09 14:25:38.000000000 -0800 +++ autotest/client/common_lib/hosts/base_classes.py 2011-02-16 22:38:22.000000000 -0800 @@ -228,11 +228,22 @@ def check_diskspace(self, path, gb): - # Note: 1 GB = 10**9 bytes (SI unit). + """Raises an error if path does not have at least gb GB free. + + @param path The path to check for free disk space. + @param gb A floating point number to compare with a granularity + of 1 MB. + + 1000 based SI units are used. + + @raises AutoservDiskFullHostError if path has less than gb GB free. + """ + one_mb = 10**6 # Bytes (SI unit). + mb_per_gb = 1000.0 logging.info('Checking for >= %s GB of space under %s on machine %s', gb, path, self.hostname) - df = self.run('df -PB %d %s | tail -1' % (10**9, path)).stdout.split() - free_space_gb = int(df[3]) + df = self.run('df -PB %d %s | tail -1' % (one_mb, path)).stdout.split() + free_space_gb = int(df[3])/mb_per_gb if free_space_gb < gb: raise error.AutoservDiskFullHostError(path, gb, free_space_gb) else: --- autotest/client/common_lib/hosts/base_classes_unittest.py 2009-07-17 15:40:18.000000000 -0700 +++ autotest/client/common_lib/hosts/base_classes_unittest.py 2011-02-16 22:38:22.000000000 -0800 @@ -3,6 +3,7 @@ import unittest import common +from autotest_lib.client.common_lib import error, utils from autotest_lib.client.common_lib.test_utils import mock from autotest_lib.client.common_lib.hosts import base_classes @@ -21,5 +22,22 @@ self.assertRaises(NotImplementedError, host.run_output, "fake command") + def test_check_diskspace(self): + self.god.stub_function(base_classes.Host, 'run') + host = base_classes.Host() + host.hostname = 'unittest-host' + test_df_tail = ('/dev/sda1 1061 939' + ' 123 89% /') + fake_cmd_status = utils.CmdResult(exit_status=0, stdout=test_df_tail) + host.run.expect_call('df -PB 1000000 /foo | tail -1').and_return( + fake_cmd_status) + self.assertRaises(error.AutoservDiskFullHostError, + host.check_diskspace, '/foo', 0.2) + host.run.expect_call('df -PB 1000000 /foo | tail -1').and_return( + fake_cmd_status) + host.check_diskspace('/foo', 0.1) + self.god.check_playback() + + if __name__ == "__main__": unittest.main() _______________________________________________ Autotest mailing list [email protected] http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
