• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import logging, os, shutil, tempfile
7from autotest_lib.client.bin import fio_util, test, utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import cryptohome
10
11CRYPTOHOMESTRESS_START = '/tmp/cryptohomestress_begin'
12CRYPTOHOMESTRESS_END = '/tmp/cryptohomestress_end'
13TEST_USER = 'test@chromium.org'
14TEST_PASSWORD = 'test'
15
16class platform_CryptohomeFio(test.test):
17    """Run FIO in the crypto partition."""
18
19    version = 2
20
21    USE_CRYPTO = 'crypto'
22    USE_PLAIN = 'plain'
23    USE_TMPFS = 'tmpfs'
24    DISK_CONFIG_KEYS = [ USE_CRYPTO, USE_PLAIN, USE_TMPFS ]
25
26    def initialize(self, from_internal_disk_only=False):
27        """ Check that we are running on the fixed device"""
28        if from_internal_disk_only and not utils.is_booted_from_internal_disk():
29            raise error.TestNAError('Test only on internal disk')
30
31        if os.path.exists(CRYPTOHOMESTRESS_END):
32            os.unlink(CRYPTOHOMESTRESS_END)
33        open(CRYPTOHOMESTRESS_START, 'w').close()
34
35    def run_once(self, runtime, disk_configs,
36                 script=None, sysctls_list=None):
37        """
38        Create a 300MB file in tmpfs/encrypted/unencrypted location
39        and run fio test.
40
41        @param disk_configs: list of keys from DISK_CONFIG_KEYS.
42        @param script: fio script to run
43        @param sysctls_list: list of dictionary of sysctls to alter.
44        """
45        if not set(disk_configs).issubset(set(self.DISK_CONFIG_KEYS)):
46            raise error.TestFail('Unknown keys in disk config')
47        for config in disk_configs:
48            for sysctls in sysctls_list or [ {} ]:
49                graph_descr = ''
50                for key, val in list(sysctls.items()):
51                    utils.sysctl(key, val)
52                    graph_descr += '-'.join([os.path.basename(key), str(val)])
53                # Mount a test cryptohome vault.
54                if config == self.USE_CRYPTO:
55                    cryptohome.mount_vault(TEST_USER, TEST_PASSWORD,
56                                           create=True)
57                    tmpdir = cryptohome.user_path(TEST_USER)
58                elif config == self.USE_TMPFS:
59                    tmpdir = None
60                else:
61                    tmpdir = self.tmpdir
62                self.__work_dir = tempfile.mkdtemp(dir=tmpdir)
63
64                results = {}
65                # TODO make these parameters to run_once & check disk for space.
66                self.__filesize = '300m'
67                self.__runtime = str(runtime)
68                env_vars = ' '.join(
69                    ['FILENAME=' + os.path.join(self.__work_dir, script),
70                     'FILESIZE=' + self.__filesize,
71                     'RUN_TIME=' + self.__runtime
72                     ])
73                job_file = os.path.join(self.bindir, script)
74                results.update(fio_util.fio_runner(self, job_file, env_vars,
75                    name_prefix=graph_descr + config))
76                self.write_perf_keyval(results)
77
78                logging.info('Finished with FS stress, cleaning up.')
79                if config == self.USE_CRYPTO:
80                    cryptohome.unmount_vault(TEST_USER)
81                    cryptohome.remove_vault(TEST_USER)
82                else:
83                    shutil.rmtree(self.__work_dir)
84
85    def cleanup(self):
86        open(CRYPTOHOMESTRESS_END, 'w').close()
87        if os.path.exists(CRYPTOHOMESTRESS_START):
88            os.unlink(CRYPTOHOMESTRESS_START)
89