Adding unit tests for libvirt driver

Also, the patch fixes some mistakes in the docs.

Change-Id: I16ce625fb0c66fc74534b10ec89831eb9516ea16
This commit is contained in:
Yaroslav Lobankov 2016-09-08 19:24:02 +03:00
parent f72f5efabc
commit f4e712600f
7 changed files with 83 additions and 13 deletions

View File

@ -44,9 +44,9 @@ Establish a connection to the cloud and verify it:
destructor.verify()
The library can also read configuration from a file and the file can be in the
following three formats: json, yaml, yml. The file can be specified in the
`OS_FAULTS_CONFIG` environment variable or can be read from one of the default
locations:
following three formats: os-faults.{json,yaml,yml}. The configuration file can
be specified in the `OS_FAULTS_CONFIG` environment variable or can be read from
one of the default locations:
* current directory
* ~/.config/os-faults
* /etc/openstack

View File

@ -40,15 +40,15 @@ def main():
# rabbitmq, nova-api, glance-api
# nodes - nodes that host the cloud, e.g. hardware server with hostname
logging.info('# Get nodes where Nova API service runs')
service = destructor.get_service(name='nova-api')
logging.info('# Get nodes where Keystone service runs')
service = destructor.get_service(name='keystone')
nodes = service.get_nodes()
logging.info('Nodes: %s', nodes)
logging.info('# Restart Nova API service on all nodes')
logging.info('# Restart Keystone service on all nodes')
service.restart()
logging.info('# Pick and reset one of Nova API service nodes')
logging.info('# Pick and reset one of Keystone service nodes')
one = nodes.pick()
one.reset()

View File

View File

@ -0,0 +1,72 @@
# 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 ddt
import mock
from os_faults.drivers import libvirt_driver
from os_faults.tests import test
DRIVER_PATH = 'os_faults.drivers.libvirt_driver'
@ddt.ddt
class LibvirtDriverTestCase(test.TestCase):
def setUp(self):
super(LibvirtDriverTestCase, self).setUp()
self.params = {'connection_uri': 'fake_connection_uri'}
self.driver = libvirt_driver.LibvirtDriver(self.params)
@mock.patch('libvirt.open')
def test__get_connection_no_cached_connection(self, mock_libvirt_open):
self.driver._get_connection()
self.assertNotEqual(self.driver._cached_conn, None)
mock_libvirt_open.assert_called_once_with(
self.params['connection_uri'])
def test__get_connection_cached_connection(self):
self.driver._cached_conn = 'some cached connection'
conn = self.driver._get_connection()
self.assertEqual(conn, 'some cached connection')
@mock.patch(DRIVER_PATH + '.LibvirtDriver._get_connection')
def test__find_domain_by_mac_address(self, mock__get_connection):
domain1 = mock.MagicMock()
domain1.XMLDesc.return_value = '52:54:00:ab:64:42'
domain2 = mock.MagicMock()
domain2.XMLDesc.return_value = '52:54:00:f9:b8:f9'
self.driver.conn.listAllDomains.return_value = [domain1, domain2]
domain = self.driver._find_domain_by_mac_address('52:54:00:f9:b8:f9')
self.assertEqual(domain, domain2)
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_mac_address')
@ddt.data(('_poweroff', 'destroy'), ('_poweron', 'create'),
('_reset', 'reset'))
def test__driver_actions(self, actions, mock__find_domain_by_mac_address):
getattr(self.driver, actions[0])('52:54:00:f9:b8:f9')
domain = mock__find_domain_by_mac_address.return_value
getattr(domain, actions[1]).assert_called_once_with()
@mock.patch('os_faults.utils.run')
@ddt.data('poweroff', 'poweron', 'reset')
def test_driver_actions(self, action, mock_run):
macs_list = ['52:54:00:f9:b8:f9', '52:54:00:ab:64:42']
getattr(self.driver, action)(macs_list)
mock_run.assert_called_once_with(getattr(self.driver, '_%s' % action),
macs_list)

View File

@ -1,6 +1,3 @@
# 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
@ -17,5 +14,4 @@ from oslotest import base
class TestCase(base.BaseTestCase):
"""Test case base class for all unit tests."""

View File

@ -17,10 +17,10 @@ test_os_faults
Tests for `os_faults` module.
"""
from os_faults.tests import base
from os_faults.tests import test
class Testos_faults(base.TestCase):
class Testos_faults(test.TestCase):
def test_something(self):
pass

View File

@ -5,6 +5,8 @@
hacking<0.11,>=0.10.0
coverage>=3.6
ddt>=1.0.1
mock>=1.2
python-subunit>=0.0.18
sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2
oslosphinx>=2.5.0 # Apache-2.0