From f90a6d42b3f5a55d5269b2d88b30e6455f6b3287 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Thu, 28 Nov 2019 07:32:19 -0500 Subject: [PATCH] Introduce unit tests structure for tripleo-ansible filters Change-Id: Ie2fea14d2cbfb2c0b78cdc3064df0a558fa28a4c --- .gitignore | 4 + .stestr.conf | 3 + ansible-requirements.txt | 1 + test-requirements.txt | 3 + tox.ini | 9 +- .../ansible_plugins/filter/helpers.py | 2 +- tripleo_ansible/tests/__init__.py | 0 tripleo_ansible/tests/base.py | 21 ++ tripleo_ansible/tests/plugins/__init__.py | 0 .../tests/plugins/filter/__init__.py | 0 .../tests/plugins/filter/test_helpers.py | 258 ++++++++++++++++++ zuul.d/layout.yaml | 1 + 12 files changed, 299 insertions(+), 3 deletions(-) create mode 100644 .stestr.conf create mode 100644 ansible-requirements.txt create mode 100644 tripleo_ansible/tests/__init__.py create mode 100644 tripleo_ansible/tests/base.py create mode 100644 tripleo_ansible/tests/plugins/__init__.py create mode 100644 tripleo_ansible/tests/plugins/filter/__init__.py create mode 100644 tripleo_ansible/tests/plugins/filter/test_helpers.py diff --git a/.gitignore b/.gitignore index 7611f2f28..dca2ce003 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,10 @@ cover nosetests.xml .testrepository .venv +.stestr +tripleo_ansible.egg-info/ +__pycache__ +build # Editors *~ diff --git a/.stestr.conf b/.stestr.conf new file mode 100644 index 000000000..1649a44cb --- /dev/null +++ b/.stestr.conf @@ -0,0 +1,3 @@ +[DEFAULT] +test_path=${TEST_PATH:-./tripleo_ansible/tests/} +top_dir=./ diff --git a/ansible-requirements.txt b/ansible-requirements.txt new file mode 100644 index 000000000..a89eb19de --- /dev/null +++ b/ansible-requirements.txt @@ -0,0 +1 @@ +ansible>=2.8 diff --git a/test-requirements.txt b/test-requirements.txt index e15dd0c3c..93e532ef6 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,2 +1,5 @@ pre-commit # MIT netaddr # BSD +mock>=2.0.0 # BSD +stestr>=2.0.0 # Apache-2.0 +oslotest>=3.2.0 # Apache-2.0 diff --git a/tox.ini b/tox.ini index 9d81c5157..127cf3584 100644 --- a/tox.ini +++ b/tox.ini @@ -26,8 +26,13 @@ setenv = PYTHONWARNINGS=ignore:DEPRECATION::pip._internal.cli.base_command,ignore::UserWarning PIP_DISABLE_PIP_VERSION_CHECK=1 sitepackages = True -deps = -r {toxinidir}/test-requirements.txt -whitelist_externals = bash +deps = + -r {toxinidir}/test-requirements.txt + -r {toxinidir}/ansible-requirements.txt +commands = stestr run {posargs} +whitelist_externals = + bash + tox [testenv:bindep] # Do not install any requirements. We want this to be fast and work even if diff --git a/tripleo_ansible/ansible_plugins/filter/helpers.py b/tripleo_ansible/ansible_plugins/filter/helpers.py index 5baa90074..985f74a5f 100644 --- a/tripleo_ansible/ansible_plugins/filter/helpers.py +++ b/tripleo_ansible/ansible_plugins/filter/helpers.py @@ -30,7 +30,7 @@ class FilterModule(object): 'list_of_keys': self.list_of_keys } - def subsort(self, dict_to_sort, attribute, null_value=None): + def subsort(self, dict_to_sort, attribute, null_value=0): """Sort a hash from a sub-element. This filter will return an dictionary ordered by the attribute diff --git a/tripleo_ansible/tests/__init__.py b/tripleo_ansible/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tripleo_ansible/tests/base.py b/tripleo_ansible/tests/base.py new file mode 100644 index 000000000..f052aba9b --- /dev/null +++ b/tripleo_ansible/tests/base.py @@ -0,0 +1,21 @@ +# 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. + +from oslotest import base + + +class TestCase(base.BaseTestCase): + + """Test case base class for all unit tests.""" diff --git a/tripleo_ansible/tests/plugins/__init__.py b/tripleo_ansible/tests/plugins/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tripleo_ansible/tests/plugins/filter/__init__.py b/tripleo_ansible/tests/plugins/filter/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tripleo_ansible/tests/plugins/filter/test_helpers.py b/tripleo_ansible/tests/plugins/filter/test_helpers.py new file mode 100644 index 000000000..701482caa --- /dev/null +++ b/tripleo_ansible/tests/plugins/filter/test_helpers.py @@ -0,0 +1,258 @@ +# 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. + +from tripleo_ansible.ansible_plugins.filter import helpers +from tripleo_ansible.tests import base as tests_base + + +class TestHelperFilters(tests_base.TestCase): + + def setUp(self): + super(TestHelperFilters, self).setUp() + self.filters = helpers.FilterModule() + + def test_subsort(self): + dict = { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone' + }, + 'haproxy': { + 'image': 'quay.io/tripleo/haproxy' + }, + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + expected_ordered_dict = { + 0: [ + {'haproxy': { + 'image': 'quay.io/tripleo/haproxy', + 'start_order': 0 + }}, + {'mysql': { + 'image': 'quay.io/tripleo/mysql', + 'start_order': 0 + }} + ], + 1: [ + {'keystone': { + 'image': 'quay.io/tripleo/keystone', + 'start_order': 1 + }} + ] + } + result = self.filters.subsort(dict_to_sort=dict, + attribute='start_order') + self.assertEqual(result, expected_ordered_dict) + + def test_subsort_with_null_value(self): + dict = { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone' + }, + 'haproxy': { + 'image': 'quay.io/tripleo/haproxy' + }, + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + expected_ordered_dict = { + 0: [ + {'mysql': { + 'image': 'quay.io/tripleo/mysql', + 'start_order': 0 + }} + ], + 1: [ + {'keystone': { + 'image': 'quay.io/tripleo/keystone', + 'start_order': 1 + }} + ], + 5: [ + {'haproxy': { + 'image': 'quay.io/tripleo/haproxy', + 'start_order': 5 + }} + ] + } + result = self.filters.subsort(dict_to_sort=dict, + attribute='start_order', null_value=5) + self.assertEqual(result, expected_ordered_dict) + + def test_singledict(self): + list = [ + { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone' + }, + }, + { + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + ] + expected_dict = { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone' + }, + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + result = self.filters.singledict(list) + self.assertEqual(result, expected_dict) + + def test_list_of_keys(self): + keys = [ + { + 'foo1': 'bar1' + }, + { + 'foo2': 'bar2' + }, + ] + expected_list = ['foo1', 'foo2'] + result = self.filters.list_of_keys(keys) + self.assertEqual(result, expected_list) + + def test_haskey(self): + data = [ + { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone', + 'restart': 'always' + }, + }, + { + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + ] + expected_list = [ + { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone', + 'restart': 'always' + }, + } + ] + result = self.filters.haskey(batched_container_data=data, + attribute='restart', value='always') + self.assertEqual(result, expected_list) + + def test_haskey_reverse(self): + data = [ + { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone', + 'restart': 'always' + }, + }, + { + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + ] + expected_list = [ + { + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + }, + } + ] + result = self.filters.haskey(batched_container_data=data, + attribute='restart', + value='always', + reverse=True) + self.assertEqual(result, expected_list) + + def test_haskey_any(self): + data = [ + { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone', + 'restart': 'always' + }, + }, + { + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + ] + expected_list = [ + { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone', + 'restart': 'always' + }, + } + ] + result = self.filters.haskey(batched_container_data=data, + attribute='restart', + any=True) + self.assertEqual(result, expected_list) + + def test_haskey_any_reverse(self): + data = [ + { + 'keystone': { + 'start_order': 1, + 'image': 'quay.io/tripleo/keystone', + 'restart': 'always' + }, + }, + { + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + } + } + ] + expected_list = [ + { + 'mysql': { + 'start_order': 0, + 'image': 'quay.io/tripleo/mysql' + }, + } + ] + result = self.filters.haskey(batched_container_data=data, + attribute='restart', + reverse=True, + any=True) + self.assertEqual(result, expected_list) diff --git a/zuul.d/layout.yaml b/zuul.d/layout.yaml index 486e06749..c68324e6e 100644 --- a/zuul.d/layout.yaml +++ b/zuul.d/layout.yaml @@ -3,6 +3,7 @@ templates: - tripleo-ansible-molecule-jobs - release-notes-jobs-python3 + - openstack-python3-ussuri-jobs check: jobs: - openstack-tox-linters