From 794bffd7011feb31b0178a2dd8d3abed46fe6347 Mon Sep 17 00:00:00 2001 From: Will Miller Date: Fri, 14 Sep 2018 13:35:27 +0000 Subject: [PATCH] Add basis for action plugin tests --- ansible/action_plugins/__init__.py | 0 ansible/action_plugins/tenks_update_state.py | 5 +- test-requirements.txt | 3 + tests/__init__.py | 0 tests/test_tenks_update_state.py | 81 ++++++++++++++++++++ tox.ini | 2 + 6 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 ansible/action_plugins/__init__.py create mode 100644 tests/__init__.py create mode 100644 tests/test_tenks_update_state.py diff --git a/ansible/action_plugins/__init__.py b/ansible/action_plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ansible/action_plugins/tenks_update_state.py b/ansible/action_plugins/tenks_update_state.py index 2efa503..3d23a1a 100644 --- a/ansible/action_plugins/tenks_update_state.py +++ b/ansible/action_plugins/tenks_update_state.py @@ -86,7 +86,7 @@ class ActionModule(ActionBase): old_idxs = {} new_idxs = {} next_idx = 0 - used_idxs = old_idxs.values() + used_idxs = list(old_idxs.values()) for name, dev in six.iteritems(specified_mappings): try: # We need to re-use the IDXs of any existing physnets. @@ -97,8 +97,7 @@ class ActionModule(ActionBase): next_idx += 1 used_idxs.append(next_idx) idx = next_idx - finally: - new_idxs[name] = idx + new_idxs[name] = idx state[hostname]['physnet_indices'] = new_idxs def _process_specs(self, localhost_vars, args): diff --git a/test-requirements.txt b/test-requirements.txt index aa79722..f41301b 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -4,3 +4,6 @@ ansible-lint>=3.0.0 # MIT flake8>=3.5.0 # MIT +# Required for Python 2 +mock>=2.0.0 # BSD +stestr>=1.0.0 # Apache-2.0 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_tenks_update_state.py b/tests/test_tenks_update_state.py new file mode 100644 index 0000000..8908085 --- /dev/null +++ b/tests/test_tenks_update_state.py @@ -0,0 +1,81 @@ +# Copyright (c) 2018 StackHPC Ltd. +# +# 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 imp +import os +import sys + +import unittest + +# Python 2/3 compatibility. +try: + from unittest.mock import MagicMock +except ImportError: + from mock import MagicMock + + +# Import method lifted from kolla_ansible's test_merge_config.py +PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../')) +PLUGIN_FILE = os.path.join(PROJECT_DIR, + 'ansible/action_plugins/tenks_update_state.py') + +tus = imp.load_source('tenks_update_state', PLUGIN_FILE) + +class TestTenksUpdateState(unittest.TestCase): + def setUp(self): + # Pass dummy arguments to allow instantiation of action plugin. + self.mod = tus.ActionModule(None, None, None, None, None, None) + + # Minimal inputs required. + self.node_types = { + 'type0': { + 'memory_mb': 1024, + 'vcpus': 2, + 'volumes': [ + { + 'capacity': '10GB', + }, + ], + 'physical_networks': [ + 'physnet0', + ], + }, + } + self.specs = [ + { + 'type': 'type0', + 'count': 2, + 'ironic_config': { + 'resource_class': 'testrc', + }, + }, + ] + self.hypervisor_vars = { + 'foo': { + 'physnet_mappings': { + 'physnet0': 'dev0', + }, + 'ipmi_port_range_start': 100, + 'ipmi_port_range_end': 102, + }, + } + + def test__set_physnet_idxs_no_state(self): + state = {} + self.mod._set_physnet_idxs(state, self.hypervisor_vars) + expected_indices = { + 'physnet0': 0, + } + self.assertEqual(state['foo']['physnet_indices'], expected_indices) diff --git a/tox.ini b/tox.ini index f0c4e94..baa4108 100644 --- a/tox.ini +++ b/tox.ini @@ -21,6 +21,8 @@ deps = -c{env:UPPER_CONSTRAINTS_FILE:https://git.openstack.org/cgit/openstack/requirements/plain/upper-constraints.txt?h=stable/rocky} -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt +commands = + stestr run {posargs} [testenv:pep8] basepython = python2.7