From 65636ba8138963ffe7e3d965217624fb4bb274e7 Mon Sep 17 00:00:00 2001 From: Alex Schultz Date: Mon, 16 Dec 2019 15:07:06 -0700 Subject: [PATCH] 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 --- plugins/filter/shell_args.py | 48 +++++++++++++++++ tests/__init__.py | 0 tests/base.py | 23 ++++++++ tests/plugins/__init__.py | 0 tests/plugins/filter/__init__.py | 0 tests/plugins/filter/test_shell_args.py | 70 +++++++++++++++++++++++++ zuul.d/layout.yaml | 2 + 7 files changed, 143 insertions(+) create mode 100644 plugins/filter/shell_args.py create mode 100644 tests/__init__.py create mode 100644 tests/base.py create mode 100644 tests/plugins/__init__.py create mode 100644 tests/plugins/filter/__init__.py create mode 100644 tests/plugins/filter/test_shell_args.py diff --git a/plugins/filter/shell_args.py b/plugins/filter/shell_args.py new file mode 100644 index 0000000..6066784 --- /dev/null +++ b/plugins/filter/shell_args.py @@ -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) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 0000000..1c30cdb --- /dev/null +++ b/tests/base.py @@ -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.""" diff --git a/tests/plugins/__init__.py b/tests/plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/plugins/filter/__init__.py b/tests/plugins/filter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/plugins/filter/test_shell_args.py b/tests/plugins/filter/test_shell_args.py new file mode 100644 index 0000000..3aad3a7 --- /dev/null +++ b/tests/plugins/filter/test_shell_args.py @@ -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)) diff --git a/zuul.d/layout.yaml b/zuul.d/layout.yaml index dfcb7f1..4bb7832 100644 --- a/zuul.d/layout.yaml +++ b/zuul.d/layout.yaml @@ -1,4 +1,6 @@ - project: + templates: + - openstack-python3-ussuri-jobs check: jobs: - openstack-tox-linters