/usr/lib/python2.7/dist-packages/os_faults/tests/unit/drivers/power/test_libvirt.py is in python-os-faults 0.1.17-0ubuntu1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | # 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.api import node_collection
from os_faults.drivers.power import libvirt
from os_faults import error
from os_faults.tests.unit import test
DRIVER_PATH = 'os_faults.drivers.power.libvirt'
@ddt.ddt
class LibvirtDriverTestCase(test.TestCase):
def setUp(self):
super(LibvirtDriverTestCase, self).setUp()
self.params = {'connection_uri': 'fake_connection_uri'}
self.driver = libvirt.LibvirtDriver(self.params)
self.host = node_collection.Host(
ip='10.0.0.2', mac='00:00:00:00:00:00', fqdn='node1.com')
@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_host_mac(self, mock__get_connection):
host = node_collection.Host(ip='10.0.0.2', mac=':54:00:f9:b8:f9')
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_host(host)
self.assertEqual(domain, domain2)
@mock.patch(DRIVER_PATH + '.LibvirtDriver._get_connection')
def test__find_domain_by_host_name(self, mock__get_connection):
host = node_collection.Host(ip='10.0.0.2', libvirt_name='foo')
domain1 = mock.MagicMock()
domain1.XMLDesc.return_value = '52:54:00:ab:64:42'
domain1.name.return_value = 'bar'
domain2 = mock.MagicMock()
domain2.XMLDesc.return_value = '52:54:00:f9:b8:f9'
domain2.name.return_value = 'foo'
self.driver.conn.listAllDomains.return_value = [domain1, domain2]
domain = self.driver._find_domain_by_host(host)
self.assertEqual(domain, domain2)
@mock.patch(DRIVER_PATH + '.LibvirtDriver._get_connection')
def test__find_domain_by_host_domain_not_found(
self, mock__get_connection):
host = node_collection.Host(ip='10.0.0.2')
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]
self.assertRaises(error.PowerManagementError,
self.driver._find_domain_by_host, host)
@mock.patch(DRIVER_PATH + '.LibvirtDriver._get_connection')
def test_supports(self, mock__get_connection):
domain1 = mock.MagicMock()
domain1.XMLDesc.return_value = '52:54:00:ab:64:42'
domain2 = mock.MagicMock()
domain2.XMLDesc.return_value = '00:00:00:00:00:00'
self.driver.conn.listAllDomains.return_value = [domain1, domain2]
self.assertTrue(self.driver.supports(self.host))
@mock.patch(DRIVER_PATH + '.LibvirtDriver._get_connection')
def test_supports_false(self, mock__get_connection):
self.driver.conn.listAllDomains.return_value = []
self.assertFalse(self.driver.supports(self.host))
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_host')
@ddt.data(('poweroff', 'destroy'), ('poweron', 'create'),
('reset', 'reset'), ('shutdown', 'shutdown'))
def test_driver_actions(self, actions, mock__find_domain_by_host):
getattr(self.driver, actions[0])(self.host)
domain = mock__find_domain_by_host.return_value
getattr(domain, actions[1]).assert_called_once_with()
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_host')
def test_snapshot(self, mock__find_domain_by_host):
self.driver.snapshot(self.host, 'foo', suspend=False)
domain = mock__find_domain_by_host.return_value
domain.snapshotCreateXML.assert_called_once_with(
'<domainsnapshot><name>foo</name></domainsnapshot>')
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_host')
def test_snapshot_suspend(self, mock__find_domain_by_host):
self.driver.snapshot(self.host, 'foo', suspend=True)
domain = mock__find_domain_by_host.return_value
domain.assert_has_calls((
mock.call.suspend(),
mock.call.snapshotCreateXML(
'<domainsnapshot><name>foo</name></domainsnapshot>'),
mock.call.resume(),
))
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_host')
def test_revert(self, mock__find_domain_by_host):
self.driver.revert(self.host, 'foo', resume=False)
domain = mock__find_domain_by_host.return_value
snapshot = domain.snapshotLookupByName.return_value
domain.snapshotLookupByName.assert_called_once_with('foo')
domain.revertToSnapshot.assert_called_once_with(snapshot)
self.assertFalse(domain.resume.called)
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_host')
def test_revert_resume(self, mock__find_domain_by_host):
self.driver.revert(self.host, 'foo', resume=True)
domain = mock__find_domain_by_host.return_value
snapshot = domain.snapshotLookupByName.return_value
domain.snapshotLookupByName.assert_called_once_with('foo')
domain.revertToSnapshot.assert_called_once_with(snapshot)
domain.resume.assert_called_once_with()
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_host')
def test_revert_destroy(self, mock__find_domain_by_host):
domain = mock__find_domain_by_host.return_value
domain.isActive.return_value = True
self.driver.revert(self.host, 'foo', resume=True)
domain.destroy.assert_called_once_with()
@mock.patch(DRIVER_PATH + '.LibvirtDriver._find_domain_by_host')
def test_revert_destroy_nonactive(self, mock__find_domain_by_host):
domain = mock__find_domain_by_host.return_value
domain.isActive.return_value = False
self.driver.revert(self.host, 'foo', resume=True)
self.assertFalse(domain.destroy.called)
|