Keep cache of nodes under discovery in the local database
Introduces SQLite database to keep a cache of nodes for discovery. Move node lookup code into a new module `node_cache`. This will greatly improve performance by doing less network requests and will allow both implementing timeouts and getting rid of using maintenance mode. Change-Id: I543cb32d9d04f55bcf4573e9e0d341317cae4df6 Partial-Bug: #1391868 Partial-Bug: #1391871
This commit is contained in:
parent
baef340b28
commit
cb32def5c5
|
@ -192,6 +192,9 @@ Change Log
|
|||
v1.0.0
|
||||
~~~~~~
|
||||
|
||||
* Cache nodes under discovery in a local SQLite database. Set ``database``
|
||||
configuration option to persist this database. Improves performance by
|
||||
making less calls to Ironic API.
|
||||
* Create ``CONTRIBUTING.rst``.
|
||||
|
||||
v0.2.4
|
||||
|
|
|
@ -32,6 +32,8 @@
|
|||
; Whether to authenticate with Keystone on discovery initialization endpoint.
|
||||
; Note that discovery postback endpoint is never authenticated.
|
||||
;authenticate = true
|
||||
; SQLite3 database to store nodes under discovery, defaults to in-memory.
|
||||
;database =
|
||||
|
||||
; Debug mode enabled/disabled.
|
||||
;debug = false
|
||||
|
|
|
@ -23,7 +23,8 @@ DEFAULTS = {
|
|||
'firewall_update_period': '15',
|
||||
'ports_for_inactive_interfaces': 'false',
|
||||
'ironic_retry_attempts': '5',
|
||||
'ironic_retry_period': '5'
|
||||
'ironic_retry_period': '5',
|
||||
'database': '',
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -19,11 +19,11 @@ from ironicclient import exceptions
|
|||
|
||||
from ironic_discoverd import conf
|
||||
from ironic_discoverd import firewall
|
||||
from ironic_discoverd import node_cache
|
||||
from ironic_discoverd import utils
|
||||
|
||||
|
||||
LOG = logging.getLogger("discoverd")
|
||||
ALLOW_SEARCH_BY_MAC = True
|
||||
|
||||
|
||||
def process(node_info):
|
||||
|
@ -71,47 +71,20 @@ def process(node_info):
|
|||
'ipmi_address': node_info.get('ipmi_address')})
|
||||
LOG.info('Eligible interfaces are %s', valid_interfaces)
|
||||
|
||||
uuid = node_cache.pop_node(bmc_address=node_info['ipmi_address'],
|
||||
mac=valid_macs)
|
||||
if uuid is None:
|
||||
LOG.debug('Unable to find a node, cannot proceed')
|
||||
return
|
||||
|
||||
ironic = utils.get_client()
|
||||
bmc_known = bool(node_info.get('ipmi_address'))
|
||||
if bmc_known:
|
||||
# TODO(dtantsur): bulk loading
|
||||
nodes = ironic.node.list(maintenance=True, limit=0,
|
||||
sort_key='created_at',
|
||||
sort_dir='desc', detail=True)
|
||||
address = node_info['ipmi_address']
|
||||
for node in nodes:
|
||||
if node.driver_info.get('ipmi_address') == address:
|
||||
break
|
||||
else:
|
||||
LOG.error('Unable to find node with ipmi_address %s',
|
||||
node_info['ipmi_address'])
|
||||
return
|
||||
elif ALLOW_SEARCH_BY_MAC:
|
||||
# In case of testing with vms and pxe_ssh driver
|
||||
LOG.warning('No BMC address provided, trying to use MAC '
|
||||
'addresses for finding node')
|
||||
port = None
|
||||
for mac in valid_macs:
|
||||
try:
|
||||
port = ironic.port.get_by_address(mac)
|
||||
except exceptions.NotFound:
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
if port is not None:
|
||||
try:
|
||||
node = ironic.node.get(port.node_uuid)
|
||||
except exceptions.NotFound:
|
||||
node = None
|
||||
|
||||
if port is None or node is None:
|
||||
LOG.error('Unable to find node with macs %s',
|
||||
valid_macs)
|
||||
return
|
||||
else:
|
||||
LOG.error('No ipmi_address provided and searching by MAC is not '
|
||||
'allowed')
|
||||
node = ironic.node.get(uuid)
|
||||
except exceptions.NotFound as exc:
|
||||
LOG.error('Node UUID %(uuid)s is in the cache, but not found '
|
||||
'by Ironic: %(exc)s',
|
||||
{'uuid': uuid,
|
||||
'exc': exc})
|
||||
return
|
||||
|
||||
if not node.extra.get('on_discovery'):
|
||||
|
@ -153,16 +126,10 @@ def _process_node(ironic, node, node_info, valid_macs):
|
|||
'management configuration:\n%s', node.uuid, exc)
|
||||
|
||||
|
||||
class DiscoveryFailed(Exception):
|
||||
def __init__(self, msg, code=400):
|
||||
super(DiscoveryFailed, self).__init__(msg)
|
||||
self.http_code = code
|
||||
|
||||
|
||||
def discover(uuids):
|
||||
"""Initiate discovery for given node uuids."""
|
||||
if not uuids:
|
||||
raise DiscoveryFailed("No nodes to discover")
|
||||
raise utils.DiscoveryFailed("No nodes to discover")
|
||||
|
||||
ironic = utils.get_client()
|
||||
LOG.debug('Validating nodes %s', uuids)
|
||||
|
@ -172,10 +139,10 @@ def discover(uuids):
|
|||
node = ironic.node.get(uuid)
|
||||
except exceptions.NotFound:
|
||||
LOG.error('Node %s cannot be found', uuid)
|
||||
raise DiscoveryFailed("Cannot find node %s" % uuid, code=404)
|
||||
raise utils.DiscoveryFailed("Cannot find node %s" % uuid, code=404)
|
||||
except exceptions.HttpError as exc:
|
||||
LOG.exception('Cannot get node %s', uuid)
|
||||
raise DiscoveryFailed("Cannot get node %s: %s" % (uuid, exc))
|
||||
raise utils.DiscoveryFailed("Cannot get node %s: %s" % (uuid, exc))
|
||||
|
||||
_validate(ironic, node)
|
||||
|
||||
|
@ -192,7 +159,7 @@ def _validate(ironic, node):
|
|||
if node.instance_uuid:
|
||||
LOG.error('Refusing to discover node %s with assigned instance_uuid',
|
||||
node.uuid)
|
||||
raise DiscoveryFailed(
|
||||
raise utils.DiscoveryFailed(
|
||||
'Refusing to discover node %s with assigned instance uuid' %
|
||||
node.uuid)
|
||||
|
||||
|
@ -202,7 +169,7 @@ def _validate(ironic, node):
|
|||
LOG.error('Refusing to discover node %s with power_state "%s" '
|
||||
'and maintenance mode off',
|
||||
node.uuid, power_state)
|
||||
raise DiscoveryFailed(
|
||||
raise utils.DiscoveryFailed(
|
||||
'Refusing to discover node %s with power state "%s" and '
|
||||
'maintenance mode off' %
|
||||
(node.uuid, power_state))
|
||||
|
@ -211,7 +178,7 @@ def _validate(ironic, node):
|
|||
if not validation.power['result']:
|
||||
LOG.error('Failed validation of power interface for node %s, '
|
||||
'reason: %s', node.uuid, validation.power['reason'])
|
||||
raise DiscoveryFailed('Failed validation of power interface for '
|
||||
raise utils.DiscoveryFailed('Failed validation of power interface for '
|
||||
'node %s' % node.uuid)
|
||||
|
||||
|
||||
|
@ -228,15 +195,18 @@ def _background_discover(ironic, nodes):
|
|||
|
||||
ironic.node.update(node.uuid, patch + node_patch)
|
||||
|
||||
to_exclude = set()
|
||||
all_macs = set()
|
||||
for node in nodes:
|
||||
# TODO(dtantsur): pagination
|
||||
ports = ironic.node.list_ports(node.uuid, limit=0)
|
||||
to_exclude.update(p.address for p in ports)
|
||||
macs = [p.address for p in ironic.node.list_ports(node.uuid, limit=0)]
|
||||
all_macs.update(macs)
|
||||
node_cache.add_node(node.uuid,
|
||||
bmc_address=node.driver_info.get('ipmi_address'),
|
||||
mac=macs)
|
||||
|
||||
if to_exclude:
|
||||
LOG.info('Whitelisting MAC\'s %s in the firewall', to_exclude)
|
||||
firewall.whitelist_macs(to_exclude)
|
||||
if all_macs:
|
||||
LOG.info('Whitelisting MAC\'s %s in the firewall', all_macs)
|
||||
firewall.whitelist_macs(all_macs)
|
||||
firewall.update_filters(ironic)
|
||||
|
||||
for node in nodes:
|
||||
|
|
|
@ -24,6 +24,7 @@ from keystoneclient import exceptions
|
|||
from ironic_discoverd import conf
|
||||
from ironic_discoverd import discoverd
|
||||
from ironic_discoverd import firewall
|
||||
from ironic_discoverd import node_cache
|
||||
from ironic_discoverd import utils
|
||||
|
||||
|
||||
|
@ -56,7 +57,7 @@ def post_discover():
|
|||
LOG.debug("Got JSON %s", data)
|
||||
try:
|
||||
discoverd.discover(data)
|
||||
except discoverd.DiscoveryFailed as exc:
|
||||
except utils.DiscoveryFailed as exc:
|
||||
return str(exc), exc.http_code
|
||||
else:
|
||||
return "", 202
|
||||
|
@ -87,6 +88,7 @@ def main():
|
|||
if not conf.getboolean('discoverd', 'authenticate'):
|
||||
LOG.warning('Starting unauthenticated, please check configuration')
|
||||
|
||||
node_cache.init()
|
||||
firewall.init()
|
||||
utils.check_ironic_available()
|
||||
|
||||
|
|
|
@ -0,0 +1,125 @@
|
|||
# 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.
|
||||
|
||||
"""Cache for nodes currently under discovery."""
|
||||
|
||||
import logging
|
||||
import sqlite3
|
||||
import time
|
||||
|
||||
from ironic_discoverd import conf
|
||||
from ironic_discoverd import utils
|
||||
|
||||
|
||||
LOG = logging.getLogger("discoverd")
|
||||
_DB = None
|
||||
_SCHEMA = """
|
||||
create table if not exists nodes
|
||||
(uuid text primary key, started_at real);
|
||||
|
||||
create table if not exists attributes
|
||||
(name text, value text, uuid text,
|
||||
primary key (name, value),
|
||||
foreign key (uuid) references nodes);
|
||||
"""
|
||||
|
||||
|
||||
def init():
|
||||
"""Initialize the database."""
|
||||
global _DB
|
||||
conn = conf.get('discoverd', 'database') or ':memory:'
|
||||
_DB = sqlite3.connect(conn)
|
||||
_DB.executescript(_SCHEMA)
|
||||
|
||||
|
||||
def _db():
|
||||
if _DB is None:
|
||||
init()
|
||||
return _DB
|
||||
|
||||
|
||||
def add_node(uuid, **attributes):
|
||||
"""Store information about a node under discovery.
|
||||
|
||||
All existing information about this node is dropped.
|
||||
Empty values are skipped.
|
||||
|
||||
:param uuid: Ironic node UUID
|
||||
:param attributes: attributes known about this node (like macs, BMC etc)
|
||||
"""
|
||||
drop_node(uuid)
|
||||
with _db():
|
||||
_db().execute("insert into nodes(uuid, started_at) "
|
||||
"values(?, ?)", (uuid, time.time()))
|
||||
for (name, value) in attributes.items():
|
||||
if not value:
|
||||
continue
|
||||
if not isinstance(value, list):
|
||||
value = [value]
|
||||
|
||||
try:
|
||||
_db().executemany("insert into attributes(name, value, uuid) "
|
||||
"values(?, ?, ?)",
|
||||
[(name, v, uuid) for v in value])
|
||||
except sqlite3.IntegrityError:
|
||||
raise utils.DiscoveryFailed(
|
||||
'Some or all of %(name)s\'s %(value)s are already '
|
||||
'on discovery' %
|
||||
{'name': name, 'value': value})
|
||||
|
||||
|
||||
def drop_node(uuid):
|
||||
"""Forget information about node with given uuid."""
|
||||
with _db():
|
||||
_db().execute("delete from nodes where uuid=?", (uuid,))
|
||||
_db().execute("delete from attributes where uuid=?", (uuid,))
|
||||
|
||||
|
||||
def pop_node(**attributes):
|
||||
"""Find node in cache.
|
||||
|
||||
This function also deletes a node from the cache, thus it's name.
|
||||
|
||||
:param attributes: attributes known about this node (like macs, BMC etc)
|
||||
:returns: UUID or None
|
||||
"""
|
||||
# NOTE(dtantsur): sorting is not required, but gives us predictability
|
||||
found = set()
|
||||
for (name, value) in sorted(attributes.items()):
|
||||
if not value:
|
||||
LOG.warning('Empty value for attribute %s', name)
|
||||
continue
|
||||
if not isinstance(value, list):
|
||||
value = [value]
|
||||
|
||||
LOG.debug('Trying to use %s %s for discovery' % (name, value))
|
||||
rows = _db().execute('select distinct uuid from attributes where ' +
|
||||
' OR '.join('name=? AND value=?' for _ in value),
|
||||
sum(([name, v] for v in value), [])).fetchall()
|
||||
if rows:
|
||||
found.update(item[0] for item in rows)
|
||||
|
||||
if not found:
|
||||
LOG.error('Could not find a node based on attributes %s',
|
||||
list(attributes))
|
||||
return
|
||||
elif len(found) > 1:
|
||||
LOG.error('Multiple nodes were matched based on attributes %(keys)s: '
|
||||
'%(uuids)s',
|
||||
{'keys': list(attributes),
|
||||
'uuids': list(found)})
|
||||
return
|
||||
|
||||
uuid = found.pop()
|
||||
drop_node(uuid)
|
||||
return uuid
|
|
@ -26,20 +26,24 @@ from ironic_discoverd import conf
|
|||
from ironic_discoverd import discoverd
|
||||
from ironic_discoverd import firewall
|
||||
from ironic_discoverd import main
|
||||
from ironic_discoverd import node_cache
|
||||
from ironic_discoverd import utils
|
||||
|
||||
|
||||
def init_conf():
|
||||
conf.init_conf()
|
||||
conf.CONF.add_section('discoverd')
|
||||
conf.CONF.set('discoverd', 'database', '')
|
||||
node_cache._DB = None
|
||||
|
||||
|
||||
# FIXME(dtantsur): this test suite is far from being complete
|
||||
@patch.object(firewall, 'update_filters', autospec=True)
|
||||
@patch.object(node_cache, 'pop_node', autospec=True)
|
||||
@patch.object(utils, 'get_client', autospec=True)
|
||||
class TestProcess(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.node = Mock(driver_info={},
|
||||
self.node = Mock(driver_info={'ipmi_address': '1.2.3.4'},
|
||||
properties={'cpu_arch': 'i386', 'local_gb': 40},
|
||||
uuid='uuid',
|
||||
extra={'on_discovery': 'true'})
|
||||
|
@ -50,6 +54,7 @@ class TestProcess(unittest.TestCase):
|
|||
{'op': 'add', 'path': '/properties/memory_mb', 'value': '1024'},
|
||||
]
|
||||
self.data = {
|
||||
'ipmi_address': '1.2.3.4',
|
||||
'cpus': 2,
|
||||
'cpu_arch': 'x86_64',
|
||||
'memory_mb': 1024,
|
||||
|
@ -67,60 +72,20 @@ class TestProcess(unittest.TestCase):
|
|||
'66:55:44:33:22:11'])
|
||||
init_conf()
|
||||
|
||||
def _do_test_bmc(self, client_mock, filters_mock):
|
||||
self.node.driver_info['ipmi_address'] = '1.2.3.4'
|
||||
def _do_test(self, client_mock, pop_mock, filters_mock):
|
||||
cli = client_mock.return_value
|
||||
cli.node.list.return_value = [
|
||||
Mock(driver_info={}),
|
||||
Mock(driver_info={'ipmi_address': '4.3.2.1'}),
|
||||
self.node,
|
||||
Mock(driver_info={'ipmi_address': '1.2.1.2'}),
|
||||
]
|
||||
cli.port.create.side_effect = [None, exceptions.Conflict()]
|
||||
|
||||
self.data['ipmi_address'] = '1.2.3.4'
|
||||
discoverd.process(self.data)
|
||||
|
||||
self.assertTrue(cli.node.list.called)
|
||||
self.assertFalse(cli.port.get_by_address.called)
|
||||
cli.node.update.assert_called_once_with(self.node.uuid, self.patch)
|
||||
cli.port.create.assert_any_call(node_uuid=self.node.uuid,
|
||||
address='11:22:33:44:55:66')
|
||||
cli.port.create.assert_any_call(node_uuid=self.node.uuid,
|
||||
address='66:55:44:33:22:11')
|
||||
self.assertEqual(2, cli.port.create.call_count)
|
||||
filters_mock.assert_called_once_with(cli)
|
||||
self.assertEqual(set(), firewall.MACS_DISCOVERY)
|
||||
cli.node.set_power_state.assert_called_once_with(self.node.uuid, 'off')
|
||||
|
||||
def test_bmc(self, client_mock, filters_mock):
|
||||
self._do_test_bmc(client_mock, filters_mock)
|
||||
|
||||
def test_bmc_deprecated_macs(self, client_mock, filters_mock):
|
||||
del self.data['interfaces']
|
||||
self.data['macs'] = self.macs
|
||||
self._do_test_bmc(client_mock, filters_mock)
|
||||
|
||||
def test_bmc_ports_for_inactive(self, client_mock, filters_mock):
|
||||
del self.data['interfaces']['em4']
|
||||
conf.CONF.set('discoverd', 'ports_for_inactive_interfaces',
|
||||
'true')
|
||||
self._do_test_bmc(client_mock, filters_mock)
|
||||
|
||||
def test_macs(self, client_mock, filters_mock):
|
||||
discoverd.ALLOW_SEARCH_BY_MAC = True
|
||||
cli = client_mock.return_value
|
||||
cli.port.get_by_address.side_effect = [
|
||||
exceptions.NotFound(), Mock(node_uuid=self.node.uuid)]
|
||||
cli.port.create.side_effect = [None, exceptions.Conflict()]
|
||||
pop_mock.return_value = self.node.uuid
|
||||
cli.node.get.return_value = self.node
|
||||
|
||||
discoverd.process(self.data)
|
||||
|
||||
self.assertFalse(cli.node.list.called)
|
||||
cli.port.get_by_address.assert_any_call('11:22:33:44:55:66')
|
||||
cli.port.get_by_address.assert_any_call('66:55:44:33:22:11')
|
||||
pop_mock.assert_called_once_with(bmc_address='1.2.3.4',
|
||||
mac=ANY)
|
||||
cli.node.get.assert_called_once_with(self.node.uuid)
|
||||
self.assertEqual(['11:22:33:44:55:66', '66:55:44:33:22:11'],
|
||||
sorted(pop_mock.call_args[1]['mac']))
|
||||
|
||||
cli.node.update.assert_called_once_with(self.node.uuid, self.patch)
|
||||
cli.port.create.assert_any_call(node_uuid=self.node.uuid,
|
||||
address='11:22:33:44:55:66')
|
||||
|
@ -131,28 +96,68 @@ class TestProcess(unittest.TestCase):
|
|||
self.assertEqual(set(), firewall.MACS_DISCOVERY)
|
||||
cli.node.set_power_state.assert_called_once_with(self.node.uuid, 'off')
|
||||
|
||||
def test_ok(self, client_mock, pop_mock, filters_mock):
|
||||
self._do_test(client_mock, pop_mock, filters_mock)
|
||||
|
||||
def test_deprecated_macs(self, client_mock, pop_mock, filters_mock):
|
||||
del self.data['interfaces']
|
||||
self.data['macs'] = self.macs
|
||||
self._do_test(client_mock, pop_mock, filters_mock)
|
||||
|
||||
def test_ports_for_inactive(self, client_mock, pop_mock, filters_mock):
|
||||
del self.data['interfaces']['em4']
|
||||
conf.CONF.set('discoverd', 'ports_for_inactive_interfaces',
|
||||
'true')
|
||||
self._do_test(client_mock, pop_mock, filters_mock)
|
||||
|
||||
def test_not_found(self, client_mock, pop_mock, filters_mock):
|
||||
cli = client_mock.return_value
|
||||
pop_mock.return_value = None
|
||||
|
||||
discoverd.process(self.data)
|
||||
|
||||
self.assertFalse(cli.node.update.called)
|
||||
self.assertFalse(cli.port.create.called)
|
||||
self.assertFalse(cli.node.set_power_state.called)
|
||||
|
||||
def test_not_found_in_ironic(self, client_mock, pop_mock, filters_mock):
|
||||
cli = client_mock.return_value
|
||||
pop_mock.return_value = self.node.uuid
|
||||
cli.node.get.side_effect = exceptions.NotFound()
|
||||
|
||||
discoverd.process(self.data)
|
||||
|
||||
cli.node.get.assert_called_once_with(self.node.uuid)
|
||||
self.assertFalse(cli.node.update.called)
|
||||
self.assertFalse(cli.port.create.called)
|
||||
self.assertFalse(cli.node.set_power_state.called)
|
||||
|
||||
|
||||
@patch.object(eventlet.greenthread, 'spawn_n',
|
||||
side_effect=lambda f, *a: f(*a) and None)
|
||||
@patch.object(firewall, 'update_filters', autospec=True)
|
||||
@patch.object(node_cache, 'add_node', autospec=True)
|
||||
@patch.object(utils, 'get_client', autospec=True)
|
||||
class TestDiscover(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super(TestDiscover, self).setUp()
|
||||
self.node1 = Mock(driver='pxe_ssh',
|
||||
uuid='uuid1',
|
||||
driver_info={},
|
||||
maintenance=True,
|
||||
instance_uuid=None,
|
||||
# allowed with maintenance=True
|
||||
power_state='power on')
|
||||
self.node2 = Mock(driver='pxe_ipmitool',
|
||||
uuid='uuid2',
|
||||
driver_info={'ipmi_address': '1.2.3.4'},
|
||||
maintenance=False,
|
||||
instance_uuid=None,
|
||||
power_state=None,
|
||||
extra={'on_discovery': True})
|
||||
self.node3 = Mock(driver='pxe_ipmitool',
|
||||
uuid='uuid3',
|
||||
driver_info={'ipmi_address': '1.2.3.5'},
|
||||
maintenance=False,
|
||||
instance_uuid=None,
|
||||
power_state='power off',
|
||||
|
@ -161,25 +166,40 @@ class TestDiscover(unittest.TestCase):
|
|||
init_conf()
|
||||
|
||||
@patch.object(time, 'time', lambda: 42.0)
|
||||
def test_ok(self, client_mock, filters_mock, spawn_mock):
|
||||
def test_ok(self, client_mock, add_mock, filters_mock, spawn_mock):
|
||||
cli = client_mock.return_value
|
||||
cli.node.get.side_effect = [
|
||||
self.node1,
|
||||
self.node2,
|
||||
self.node3,
|
||||
]
|
||||
cli.node.list_ports.return_value = [Mock(address='1'),
|
||||
Mock(address='2')]
|
||||
ports = [
|
||||
[Mock(address='1-1'), Mock(address='1-2')],
|
||||
[Mock(address='2-1'), Mock(address='2-2')],
|
||||
[Mock(address='3-1'), Mock(address='3-2')],
|
||||
]
|
||||
cli.node.list_ports.side_effect = ports
|
||||
|
||||
discoverd.discover(['uuid1', 'uuid2', 'uuid3'])
|
||||
|
||||
self.assertEqual(3, cli.node.get.call_count)
|
||||
self.assertEqual(3, cli.node.list_ports.call_count)
|
||||
self.assertEqual(3, add_mock.call_count)
|
||||
cli.node.list_ports.assert_any_call('uuid1', limit=0)
|
||||
cli.node.list_ports.assert_any_call('uuid2', limit=0)
|
||||
cli.node.list_ports.assert_any_call('uuid3', limit=0)
|
||||
add_mock.assert_any_call(self.node1.uuid,
|
||||
bmc_address=None,
|
||||
mac=['1-1', '1-2'])
|
||||
add_mock.assert_any_call(self.node2.uuid,
|
||||
bmc_address='1.2.3.4',
|
||||
mac=['2-1', '2-2'])
|
||||
add_mock.assert_any_call(self.node3.uuid,
|
||||
bmc_address='1.2.3.5',
|
||||
mac=['3-1', '3-2'])
|
||||
filters_mock.assert_called_once_with(cli)
|
||||
self.assertEqual(set(['1', '2']), firewall.MACS_DISCOVERY)
|
||||
self.assertEqual(set(port.address for l in ports for port in l),
|
||||
firewall.MACS_DISCOVERY)
|
||||
self.assertEqual(3, cli.node.set_power_state.call_count)
|
||||
cli.node.set_power_state.assert_called_with(ANY, 'reboot')
|
||||
patch = [{'op': 'add', 'path': '/extra/on_discovery', 'value': 'true'},
|
||||
|
@ -198,13 +218,14 @@ class TestDiscover(unittest.TestCase):
|
|||
spawn_mock.assert_called_once_with(discoverd._background_discover,
|
||||
cli, ANY)
|
||||
|
||||
def test_failed_to_get_node(self, client_mock, filters_mock, spawn_mock):
|
||||
def test_failed_to_get_node(self, client_mock, add_mock, filters_mock,
|
||||
spawn_mock):
|
||||
cli = client_mock.return_value
|
||||
cli.node.get.side_effect = [
|
||||
self.node1,
|
||||
exceptions.NotFound(),
|
||||
]
|
||||
self.assertRaisesRegexp(discoverd.DiscoveryFailed,
|
||||
self.assertRaisesRegexp(utils.DiscoveryFailed,
|
||||
'Cannot find node uuid2',
|
||||
discoverd.discover, ['uuid1', 'uuid2'])
|
||||
|
||||
|
@ -212,7 +233,7 @@ class TestDiscover(unittest.TestCase):
|
|||
exceptions.Conflict(),
|
||||
self.node1,
|
||||
]
|
||||
self.assertRaisesRegexp(discoverd.DiscoveryFailed,
|
||||
self.assertRaisesRegexp(utils.DiscoveryFailed,
|
||||
'Cannot get node uuid1',
|
||||
discoverd.discover, ['uuid1', 'uuid2'])
|
||||
|
||||
|
@ -221,8 +242,9 @@ class TestDiscover(unittest.TestCase):
|
|||
self.assertEqual(0, filters_mock.call_count)
|
||||
self.assertEqual(0, cli.node.set_power_state.call_count)
|
||||
self.assertEqual(0, cli.node.update.call_count)
|
||||
self.assertFalse(add_mock.called)
|
||||
|
||||
def test_failed_to_validate_node(self, client_mock, filters_mock,
|
||||
def test_failed_to_validate_node(self, client_mock, add_mock, filters_mock,
|
||||
spawn_mock):
|
||||
cli = client_mock.return_value
|
||||
cli.node.get.side_effect = [
|
||||
|
@ -234,7 +256,7 @@ class TestDiscover(unittest.TestCase):
|
|||
Mock(power={'result': False, 'reason': 'oops'}),
|
||||
]
|
||||
self.assertRaisesRegexp(
|
||||
discoverd.DiscoveryFailed,
|
||||
utils.DiscoveryFailed,
|
||||
'Failed validation of power interface for node uuid2',
|
||||
discoverd.discover, ['uuid1', 'uuid2'])
|
||||
|
||||
|
@ -244,14 +266,17 @@ class TestDiscover(unittest.TestCase):
|
|||
self.assertEqual(0, filters_mock.call_count)
|
||||
self.assertEqual(0, cli.node.set_power_state.call_count)
|
||||
self.assertEqual(0, cli.node.update.call_count)
|
||||
self.assertFalse(add_mock.called)
|
||||
|
||||
def test_no_uuids(self, client_mock, filters_mock, spawn_mock):
|
||||
self.assertRaisesRegexp(discoverd.DiscoveryFailed,
|
||||
def test_no_uuids(self, client_mock, add_mock, filters_mock, spawn_mock):
|
||||
self.assertRaisesRegexp(utils.DiscoveryFailed,
|
||||
'No nodes to discover',
|
||||
discoverd.discover, [])
|
||||
self.assertFalse(client_mock.called)
|
||||
self.assertFalse(add_mock.called)
|
||||
|
||||
def test_with_instance_uuid(self, client_mock, filters_mock, spawn_mock):
|
||||
def test_with_instance_uuid(self, client_mock, add_mock, filters_mock,
|
||||
spawn_mock):
|
||||
self.node2.instance_uuid = 'uuid'
|
||||
cli = client_mock.return_value
|
||||
cli.node.get.side_effect = [
|
||||
|
@ -259,7 +284,7 @@ class TestDiscover(unittest.TestCase):
|
|||
self.node2,
|
||||
]
|
||||
self.assertRaisesRegexp(
|
||||
discoverd.DiscoveryFailed,
|
||||
utils.DiscoveryFailed,
|
||||
'node uuid2 with assigned instance uuid',
|
||||
discoverd.discover, ['uuid1', 'uuid2'])
|
||||
|
||||
|
@ -268,8 +293,10 @@ class TestDiscover(unittest.TestCase):
|
|||
self.assertEqual(0, filters_mock.call_count)
|
||||
self.assertEqual(0, cli.node.set_power_state.call_count)
|
||||
self.assertEqual(0, cli.node.update.call_count)
|
||||
self.assertFalse(add_mock.called)
|
||||
|
||||
def test_wrong_power_state(self, client_mock, filters_mock, spawn_mock):
|
||||
def test_wrong_power_state(self, client_mock, add_mock, filters_mock,
|
||||
spawn_mock):
|
||||
self.node2.power_state = 'power on'
|
||||
self.node2.maintenance = False
|
||||
cli = client_mock.return_value
|
||||
|
@ -278,7 +305,7 @@ class TestDiscover(unittest.TestCase):
|
|||
self.node2,
|
||||
]
|
||||
self.assertRaisesRegexp(
|
||||
discoverd.DiscoveryFailed,
|
||||
utils.DiscoveryFailed,
|
||||
'node uuid2 with power state "power on"',
|
||||
discoverd.discover, ['uuid1', 'uuid2'])
|
||||
|
||||
|
@ -287,6 +314,7 @@ class TestDiscover(unittest.TestCase):
|
|||
self.assertEqual(0, filters_mock.call_count)
|
||||
self.assertEqual(0, cli.node.set_power_state.call_count)
|
||||
self.assertEqual(0, cli.node.update.call_count)
|
||||
self.assertFalse(add_mock.called)
|
||||
|
||||
|
||||
class TestApi(unittest.TestCase):
|
||||
|
@ -305,7 +333,7 @@ class TestApi(unittest.TestCase):
|
|||
@patch.object(discoverd, 'discover', autospec=True)
|
||||
def test_discover_failed(self, discover_mock):
|
||||
conf.CONF.set('discoverd', 'authenticate', 'false')
|
||||
discover_mock.side_effect = discoverd.DiscoveryFailed("boom")
|
||||
discover_mock.side_effect = utils.DiscoveryFailed("boom")
|
||||
res = self.app.post('/v1/discover', data='["uuid1"]')
|
||||
self.assertEqual(400, res.status_code)
|
||||
self.assertEqual(b"boom", res.data)
|
||||
|
@ -399,5 +427,108 @@ class TestCheckIronicAvailable(unittest.TestCase):
|
|||
self.assertEqual(attempts, sleep_mock.call_count)
|
||||
|
||||
|
||||
class TestNodeCache(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super(TestNodeCache, self).setUp()
|
||||
init_conf()
|
||||
self.db = node_cache._db()
|
||||
self.node = Mock(driver_info={'ipmi_address': '1.2.3.4'},
|
||||
uuid='uuid')
|
||||
self.macs = ['11:22:33:44:55:66', '66:55:44:33:22:11']
|
||||
|
||||
def test_add_node(self):
|
||||
# Ensure previous node information is cleared
|
||||
self.db.execute("insert into nodes(uuid) values(?)", (self.node.uuid,))
|
||||
self.db.execute("insert into nodes(uuid) values('uuid2')")
|
||||
self.db.execute("insert into attributes(name, value, uuid) "
|
||||
"values(?, ?, ?)",
|
||||
('mac', '11:22:11:22:11:22', self.node.uuid))
|
||||
|
||||
node_cache.add_node(self.node.uuid, mac=self.macs,
|
||||
bmc_address='1.2.3.4', foo=None)
|
||||
|
||||
res = self.db.execute("select uuid, started_at "
|
||||
"from nodes order by uuid").fetchall()
|
||||
self.assertEqual(['uuid', 'uuid2'], [t[0] for t in res])
|
||||
self.assertTrue(time.time() - 60 < res[0][1] < time.time() + 60)
|
||||
|
||||
res = self.db.execute("select name, value, uuid from attributes "
|
||||
"order by name, value").fetchall()
|
||||
self.assertEqual([('bmc_address', '1.2.3.4', 'uuid'),
|
||||
('mac', '11:22:33:44:55:66', 'uuid'),
|
||||
('mac', '66:55:44:33:22:11', 'uuid')],
|
||||
res)
|
||||
|
||||
def test_add_node_duplicate_mac(self):
|
||||
self.db.execute("insert into nodes(uuid) values(?)", ('another-uuid',))
|
||||
self.db.execute("insert into attributes(name, value, uuid) "
|
||||
"values(?, ?, ?)",
|
||||
('mac', '11:22:11:22:11:22', 'another-uuid'))
|
||||
|
||||
self.assertRaises(utils.DiscoveryFailed,
|
||||
node_cache.add_node,
|
||||
self.node.uuid, mac=['11:22:11:22:11:22'])
|
||||
|
||||
def test_drop_node(self):
|
||||
self.db.execute("insert into nodes(uuid) values(?)", (self.node.uuid,))
|
||||
self.db.execute("insert into nodes(uuid) values('uuid2')")
|
||||
self.db.execute("insert into attributes(name, value, uuid) "
|
||||
"values(?, ?, ?)",
|
||||
('mac', '11:22:11:22:11:22', self.node.uuid))
|
||||
|
||||
node_cache.drop_node(self.node.uuid)
|
||||
|
||||
self.assertEqual([('uuid2',)], self.db.execute(
|
||||
"select uuid from nodes").fetchall())
|
||||
self.assertEqual([], self.db.execute(
|
||||
"select * from attributes").fetchall())
|
||||
|
||||
|
||||
class TestNodeCachePop(unittest.TestCase):
|
||||
def setUp(self):
|
||||
super(TestNodeCachePop, self).setUp()
|
||||
init_conf()
|
||||
self.db = node_cache._db()
|
||||
self.uuid = 'uuid'
|
||||
self.macs = ['11:22:33:44:55:66', '66:55:44:33:22:11']
|
||||
self.macs2 = ['00:00:00:00:00:00']
|
||||
node_cache.add_node(self.uuid,
|
||||
bmc_address='1.2.3.4',
|
||||
mac=self.macs)
|
||||
|
||||
def test_no_data(self):
|
||||
self.assertIsNone(node_cache.pop_node())
|
||||
self.assertIsNone(node_cache.pop_node(mac=[]))
|
||||
|
||||
def test_bmc(self):
|
||||
res = node_cache.pop_node(bmc_address='1.2.3.4')
|
||||
self.assertEqual(self.uuid, res)
|
||||
self.assertEqual([], self.db.execute(
|
||||
"select * from attributes").fetchall())
|
||||
|
||||
def test_macs(self):
|
||||
res = node_cache.pop_node(mac=['11:22:33:33:33:33', self.macs[1]])
|
||||
self.assertEqual(self.uuid, res)
|
||||
self.assertEqual([], self.db.execute(
|
||||
"select * from attributes").fetchall())
|
||||
|
||||
def test_macs_not_found(self):
|
||||
res = node_cache.pop_node(mac=['11:22:33:33:33:33',
|
||||
'66:66:44:33:22:11'])
|
||||
self.assertIsNone(res)
|
||||
|
||||
def test_macs_multiple_found(self):
|
||||
node_cache.add_node('uuid2', mac=self.macs2)
|
||||
res = node_cache.pop_node(mac=[self.macs[0], self.macs2[0]])
|
||||
self.assertIsNone(res)
|
||||
|
||||
def test_both(self):
|
||||
res = node_cache.pop_node(bmc_address='1.2.3.4',
|
||||
mac=self.macs)
|
||||
self.assertEqual(self.uuid, res)
|
||||
self.assertEqual([], self.db.execute(
|
||||
"select * from attributes").fetchall())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
@ -26,6 +26,12 @@ LOG = logging.getLogger('discoverd')
|
|||
OS_ARGS = ('os_password', 'os_username', 'os_auth_url', 'os_tenant_name')
|
||||
|
||||
|
||||
class DiscoveryFailed(Exception):
|
||||
def __init__(self, msg, code=400):
|
||||
super(DiscoveryFailed, self).__init__(msg)
|
||||
self.http_code = code
|
||||
|
||||
|
||||
def get_client(): # pragma: no cover
|
||||
args = dict((k, conf.get('discoverd', k)) for k in OS_ARGS)
|
||||
return client.get_client(1, **args)
|
||||
|
|
Loading…
Reference in New Issue