Flatten the emulator resources

It doesn't look like we'll get more alternative implementations for
all resources except for systems, nor do we allow loading 3rd party
ones. For the sake of simpler development, flatten the package
structure and get rid of most abstract classes.

Change-Id: I7f84b535520b0be8e8c636d55e109d0402471dc0
This commit is contained in:
Dmitry Tantsur 2020-08-06 10:15:14 +02:00
parent 05127d12ae
commit c5b99058b2
34 changed files with 153 additions and 403 deletions

View File

@ -17,21 +17,6 @@
class DriverBase(object):
"""Common base for Redfish Systems, Managers and Chassis models"""
@classmethod
def initialize(cls, **kwargs):
"""Initialize class attributes
Since drivers may need to cache thing short-term. The emulator
instantiates the driver every time it serves a client query.
Driver objects can cache whenever it makes sense for the duration
of a single session. It is guaranteed that the driver object will
never be reused for any other session.
The `initialize` method is provided to set up the driver in a way
that would affect all the subsequent sessions.
:params **kwargs: driver-specific parameters
:returns: initialized driver class
"""
return cls
def __init__(self, config, logger):
self._config = config
self._logger = logger

View File

@ -25,15 +25,15 @@ import flask
from werkzeug import exceptions as wz_exc
from sushy_tools.emulator import memoize
from sushy_tools.emulator.resources.chassis import staticdriver as chsdriver
from sushy_tools.emulator.resources.drives import staticdriver as drvdriver
from sushy_tools.emulator.resources.indicators import staticdriver as inddriver
from sushy_tools.emulator.resources.managers import fakedriver as fakemgrdriver
from sushy_tools.emulator.resources.storage import staticdriver as stgdriver
from sushy_tools.emulator.resources import chassis as chsdriver
from sushy_tools.emulator.resources import drives as drvdriver
from sushy_tools.emulator.resources import indicators as inddriver
from sushy_tools.emulator.resources import managers as mgrdriver
from sushy_tools.emulator.resources import storage as stgdriver
from sushy_tools.emulator.resources.systems import libvirtdriver
from sushy_tools.emulator.resources.systems import novadriver
from sushy_tools.emulator.resources.vmedia import staticdriver as vmddriver
from sushy_tools.emulator.resources.volumes import staticdriver as voldriver
from sushy_tools.emulator.resources import vmedia as vmddriver
from sushy_tools.emulator.resources import volumes as voldriver
from sushy_tools import error
from sushy_tools.error import FishyError
@ -78,38 +78,38 @@ class Application(flask.Flask):
@property
@memoize.memoize()
def managers(self):
return fakemgrdriver.FakeDriver.initialize(app.config, app.logger)(
self.systems, self.chassis)
return mgrdriver.FakeDriver(self.config, self.logger,
self.systems, self.chassis)
@property
@memoize.memoize()
def chassis(self):
return chsdriver.StaticDriver.initialize(app.config, app.logger)()
return chsdriver.StaticDriver(self.config, self.logger)
@property
@memoize.memoize()
def indicators(self):
return inddriver.StaticDriver.initialize(app.config, app.logger)()
return inddriver.StaticDriver(self.config, self.logger)
@property
@memoize.memoize()
def vmedia(self):
return vmddriver.StaticDriver.initialize(app.config, app.logger)()
return vmddriver.StaticDriver(self.config, self.logger)
@property
@memoize.memoize()
def storage(self):
return stgdriver.StaticDriver.initialize(app.config, app.logger)()
return stgdriver.StaticDriver(self.config, self.logger)
@property
@memoize.memoize()
def drives(self):
return drvdriver.StaticDriver.initialize(app.config, app.logger)()
return drvdriver.StaticDriver(self.config, self.logger)
@property
@memoize.memoize()
def volumes(self):
return voldriver.StaticDriver.initialize(app.config, app.logger)()
return voldriver.StaticDriver(self.config, self.logger)
app = Application()

View File

@ -17,24 +17,11 @@
class DriverBase(object):
"""Common base for emulated Redfish resource drivers"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
"""Initialize class attributes
Since drivers may need to cache thing short-term. The emulator
instantiates the driver every time it serves a client query.
Driver objects can cache whenever it makes sense for the duration
of a single session. It is guaranteed that the driver object will
never be reused for any other session.
The `initialize` method is provided to set up the driver in a way
that would affect all the subsequent sessions.
def __init__(self, config, logger):
"""Initialize a driver.
:params config: system configuration dict
:params logger: system logger object
:params *args: driver-specific parameters
:params **kwargs: driver-specific parameters
:returns: initialized driver class
"""
return cls
self._config = config
self._logger = logger

View File

@ -15,14 +15,15 @@
import uuid
from sushy_tools.emulator.resources.chassis.base import AbstractChassisDriver
from sushy_tools.emulator import base
from sushy_tools import error
class StaticDriver(AbstractChassisDriver):
class StaticDriver(base.DriverBase):
"""Redfish chassis backed by configuration file"""
def __init__(self):
def __init__(self, config, logger):
super().__init__(config, logger)
chassis = self._config.get('SUSHY_EMULATOR_CHASSIS')
if not chassis:

View File

@ -1,70 +0,0 @@
# Copyright 2019 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 abc
from sushy_tools.emulator import base
class AbstractChassisDriver(base.DriverBase, metaclass=abc.ABCMeta):
"""Base class backing Redfish Chassis"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
cls._config = config
cls._logger = logger
return cls
@property
@abc.abstractmethod
def driver(self):
"""Return human-friendly driver information
:returns: driver information as `str`
"""
@property
@abc.abstractmethod
def chassis(self):
"""Return available Redfish chassis
:returns: list of UUIDs representing the chassis
"""
@abc.abstractmethod
def uuid(self, identity):
"""Get Redfish chassis UUID
The universal unique identifier (UUID) for this chassis. Can be used
in place of chassis name if there are duplicates.
If driver backend does not support non-unique chassis identity,
this method may just return the `identity`.
:returns: Redfish chassis UUID
"""
@abc.abstractmethod
def name(self, identity):
"""Get Redfish chassis name by UUID
The universal unique identifier (UUID) for this Redfish chassis.
Can be used in place of chassis name if there are duplicates.
If driver backend does not support chassis names, this method may
just return the `identity`.
:returns: Redfish chassis name
"""

View File

@ -15,21 +15,16 @@
import uuid
from sushy_tools.emulator.resources.base import DriverBase
from sushy_tools.emulator.resources import base
from sushy_tools import error
class StaticDriver(DriverBase):
class StaticDriver(base.DriverBase):
"""Redfish storage drives backed by configuration file"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
cls._config = config
cls._logger = logger
cls._drives = cls._config.get('SUSHY_EMULATOR_DRIVES', {})
return cls
def __init__(self, config, logger):
super().__init__(config, logger)
self._drives = self._config.get('SUSHY_EMULATOR_DRIVES', {})
@property
def driver(self):

View File

@ -14,31 +14,25 @@
# under the License.
from sushy_tools.emulator import memoize
from sushy_tools.emulator.resources.base import DriverBase
from sushy_tools.emulator.resources import base
from sushy_tools import error
class StaticDriver(DriverBase):
class StaticDriver(base.DriverBase):
"""Redfish indicator LED simulator
Maintain indicators states in memory. Does not light up
anything.
"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
cls._config = config
cls._logger = logger
cls._indicators = memoize.PersistentDict()
if hasattr(cls._indicators, 'make_permanent'):
cls._indicators.make_permanent(
cls._config.get('SUSHY_EMULATOR_STATE_DIR'), 'indicators')
cls._indicators.update(
cls._config.get('SUSHY_EMULATOR_INDICATOR_LEDS', {}))
return cls
def __init__(self, config, logger):
super().__init__(config, logger)
self._indicators = memoize.PersistentDict()
if hasattr(self._indicators, 'make_permanent'):
self._indicators.make_permanent(
self._config.get('SUSHY_EMULATOR_STATE_DIR'), 'indicators')
self._indicators.update(
self._config.get('SUSHY_EMULATOR_INDICATOR_LEDS', {}))
@property
def driver(self):

View File

@ -10,14 +10,15 @@
# License for the specific language governing permissions and limitations
# under the License.
from sushy_tools.emulator.resources.managers.base import AbstractManagersDriver
from sushy_tools.emulator.resources import base
from sushy_tools import error
class FakeDriver(AbstractManagersDriver):
class FakeDriver(base.DriverBase):
"""Redfish manager that copied systems."""
def __init__(self, systems, chassis):
def __init__(self, config, logger, systems, chassis):
super().__init__(config, logger)
self._systems = systems
self._chassis = chassis

View File

@ -1,77 +0,0 @@
# Copyright 2019 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 abc
from sushy_tools.emulator import base
class AbstractManagersDriver(base.DriverBase, metaclass=abc.ABCMeta):
"""Base class backing Redfish Managers"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
cls._config = config
cls._logger = logger
return cls
@property
@abc.abstractmethod
def driver(self):
"""Return human-friendly driver information
:returns: driver information as `str`
"""
@abc.abstractmethod
def get_manager(self, identity):
"""Get a manager by its identity
:returns: Redfish manager object.
"""
@property
@abc.abstractmethod
def managers(self):
"""Return available Redfish managers
:returns: list of UUIDs representing the managers
"""
@abc.abstractmethod
def get_managed_systems(self, manager, systems_driver):
"""Get systems managed by this manager.
:param manager: Redfish manager object.
:param systems_driver: A systems driver.
:returns: List of Redfish system UUIDs.
"""
@abc.abstractmethod
def get_managed_chassis(self, manager, chassis_driver):
"""Get chassis managed by this manager.
:param manager: Redfish manager object.
:param chassis_driver: A chassis driver.
:returns: List of Redfish chassis UUIDs.
"""
@abc.abstractmethod
def get_managers_for_system(self, ident):
"""Get managers that manage the given system.
:param ident: System UUID.
:returns: list of UUIDs representing the managers
"""

View File

@ -15,21 +15,16 @@
import uuid
from sushy_tools.emulator.resources.base import DriverBase
from sushy_tools.emulator.resources import base
from sushy_tools import error
class StaticDriver(DriverBase):
class StaticDriver(base.DriverBase):
"""Redfish storage backed by configuration file"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
cls._config = config
cls._logger = logger
cls._storage = cls._config.get('SUSHY_EMULATOR_STORAGE', {})
return cls
def __init__(self, config, logger):
super().__init__(config, logger)
self._storage = self._config.get('SUSHY_EMULATOR_STORAGE', {})
@property
def driver(self):

View File

@ -15,12 +15,16 @@
import abc
from sushy_tools.emulator.resources.base import DriverBase
class AbstractSystemsDriver(DriverBase, metaclass=abc.ABCMeta):
class AbstractSystemsDriver(metaclass=abc.ABCMeta):
"""Base class for all virtualization drivers"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
"""Initialize class attribute."""
cls._config = config
cls._logger = logger
@property
@abc.abstractmethod
def driver(self):

View File

@ -21,27 +21,21 @@ from urllib import parse as urlparse
import requests
from sushy_tools.emulator import memoize
from sushy_tools.emulator.resources.base import DriverBase
from sushy_tools.emulator.resources import base
from sushy_tools import error
class StaticDriver(DriverBase):
"""Redfish virtual media simulator
class StaticDriver(base.DriverBase):
"""Redfish virtual media simulator."""
"""
def __init__(self, config, logger):
super().__init__(config, logger)
self._devices = memoize.PersistentDict()
if hasattr(self._devices, 'make_permanent'):
self._devices.make_permanent(
self._config.get('SUSHY_EMULATOR_STATE_DIR'), 'vmedia')
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
cls._config = config
cls._logger = logger
cls._devices = memoize.PersistentDict()
if hasattr(cls._devices, 'make_permanent'):
cls._devices.make_permanent(
cls._config.get('SUSHY_EMULATOR_STATE_DIR'), 'vmedia')
device_types = cls._config.get(
device_types = self._config.get(
'SUSHY_EMULATOR_VMEDIA_DEVICES')
if device_types is None:
device_types = {
@ -61,9 +55,7 @@ class StaticDriver(DriverBase):
}
}
cls._device_types = device_types
return cls
self._device_types = device_types
def _get_device(self, identity, device):
try:

View File

@ -16,29 +16,23 @@
import uuid
from sushy_tools.emulator import memoize
from sushy_tools.emulator.resources.base import DriverBase
from sushy_tools.emulator.resources import base
class StaticDriver(DriverBase):
class StaticDriver(base.DriverBase):
"""Redfish Volumes emulated in libvirt backed by the config file
Maintains the libvirt volumes in memory.
"""
@classmethod
def initialize(cls, config, logger, *args, **kwargs):
cls._config = config
cls._logger = logger
cls._volumes = memoize.PersistentDict()
def __init__(self, config, logger):
super().__init__(config, logger)
self._volumes = memoize.PersistentDict()
self._volumes.make_permanent(
self._config.get('SUSHY_EMULATOR_STATE_DIR'), 'volumes')
if hasattr(cls._volumes, 'make_permanent'):
cls._volumes.make_permanent(
cls._config.get('SUSHY_EMULATOR_STATE_DIR'), 'volumes')
cls._volumes.update(
cls._config.get('SUSHY_EMULATOR_VOLUMES', {}))
return cls
self._volumes.update(
self._config.get('SUSHY_EMULATOR_VOLUMES', {}))
@property
def driver(self):

View File

@ -17,7 +17,7 @@ import uuid
from oslotest import base
from sushy_tools.emulator.resources.chassis.staticdriver import StaticDriver
from sushy_tools.emulator.resources.chassis import StaticDriver
from sushy_tools import error
@ -44,11 +44,8 @@ class StaticDriverTestCase(base.BaseTestCase):
self.mgr_name = 'manager01'
self.mgr_uuid = self.uuid.replace('8', '2')
test_driver = StaticDriver.initialize(
{'SUSHY_EMULATOR_CHASSIS': self.chassis},
mock.MagicMock())
self.test_driver = test_driver()
self.test_driver = StaticDriver(
{'SUSHY_EMULATOR_CHASSIS': self.chassis}, mock.MagicMock())
systems_mock = mock.MagicMock()
systems_mock.name.return_value = self.sys_name

View File

@ -16,7 +16,7 @@
from oslotest import base
from six.moves import mock
from sushy_tools.emulator.resources.drives.staticdriver import StaticDriver
from sushy_tools.emulator.resources.drives import StaticDriver
class StaticDriverTestCase(base.BaseTestCase):
@ -44,14 +44,12 @@ class StaticDriverTestCase(base.BaseTestCase):
}
def test_get_drives(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
drv_col = test_driver.get_drives(self.SYSTEM_UUID, self.STORAGE_ID)
self.assertEqual(self.DRIVE_COL, drv_col)
def test_get_all_drives(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
drives = test_driver.get_all_drives()
self.assertEqual({('da69abcc-dae0-4913-9a7b-d344043097c0', '1',
'32ADF365C6C1B7BD'),

View File

@ -16,12 +16,10 @@ from unittest import mock
from oslotest import base
from sushy_tools.emulator.resources.indicators.staticdriver import StaticDriver
from sushy_tools.emulator.resources.indicators import StaticDriver
from sushy_tools import error
@mock.patch('sushy_tools.emulator.resources.indicators'
'.staticdriver.memoize.PersistentDict', new=dict)
class StaticDriverTestCase(base.BaseTestCase):
UUID = "58893887-8974-2487-2389-841168418919"
@ -33,30 +31,27 @@ class StaticDriverTestCase(base.BaseTestCase):
}
}
def setUp(self):
super().setUp()
with mock.patch('sushy_tools.emulator.memoize.PersistentDict',
return_value={}, autospec=True):
self.test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
def test_indicators(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
indicators = test_driver.indicators
indicators = self.test_driver.indicators
self.assertEqual([self.UUID], indicators)
def test_get_indicator_state(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
state = test_driver.get_indicator_state(self.UUID)
state = self.test_driver.get_indicator_state(self.UUID)
self.assertEqual('Off', state)
def test_set_indicator_state_ok(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
test_driver.set_indicator_state(self.UUID, 'Lit')
state = test_driver.get_indicator_state(self.UUID)
self.test_driver.set_indicator_state(self.UUID, 'Lit')
state = self.test_driver.get_indicator_state(self.UUID)
self.assertEqual('Lit', state)
def test_set_indicator_state_fail(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
self.assertRaises(
error.FishyError,
test_driver.set_indicator_state,
self.test_driver.set_indicator_state,
self.UUID, 'Blah')

View File

@ -16,7 +16,7 @@
from oslotest import base
from six.moves import mock
from sushy_tools.emulator.resources.managers import fakedriver as fakemgrdriver
from sushy_tools.emulator.resources import managers
from sushy_tools import error
@ -32,8 +32,8 @@ class FakeDriverTestCase(base.BaseTestCase):
'Id': self.identity,
'Name': 'name-Manager'}
self.chassis = mock.Mock(chassis=[])
test_driver = fakemgrdriver.FakeDriver.initialize({}, mock.Mock())
self.test_driver = test_driver(self.systems, self.chassis)
self.test_driver = managers.FakeDriver({}, mock.Mock(),
self.systems, self.chassis)
def test_get_manager_not_found(self):
self.systems.uuid.side_effect = error.FishyError('boom')
@ -45,8 +45,8 @@ class FakeDriverTestCase(base.BaseTestCase):
self.assertEqual(self.manager, manager)
def test_managers(self):
managers = self.test_driver.managers
self.assertEqual([self.identity], managers)
result = self.test_driver.managers
self.assertEqual([self.identity], result)
def test_managed_systems(self):
self.assertEqual(

View File

@ -16,7 +16,7 @@
from oslotest import base
from six.moves import mock
from sushy_tools.emulator.resources.storage.staticdriver import StaticDriver
from sushy_tools.emulator.resources.storage import StaticDriver
class StaticDriverTestCase(base.BaseTestCase):
@ -42,13 +42,11 @@ class StaticDriverTestCase(base.BaseTestCase):
}
def test_get_storage_col(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
stg_col = test_driver.get_storage_col(self.UUID)
self.assertEqual(self.STORAGE_COL, stg_col)
def test_get_all_storage(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
stg = test_driver.get_all_storage()
self.assertEqual([(self.UUID, '1')], stg)

View File

@ -12,14 +12,15 @@
# 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 builtins
from unittest import mock
from oslotest import base
from sushy_tools.emulator.resources.vmedia.staticdriver import StaticDriver
from sushy_tools.emulator.resources import vmedia
@mock.patch('sushy_tools.emulator.memoize.PersistentDict', new=dict)
class StaticDriverTestCase(base.BaseTestCase):
UUID = 'ZZZ-YYY-XXX'
@ -43,54 +44,40 @@ class StaticDriverTestCase(base.BaseTestCase):
}
}
def test_devices(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
def setUp(self):
super().setUp()
with mock.patch('sushy_tools.emulator.memoize.PersistentDict',
return_value={}, autospec=True):
self.test_driver = vmedia.StaticDriver(self.CONFIG,
mock.MagicMock())
devices = test_driver.devices
def test_devices(self):
devices = self.test_driver.devices
self.assertEqual(['Cd', 'Floppy'], sorted(devices))
def test_get_device_name(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
device_name = test_driver.get_device_name(
device_name = self.test_driver.get_device_name(
self.UUID, 'Cd')
self.assertEqual('Virtual CD', device_name)
def test_get_device_media_types(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
media_types = test_driver.get_device_media_types(
media_types = self.test_driver.get_device_media_types(
self.UUID, 'Cd')
self.assertEqual(['CD', 'DVD'], media_types)
def test_get_device_image_info(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
dev_info = test_driver.get_device_image_info(
dev_info = self.test_driver.get_device_image_info(
self.UUID, 'Cd')
expected = ('', '', False, False)
self.assertEqual(expected, dev_info)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.StaticDriver._get_device',
autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.open')
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.os.rename', autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.tempfile', autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.requests', autospec=True)
@mock.patch.object(vmedia.StaticDriver, '_get_device', autospec=True)
@mock.patch.object(builtins, 'open', autospec=True)
@mock.patch.object(vmedia.os, 'rename', autospec=True)
@mock.patch.object(vmedia, 'tempfile', autospec=True)
@mock.patch.object(vmedia, 'requests', autospec=True)
def test_insert_image(self, mock_requests, mock_tempfile, mock_rename,
mock_open, mock_get_device):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
device_info = {}
mock_get_device.return_value = device_info
@ -104,7 +91,7 @@ class StaticDriverTestCase(base.BaseTestCase):
'content-disposition': 'attachment; filename="fish.iso"'
}
local_file = test_driver.insert_image(
local_file = self.test_driver.insert_image(
self.UUID, 'Cd', 'http://fish.it/red.iso', inserted=True,
write_protected=False)
@ -119,23 +106,14 @@ class StaticDriverTestCase(base.BaseTestCase):
self.assertTrue(device_info['Inserted'])
self.assertFalse(device_info['WriteProtected'])
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.StaticDriver._get_device',
autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.open')
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.os.rename', autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.tempfile', autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.requests', autospec=True)
@mock.patch.object(vmedia.StaticDriver, '_get_device', autospec=True)
@mock.patch.object(builtins, 'open', autospec=True)
@mock.patch.object(vmedia.os, 'rename', autospec=True)
@mock.patch.object(vmedia, 'tempfile', autospec=True)
@mock.patch.object(vmedia, 'requests', autospec=True)
def test_insert_image_no_local_file(self, mock_requests, mock_tempfile,
mock_rename, mock_open,
mock_get_device):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
device_info = {}
mock_get_device.return_value = device_info
@ -147,7 +125,7 @@ class StaticDriverTestCase(base.BaseTestCase):
mock_rsp = mock_requests.get.return_value.__enter__.return_value
mock_rsp.headers = {}
local_file = test_driver.insert_image(
local_file = self.test_driver.insert_image(
self.UUID, 'Cd', 'http://fish.it/red.iso', inserted=True,
write_protected=False)
@ -162,23 +140,14 @@ class StaticDriverTestCase(base.BaseTestCase):
self.assertTrue(device_info['Inserted'])
self.assertFalse(device_info['WriteProtected'])
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.StaticDriver._get_device',
autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.open')
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.os.rename', autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.tempfile', autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.requests', autospec=True)
@mock.patch.object(vmedia.StaticDriver, '_get_device', autospec=True)
@mock.patch.object(builtins, 'open', autospec=True)
@mock.patch.object(vmedia.os, 'rename', autospec=True)
@mock.patch.object(vmedia, 'tempfile', autospec=True)
@mock.patch.object(vmedia, 'requests', autospec=True)
def test_insert_image_full_url_v6(self, mock_requests, mock_tempfile,
mock_rename, mock_open,
mock_get_device):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
device_info = {}
mock_get_device.return_value = device_info
@ -191,7 +160,7 @@ class StaticDriverTestCase(base.BaseTestCase):
mock_rsp.headers = {}
full_url = 'http://[::2]:80/redfish/boot-abc?filename=tmp.iso'
local_file = test_driver.insert_image(
local_file = self.test_driver.insert_image(
self.UUID, 'Cd', full_url,
inserted=True, write_protected=False)
@ -205,21 +174,15 @@ class StaticDriverTestCase(base.BaseTestCase):
self.assertTrue(device_info['Inserted'])
self.assertFalse(device_info['WriteProtected'])
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.StaticDriver._get_device',
autospec=True)
@mock.patch('sushy_tools.emulator.resources.vmedia'
'.staticdriver.os.unlink', autospec=True)
@mock.patch.object(vmedia.StaticDriver, '_get_device', autospec=True)
@mock.patch.object(vmedia.os, 'unlink', autospec=True)
def test_eject_image(self, mock_unlink, mock_get_device):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
device_info = {
'_local_file': '/tmp/fish.iso'
}
mock_get_device.return_value = device_info
test_driver.eject_image(self.UUID, 'Cd')
self.test_driver.eject_image(self.UUID, 'Cd')
self.assertEqual('', device_info['Image'])
self.assertEqual('', device_info['ImageName'])

View File

@ -16,11 +16,11 @@ from unittest import mock
from oslotest import base
from sushy_tools.emulator.resources.volumes.staticdriver import StaticDriver
from sushy_tools.emulator.resources.volumes import StaticDriver
@mock.patch('sushy_tools.emulator.resources.volumes'
'.staticdriver.memoize.PersistentDict', new=dict)
@mock.patch('sushy_tools.emulator.resources.volumes.memoize.PersistentDict',
new=dict)
class StaticDriverTestCase(base.BaseTestCase):
SYSTEM_UUID = "da69abcc-dae0-4913-9a7b-d344043097c0"
@ -50,16 +50,16 @@ class StaticDriverTestCase(base.BaseTestCase):
}
}
def setUp(self):
super().setUp()
self.test_driver = StaticDriver(self.CONFIG, mock.MagicMock())
def test_get_volumes_col(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
vol_col = test_driver.get_volumes_col(self.SYSTEM_UUID,
self.STORAGE_ID)
vol_col = self.test_driver.get_volumes_col(self.SYSTEM_UUID,
self.STORAGE_ID)
self.assertEqual(self.VOLUMES_COL, vol_col)
def test_add_volume(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
vol = {
"libvirtPoolName": "sushyPool",
"libvirtVolName": "testVol2",
@ -68,14 +68,12 @@ class StaticDriverTestCase(base.BaseTestCase):
"VolumeType": "Mirrored",
"CapacityBytes": 76584
}
test_driver.add_volume(self.SYSTEM_UUID, self.STORAGE_ID, vol)
vol_col = test_driver.get_volumes_col(self.SYSTEM_UUID,
self.STORAGE_ID)
self.test_driver.add_volume(self.SYSTEM_UUID, self.STORAGE_ID, vol)
vol_col = self.test_driver.get_volumes_col(self.SYSTEM_UUID,
self.STORAGE_ID)
self.assertTrue(vol in vol_col)
def test_delete_volume(self):
test_driver = StaticDriver.initialize(
self.CONFIG, mock.MagicMock())()
vol = {
"libvirtPoolName": "sushyPool",
"libvirtVolName": "testVol",
@ -84,7 +82,7 @@ class StaticDriverTestCase(base.BaseTestCase):
"VolumeType": "Mirrored",
"CapacityBytes": 23748
}
test_driver.delete_volume(self.SYSTEM_UUID, self.STORAGE_ID, vol)
vol_col = test_driver.get_volumes_col(self.SYSTEM_UUID,
self.STORAGE_ID)
self.test_driver.delete_volume(self.SYSTEM_UUID, self.STORAGE_ID, vol)
vol_col = self.test_driver.get_volumes_col(self.SYSTEM_UUID,
self.STORAGE_ID)
self.assertFalse(vol in vol_col)