Add Tobiko CLI tool to execute tobiko tools

This patch adds base `tobiko` CLI tool and one command for it which is
`ping`.
Using this new CLI tool it is now possible to do `tobiko ping <server>`
to run tobiko function to ping some server and store results in the
file. This can be used e.g. to run background ping process in the POD on
OpenShift and use that in the Tobiko tests.

Related: #TOBIKO-100
Change-Id: I33aea73cead5a4f406ce1a08c819fe056e17e9a1
This commit is contained in:
Slawek Kaplonski 2024-11-19 16:15:25 +01:00
parent a6e9824990
commit 21d5151e39
4 changed files with 144 additions and 0 deletions

View File

@ -28,8 +28,11 @@ packages =
[entry_points]
console_scripts =
tobiko = tobiko.cmd:main
tobiko-fixture = tobiko.cmd.fixture:main
tobiko-keystone-credentials = tobiko.openstack.keystone._credentials:print_credentials
tobiko.cli_commands =
ping = tobiko.cmd:TobikoPing
oslo.config.opts =
tobiko = tobiko.config:list_tobiko_options

27
tobiko/cmd/__init__.py Normal file
View File

@ -0,0 +1,27 @@
# Copyright (c) 2024 Red Hat, Inc.
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import sys
from tobiko.cmd import _ping
from tobiko.cmd import _main
main = _main.main
TobikoPing = _ping.TobikoPing
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))

54
tobiko/cmd/_main.py Normal file
View File

@ -0,0 +1,54 @@
# Copyright (c) 2024 Red Hat, Inc.
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import sys
from cliff import app
from cliff import commandmanager
from oslo_log import log as logging
from pbr import version
class Main(app.App):
log = logging.getLogger(__name__)
def __init__(self):
super(Main, self).__init__(
description='Tobiko CLI application',
version=version.VersionInfo('tobiko').version_string_with_vcs(),
command_manager=commandmanager.CommandManager(
'tobiko.cli_commands'),
deferred_help=True,
)
def initialize_app(self, argv):
self.log.debug('tobiko initialize_app')
def prepare_to_run_command(self, cmd):
self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)
def clean_up(self, cmd, result, err):
self.log.debug('tobiko clean_up %s', cmd.__class__.__name__)
if err:
self.log.debug('tobiko got an error: %s', err)
def main(argv=sys.argv[1:]):
the_app = Main()
return the_app.run(argv)

60
tobiko/cmd/_ping.py Normal file
View File

@ -0,0 +1,60 @@
# Copyright (c) 2024 Red Hat, Inc.
#
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import absolute_import
import sys
from cliff import command
from oslo_log import log as logging
from tobiko.shell import ping
LOG = logging.getLogger(__name__)
class TobikoPing(command.Command):
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'server',
help='Address of the server to ping'
)
parser.add_argument(
'--result-file',
default='tobiko_ping_results',
help='Name of the directory where ping log files are '
'stored.'
)
return parser
def take_action(self, parsed_args):
error_code = 0
try:
LOG.debug("Starting ping server: %s", parsed_args.server)
ping.write_ping_to_file(ping_ip=parsed_args.server,
output_dir=parsed_args.result_file)
LOG.debug("Finished ping server: %s", parsed_args.server)
except Exception as e:
if hasattr(e, 'errno'):
error_code = e.errno
else:
error_code = 1
LOG.error("Failed to ping server '%s'. Error: %s",
parsed_args.server, e)
if error_code:
sys.exit(error_code)