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 | |
Cao Xuan Hoang | 36fe23c | 2016-08-25 16:11:14 +0700 | [diff] [blame] | 25 | from tempest.cmd import workspace |
step6829 | 80c14ec | 2016-02-23 14:53:52 -0500 | [diff] [blame] | 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 | |
Matthew Treinish | 3fe57b3 | 2016-06-21 14:39:00 -0400 | [diff] [blame] | 43 | There are 3 dirs that get tried in priority order. First is /etc/tempest, |
| 44 | if that doesn't exist it looks for a tempest dir in the XDG_CONFIG_HOME |
| 45 | dir (defaulting to ~/.config/tempest) and last it tries for a |
| 46 | ~/.tempest/etc directory. If none of these exist a ~/.tempest/etc |
| 47 | directory will be created. |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 48 | |
| 49 | :return: default config dir |
| 50 | """ |
Ken'ichi Ohmichi | 0ce1652 | 2016-04-23 12:52:05 -0700 | [diff] [blame] | 51 | global_conf_dir = '/etc/tempest' |
Matthew Treinish | 3fe57b3 | 2016-06-21 14:39:00 -0400 | [diff] [blame] | 52 | xdg_config = os.environ.get('XDG_CONFIG_HOME', |
| 53 | os.path.expanduser('~/.config')) |
| 54 | user_xdg_global_path = os.path.join(xdg_config, 'tempest') |
| 55 | user_global_path = os.path.join(os.path.expanduser('~'), '.tempest/etc') |
| 56 | if os.path.isdir(global_conf_dir): |
Ken'ichi Ohmichi | 0ce1652 | 2016-04-23 12:52:05 -0700 | [diff] [blame] | 57 | return global_conf_dir |
Matthew Treinish | 3fe57b3 | 2016-06-21 14:39:00 -0400 | [diff] [blame] | 58 | elif os.path.isdir(user_xdg_global_path): |
| 59 | return user_xdg_global_path |
| 60 | elif os.path.isdir(user_global_path): |
| 61 | return user_global_path |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 62 | else: |
Matthew Treinish | 3fe57b3 | 2016-06-21 14:39:00 -0400 | [diff] [blame] | 63 | os.makedirs(user_global_path) |
| 64 | return user_global_path |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 65 | |
| 66 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 67 | class TempestInit(command.Command): |
| 68 | """Setup a local working environment for running tempest""" |
| 69 | |
| 70 | def get_parser(self, prog_name): |
| 71 | parser = super(TempestInit, self).get_parser(prog_name) |
Masayuki Igawa | 0670a2b | 2016-09-12 14:55:11 +0900 | [diff] [blame] | 72 | parser.add_argument('dir', nargs='?', default=os.getcwd(), |
| 73 | help="The path to the workspace directory. If you " |
| 74 | "omit this argument, the workspace directory is " |
| 75 | "your current directory") |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 76 | parser.add_argument('--config-dir', '-c', default=None) |
Matthew Treinish | 054f45d | 2016-04-23 16:30:29 -0400 | [diff] [blame] | 77 | parser.add_argument('--show-global-config-dir', '-s', |
| 78 | action='store_true', dest='show_global_dir', |
| 79 | help="Print the global config dir location, " |
| 80 | "then exit") |
step6829 | 80c14ec | 2016-02-23 14:53:52 -0500 | [diff] [blame] | 81 | parser.add_argument('--name', help="The workspace name", default=None) |
| 82 | parser.add_argument('--workspace-path', default=None, |
| 83 | help="The path to the workspace file, the default " |
Masayuki Igawa | 0670a2b | 2016-09-12 14:55:11 +0900 | [diff] [blame] | 84 | "is ~/.tempest/workspace.yaml") |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 85 | return parser |
| 86 | |
| 87 | def generate_testr_conf(self, local_path): |
| 88 | testr_conf_path = os.path.join(local_path, '.testr.conf') |
| 89 | top_level_path = os.path.dirname(os.path.dirname(__file__)) |
| 90 | discover_path = os.path.join(top_level_path, 'test_discover') |
| 91 | testr_conf = TESTR_CONF % (top_level_path, discover_path) |
| 92 | with open(testr_conf_path, 'w+') as testr_conf_file: |
| 93 | testr_conf_file.write(testr_conf) |
| 94 | |
Matthew Treinish | 3c39bb6 | 2016-08-09 14:44:44 -0400 | [diff] [blame] | 95 | def get_configparser(self, conf_path): |
Janonymous | 8254a3f | 2016-09-15 10:38:48 +0530 | [diff] [blame] | 96 | config_parse = moves.configparser.ConfigParser() |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 97 | config_parse.optionxform = str |
Matthew Treinish | 3c39bb6 | 2016-08-09 14:44:44 -0400 | [diff] [blame] | 98 | # get any existing values if a config file already exists |
| 99 | if os.path.isfile(conf_path): |
| 100 | # use read() for Python 2 and 3 compatibility |
| 101 | config_parse.read(conf_path) |
| 102 | return config_parse |
| 103 | |
| 104 | def update_local_conf(self, conf_path, lock_dir, log_dir): |
| 105 | config_parse = self.get_configparser(conf_path) |
| 106 | # Set local lock_dir in tempest conf |
| 107 | if not config_parse.has_section('oslo_concurrency'): |
| 108 | config_parse.add_section('oslo_concurrency') |
| 109 | config_parse.set('oslo_concurrency', 'lock_path', lock_dir) |
| 110 | # Set local log_dir in tempest conf |
| 111 | config_parse.set('DEFAULT', 'log_dir', log_dir) |
| 112 | # Set default log filename to tempest.log |
| 113 | config_parse.set('DEFAULT', 'log_file', 'tempest.log') |
| 114 | |
| 115 | # write out a new file with the updated configurations |
| 116 | with open(conf_path, 'w+') as conf_file: |
ghanshyam | 0e1dd84 | 2016-04-27 07:59:23 +0900 | [diff] [blame] | 117 | config_parse.write(conf_file) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 118 | |
| 119 | def copy_config(self, etc_dir, config_dir): |
Matthew Treinish | d5cef95 | 2016-06-07 16:54:55 -0400 | [diff] [blame] | 120 | if os.path.isdir(config_dir): |
| 121 | shutil.copytree(config_dir, etc_dir) |
| 122 | else: |
| 123 | LOG.warning("Global config dir %s can't be found" % config_dir) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 124 | |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame] | 125 | def generate_sample_config(self, local_dir): |
| 126 | conf_generator = os.path.join(os.path.dirname(__file__), |
| 127 | 'config-generator.tempest.conf') |
| 128 | output_file = os.path.join(local_dir, 'etc/tempest.conf.sample') |
| 129 | if os.path.isfile(conf_generator): |
| 130 | generator.main(['--config-file', conf_generator, '--output-file', |
| 131 | output_file]) |
Matthew Treinish | d5cef95 | 2016-06-07 16:54:55 -0400 | [diff] [blame] | 132 | else: |
| 133 | LOG.warning("Skipping sample config generation because global " |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame] | 134 | "config file %s can't be found" % conf_generator) |
Matthew Treinish | c8a39b4 | 2015-07-27 17:07:37 -0400 | [diff] [blame] | 135 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 136 | def create_working_dir(self, local_dir, config_dir): |
Jake Yip | 9346483 | 2016-06-18 00:57:40 +1000 | [diff] [blame] | 137 | # make sure we are working with abspath however tempest init is called |
| 138 | local_dir = os.path.abspath(local_dir) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 139 | # Create local dir if missing |
| 140 | if not os.path.isdir(local_dir): |
| 141 | LOG.debug('Creating local working dir: %s' % local_dir) |
| 142 | os.mkdir(local_dir) |
Marc Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 143 | elif not os.listdir(local_dir) == []: |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 144 | raise OSError("Directory you are trying to initialize already " |
Marc Koderer | 090b5dc | 2015-11-04 10:35:48 +0100 | [diff] [blame] | 145 | "exists and is not empty: %s" % local_dir) |
David Paterson | 0bf52d4 | 2015-04-13 21:55:58 -0400 | [diff] [blame] | 146 | |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 147 | lock_dir = os.path.join(local_dir, 'tempest_lock') |
| 148 | etc_dir = os.path.join(local_dir, 'etc') |
| 149 | config_path = os.path.join(etc_dir, 'tempest.conf') |
| 150 | log_dir = os.path.join(local_dir, 'logs') |
| 151 | testr_dir = os.path.join(local_dir, '.testrepository') |
| 152 | # Create lock dir |
| 153 | if not os.path.isdir(lock_dir): |
| 154 | LOG.debug('Creating lock dir: %s' % lock_dir) |
| 155 | os.mkdir(lock_dir) |
| 156 | # Create log dir |
| 157 | if not os.path.isdir(log_dir): |
| 158 | LOG.debug('Creating log dir: %s' % log_dir) |
| 159 | os.mkdir(log_dir) |
| 160 | # Create and copy local etc dir |
| 161 | self.copy_config(etc_dir, config_dir) |
Matthew Treinish | c8a39b4 | 2015-07-27 17:07:37 -0400 | [diff] [blame] | 162 | # Generate the sample config file |
Matthew Treinish | bdef1c7 | 2016-06-21 18:06:49 -0400 | [diff] [blame] | 163 | self.generate_sample_config(local_dir) |
Matthew Treinish | f610aca | 2015-06-30 15:32:34 -0400 | [diff] [blame] | 164 | # Update local confs to reflect local paths |
| 165 | self.update_local_conf(config_path, lock_dir, log_dir) |
| 166 | # Generate a testr conf file |
| 167 | self.generate_testr_conf(local_dir) |
| 168 | # setup local testr working dir |
| 169 | if not os.path.isdir(testr_dir): |
| 170 | subprocess.call(['testr', 'init'], cwd=local_dir) |
| 171 | |
| 172 | def take_action(self, parsed_args): |
Cao Xuan Hoang | 36fe23c | 2016-08-25 16:11:14 +0700 | [diff] [blame] | 173 | workspace_manager = workspace.WorkspaceManager( |
| 174 | parsed_args.workspace_path) |
step6829 | 80c14ec | 2016-02-23 14:53:52 -0500 | [diff] [blame] | 175 | name = parsed_args.name or parsed_args.dir.split(os.path.sep)[-1] |
| 176 | workspace_manager.register_new_workspace( |
| 177 | name, parsed_args.dir, init=True) |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 178 | config_dir = parsed_args.config_dir or get_tempest_default_config_dir() |
Matthew Treinish | 054f45d | 2016-04-23 16:30:29 -0400 | [diff] [blame] | 179 | if parsed_args.show_global_dir: |
| 180 | print("Global config dir is located at: %s" % config_dir) |
| 181 | sys.exit(0) |
Andrea Frittoli (andreaf) | 5a69e55 | 2015-07-31 18:40:17 +0100 | [diff] [blame] | 182 | self.create_working_dir(parsed_args.dir, config_dir) |