def test_serialize(self): metadata = { 'name': 'image1', 'is_public': True, 'foo': 'bar', 'properties': { 'prop1': 'propvalue1', 'mappings': [{ 'virtual': 'aaa', 'device': 'bbb' }, { 'virtual': 'xxx', 'device': 'yyy' }], 'block_device_mapping': [{ 'virtual_device': 'fake', 'device_name': '/dev/fake' }, { 'virtual_device': 'ephemeral0', 'device_name': '/dev/fake0' }] } } converted_expected = { 'name': 'image1', 'is_public': True, 'foo': 'bar', 'properties': { 'prop1': 'propvalue1' } } converted = service_utils._convert(metadata, 'to') self.assertEqual(metadata, service_utils._convert(converted, 'from')) # Fields that rely on dict ordering can't be compared as text mappings = jsonutils.loads(converted['properties'].pop('mappings')) self.assertEqual([{ "device": "bbb", "virtual": "aaa" }, { "device": "yyy", "virtual": "xxx" }], mappings) bd_mapping = jsonutils.loads( converted['properties'].pop('block_device_mapping')) self.assertEqual([{ "virtual_device": "fake", "device_name": "/dev/fake" }, { "virtual_device": "ephemeral0", "device_name": "/dev/fake0" }], bd_mapping) # Compare the remaining self.assertEqual(converted_expected, converted)
def __init__(self, node, **kwargs): self.state = None self.retries = None self.node_id = node['id'] self.power_info = json.loads(node['control_info']) self.address = self.power_info.get('ipmi_address', None) self.user = self.power_info.get('ipmi_username', None) self.password = self.power_info.get('ipmi_password', None) self.port = self.power_info.get('terminal_port', None) if self.node_id is None: raise exception.InvalidParameterValue( _("Node id not supplied " "to IPMI")) if self.address is None: raise exception.InvalidParameterValue( _("Address not supplied " "to IPMI")) if self.user is None: raise exception.InvalidParameterValue( _("User not supplied " "to IPMI")) if self.password is None: raise exception.InvalidParameterValue( _("Password not supplied " "to IPMI"))
def test__get_image_file_path(self): node = self._create_test_node( id=345, driver='fake_pxe', driver_info=json.loads(db_utils.pxe_info), ) info = pxe._parse_driver_info(node) self.assertEqual('/var/lib/ironic/images/fake_instance_name/disk', pxe._get_image_file_path(info))
def setUp(self): super(PXEDriverTestCase, self).setUp() self.driver = mgr_utils.get_mocked_node_manager(driver='fake_pxe') n = db_utils.get_test_node( driver='fake_pxe', driver_info=json.loads(db_utils.pxe_info), instance_uuid='instance_uuid_123') self.dbapi = dbapi.get_instance() self.node = self.dbapi.create_node(n)
def setUp(self): super(PXEPrivateMethodsTestCase, self).setUp() n = { 'driver': 'fake_pxe', 'driver_info': json.loads(db_utils.pxe_info), 'instance_uuid': 'instance_uuid_123', 'id': 123} self.dbapi = dbapi.get_instance() self.node = self._create_test_node(**n)
def load_json(cls, data, default_rule=None): """ Allow loading of JSON rule data. """ # Suck in the JSON data and parse the rules rules = dict( (k, parse_rule(v)) for k, v in jsonutils.loads(data).items()) return cls(rules, default_rule)
def load_json(cls, data, default_rule=None): """ Allow loading of JSON rule data. """ # Suck in the JSON data and parse the rules rules = dict((k, parse_rule(v)) for k, v in jsonutils.loads(data).items()) return cls(rules, default_rule)
def test_serialize(self): metadata = {'name': 'image1', 'is_public': True, 'foo': 'bar', 'properties': { 'prop1': 'propvalue1', 'mappings': [ {'virtual': 'aaa', 'device': 'bbb'}, {'virtual': 'xxx', 'device': 'yyy'}], 'block_device_mapping': [ {'virtual_device': 'fake', 'device_name': '/dev/fake'}, {'virtual_device': 'ephemeral0', 'device_name': '/dev/fake0'}]}} converted_expected = { 'name': 'image1', 'is_public': True, 'foo': 'bar', 'properties': {'prop1': 'propvalue1'} } converted = service_utils._convert(metadata, 'to') self.assertEqual(metadata, service_utils._convert(converted, 'from')) # Fields that rely on dict ordering can't be compared as text mappings = jsonutils.loads(converted['properties'] .pop('mappings')) self.assertEqual([{"device": "bbb", "virtual": "aaa"}, {"device": "yyy", "virtual": "xxx"}], mappings) bd_mapping = jsonutils.loads(converted['properties'] .pop('block_device_mapping')) self.assertEqual([{"virtual_device": "fake", "device_name": "/dev/fake"}, {"virtual_device": "ephemeral0", "device_name": "/dev/fake0"}], bd_mapping) # Compare the remaining self.assertEqual(converted_expected, converted)
def test__parse_driver_info_good(self): # make sure we get back the expected things node = self._create_test_node( driver='fake_pxe', driver_info=json.loads(db_utils.pxe_info)) info = pxe._parse_driver_info(node) self.assertIsNotNone(info.get('instance_name')) self.assertIsNotNone(info.get('image_source')) self.assertIsNotNone(info.get('deploy_kernel')) self.assertIsNotNone(info.get('deploy_ramdisk')) self.assertIsNotNone(info.get('root_gb')) self.mox.VerifyAll()
def _convert(metadata, method): metadata = copy.deepcopy(metadata) properties = metadata.get('properties') if properties: for attr in _CONVERT_PROPS: if attr in properties: prop = properties[attr] if method == 'from': if isinstance(prop, six.string_types): properties[attr] = jsonutils.loads(prop) if method == 'to': if not isinstance(prop, six.string_types): properties[attr] = jsonutils.dumps(prop) return metadata
def _unpack_json_msg(self, msg): """Load the JSON data in msg if msg.content_type indicates that it is necessary. Put the loaded data back into msg.content and update msg.content_type appropriately. A Qpid Message containing a dict will have a content_type of 'amqp/map', whereas one containing a string that needs to be converted back from JSON will have a content_type of JSON_CONTENT_TYPE. :param msg: a Qpid Message object :returns: None """ if msg.content_type == JSON_CONTENT_TYPE: msg.content = jsonutils.loads(msg.content) msg.content_type = 'amqp/map'
def _parse_driver_info(node): driver_info = json.loads(node.get('driver_info', '')) ssh_info = driver_info.get('ssh') address = ssh_info.get('address', None) username = ssh_info.get('username', None) password = ssh_info.get('password', None) port = ssh_info.get('port', 22) key_filename = ssh_info.get('key_filename', None) virt_type = ssh_info.get('virt_type', None) # NOTE(deva): we map 'address' from API to 'host' for common utils res = { 'host': address, 'username': username, 'port': port, 'virt_type': virt_type, 'uuid': node.get('uuid') } if not virt_type: raise exception.InvalidParameterValue( _("SSHPowerDriver requires virt_type be set.")) cmd_set = COMMAND_SETS.get(virt_type, None) if not cmd_set: raise exception.InvalidParameterValue( _("SSHPowerDriver unknown virt_type (%s).") % cmd_set) res['cmd_set'] = cmd_set if not address or not username: raise exception.InvalidParameterValue( _("SSHPowerDriver requires both address and username be set.")) if password: res['password'] = password else: if not key_filename: raise exception.InvalidParameterValue( _("SSHPowerDriver requires either password or " "key_filename be set.")) if not os.path.isfile(key_filename): raise exception.FileNotFound(file_path=key_filename) res['key_filename'] = key_filename return res
def _parse_driver_info(node): driver_info = json.loads(node.get('driver_info', '')) ssh_info = driver_info.get('ssh') address = ssh_info.get('address', None) username = ssh_info.get('username', None) password = ssh_info.get('password', None) port = ssh_info.get('port', 22) key_filename = ssh_info.get('key_filename', None) virt_type = ssh_info.get('virt_type', None) # NOTE(deva): we map 'address' from API to 'host' for common utils res = { 'host': address, 'username': username, 'port': port, 'virt_type': virt_type, 'uuid': node.get('uuid') } if not virt_type: raise exception.InvalidParameterValue(_( "SSHPowerDriver requires virt_type be set.")) cmd_set = COMMAND_SETS.get(virt_type, None) if not cmd_set: raise exception.InvalidParameterValue(_( "SSHPowerDriver unknown virt_type (%s).") % cmd_set) res['cmd_set'] = cmd_set if not address or not username: raise exception.InvalidParameterValue(_( "SSHPowerDriver requires both address and username be set.")) if password: res['password'] = password else: if not key_filename: raise exception.InvalidParameterValue(_( "SSHPowerDriver requires either password or " "key_filename be set.")) if not os.path.isfile(key_filename): raise exception.FileNotFound(file_path=key_filename) res['key_filename'] = key_filename return res
def _parse_driver_info(node): driver_info = json.loads(node.get('driver_info', '')) ipmi_info = driver_info.get('ipmi') address = ipmi_info.get('address', None) username = ipmi_info.get('username', None) password = ipmi_info.get('password', None) port = ipmi_info.get('terminal_port', None) if not address or not username or not password: raise exception.InvalidParameterValue(_( "IPMI credentials not supplied to IPMI driver.")) return { 'address': address, 'username': username, 'password': password, 'port': port, 'uuid': node.get('uuid') }
def _parse_driver_info(node): driver_info = json.loads(node.get('driver_info', '')) ipmi_info = driver_info.get('ipmi') address = ipmi_info.get('address', None) username = ipmi_info.get('username', None) password = ipmi_info.get('password', None) port = ipmi_info.get('terminal_port', None) if not address or not username or not password: raise exception.InvalidParameterValue( _("IPMI credentials not supplied to IPMI driver.")) return { 'address': address, 'username': username, 'password': password, 'port': port, 'uuid': node.get('uuid') }
def deserialize_msg(msg): # NOTE(russellb): Hang on to your hats, this road is about to # get a little bumpy. # # Robustness Principle: # "Be strict in what you send, liberal in what you accept." # # At this point we have to do a bit of guessing about what it # is we just received. Here is the set of possibilities: # # 1) We received a dict. This could be 2 things: # # a) Inspect it to see if it looks like a standard message envelope. # If so, great! # # b) If it doesn't look like a standard message envelope, it could either # be a notification, or a message from before we added a message # envelope (referred to as version 1.0). # Just return the message as-is. # # 2) It's any other non-dict type. Just return it and hope for the best. # This case covers return values from rpc.call() from before message # envelopes were used. (messages to call a method were always a dict) if not isinstance(msg, dict): # See #2 above. return msg base_envelope_keys = (_VERSION_KEY, _MESSAGE_KEY) if not all(map(lambda key: key in msg, base_envelope_keys)): # See #1.b above. return msg # At this point we think we have the message envelope # format we were expecting. (#1.a above) if not version_is_compatible(_RPC_ENVELOPE_VERSION, msg[_VERSION_KEY]): raise UnsupportedRpcEnvelopeVersion(version=msg[_VERSION_KEY]) raw_msg = jsonutils.loads(msg[_MESSAGE_KEY]) return raw_msg
def deserialize_remote_exception(conf, data): failure = jsonutils.loads(str(data)) trace = failure.get('tb', []) message = failure.get('message', "") + "\n" + "\n".join(trace) name = failure.get('class') module = failure.get('module') # NOTE(ameade): We DO NOT want to allow just any module to be imported, in # order to prevent arbitrary code execution. if module not in conf.allowed_rpc_exception_modules: return RemoteError(name, failure.get('message'), trace) try: mod = importutils.import_module(module) klass = getattr(mod, name) if not issubclass(klass, Exception): raise TypeError("Can only deserialize Exceptions") failure = klass(*failure.get('args', []), **failure.get('kwargs', {})) except (AttributeError, TypeError, ImportError): return RemoteError(name, failure.get('message'), trace) ex_type = type(failure) str_override = lambda self: message new_ex_type = type(ex_type.__name__ + _REMOTE_POSTFIX, (ex_type, ), { '__str__': str_override, '__unicode__': str_override }) new_ex_type.__module__ = '%s%s' % (module, _REMOTE_POSTFIX) try: # NOTE(ameade): Dynamically create a new exception type and swap it in # as the new type for the exception. This only works on user defined # Exceptions and not core python exceptions. This is important because # we cannot necessarily change an exception message so we must override # the __str__ method. failure.__class__ = new_ex_type except TypeError: # NOTE(ameade): If a core exception then just add the traceback to the # first exception argument. failure.args = (message, ) + failure.args[1:] return failure
def deserialize_remote_exception(conf, data): failure = jsonutils.loads(str(data)) trace = failure.get("tb", []) message = failure.get("message", "") + "\n" + "\n".join(trace) name = failure.get("class") module = failure.get("module") # NOTE(ameade): We DO NOT want to allow just any module to be imported, in # order to prevent arbitrary code execution. if module not in conf.allowed_rpc_exception_modules: return RemoteError(name, failure.get("message"), trace) try: mod = importutils.import_module(module) klass = getattr(mod, name) if not issubclass(klass, Exception): raise TypeError("Can only deserialize Exceptions") failure = klass(*failure.get("args", []), **failure.get("kwargs", {})) except (AttributeError, TypeError, ImportError): return RemoteError(name, failure.get("message"), trace) ex_type = type(failure) str_override = lambda self: message new_ex_type = type( ex_type.__name__ + _REMOTE_POSTFIX, (ex_type,), {"__str__": str_override, "__unicode__": str_override} ) new_ex_type.__module__ = "%s%s" % (module, _REMOTE_POSTFIX) try: # NOTE(ameade): Dynamically create a new exception type and swap it in # as the new type for the exception. This only works on user defined # Exceptions and not core python exceptions. This is important because # we cannot necessarily change an exception message so we must override # the __str__ method. failure.__class__ = new_ex_type except TypeError: # NOTE(ameade): If a core exception then just add the traceback to the # first exception argument. failure.args = (message,) + failure.args[1:] return failure
def _parse_driver_info(node): driver_info = json.loads(node.get('driver_info', '')) plm_device = driver_info.get('plm_device') # NOTE(deva): we map 'address' from API to 'host' for common utils res = { 'plm_device': plm_device, 'uuid': node.get('uuid') } if not plm_device: raise exception.InvalidParameterValue(_( "PLM-DeviceDriver requires plm_device be set.")) cmd_set = COMMAND_SETS.get('PLM', None) if not cmd_set: raise exception.InvalidParameterValue(_( "PLM-DeviceDriver unknown command set (%s).") % cmd_set) res['cmd_set'] = cmd_set return res
def __init__(self, node, **kwargs): self.state = None self.retries = None self.node_id = node['id'] self.power_info = json.loads(node['power_info']) self.address = self.power_info.get('address', None) self.user = self.power_info.get('user', None) self.password = self.power_info.get('password', None) self.port = self.power_info.get('terminal_port', None) if self.node_id is None: raise exception.InvalidParameterValue(_("Node id not supplied " "to IPMI")) if self.address is None: raise exception.InvalidParameterValue(_("Address not supplied " "to IPMI")) if self.user is None: raise exception.InvalidParameterValue(_("User not supplied " "to IPMI")) if self.password is None: raise exception.InvalidParameterValue(_("Password not supplied " "to IPMI"))
import mox import paramiko from ironic.openstack.common import jsonutils as json from ironic.common import exception from ironic.common import states from ironic.conductor import task_manager from ironic.db import api as dbapi from ironic.drivers.modules import ssh from ironic.tests import base from ironic.tests.conductor import utils as mgr_utils from ironic.tests.db import base as db_base from ironic.tests.db import utils as db_utils INFO_DICT = json.loads(db_utils.ssh_info).get('ssh') class SSHValidateParametersTestCase(base.TestCase): def test__parse_driver_info_good(self): # make sure we get back the expected things node = db_utils.get_test_node( driver='fake_ssh', driver_info=db_utils.ssh_info) info = ssh._parse_driver_info(node) self.assertIsNotNone(info.get('host')) self.assertIsNotNone(info.get('username')) self.assertIsNotNone(info.get('password')) self.assertIsNotNone(info.get('port')) self.assertIsNotNone(info.get('virt_type'))
"ssh_virt_type": "vbox", "ssh_key_filename": "/not/real/file", } ) pxe_info = json.dumps( { "pxe_instance_name": "fake_instance_name", "pxe_image_source": "glance://image_uuid", "pxe_deploy_kernel": "glance://deploy_kernel_uuid", "pxe_deploy_ramdisk": "glance://deploy_ramdisk_uuid", "pxe_root_gb": 100, } ) pxe_ssh_info = json.dumps(dict(json.loads(pxe_info), **json.loads(ssh_info))) pxe_ipmi_info = json.dumps(dict(json.loads(pxe_info), **json.loads(ipmi_info))) properties = {"cpu_arch": "x86_64", "cpu_num": "8", "storage": "1024", "memory": "4096"} def get_test_node(**kw): node = { "id": kw.get("id", 123), "uuid": kw.get("uuid", "1be26c0b-03f2-4d2e-ae87-c02d7f33c123"), "chassis_id": kw.get("chassis_id", 42), "power_state": kw.get("power_state", states.NOSTATE), "target_power_state": kw.get("target_power_state", states.NOSTATE), "provision_state": kw.get("provision_state", states.NOSTATE), "target_provision_state": kw.get("target_provision_state", states.NOSTATE),
} }) pxe_info = json.dumps( { 'pxe': { "instance_name": "fake_instance_name", "image_source": "glance://image_uuid", "deploy_kernel": "glance://deploy_kernel_uuid", "deploy_ramdisk": "glance://deploy_ramdisk_uuid", "root_gb": 100, } }) pxe_ssh_info = json.dumps( dict(json.loads(pxe_info), **json.loads(ssh_info))) pxe_ipmi_info = json.dumps( dict(json.loads(pxe_info), **json.loads(ipmi_info))) properties = { "cpu_arch": "x86_64", "cpu_num": "8", "storage": "1024", "memory": "4096", } def get_test_node(**kw): node = { 'id': kw.get('id', 123),
def _deserialize(data): """ Deserialization wrapper """ LOG.debug(_("Deserializing: %s"), data) return jsonutils.loads(data)
from ironic.common import utils from ironic.conductor import task_manager from ironic.db import api as dbapi from ironic.drivers.modules import pxe from ironic.openstack.common import context from ironic.openstack.common import fileutils from ironic.openstack.common import jsonutils as json from ironic.tests import base from ironic.tests.conductor import utils as mgr_utils from ironic.tests.db import base as db_base from ironic.tests.db import utils as db_utils CONF = cfg.CONF INFO_DICT = json.loads(db_utils.pxe_info) class PXEValidateParametersTestCase(base.TestCase): def setUp(self): super(PXEValidateParametersTestCase, self).setUp() self.dbapi = dbapi.get_instance() def _create_test_node(self, **kwargs): n = db_utils.get_test_node(**kwargs) return self.dbapi.create_node(n) def test__parse_driver_info_good(self): # make sure we get back the expected things node = self._create_test_node(
import paramiko from ironic.openstack.common import context from ironic.openstack.common import jsonutils as json from ironic.common import exception from ironic.common import states from ironic.conductor import task_manager from ironic.db import api as dbapi from ironic.drivers.modules import ssh from ironic.tests import base from ironic.tests.conductor import utils as mgr_utils from ironic.tests.db import base as db_base from ironic.tests.db import utils as db_utils INFO_DICT = json.loads(db_utils.ssh_info) class SSHValidateParametersTestCase(base.TestCase): def test__parse_driver_info_good(self): # make sure we get back the expected things node = db_utils.get_test_node( driver='fake_ssh', driver_info=INFO_DICT) info = ssh._parse_driver_info(node) self.assertIsNotNone(info.get('host')) self.assertIsNotNone(info.get('username')) self.assertIsNotNone(info.get('password')) self.assertIsNotNone(info.get('port')) self.assertIsNotNone(info.get('virt_type'))
from ironic.common import exception from ironic.common import states from ironic.conductor import task_manager from ironic.db import api as db_api from ironic.drivers.modules import ipminative from ironic.openstack.common import jsonutils as json from ironic.tests import base from ironic.tests.conductor import utils as mgr_utils from ironic.tests.db import base as db_base from ironic.tests.db import utils as db_utils from oslo.config import cfg CONF = cfg.CONF INFO_DICT = json.loads(db_utils.ipmi_info) class IPMINativePrivateMethodTestCase(base.TestCase): """Test cases for ipminative private methods.""" def setUp(self): super(IPMINativePrivateMethodTestCase, self).setUp() n = db_utils.get_test_node( driver='fake_ipminative', driver_info=INFO_DICT) self.dbapi = db_api.get_instance() self.node = self.dbapi.create_node(n) self.info = ipminative._parse_driver_info(self.node) ipmi_patch = mock.patch('pyghmi.ipmi.command.Command') self.ipmi_mock = ipmi_patch.start()
def _deserialize(data): """Deserialization wrapper.""" LOG.debug(_("Deserializing: %s"), data) return jsonutils.loads(data)
from ironic.common import utils from ironic.conductor import task_manager from ironic.db import api as dbapi from ironic.drivers.modules import pxe from ironic.openstack.common import context from ironic.openstack.common import fileutils from ironic.openstack.common import jsonutils as json from ironic.tests import base from ironic.tests.conductor import utils as mgr_utils from ironic.tests.db import base as db_base from ironic.tests.db import utils as db_utils CONF = cfg.CONF INFO_DICT = json.loads(db_utils.pxe_info).get('pxe') class PXEValidateParametersTestCase(base.TestCase): def setUp(self): super(PXEValidateParametersTestCase, self).setUp() self.dbapi = dbapi.get_instance() def _create_test_node(self, **kwargs): n = db_utils.get_test_node(**kwargs) return self.dbapi.create_node(n) def test__parse_driver_info_good(self): # make sure we get back the expected things node = self._create_test_node(