Add filters for shell cmdline
This change adds three filters that can be used to construct shell command lines in roles. * shell_arg_enabled - skips adding an arg if enabled=False. * shell_arg_list - loops through a list to generate a list of command line args. * shell_logging_path - create a string to capture stdout/stderr to file. Change-Id: I5dc43e703f7ebe2ca94f4cbf650a7dee3c93ab5f
This commit is contained in:
parent
6bf883795e
commit
65636ba813
48
plugins/filter/shell_args.py
Normal file
48
plugins/filter/shell_args.py
Normal file
@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python
|
||||
# Copyright 2019 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.
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
def filters(self):
|
||||
return {
|
||||
'shell_arg_enabled': self.shell_arg_enabled,
|
||||
'shell_arg_list': self.shell_arg_list,
|
||||
'shell_logging_path': self.shell_logging_path
|
||||
}
|
||||
|
||||
def shell_arg_enabled(self, arg, enabled=True):
|
||||
if not enabled:
|
||||
return ''
|
||||
return arg
|
||||
|
||||
def shell_arg_list(self, arg, parameter=None):
|
||||
if not isinstance(arg, (list, tuple)):
|
||||
arg = [arg]
|
||||
return_value = []
|
||||
for a in arg:
|
||||
if parameter:
|
||||
return_value.append("{} {}".format(parameter, a))
|
||||
else:
|
||||
return_value.append(a)
|
||||
return ' '.join(return_value)
|
||||
|
||||
def shell_logging_path(self, location, combine=True, enabled=False):
|
||||
output = ''
|
||||
if not enabled:
|
||||
return output
|
||||
if combine:
|
||||
output = "{} {}".format(output, '2>&1')
|
||||
return "{} >{}".format(output, location)
|
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
23
tests/base.py
Normal file
23
tests/base.py
Normal file
@ -0,0 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2010-2011 OpenStack Foundation
|
||||
# Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# 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 oslotest import base
|
||||
|
||||
|
||||
class TestCase(base.BaseTestCase):
|
||||
|
||||
"""Test case base class for all unit tests."""
|
0
tests/plugins/__init__.py
Normal file
0
tests/plugins/__init__.py
Normal file
0
tests/plugins/filter/__init__.py
Normal file
0
tests/plugins/filter/__init__.py
Normal file
70
tests/plugins/filter/test_shell_args.py
Normal file
70
tests/plugins/filter/test_shell_args.py
Normal file
@ -0,0 +1,70 @@
|
||||
# Copyright 2019 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.
|
||||
|
||||
import mock
|
||||
|
||||
from tests import base as tests_base
|
||||
from plugins.filter import shell_args
|
||||
|
||||
|
||||
class TestShellArgsFilters(tests_base.TestCase):
|
||||
def setUp(self):
|
||||
super(TestShellArgsFilters, self).setUp()
|
||||
self.filter = shell_args.FilterModule()
|
||||
|
||||
def test_shell_arg_enabled_default(self):
|
||||
self.assertEqual('foo',
|
||||
self.filter.shell_arg_enabled('foo'))
|
||||
|
||||
def test_shell_arg_enabled_disabled(self):
|
||||
self.assertEqual('',
|
||||
self.filter.shell_arg_enabled('foo', enabled=False))
|
||||
|
||||
def test_shell_arg_list_default(self):
|
||||
arg = ['a', 'b']
|
||||
expected = 'a b'
|
||||
self.assertEqual(expected, self.filter.shell_arg_list(arg))
|
||||
|
||||
def test_shell_arg_list_string(self):
|
||||
arg = 'a'
|
||||
expected = 'a'
|
||||
self.assertEqual(expected, self.filter.shell_arg_list(arg))
|
||||
|
||||
def test_shell_arg_list_param(self):
|
||||
arg = ['a', 'b']
|
||||
expected = '--p a --p b'
|
||||
self.assertEqual(expected,
|
||||
self.filter.shell_arg_list(arg, parameter='--p'))
|
||||
|
||||
def test_shell_logging_path_default(self):
|
||||
location = '/tmp/foo.log'
|
||||
expected = ''
|
||||
self.assertEqual(expected,
|
||||
self.filter.shell_logging_path(location))
|
||||
|
||||
def test_shell_logging_path_enabled(self):
|
||||
location = '/tmp/foo.log'
|
||||
expected = ' 2>&1 >/tmp/foo.log'
|
||||
self.assertEqual(expected,
|
||||
self.filter.shell_logging_path(location,
|
||||
enabled=True))
|
||||
|
||||
def test_shell_logging_path_enabled_not_combined(self):
|
||||
location = '/tmp/foo.log'
|
||||
expected = ' >/tmp/foo.log'
|
||||
self.assertEqual(expected,
|
||||
self.filter.shell_logging_path(location,
|
||||
combine=False,
|
||||
enabled=True))
|
@ -1,4 +1,6 @@
|
||||
- project:
|
||||
templates:
|
||||
- openstack-python3-ussuri-jobs
|
||||
check:
|
||||
jobs:
|
||||
- openstack-tox-linters
|
||||
|
Loading…
x
Reference in New Issue
Block a user