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 |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame^] | 21 | from oslo_config import generator |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 22 | from oslo_log import log as logging |
| 23 | from six import moves |
| 24 | |
step6829 | 80c14ec | 2016-02-23 14:53:52 -0500 | [diff] [blame] | 25 | from tempest.cmd.workspace import WorkspaceManager |
| 26 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 27 | LOG = logging.getLogger(__name__) |
| 28 | |
| 29 | TESTR_CONF = """[DEFAULT] |
| 30 | test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \\ |
| 31 | OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \\ |
| 32 | OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-500} \\ |
| 33 | ${PYTHON:-python} -m subunit.run discover -t %s %s $LISTOPT $IDOPTION |
| 34 | test_id_option=--load-list $IDFILE |
| 35 | test_list_option=--list |
| 36 | group_regex=([^\.]*\.)* |
| 37 | """ |
| 38 | |
| 39 | |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 40 | def get_tempest_default_config_dir(): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 41 | """Get default config directory of tempest |
| 42 | |
| 43 | Returns the correct default config dir to support both cases of |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 44 | tempest being or not installed in a virtualenv. |
| 45 | Cases considered: |
| 46 | - no virtual env, python2: real_prefix and base_prefix not set |
| 47 | - no virtual env, python3: real_prefix not set, base_prefix set and |
| 48 | identical to prefix |
| 49 | - virtualenv, python2: real_prefix and prefix are set and different |
| 50 | - virtualenv, python3: real_prefix not set, base_prefix and prefix are |
| 51 | set and identical |
| 52 | - pyvenv, any python version: real_prefix not set, base_prefix and prefix |
| 53 | are set and different |
| 54 | |
| 55 | :return: default config dir |
| 56 | """ |
| 57 | real_prefix = getattr(sys, 'real_prefix', None) |
| 58 | base_prefix = getattr(sys, 'base_prefix', None) |
| 59 | prefix = sys.prefix |
Ken'ichi Ohmichi | 0ce1652 | 2016-04-23 12:52:05 -0700 | [diff] [blame] | 60 | global_conf_dir = '/etc/tempest' |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 61 | if (real_prefix is None and |
Ken'ichi Ohmichi | 0ce1652 | 2016-04-23 12:52:05 -0700 | [diff] [blame] | 62 | (base_prefix is None or base_prefix == prefix) and |
| 63 | os.path.isdir(global_conf_dir)): |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 64 | # Probably not running in a virtual environment. |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 65 | # NOTE(andreaf) we cannot distinguish this case from the case of |
| 66 | # a virtual environment created with virtualenv, and running python3. |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 67 | # Also if it appears we are not in virtual env and fail to find |
| 68 | # global config: '/etc/tempest', fall back to |
| 69 | # '[sys.prefix]/etc/tempest' |
Ken'ichi Ohmichi | 0ce1652 | 2016-04-23 12:52:05 -0700 | [diff] [blame] | 70 | return global_conf_dir |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 71 | else: |
Ken'ichi Ohmichi | 7e054d4 | 2016-04-23 13:00:08 -0700 | [diff] [blame] | 72 | conf_dir = os.path.join(prefix, 'etc/tempest') |
| 73 | if os.path.isdir(conf_dir): |
| 74 | return conf_dir |
| 75 | else: |
| 76 | # NOTE: The prefix is gotten from the path which pyconfig.h is |
| 77 | # installed under. Some envs contain it under /usr/include, not |
| 78 | # /user/local/include. Then prefix becomes /usr on such envs. |
| 79 | # However, etc/tempest is installed under /usr/local and the bove |
| 80 | # path logic mismatches. This is a workaround for such envs. |
| 81 | return os.path.join(prefix, 'local/etc/tempest') |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 82 | |
| 83 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 84 | class TempestInit(command.Command): |
| 85 | """Setup a local working environment for running tempest""" |
| 86 | |
| 87 | def get_parser(self, prog_name): |
| 88 | parser = super(TempestInit, self).get_parser(prog_name) |
| 89 | parser.add_argument('dir', nargs='?', default=os.getcwd()) |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 90 | parser.add_argument('--config-dir', '-c', default=None) |
Matthew Treinish | 054f45d | 2016-04-23 16:30:29 -0400 | [diff] [blame] | 91 | parser.add_argument('--show-global-config-dir', '-s', |
| 92 | action='store_true', dest='show_global_dir', |
| 93 | help="Print the global config dir location, " |
| 94 | "then exit") |
step6829 | 80c14ec | 2016-02-23 14:53:52 -0500 | [diff] [blame] | 95 | parser.add_argument('--name', help="The workspace name", default=None) |
| 96 | parser.add_argument('--workspace-path', default=None, |
| 97 | help="The path to the workspace file, the default " |
| 98 | "is ~/.tempest/workspace") |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 99 | return parser |
| 100 | |
| 101 | def generate_testr_conf(self, local_path): |
| 102 | testr_conf_path = os.path.join(local_path, '.testr.conf') |
| 103 | top_level_path = os.path.dirname(os.path.dirname(__file__)) |
| 104 | discover_path = os.path.join(top_level_path, 'test_discover') |
| 105 | testr_conf = TESTR_CONF % (top_level_path, discover_path) |
| 106 | with open(testr_conf_path, 'w+') as testr_conf_file: |
| 107 | testr_conf_file.write(testr_conf) |
| 108 | |
| 109 | def update_local_conf(self, conf_path, lock_dir, log_dir): |
| 110 | config_parse = moves.configparser.SafeConfigParser() |
| 111 | config_parse.optionxform = str |
Masayuki Igawa | 63cb9a3 | 2016-04-27 06:23:36 +0900 | [diff] [blame] | 112 | with open(conf_path, 'a+') as conf_file: |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 113 | # Set local lock_dir in tempest conf |
| 114 | if not config_parse.has_section('oslo_concurrency'): |
| 115 | config_parse.add_section('oslo_concurrency') |
| 116 | config_parse.set('oslo_concurrency', 'lock_path', lock_dir) |
| 117 | # Set local log_dir in tempest conf |
| 118 | config_parse.set('DEFAULT', 'log_dir', log_dir) |
| 119 | # Set default log filename to tempest.log |
| 120 | config_parse.set('DEFAULT', 'log_file', 'tempest.log') |
ghanshyam | 0e1dd84 | 2016-04-27 07:59:23 +0900 | [diff] [blame] | 121 | config_parse.write(conf_file) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 122 | |
| 123 | def copy_config(self, etc_dir, config_dir): |
Matthew Treinish | d5cef95 | 2016-06-07 16:54:55 -0400 | [diff] [blame] | 124 | if os.path.isdir(config_dir): |
| 125 | shutil.copytree(config_dir, etc_dir) |
| 126 | else: |
| 127 | LOG.warning("Global config dir %s can't be found" % config_dir) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 128 | |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame^] | 129 | def generate_sample_config(self, local_dir): |
| 130 | conf_generator = os.path.join(os.path.dirname(__file__), |
| 131 | 'config-generator.tempest.conf') |
| 132 | output_file = os.path.join(local_dir, 'etc/tempest.conf.sample') |
| 133 | if os.path.isfile(conf_generator): |
| 134 | generator.main(['--config-file', conf_generator, '--output-file', |
| 135 | output_file]) |
Matthew Treinish | d5cef95 | 2016-06-07 16:54:55 -0400 | [diff] [blame] | 136 | else: |
| 137 | LOG.warning("Skipping sample config generation because global " |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame^] | 138 | "config file %s can't be found" % conf_generator) |
Matthew Treinish | c8a39b4 | 2015-07-27 17:07:37 -0400 | [diff] [blame] | 139 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 140 | def create_working_dir(self, local_dir, config_dir): |
| 141 | # Create local dir if missing |
| 142 | if not os.path.isdir(local_dir): |
| 143 | LOG.debug('Creating local working dir: %s' % local_dir) |
| 144 | os.mkdir(local_dir) |
Marc Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 145 | elif not os.listdir(local_dir) == []: |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 146 | raise OSError("Directory you are trying to initialize already " |
Marc Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 147 | "exists and is not empty: %s" % local_dir) |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 148 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 149 | lock_dir = os.path.join(local_dir, 'tempest_lock') |
| 150 | etc_dir = os.path.join(local_dir, 'etc') |
| 151 | config_path = os.path.join(etc_dir, 'tempest.conf') |
| 152 | log_dir = os.path.join(local_dir, 'logs') |
| 153 | testr_dir = os.path.join(local_dir, '.testrepository') |
| 154 | # Create lock dir |
| 155 | if not os.path.isdir(lock_dir): |
| 156 | LOG.debug('Creating lock dir: %s' % lock_dir) |
| 157 | os.mkdir(lock_dir) |
| 158 | # Create log dir |
| 159 | if not os.path.isdir(log_dir): |
| 160 | LOG.debug('Creating log dir: %s' % log_dir) |
| 161 | os.mkdir(log_dir) |
| 162 | # Create and copy local etc dir |
| 163 | self.copy_config(etc_dir, config_dir) |
Matthew Treinish | c8a39b4 | 2015-07-27 17:07:37 -0400 | [diff] [blame] | 164 | # Generate the sample config file |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame^] | 165 | self.generate_sample_config(local_dir) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 166 | # Update local confs to reflect local paths |
| 167 | self.update_local_conf(config_path, lock_dir, log_dir) |
| 168 | # Generate a testr conf file |
| 169 | self.generate_testr_conf(local_dir) |
| 170 | # setup local testr working dir |
| 171 | if not os.path.isdir(testr_dir): |
| 172 | subprocess.call(['testr', 'init'], cwd=local_dir) |
| 173 | |
| 174 | def take_action(self, parsed_args): |
step6829 | 80c14ec | 2016-02-23 14:53:52 -0500 | [diff] [blame] | 175 | workspace_manager = WorkspaceManager(parsed_args.workspace_path) |
| 176 | name = parsed_args.name or parsed_args.dir.split(os.path.sep)[-1] |
| 177 | workspace_manager.register_new_workspace( |
| 178 | name, parsed_args.dir, init=True) |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 179 | config_dir = parsed_args.config_dir or get_tempest_default_config_dir() |
Matthew Treinish | 054f45d | 2016-04-23 16:30:29 -0400 | [diff] [blame] | 180 | if parsed_args.show_global_dir: |
| 181 | print("Global config dir is located at: %s" % config_dir) |
| 182 | sys.exit(0) |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 183 | self.create_working_dir(parsed_args.dir, config_dir) |