blob: d84f3a33455401b054ad289a173f3a94610c5924 [file] [log] [blame]
Matthew Treinishf610aca2015-06-30 15:32:34 -04001# 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
15import os
16import shutil
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +010017import sys
Matthew Treinishf610aca2015-06-30 15:32:34 -040018
19from cliff import command
Matthew Treinishbdef1c72016-06-21 18:06:49 -040020from oslo_config import generator
Matthew Treinishf610aca2015-06-30 15:32:34 -040021from oslo_log import log as logging
22from six import moves
Chandan Kumar8a4396e2017-09-15 12:18:10 +053023from stestr import commands
Matthew Treinishf610aca2015-06-30 15:32:34 -040024
Cao Xuan Hoang36fe23c2016-08-25 16:11:14 +070025from tempest.cmd import workspace
step682980c14ec2016-02-23 14:53:52 -050026
Matthew Treinishf610aca2015-06-30 15:32:34 -040027LOG = logging.getLogger(__name__)
28
Stephen Finucane7f4a6212018-07-06 13:58:21 +010029STESTR_CONF = r"""[DEFAULT]
Chandan Kumar8a4396e2017-09-15 12:18:10 +053030test_path=%s
31top_dir=%s
Matthew Treinishf610aca2015-06-30 15:32:34 -040032group_regex=([^\.]*\.)*
33"""
34
35
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +010036def get_tempest_default_config_dir():
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +000037 """Get default config directory of tempest
38
Matthew Treinish3fe57b32016-06-21 14:39:00 -040039 There are 3 dirs that get tried in priority order. First is /etc/tempest,
40 if that doesn't exist it looks for a tempest dir in the XDG_CONFIG_HOME
41 dir (defaulting to ~/.config/tempest) and last it tries for a
42 ~/.tempest/etc directory. If none of these exist a ~/.tempest/etc
43 directory will be created.
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +010044
45 :return: default config dir
46 """
Ken'ichi Ohmichi0ce16522016-04-23 12:52:05 -070047 global_conf_dir = '/etc/tempest'
Matthew Treinish3fe57b32016-06-21 14:39:00 -040048 xdg_config = os.environ.get('XDG_CONFIG_HOME',
49 os.path.expanduser('~/.config'))
50 user_xdg_global_path = os.path.join(xdg_config, 'tempest')
51 user_global_path = os.path.join(os.path.expanduser('~'), '.tempest/etc')
52 if os.path.isdir(global_conf_dir):
Ken'ichi Ohmichi0ce16522016-04-23 12:52:05 -070053 return global_conf_dir
Matthew Treinish3fe57b32016-06-21 14:39:00 -040054 elif os.path.isdir(user_xdg_global_path):
55 return user_xdg_global_path
56 elif os.path.isdir(user_global_path):
57 return user_global_path
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +010058 else:
Matthew Treinish3fe57b32016-06-21 14:39:00 -040059 os.makedirs(user_global_path)
60 return user_global_path
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +010061
62
Matthew Treinishf610aca2015-06-30 15:32:34 -040063class TempestInit(command.Command):
64 """Setup a local working environment for running tempest"""
65
66 def get_parser(self, prog_name):
67 parser = super(TempestInit, self).get_parser(prog_name)
Masayuki Igawa0670a2b2016-09-12 14:55:11 +090068 parser.add_argument('dir', nargs='?', default=os.getcwd(),
69 help="The path to the workspace directory. If you "
70 "omit this argument, the workspace directory is "
71 "your current directory")
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +010072 parser.add_argument('--config-dir', '-c', default=None)
Matthew Treinish054f45d2016-04-23 16:30:29 -040073 parser.add_argument('--show-global-config-dir', '-s',
74 action='store_true', dest='show_global_dir',
75 help="Print the global config dir location, "
76 "then exit")
step682980c14ec2016-02-23 14:53:52 -050077 parser.add_argument('--name', help="The workspace name", default=None)
78 parser.add_argument('--workspace-path', default=None,
79 help="The path to the workspace file, the default "
Masayuki Igawa0670a2b2016-09-12 14:55:11 +090080 "is ~/.tempest/workspace.yaml")
Matthew Treinishf610aca2015-06-30 15:32:34 -040081 return parser
82
Chandan Kumar8a4396e2017-09-15 12:18:10 +053083 def generate_stestr_conf(self, local_path):
84 stestr_conf_path = os.path.join(local_path, '.stestr.conf')
Matthew Treinishf610aca2015-06-30 15:32:34 -040085 top_level_path = os.path.dirname(os.path.dirname(__file__))
86 discover_path = os.path.join(top_level_path, 'test_discover')
Chandan Kumar8a4396e2017-09-15 12:18:10 +053087 stestr_conf = STESTR_CONF % (discover_path, top_level_path)
88 with open(stestr_conf_path, 'w+') as stestr_conf_file:
89 stestr_conf_file.write(stestr_conf)
Matthew Treinishf610aca2015-06-30 15:32:34 -040090
Matthew Treinish3c39bb62016-08-09 14:44:44 -040091 def get_configparser(self, conf_path):
Janonymous8254a3f2016-09-15 10:38:48 +053092 config_parse = moves.configparser.ConfigParser()
Matthew Treinishf610aca2015-06-30 15:32:34 -040093 config_parse.optionxform = str
Matthew Treinish3c39bb62016-08-09 14:44:44 -040094 # get any existing values if a config file already exists
95 if os.path.isfile(conf_path):
96 # use read() for Python 2 and 3 compatibility
97 config_parse.read(conf_path)
98 return config_parse
99
100 def update_local_conf(self, conf_path, lock_dir, log_dir):
101 config_parse = self.get_configparser(conf_path)
102 # Set local lock_dir in tempest conf
103 if not config_parse.has_section('oslo_concurrency'):
104 config_parse.add_section('oslo_concurrency')
105 config_parse.set('oslo_concurrency', 'lock_path', lock_dir)
106 # Set local log_dir in tempest conf
107 config_parse.set('DEFAULT', 'log_dir', log_dir)
108 # Set default log filename to tempest.log
109 config_parse.set('DEFAULT', 'log_file', 'tempest.log')
110
111 # write out a new file with the updated configurations
112 with open(conf_path, 'w+') as conf_file:
ghanshyam0e1dd842016-04-27 07:59:23 +0900113 config_parse.write(conf_file)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400114
115 def copy_config(self, etc_dir, config_dir):
Matthew Treinishd5cef952016-06-07 16:54:55 -0400116 if os.path.isdir(config_dir):
117 shutil.copytree(config_dir, etc_dir)
118 else:
Jordan Pittier525ec712016-12-07 17:51:26 +0100119 LOG.warning("Global config dir %s can't be found", config_dir)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400120
Matthew Treinishbdef1c72016-06-21 18:06:49 -0400121 def generate_sample_config(self, local_dir):
122 conf_generator = os.path.join(os.path.dirname(__file__),
123 'config-generator.tempest.conf')
124 output_file = os.path.join(local_dir, 'etc/tempest.conf.sample')
125 if os.path.isfile(conf_generator):
126 generator.main(['--config-file', conf_generator, '--output-file',
127 output_file])
Matthew Treinishd5cef952016-06-07 16:54:55 -0400128 else:
129 LOG.warning("Skipping sample config generation because global "
Jordan Pittier525ec712016-12-07 17:51:26 +0100130 "config file %s can't be found", conf_generator)
Matthew Treinishc8a39b42015-07-27 17:07:37 -0400131
Matthew Treinishf610aca2015-06-30 15:32:34 -0400132 def create_working_dir(self, local_dir, config_dir):
Jake Yip93464832016-06-18 00:57:40 +1000133 # make sure we are working with abspath however tempest init is called
134 local_dir = os.path.abspath(local_dir)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400135 # Create local dir if missing
136 if not os.path.isdir(local_dir):
Jordan Pittier525ec712016-12-07 17:51:26 +0100137 LOG.debug('Creating local working dir: %s', local_dir)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400138 os.mkdir(local_dir)
wangqi814a87c2018-04-19 02:31:40 +0000139 elif os.listdir(local_dir):
David Paterson0bf52d42015-04-13 21:55:58 -0400140 raise OSError("Directory you are trying to initialize already "
Marc Koderer090b5dc2015-11-04 10:35:48 +0100141 "exists and is not empty: %s" % local_dir)
David Paterson0bf52d42015-04-13 21:55:58 -0400142
Matthew Treinishf610aca2015-06-30 15:32:34 -0400143 lock_dir = os.path.join(local_dir, 'tempest_lock')
144 etc_dir = os.path.join(local_dir, 'etc')
145 config_path = os.path.join(etc_dir, 'tempest.conf')
146 log_dir = os.path.join(local_dir, 'logs')
Chandan Kumar8a4396e2017-09-15 12:18:10 +0530147 stestr_dir = os.path.join(local_dir, '.stestr')
Matthew Treinishf610aca2015-06-30 15:32:34 -0400148 # Create lock dir
149 if not os.path.isdir(lock_dir):
Jordan Pittier525ec712016-12-07 17:51:26 +0100150 LOG.debug('Creating lock dir: %s', lock_dir)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400151 os.mkdir(lock_dir)
152 # Create log dir
153 if not os.path.isdir(log_dir):
Jordan Pittier525ec712016-12-07 17:51:26 +0100154 LOG.debug('Creating log dir: %s', log_dir)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400155 os.mkdir(log_dir)
156 # Create and copy local etc dir
157 self.copy_config(etc_dir, config_dir)
Matthew Treinishc8a39b42015-07-27 17:07:37 -0400158 # Generate the sample config file
Matthew Treinishbdef1c72016-06-21 18:06:49 -0400159 self.generate_sample_config(local_dir)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400160 # Update local confs to reflect local paths
161 self.update_local_conf(config_path, lock_dir, log_dir)
Chandan Kumar8a4396e2017-09-15 12:18:10 +0530162 # Generate a stestr conf file
163 self.generate_stestr_conf(local_dir)
164 # setup local stestr working dir
165 if not os.path.isdir(stestr_dir):
166 commands.init_command(repo_url=local_dir)
Matthew Treinishf610aca2015-06-30 15:32:34 -0400167
168 def take_action(self, parsed_args):
Cao Xuan Hoang36fe23c2016-08-25 16:11:14 +0700169 workspace_manager = workspace.WorkspaceManager(
170 parsed_args.workspace_path)
step682980c14ec2016-02-23 14:53:52 -0500171 name = parsed_args.name or parsed_args.dir.split(os.path.sep)[-1]
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +0100172 config_dir = parsed_args.config_dir or get_tempest_default_config_dir()
Matthew Treinish054f45d2016-04-23 16:30:29 -0400173 if parsed_args.show_global_dir:
174 print("Global config dir is located at: %s" % config_dir)
175 sys.exit(0)
Andrea Frittoli (andreaf)5a69e552015-07-31 18:40:17 +0100176 self.create_working_dir(parsed_args.dir, config_dir)
Masayuki Igawaf8026412016-09-12 17:12:35 +0900177 workspace_manager.register_new_workspace(
178 name, parsed_args.dir, init=True)