1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import commands
6import os
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9
10class build_RootFilesystemSize(test.test):
11    """Test that we have a minimal amount of free space on rootfs."""
12    version = 1
13
14
15    PROD_IMAGE_ROOTFS_BYTES = '/root/bytes-rootfs-prod'
16
17
18    def run_once(self):
19        """Run the free space on rootfs test."""
20
21        # Report the production size.
22        if os.path.exists(self.PROD_IMAGE_ROOTFS_BYTES):
23            rootfs_bytes_str = ''
24            with open(self.PROD_IMAGE_ROOTFS_BYTES, 'r') as f:
25                rootfs_bytes_str = f.read()
26            if rootfs_bytes_str:
27                self.output_perf_value('bytes_rootfs_prod',
28                                       value=float(rootfs_bytes_str),
29                                       units='bytes',
30                                       higher_is_better=False)
31
32        # Report the current (test) size.
33        (status, output) = commands.getstatusoutput(
34            'df -B1 --print-type / | tail -1')
35        if status != 0:
36            raise error.TestFail('Could not get size of rootfs')
37
38        # Expected output format:
39        # Filesystem     Type     1B-blocks      Used Available Use% Mounted on
40        # /dev/root      ext2    1056858112 768479232 288378880  73% /
41        output_columns = output.split()
42        fs_type = output_columns[1]
43        used = int(output_columns[3])
44        free = int(output_columns[4])
45
46        self.output_perf_value('bytes_rootfs_test', value=float(used),
47                               units='bytes', higher_is_better=False)
48
49        # Ignore squashfs as the free space will be reported as 0.
50        if fs_type == 'squashfs':
51            return
52
53        # Fail if we are running out of free space on rootfs (15 MiB or
54        # 2% free space).
55        required_free_space = min(15 * 1024 * 1024, used * 0.02)
56
57        if free < required_free_space:
58            raise error.TestFail('%s bytes free is less than the %s required.' %
59                                 (free, required_free_space))
60