1#!/usr/bin/python2
2#
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""
8This testcase exercises the file system by ensuring we can create a sufficient
9number of files into one directory. In this case we will create 150,000 files on
10the stateful partition and 2,000 files on the /tmp partition.
11"""
12
13__author__ = ['kdlucas@chromium.org (Kelly Lucas)',
14              'dalecurtis@chromium.org (Dale Curtis)']
15
16import os
17import shutil
18
19from autotest_lib.client.bin import test
20from autotest_lib.client.common_lib import error
21
22
23class platform_FileNum(test.test):
24    """Test file number limitations per directory."""
25    version = 1
26
27    _TEST_PLAN = [
28        {'dir': '/mnt/stateful_partition', 'count': 150000},
29        {'dir': '/tmp', 'count': 2000}]
30
31    _TEST_TEXT = 'ChromeOS rocks with fast response and low maintenance costs!'
32
33    def create_files(self, target_dir, count):
34        """Create the number of files specified by count in target_dir.
35
36        Args:
37            target_dir: Directory to create files in.
38            count: Number of files to create.
39        Returns:
40            Number of files created.
41        """
42        create_dir = os.path.join(target_dir, 'createdir')
43        try:
44            if os.path.exists(create_dir):
45                shutil.rmtree(create_dir)
46
47            os.makedirs(create_dir)
48
49            for i in xrange(count):
50                f = open(os.path.join(create_dir, '%d.txt' % i), 'w')
51                f.write(self._TEST_TEXT)
52                f.close()
53
54            total_created = len(os.listdir(create_dir))
55        finally:
56            shutil.rmtree(create_dir)
57
58        return total_created
59
60    def run_once(self):
61        for item in self._TEST_PLAN:
62            actual_count = self.create_files(item['dir'], item['count'])
63            if actual_count != item['count']:
64                raise error.TestFail(
65                    'File creation count in %s is incorrect! Found %d files '
66                    'when there should have been %d!'
67                    % (item['dir'], actual_count, item['count']))
68