# Copyright 2016 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. import libvirt import mock import threading from pyghmi.ipmi import bmc from virtualbmc.tests.unit import base from virtualbmc.tests.unit import utils as test_utils from virtualbmc import utils from virtualbmc import vbmc DOMAIN_XML_TEMPLATE = """\ hvm """ @mock.patch.object(utils, 'libvirt_open') @mock.patch.object(utils, 'get_libvirt_domain') class VirtualBMCTestCase(base.TestCase): def setUp(self): super(VirtualBMCTestCase, self).setUp() self.domain = test_utils.get_domain() # NOTE(lucasagomes): pyghmi's Bmc does create a socket in the # constructor so we need to mock it here mock.patch('pyghmi.ipmi.bmc.Bmc.__init__', lambda *args, **kwargs: None).start() self.vbmc = vbmc.VirtualBMC(**self.domain) def _assert_libvirt_calls(self, mock_libvirt_domain, mock_libvirt_open, readonly=False): """Helper method to assert that the LibVirt calls were invoked.""" mock_libvirt_domain.assert_called_once_with( mock.ANY, self.domain['domain_name']) params = {'sasl_password': self.domain['libvirt_sasl_password'], 'sasl_username': self.domain['libvirt_sasl_username'], 'uri': self.domain['libvirt_uri']} if readonly: params['readonly'] = True mock_libvirt_open.assert_called_once_with(**params) def test_get_boot_device(self, mock_libvirt_domain, mock_libvirt_open): for boot_device in vbmc.GET_BOOT_DEVICES_MAP: domain_xml = DOMAIN_XML_TEMPLATE % boot_device mock_libvirt_domain.return_value.XMLDesc.return_value = domain_xml ret = self.vbmc.get_boot_device() self.assertEqual(vbmc.GET_BOOT_DEVICES_MAP[boot_device], ret) self._assert_libvirt_calls(mock_libvirt_domain, mock_libvirt_open, readonly=True) # reset mocks for the next iteraction mock_libvirt_domain.reset_mock() mock_libvirt_open.reset_mock() def test_set_boot_device(self, mock_libvirt_domain, mock_libvirt_open): for boot_device in vbmc.SET_BOOT_DEVICES_MAP: domain_xml = DOMAIN_XML_TEMPLATE % 'foo' mock_libvirt_domain.return_value.XMLDesc.return_value = domain_xml conn = mock_libvirt_open.return_value.__enter__.return_value self.vbmc.set_boot_device(boot_device) expected = ('' % vbmc.SET_BOOT_DEVICES_MAP[boot_device]) self.assertIn(expected, str(conn.defineXML.call_args)) self.assertEqual(1, str(conn.defineXML.call_args).count('