From b0c1da528f7eb23edf0e9ba5115657242b01733d Mon Sep 17 00:00:00 2001 From: abregman Date: Wed, 2 Jan 2019 10:47:51 +0200 Subject: [PATCH] Add support for faults Added: * Faults manager - a compnent for managing faults (load them, connect os-faults to cloud, exdcute faults, etc.). * Faults for test_floatingip test class Also, modified test_floatingip tests to run faults if faults manager set up properly (loaded configuration file, managed to connect the nodes of the cloud, etc.). Change-Id: I1dd48bc9a7cc385a46d9b3b2382000581a614b42 --- Pipfile | 3 +- extra-requirements.txt | 3 +- requirements.txt | 1 + tobiko/common/managers/fault.py | 72 ++++++++++++++++++++ tobiko/common/managers/fixture.py | 2 +- tobiko/tests/scenario/base.py | 1 + tobiko/tests/scenario/faults/test_floatingip | 2 + tobiko/tests/scenario/test_floatingip.py | 13 ++++ 8 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 tobiko/common/managers/fault.py create mode 100644 tobiko/tests/scenario/faults/test_floatingip diff --git a/Pipfile b/Pipfile index ab6757e36..c014ddbad 100644 --- a/Pipfile +++ b/Pipfile @@ -4,8 +4,9 @@ verify_ssl = true name = "pypi" [packages] -tobiko = {editable = true, path = "."} +tobiko = {editable = true,path = "."} ansible = "*" +testscenarios = "*" [dev-packages] diff --git a/extra-requirements.txt b/extra-requirements.txt index 657b5b2d7..698000723 100644 --- a/extra-requirements.txt +++ b/extra-requirements.txt @@ -1,4 +1,5 @@ -# Optional tobiko requirements +# Tobiko extra requirements ansible>=2.4.0 # GPLv3 +os-faults>=0.1.18 # Apache-2.0 tempest>=17.1.0 # Apache-2.0 diff --git a/requirements.txt b/requirements.txt index c167b5c6f..a762d2691 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ python-heatclient>=1.5.0 # Apache-2.0 python-neutronclient>=6.7.0 # Apache-2.0 stestr>=2.0 # Apache-2.0 testtools>=2.2.0 # MIT +testscenarios>=0.4 # Apache-2.0/BSD diff --git a/tobiko/common/managers/fault.py b/tobiko/common/managers/fault.py new file mode 100644 index 000000000..72864b409 --- /dev/null +++ b/tobiko/common/managers/fault.py @@ -0,0 +1,72 @@ +# Copyright 2018 Red Hat +# +# 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 os + +import os_faults +import yaml + +from oslo_log import log + +LOG = log.getLogger(__name__) + +try: + FileNotFoundError +except NameError: + FileNotFoundError = IOError # pylint: disable=redefined-builtin + +CONF_DIR = os.path.expanduser('~') +CONF_FILE = 'os-faults.yaml' + + +class FaultManager(): + """Manages faults.""" + + def __init__(self, test_file): + self.faults_dir = os.path.join(os.path.dirname(test_file), 'faults') + faults_f_name = os.path.splitext(os.path.basename(test_file))[0] + self.faults_file = os.path.join(self.faults_dir, faults_f_name) + fault_config_f = os.path.join(CONF_DIR, CONF_FILE) + try: + self.cloud = os_faults.connect(config_filename=fault_config_f) + self.cloud.verify() + self.scenarios = self.get_scenarios() + except os_faults.ansible.executor.AnsibleExecutionUnreachable: + LOG.warning("Couldn't verify connectivity to the" + " cloud with os-faults configuration") + self.scenarios = None + except FileNotFoundError: + LOG.warning("Couldn't find os-faults configuration file") + self.scenarios = None + + def get_scenarios(self): + """Returns list of scenarios based on defined faults. + + A scenario composed out of scenario name and the fault to execute. + """ + scenarios = [] + with open(self.faults_file, 'r') as stream: + faults_yaml = yaml.load(stream) + for fault in faults_yaml: + scenarios.append((fault['name'], dict(fault=fault['action']))) + return scenarios + + def run_fault(self, fault): + """Executes given fault.""" + if self.scenarios: + os_faults.human_api(self.cloud, fault) + else: + LOG.debug("Skipped fault: '{}' since".format(fault), + " scenarios are not defined.") diff --git a/tobiko/common/managers/fixture.py b/tobiko/common/managers/fixture.py index e73c14deb..04a2a50b8 100644 --- a/tobiko/common/managers/fixture.py +++ b/tobiko/common/managers/fixture.py @@ -73,7 +73,7 @@ FIXTURES = FixtureManager() class FixtureMeta(abc.ABCMeta): - def __new__(cls, name, bases, members): + def __new__(cls, name, bases, members): # pylint: disable=arguments-differ fixture_class = super(FixtureMeta, cls).__new__(cls, name, bases, members) if not inspect.isabstract(fixture_class): diff --git a/tobiko/tests/scenario/base.py b/tobiko/tests/scenario/base.py index 55d54ff89..1c8319037 100644 --- a/tobiko/tests/scenario/base.py +++ b/tobiko/tests/scenario/base.py @@ -32,6 +32,7 @@ class ScenarioTestsBase(base.TobikoTest): templates_dir = os.path.join(os.path.dirname(__file__), 'templates') stacks = stack_manager.StackManager(clients, templates_dir) stack = None + fault = None @classmethod def setUpClass(cls): diff --git a/tobiko/tests/scenario/faults/test_floatingip b/tobiko/tests/scenario/faults/test_floatingip new file mode 100644 index 000000000..88724a95f --- /dev/null +++ b/tobiko/tests/scenario/faults/test_floatingip @@ -0,0 +1,2 @@ +- name: 'Restart OVS' + action: 'restart openvswitch service' diff --git a/tobiko/tests/scenario/test_floatingip.py b/tobiko/tests/scenario/test_floatingip.py index 4d33b8058..4c2d4cc02 100644 --- a/tobiko/tests/scenario/test_floatingip.py +++ b/tobiko/tests/scenario/test_floatingip.py @@ -14,13 +14,24 @@ # under the License. from __future__ import absolute_import +import testscenarios +from testscenarios.scenarios import multiply_scenarios + from tobiko.tests.scenario import base from tobiko.common.asserts import assert_ping +from tobiko.common.managers import fault + +load_tests = testscenarios.load_tests_apply_scenarios class FloatingIPTest(base.ScenarioTestsBase): """Tests server connectivity""" + fault_manager = fault.FaultManager(__file__) + test_faults = fault_manager.scenarios + if test_faults: + scenarios = multiply_scenarios(test_faults) + @classmethod def setUpClass(cls): super(FloatingIPTest, cls).setUpClass() @@ -29,7 +40,9 @@ class FloatingIPTest(base.ScenarioTestsBase): def test_ping_floating_ip(self): """Validates connectivity to a server post upgrade.""" + self.fault_manager.run_fault(self.fault) assert_ping(self.fip) def test_ping_unreachable_floating_ip(self): + self.fault_manager.run_fault(self.fault) assert_ping(self.unreachable_fip, should_fail=True)