def test_already_finished(self): session = db.get_session() with session.begin(): (db.model_query(db.Node).filter_by(uuid=self.uuid). update({'finished_at': datetime.datetime.utcnow()})) self.assertRaises(utils.Error, node_cache.find_node, bmc_address='1.2.3.4')
def test_timeout(self, time_mock, get_lock_mock): # Add a finished node to confirm we don't try to timeout it time_mock.return_value = self.started_at session = db.get_session() finished_at = self.started_at + datetime.timedelta(seconds=60) with session.begin(): db.Node(uuid=self.uuid + '1', started_at=self.started_at, state=istate.States.waiting, finished_at=finished_at).save(session) CONF.set_override('timeout', 99) time_mock.return_value = (self.started_at + datetime.timedelta(seconds=100)) self.assertEqual([self.uuid], node_cache.clean_up()) res = [(row.state, row.finished_at, row.error) for row in db.model_query(db.Node).all()] self.assertEqual( [(istate.States.error, self.started_at + datetime.timedelta(seconds=100), 'Introspection timeout'), (istate.States.waiting, self.started_at + datetime.timedelta(seconds=60), None)], res) self.assertEqual([], db.model_query(db.Attribute).all()) self.assertEqual([], db.model_query(db.Option).all()) get_lock_mock.assert_called_once_with(self.uuid) get_lock_mock.return_value.acquire.assert_called_once_with()
def test_already_finished(self): session = db.get_session() with session.begin(): (db.model_query(db.Node).filter_by(uuid=self.uuid). update({'finished_at': 42.0})) self.assertRaises(utils.Error, node_cache.find_node, bmc_address='1.2.3.4')
def test_add_node(self): # Ensure previous node information is cleared uuid2 = uuidutils.generate_uuid() session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Node(uuid=uuid2).save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid=self.uuid).save(session) node = node_cache.add_node(self.node.uuid, mac=self.macs, bmc_address='1.2.3.4', foo=None) self.assertEqual(self.uuid, node.uuid) self.assertTrue(time.time() - 60 < node.started_at < time.time() + 60) self.assertFalse(node._locked) res = set(db.model_query(db.Node.uuid, db.Node.started_at).all()) expected = {(node.uuid, node.started_at), (uuid2, None)} self.assertEqual(expected, res) res = (db.model_query(db.Attribute.name, db.Attribute.value, db.Attribute.uuid).order_by( db.Attribute.name, db.Attribute.value).all()) self.assertEqual([('bmc_address', '1.2.3.4', self.uuid), ('mac', self.macs[0], self.uuid), ('mac', self.macs[1], self.uuid)], [(row.name, row.value, row.uuid) for row in res])
def test_inconsistency(self): session = db.get_session() with session.begin(): (db.model_query(db.Node).filter_by(uuid=self.uuid). delete()) self.assertRaises(utils.Error, node_cache.find_node, bmc_address='1.2.3.4')
def test_add_node(self): # Ensure previous node information is cleared session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Node(uuid='uuid2').save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid=self.uuid).save(session) res = node_cache.add_node(self.node.uuid, mac=self.macs, bmc_address='1.2.3.4', foo=None) self.assertEqual(self.uuid, res.uuid) self.assertTrue(time.time() - 60 < res.started_at < time.time() + 60) self.assertFalse(res._locked) res = (db.model_query(db.Node.uuid, db.Node.started_at).order_by(db.Node.uuid).all()) self.assertEqual(['1a1a1a1a-2b2b-3c3c-4d4d-5e5e5e5e5e5e', 'uuid2'], [t.uuid for t in res]) self.assertTrue(time.time() - 60 < res[0].started_at < time.time() + 60) res = (db.model_query(db.Attribute.name, db.Attribute.value, db.Attribute.uuid). order_by(db.Attribute.name, db.Attribute.value).all()) self.assertEqual([('bmc_address', '1.2.3.4', self.uuid), ('mac', self.macs[0], self.uuid), ('mac', self.macs[1], self.uuid)], [(row.name, row.value, row.uuid) for row in res])
def setUp(self): super(TestNodeInfoOptions, self).setUp() node_cache.add_node(self.uuid, bmc_address='1.2.3.4', mac=self.macs) self.node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=3.14) session = db.get_session() with session.begin(): db.Option(uuid=self.uuid, name='foo', value='"bar"').save(session)
def test_inconsistency(self): session = db.get_session() with session.begin(): (db.model_query(db.Node).filter_by(uuid=self.uuid).delete()) self.assertRaises(utils.Error, node_cache.find_node, bmc_address='1.2.3.4')
def test_add_node(self): # Ensure previous node information is cleared session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Node(uuid='uuid2').save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid=self.uuid).save(session) res = node_cache.add_node(self.node.uuid, mac=self.macs, bmc_address='1.2.3.4', foo=None) self.assertEqual(self.uuid, res.uuid) self.assertTrue(time.time() - 60 < res.started_at < time.time() + 60) res = (db.model_query(db.Node.uuid, db.Node.started_at).order_by(db.Node.uuid).all()) self.assertEqual(['1a1a1a1a-2b2b-3c3c-4d4d-5e5e5e5e5e5e', 'uuid2'], [t.uuid for t in res]) self.assertTrue(time.time() - 60 < res[0].started_at < time.time() + 60) res = (db.model_query(db.Attribute.name, db.Attribute.value, db.Attribute.uuid). order_by(db.Attribute.name, db.Attribute.value).all()) self.assertEqual([('bmc_address', '1.2.3.4', self.uuid), ('mac', self.macs[0], self.uuid), ('mac', self.macs[1], self.uuid)], [(row.name, row.value, row.uuid) for row in res])
def test_add_node(self): # Ensure previous node information is cleared uuid2 = uuidutils.generate_uuid() session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Node(uuid=uuid2).save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid=self.uuid).save(session) node = node_cache.add_node(self.node.uuid, mac=self.macs, bmc_address='1.2.3.4', foo=None) self.assertEqual(self.uuid, node.uuid) self.assertTrue(time.time() - 60 < node.started_at < time.time() + 60) self.assertFalse(node._locked) res = set(db.model_query(db.Node.uuid, db.Node.started_at).all()) expected = {(node.uuid, node.started_at), (uuid2, None)} self.assertEqual(expected, res) res = (db.model_query(db.Attribute.name, db.Attribute.value, db.Attribute.uuid). order_by(db.Attribute.name, db.Attribute.value).all()) self.assertEqual([('bmc_address', '1.2.3.4', self.uuid), ('mac', self.macs[0], self.uuid), ('mac', self.macs[1], self.uuid)], [(row.name, row.value, row.uuid) for row in res])
def test_attributes(self): node_info = node_cache.add_node(self.uuid, istate.States.starting, bmc_address='1.2.3.4', mac=self.macs) self.assertEqual({ 'bmc_address': ['1.2.3.4'], 'mac': self.macs }, node_info.attributes) # check invalidation session = db.get_session() with session.begin(): db.Attribute(uuid=uuidutils.generate_uuid(), name='foo', value='bar', node_uuid=self.uuid).save(session) # still cached self.assertEqual({ 'bmc_address': ['1.2.3.4'], 'mac': self.macs }, node_info.attributes) node_info.invalidate_cache() self.assertEqual( { 'bmc_address': ['1.2.3.4'], 'mac': self.macs, 'foo': ['bar'] }, node_info.attributes)
def test__list_node_uuids(self): session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Node(uuid='uuid2').save(session) node_uuid_list = node_cache._list_node_uuids() self.assertEqual({self.uuid, 'uuid2'}, node_uuid_list)
def test_add_node_duplicate_mac(self): session = db.get_session() with session.begin(): db.Node(uuid='another-uuid').save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid='another-uuid').save(session) self.assertRaises(utils.Error, node_cache.add_node, self.node.uuid, mac=['11:22:11:22:11:22'])
def test_delete_nodes_not_in_list(self, mock__delete_node, mock__list_node_uuids): uuid2 = 'uuid2' uuids = {self.uuid} mock__list_node_uuids.return_value = {self.uuid, uuid2} session = db.get_session() with session.begin(): node_cache.delete_nodes_not_in_list(uuids) mock__delete_node.assert_called_once_with(uuid2)
def setUp(self): super(TestNodeCacheListNode, self).setUp() self.uuid2 = uuidutils.generate_uuid() session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, started_at=datetime.datetime(1, 1, 2)).save(session) db.Node(uuid=self.uuid2, started_at=datetime.datetime(1, 1, 1), finished_at=datetime.datetime(1, 1, 3)).save(session)
def test_old_status(self): CONF.set_override('node_status_keep_time', 42) session = db.get_session() with session.begin(): db.model_query(db.Node).update({'finished_at': time.time() - 100}) self.assertEqual([], node_cache.clean_up()) self.assertEqual([], db.model_query(db.Node).all())
def setUp(self): super(TestNodeCacheCleanUp, self).setUp() self.started_at = 100.0 session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, started_at=self.started_at).save(session) for v in self.macs: db.Attribute(name='mac', value=v, uuid=self.uuid).save(session) db.Option(uuid=self.uuid, name='foo', value='bar').save(session)
def test__list_node_uuids(self): session = db.get_session() uuid2 = uuidutils.generate_uuid() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Node(uuid=uuid2).save(session) node_uuid_list = node_cache._list_node_uuids() self.assertEqual({self.uuid, uuid2}, node_uuid_list)
def test_old_status(self): CONF.set_override('node_status_keep_time', 42) session = db.get_session() with session.begin(): db.model_query(db.Node).update( {'finished_at': time.time() - 100}) self.assertEqual([], node_cache.clean_up()) self.assertEqual([], db.model_query(db.Node).all())
def setUp(self): super(TestNodeInfoOptions, self).setUp() node_cache.add_node(self.uuid, bmc_address='1.2.3.4', mac=self.macs) self.node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=3.14) session = db.get_session() with session.begin(): db.Option(uuid=self.uuid, name='foo', value='"bar"').save( session)
def test_active_macs(self): session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) values = [('mac', '11:22:11:22:11:22', self.uuid), ('mac', '22:11:22:11:22:11', self.uuid)] for value in values: db.Attribute(name=value[0], value=value[1], uuid=value[2]).save(session) self.assertEqual({'11:22:11:22:11:22', '22:11:22:11:22:11'}, node_cache.active_macs())
def test__delete_node(self): session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid=self.uuid).save(session) data = {'s': 'value', 'b': True, 'i': 42} encoded = json.dumps(data) db.Option(uuid=self.uuid, name='name', value=encoded).save(session) node_cache._delete_node(self.uuid) session = db.get_session() row_node = db.model_query(db.Node).filter_by(uuid=self.uuid).first() self.assertIsNone(row_node) row_attribute = db.model_query( db.Attribute).filter_by(uuid=self.uuid).first() self.assertIsNone(row_attribute) row_option = db.model_query( db.Option).filter_by(uuid=self.uuid).first() self.assertIsNone(row_option)
def test_ok(self): started_at = time.time() - 42 session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, started_at=started_at).save(session) info = node_cache.get_node(self.uuid) self.assertEqual(self.uuid, info.uuid) self.assertEqual(started_at, info.started_at) self.assertIsNone(info.finished_at) self.assertIsNone(info.error)
def setUp(self): super(TestNodeCacheCleanUp, self).setUp() self.started_at = 100.0 session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, started_at=self.started_at).save( session) for v in self.macs: db.Attribute(name='mac', value=v, uuid=self.uuid).save( session) db.Option(uuid=self.uuid, name='foo', value='bar').save( session)
def setUp(self): super(TestNodeInfoFinished, self).setUp() node_cache.add_node(self.uuid, istate.States.processing, bmc_address='1.2.3.4', mac=self.macs) self.node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=datetime.datetime( 3, 1, 4)) session = db.get_session() with session.begin(): db.Option(uuid=self.uuid, name='foo', value='bar').save(session)
def setUp(self): super(TestNodeInfoFinished, self).setUp() node_cache.add_node(self.uuid, istate.States.processing, bmc_address='1.2.3.4', mac=self.macs) self.node_info = node_cache.NodeInfo( uuid=self.uuid, started_at=datetime.datetime(3, 1, 4)) session = db.get_session() with session.begin(): db.Option(uuid=self.uuid, name='foo', value='bar').save( session)
def test_delete_nodes_not_in_list(self, mock__delete_node, mock__list_node_uuids, mock__get_lock_ctx): uuid2 = uuidutils.generate_uuid() uuids = {self.uuid} mock__list_node_uuids.return_value = {self.uuid, uuid2} session = db.get_session() with session.begin(): node_cache.delete_nodes_not_in_list(uuids) mock__delete_node.assert_called_once_with(uuid2) mock__get_lock_ctx.assert_called_once_with(uuid2) mock__get_lock_ctx.return_value.__enter__.assert_called_once_with()
def test_success(self): self.node_info.finished() session = db.get_session() with session.begin(): self.assertEqual((datetime.datetime(1, 1, 1), None), tuple(db.model_query( db.Node.finished_at, db.Node.error).first())) self.assertEqual([], db.model_query(db.Attribute, session=session).all()) self.assertEqual([], db.model_query(db.Option, session=session).all())
def test_success(self): self.node_info.finished() session = db.get_session() with session.begin(): self.assertEqual((42.0, None), tuple(db.model_query( db.Node.finished_at, db.Node.error).first())) self.assertEqual([], db.model_query(db.Attribute, session=session).all()) self.assertEqual([], db.model_query(db.Option, session=session).all())
def test_add_node_duplicate_mac(self): session = db.get_session() uuid = uuidutils.generate_uuid() with session.begin(): db.Node(uuid=uuid, state=istate.States.starting).save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid=uuid).save(session) self.assertRaises(utils.Error, node_cache.add_node, self.node.uuid, istate.States.starting, mac=['11:22:11:22:11:22'])
def test__delete_node(self): session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) db.Attribute(name='mac', value='11:22:11:22:11:22', uuid=self.uuid).save(session) data = {'s': 'value', 'b': True, 'i': 42} encoded = json.dumps(data) db.Option(uuid=self.uuid, name='name', value=encoded).save( session) node_cache._delete_node(self.uuid) session = db.get_session() row_node = db.model_query(db.Node).filter_by( uuid=self.uuid).first() self.assertIsNone(row_node) row_attribute = db.model_query(db.Attribute).filter_by( uuid=self.uuid).first() self.assertIsNone(row_attribute) row_option = db.model_query(db.Option).filter_by( uuid=self.uuid).first() self.assertIsNone(row_option)
def setUp(self): super(BaseTest, self).setUp() init_test_conf() self.session = db.get_session() engine = db.get_engine() db.Base.metadata.create_all(engine) engine.connect() self.addCleanup(db.get_engine().dispose) plugins_base._HOOKS_MGR = None for name in ('_', '_LI', '_LW', '_LE', '_LC'): patch = mock.patch.object(i18n, name, lambda s: s) patch.start() # 'p=patch' magic is due to how closures work self.addCleanup(lambda p=patch: p.stop())
def test_active_macs(self): session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid, state=istate.States.starting).save(session) values = [('mac', '11:22:11:22:11:22', self.uuid), ('mac', '22:11:22:11:22:11', self.uuid)] for value in values: db.Attribute(uuid=uuidutils.generate_uuid(), name=value[0], value=value[1], node_uuid=value[2]).save(session) self.assertEqual({'11:22:11:22:11:22', '22:11:22:11:22:11'}, node_cache.active_macs())
def setUp(self): super(TestNodeCacheCleanUp, self).setUp() self.started_at = datetime.datetime.utcnow() session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, state=istate.States.waiting, started_at=self.started_at).save(session) for v in self.macs: db.Attribute(uuid=uuidutils.generate_uuid(), name='mac', value=v, node_uuid=self.uuid).save(session) db.Option(uuid=self.uuid, name='foo', value='bar').save(session)
def test_locked(self): started_at = (datetime.datetime.utcnow() - datetime.timedelta(seconds=42)) session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, state=istate.States.starting, started_at=started_at).save(session) info = node_cache.get_node(self.uuid, locked=True) self.assertEqual(self.uuid, info.uuid) self.assertEqual(started_at, info.started_at) self.assertIsNone(info.finished_at) self.assertIsNone(info.error) self.assertTrue(info._locked)
def test_with_name(self): started_at = time.time() - 42 session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, started_at=started_at).save(session) ironic = mock.Mock() ironic.node.get.return_value = self.node info = node_cache.get_node('name', ironic=ironic) self.assertEqual(self.uuid, info.uuid) self.assertEqual(started_at, info.started_at) self.assertIsNone(info.finished_at) self.assertIsNone(info.error) self.assertFalse(info._locked) ironic.node.get.assert_called_once_with('name')
def test_add_attribute_same_value(self): session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid, state=istate.States.starting).save(session) node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=42) node_info.add_attribute('key', 'value') node_info.add_attribute('key', 'value') res = db.model_query(db.Attribute.name, db.Attribute.value, db.Attribute.node_uuid, session=session) self.assertEqual([('key', 'value', self.uuid), ('key', 'value', self.uuid)], [tuple(row) for row in res])
def test_add_attribute(self): session = db.get_session() with session.begin(): db.Node(uuid=self.node.uuid).save(session) node_info = node_cache.NodeInfo(uuid=self.uuid, started_at=42) node_info.add_attribute('key', 'value') res = db.model_query(db.Attribute.name, db.Attribute.value, db.Attribute.uuid, session=session) res = res.order_by(db.Attribute.name, db.Attribute.value).all() self.assertEqual([('key', 'value', self.uuid)], [tuple(row) for row in res]) self.assertRaises(utils.Error, node_info.add_attribute, 'key', 'value') # check that .attributes got invalidated and reloaded self.assertEqual({'key': ['value']}, node_info.attributes)
def setUp(self): super(BaseTest, self).setUp() if not self.IS_FUNCTIONAL: self.init_test_conf() self.session = db.get_session() engine = db.get_engine() db.Base.metadata.create_all(engine) engine.connect() self.addCleanup(db.get_engine().dispose) plugins_base._HOOKS_MGR = None node_cache._SEMAPHORES = lockutils.Semaphores() patch = mock.patch.object(i18n, '_', lambda s: s) patch.start() # 'p=patch' magic is due to how closures work self.addCleanup(lambda p=patch: p.stop()) utils._EXECUTOR = futurist.SynchronousExecutor(green=True)
def setUp(self): super(BaseTest, self).setUp() if not self.IS_FUNCTIONAL: self.init_test_conf() self.session = db.get_session() engine = db.get_engine() db.Base.metadata.create_all(engine) engine.connect() self.addCleanup(db.get_engine().dispose) plugins_base._HOOKS_MGR = None node_cache._SEMAPHORES = lockutils.Semaphores() for name in ('_', '_LI', '_LW', '_LE', '_LC'): patch = mock.patch.object(i18n, name, lambda s: s) patch.start() # 'p=patch' magic is due to how closures work self.addCleanup(lambda p=patch: p.stop()) utils._EXECUTOR = futurist.SynchronousExecutor(green=True)
def test_attributes(self): node_info = node_cache.add_node(self.uuid, bmc_address='1.2.3.4', mac=self.macs) self.assertEqual({'bmc_address': ['1.2.3.4'], 'mac': self.macs}, node_info.attributes) # check invalidation session = db.get_session() with session.begin(): db.Attribute(name='foo', value='bar', uuid=self.uuid).save(session) # still cached self.assertEqual({'bmc_address': ['1.2.3.4'], 'mac': self.macs}, node_info.attributes) node_info.invalidate_cache() self.assertEqual({'bmc_address': ['1.2.3.4'], 'mac': self.macs, 'foo': ['bar']}, node_info.attributes)
def test_timeout(self, time_mock): # Add a finished node to confirm we don't try to timeout it time_mock.return_value = self.started_at session = db.get_session() with session.begin(): db.Node(uuid=self.uuid + '1', started_at=self.started_at, finished_at=self.started_at + 60).save(session) CONF.set_override('timeout', 99) time_mock.return_value = (self.started_at + 100) self.assertEqual([self.uuid], node_cache.clean_up()) res = [(row.finished_at, row.error) for row in db.model_query(db.Node).all()] self.assertEqual([(self.started_at + 100, 'Introspection timeout'), (self.started_at + 60, None)], res) self.assertEqual([], db.model_query(db.Attribute).all()) self.assertEqual([], db.model_query(db.Option).all())
def test_with_name(self): started_at = (datetime.datetime.utcnow() - datetime.timedelta(seconds=42)) session = db.get_session() with session.begin(): db.Node(uuid=self.uuid, state=istate.States.starting, started_at=started_at).save(session) ironic = mock.Mock() ironic.node.get.return_value = self.node info = node_cache.get_node('name', ironic=ironic) self.assertEqual(self.uuid, info.uuid) self.assertEqual(started_at, info.started_at) self.assertIsNone(info.finished_at) self.assertIsNone(info.error) self.assertFalse(info._locked) ironic.node.get.assert_called_once_with('name')
def test_ok(self): db.init() session = db.get_session() db.model_query(db.Node, session=session)