Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 1 | # Copyright 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import os |
| 16 | import shutil |
| 17 | import subprocess |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 18 | import sys |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 19 | |
| 20 | from cliff import command |
| 21 | from oslo_log import log as logging |
| 22 | from six import moves |
| 23 | |
| 24 | LOG = logging.getLogger(__name__) |
| 25 | |
| 26 | TESTR_CONF = """[DEFAULT] |
| 27 | test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \\ |
| 28 | OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \\ |
| 29 | OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-500} \\ |
| 30 | ${PYTHON:-python} -m subunit.run discover -t %s %s $LISTOPT $IDOPTION |
| 31 | test_id_option=--load-list $IDFILE |
| 32 | test_list_option=--list |
| 33 | group_regex=([^\.]*\.)* |
| 34 | """ |
| 35 | |
| 36 | |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 37 | def get_tempest_default_config_dir(): |
| 38 | """Returns the correct default config dir to support both cases of |
| 39 | tempest being or not installed in a virtualenv. |
| 40 | Cases considered: |
| 41 | - no virtual env, python2: real_prefix and base_prefix not set |
| 42 | - no virtual env, python3: real_prefix not set, base_prefix set and |
| 43 | identical to prefix |
| 44 | - virtualenv, python2: real_prefix and prefix are set and different |
| 45 | - virtualenv, python3: real_prefix not set, base_prefix and prefix are |
| 46 | set and identical |
| 47 | - pyvenv, any python version: real_prefix not set, base_prefix and prefix |
| 48 | are set and different |
| 49 | |
| 50 | :return: default config dir |
| 51 | """ |
| 52 | real_prefix = getattr(sys, 'real_prefix', None) |
| 53 | base_prefix = getattr(sys, 'base_prefix', None) |
| 54 | prefix = sys.prefix |
| 55 | if real_prefix is None and base_prefix is None: |
| 56 | # Not running in a virtual environnment of any kind |
| 57 | return '/etc/tempest' |
| 58 | elif (real_prefix is None and base_prefix is not None and |
| 59 | base_prefix == prefix): |
| 60 | # Probably not running in a virtual environment |
| 61 | # NOTE(andreaf) we cannot distinguish this case from the case of |
| 62 | # a virtual environment created with virtualenv, and running python3. |
| 63 | return '/etc/tempest' |
| 64 | else: |
| 65 | return os.path.join(sys.prefix, 'etc/tempest') |
| 66 | |
| 67 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 68 | class TempestInit(command.Command): |
| 69 | """Setup a local working environment for running tempest""" |
| 70 | |
| 71 | def get_parser(self, prog_name): |
| 72 | parser = super(TempestInit, self).get_parser(prog_name) |
| 73 | parser.add_argument('dir', nargs='?', default=os.getcwd()) |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 74 | parser.add_argument('--config-dir', '-c', default=None) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 75 | return parser |
| 76 | |
| 77 | def generate_testr_conf(self, local_path): |
| 78 | testr_conf_path = os.path.join(local_path, '.testr.conf') |
| 79 | top_level_path = os.path.dirname(os.path.dirname(__file__)) |
| 80 | discover_path = os.path.join(top_level_path, 'test_discover') |
| 81 | testr_conf = TESTR_CONF % (top_level_path, discover_path) |
| 82 | with open(testr_conf_path, 'w+') as testr_conf_file: |
| 83 | testr_conf_file.write(testr_conf) |
| 84 | |
| 85 | def update_local_conf(self, conf_path, lock_dir, log_dir): |
| 86 | config_parse = moves.configparser.SafeConfigParser() |
| 87 | config_parse.optionxform = str |
| 88 | with open(conf_path, 'w+') as conf_file: |
| 89 | config_parse.readfp(conf_file) |
| 90 | # Set local lock_dir in tempest conf |
| 91 | if not config_parse.has_section('oslo_concurrency'): |
| 92 | config_parse.add_section('oslo_concurrency') |
| 93 | config_parse.set('oslo_concurrency', 'lock_path', lock_dir) |
| 94 | # Set local log_dir in tempest conf |
| 95 | config_parse.set('DEFAULT', 'log_dir', log_dir) |
| 96 | # Set default log filename to tempest.log |
| 97 | config_parse.set('DEFAULT', 'log_file', 'tempest.log') |
| 98 | |
| 99 | def copy_config(self, etc_dir, config_dir): |
| 100 | shutil.copytree(config_dir, etc_dir) |
| 101 | |
| 102 | def create_working_dir(self, local_dir, config_dir): |
| 103 | # Create local dir if missing |
| 104 | if not os.path.isdir(local_dir): |
| 105 | LOG.debug('Creating local working dir: %s' % local_dir) |
| 106 | os.mkdir(local_dir) |
| 107 | lock_dir = os.path.join(local_dir, 'tempest_lock') |
| 108 | etc_dir = os.path.join(local_dir, 'etc') |
| 109 | config_path = os.path.join(etc_dir, 'tempest.conf') |
| 110 | log_dir = os.path.join(local_dir, 'logs') |
| 111 | testr_dir = os.path.join(local_dir, '.testrepository') |
| 112 | # Create lock dir |
| 113 | if not os.path.isdir(lock_dir): |
| 114 | LOG.debug('Creating lock dir: %s' % lock_dir) |
| 115 | os.mkdir(lock_dir) |
| 116 | # Create log dir |
| 117 | if not os.path.isdir(log_dir): |
| 118 | LOG.debug('Creating log dir: %s' % log_dir) |
| 119 | os.mkdir(log_dir) |
| 120 | # Create and copy local etc dir |
| 121 | self.copy_config(etc_dir, config_dir) |
| 122 | # Update local confs to reflect local paths |
| 123 | self.update_local_conf(config_path, lock_dir, log_dir) |
| 124 | # Generate a testr conf file |
| 125 | self.generate_testr_conf(local_dir) |
| 126 | # setup local testr working dir |
| 127 | if not os.path.isdir(testr_dir): |
| 128 | subprocess.call(['testr', 'init'], cwd=local_dir) |
| 129 | |
| 130 | def take_action(self, parsed_args): |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 131 | config_dir = parsed_args.config_dir or get_tempest_default_config_dir() |
| 132 | self.create_working_dir(parsed_args.dir, config_dir) |