def test_node_get_all_with_limit_marker(self): node_ids = ['node1', 'node2', 'node3'] for v in node_ids: shared.create_node(self.ctx, self.cluster, self.profile, id=v, init_at=tu.utcnow()) nodes = db_api.node_get_all(self.ctx, limit=1) self.assertEqual(1, len(nodes)) nodes = db_api.node_get_all(self.ctx, limit=2) self.assertEqual(2, len(nodes)) nodes = db_api.node_get_all(self.ctx, limit=5) self.assertEqual(3, len(nodes)) nodes = db_api.node_get_all(self.ctx, marker='node1') self.assertEqual(2, len(nodes)) nodes = db_api.node_get_all(self.ctx, marker='node2') self.assertEqual(1, len(nodes)) nodes = db_api.node_get_all(self.ctx, marker='node3') self.assertEqual(0, len(nodes)) nodes = db_api.node_get_all(self.ctx, limit=1, marker='node1') self.assertEqual(1, len(nodes))
def test_node_get_all_with_empty_filters(self): shared.create_node(self.ctx, None, self.profile, name='node1') shared.create_node(self.ctx, None, self.profile, name='node2') filters = None results = db_api.node_get_all(self.ctx, filters=filters) self.assertEqual(2, len(results))
def test_node_get_all_with_limit_marker(self): node_ids = ['node1', 'node2', 'node3'] for v in node_ids: shared.create_node(self.ctx, self.cluster, self.profile, id=v, init_at=tu.utcnow(True)) nodes = db_api.node_get_all(self.ctx, limit=1) self.assertEqual(1, len(nodes)) nodes = db_api.node_get_all(self.ctx, limit=2) self.assertEqual(2, len(nodes)) nodes = db_api.node_get_all(self.ctx, limit=5) self.assertEqual(3, len(nodes)) nodes = db_api.node_get_all(self.ctx, marker='node1') self.assertEqual(2, len(nodes)) nodes = db_api.node_get_all(self.ctx, marker='node2') self.assertEqual(1, len(nodes)) nodes = db_api.node_get_all(self.ctx, marker='node3') self.assertEqual(0, len(nodes)) nodes = db_api.node_get_all(self.ctx, limit=1, marker='node1') self.assertEqual(1, len(nodes))
def test_node_get_by_short_id(self): node_id1 = 'same-part-unique-part' node_id2 = 'same-part-part-unique' shared.create_node(self.ctx, None, self.profile, id=node_id1, name='node-1') shared.create_node(self.ctx, None, self.profile, id=node_id2, name='node-2') for x in range(len('same-part-')): self.assertRaises(exception.MultipleChoices, db_api.node_get_by_short_id, self.ctx, node_id1[:x]) res = db_api.node_get_by_short_id(self.ctx, node_id1[:11]) self.assertEqual(node_id1, res.id) res = db_api.node_get_by_short_id(self.ctx, node_id2[:11]) self.assertEqual(node_id2, res.id) res = db_api.node_get_by_short_id(self.ctx, 'non-existent') self.assertIsNone(res)
def test_node_get_by_cluster(self): cluster1 = shared.create_cluster(self.ctx, self.profile) node0 = shared.create_node(self.ctx, None, self.profile) node1 = shared.create_node(self.ctx, self.cluster, self.profile) node2 = shared.create_node(self.ctx, self.cluster, self.profile) node3 = shared.create_node(self.ctx, cluster1, self.profile) nodes = db_api.node_get_all_by_cluster(self.ctx, self.cluster.id) self.assertEqual(2, len(nodes)) self.assertEqual(set([node1.id, node2.id]), set([nodes[0].id, nodes[1].id])) # retrieve orphan nodes nodes = db_api.node_get_all_by_cluster(self.ctx, '') self.assertEqual(1, len(nodes)) self.assertEqual(node0.id, nodes[0].id) # retrieve all nodes nodes = db_api.node_get_all_by_cluster(self.ctx, None) self.assertEqual(4, len(nodes)) self.assertEqual(node0.id, nodes[0].id) nodes = db_api.node_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(1, len(nodes)) self.assertEqual(node3.id, nodes[0].id)
def test_node_get_all_sorting(self): values = [{'id': '001', 'name': 'node1', 'status': 'ACTIVE'}, {'id': '002', 'name': 'node3', 'status': 'ERROR'}, {'id': '003', 'name': 'node2', 'status': 'UPDATING'}] for v in values: shared.create_node(self.ctx, self.cluster, self.profile, **v) nodes = db_api.node_get_all(self.ctx, sort='name,status') self.assertEqual(3, len(nodes)) # Sorted by name self.assertEqual('001', nodes[0].id) self.assertEqual('003', nodes[1].id) self.assertEqual('002', nodes[2].id) nodes = db_api.node_get_all(self.ctx, sort='status,name') self.assertEqual(3, len(nodes)) # Sorted by statuses (ascending) self.assertEqual('001', nodes[0].id) self.assertEqual('002', nodes[1].id) self.assertEqual('003', nodes[2].id) nodes = db_api.node_get_all(self.ctx, sort='status:desc,name:desc') self.assertEqual(3, len(nodes)) # Sorted by statuses (descending) self.assertEqual('003', nodes[0].id) self.assertEqual('002', nodes[1].id) self.assertEqual('001', nodes[2].id)
def test_node_count_by_cluster(self): shared.create_cluster(self.ctx, self.profile) shared.create_node(self.ctx, self.cluster, self.profile) shared.create_node(self.ctx, self.cluster, self.profile) res = db_api.node_count_by_cluster(self.ctx, self.cluster.id) self.assertEqual(2, res)
def test_node_get_all_with_admin_context(self): shared.create_node(self.ctx, None, self.profile, name='node1') shared.create_node(self.ctx, None, self.profile, name='node2') admin_ctx = utils.dummy_context(project='a_different_project', is_admin=True) results = db_api.node_get_all(admin_ctx, project_safe=True) self.assertEqual(2, len(results))
def test_node_get_by_name(self): shared.create_node(self.ctx, self.cluster, self.profile) node = db_api.node_get_by_name(self.ctx, 'test_node_name') self.assertIsNotNone(node) self.assertEqual('test_node_name', node.name) self.assertEqual(self.cluster.id, node.cluster_id) res = db_api.node_get_by_name(self.ctx, 'BogusName') self.assertIsNone(res)
def test_node_get_by_physical_id(self): shared.create_node(self.ctx, self.cluster, self.profile, physical_id=UUID1) node = db_api.node_get_by_physical_id(self.ctx, UUID1) self.assertIsNotNone(node) self.assertEqual(UUID1, node.physical_id) node = db_api.node_get_by_physical_id(self.ctx, UUID2) self.assertIsNone(node)
def test_node_count_by_cluster_admin_context(self): shared.create_cluster(self.ctx, self.profile) shared.create_node(self.ctx, self.cluster, self.profile) shared.create_node(self.ctx, self.cluster, self.profile) admin_ctx = utils.dummy_context(project='a_different_project', is_admin=True) res = db_api.node_count_by_cluster(admin_ctx, self.cluster.id, project_safe=True) self.assertEqual(2, res)
def test_node_get_by_short_id_admin_context(self): node_id = 'same-part-unique-part' shared.create_node(self.ctx, None, self.profile, id=node_id, name='node-1') admin_ctx = utils.dummy_context(project='a_different_project', is_admin=True) res = db_api.node_get_by_short_id(admin_ctx, node_id[:11], project_safe=True) self.assertIsNotNone(res)
def test_node_get_all_with_cluster_id(self): values = [{'name': 'node1'}, {'name': 'node2'}, {'name': 'node3'}] for v in values: shared.create_node(self.ctx, self.cluster, self.profile, **v) shared.create_node(self.ctx, None, self.profile, name='node0') nodes = db_api.node_get_all(self.ctx, cluster_id=self.cluster.id) self.assertEqual(3, len(nodes)) names = [node.name for node in nodes] [self.assertIn(val['name'], names) for val in values]
def test_node_get_by_name_diff_project(self): shared.create_node(self.ctx, self.cluster, self.profile) res = db_api.node_get_by_name(self.ctx, 'test_node_name') self.assertIsNotNone(res) ctx_new = utils.dummy_context(project='a_different_project') res = db_api.node_get_by_name(ctx_new, 'test_node_name') self.assertIsNone(res) res = db_api.node_get_by_name(ctx_new, 'test_node_name', project_safe=False) self.assertIsNotNone(res)
def test_event_get_all_by_cluster(self): cluster1 = shared.create_cluster(self.ctx, self.profile) cluster2 = shared.create_cluster(self.ctx, self.profile) node1_1 = shared.create_node(self.ctx, cluster1, self.profile) node1_2 = shared.create_node(self.ctx, cluster1, self.profile) node2_1 = shared.create_node(self.ctx, cluster2, self.profile) node_orphan = shared.create_node(self.ctx, None, self.profile) # 1 event for cluster 1 self.create_event(self.ctx, entity=cluster1) events = db_api.event_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(1, len(events)) events = db_api.event_get_all_by_cluster(self.ctx, cluster2.id) self.assertEqual(0, len(events)) # two more events for cluster 1, with one for an orphan node self.create_event(self.ctx, entity=node1_1) self.create_event(self.ctx, entity=node1_2) self.create_event(self.ctx, entity=node_orphan) events = db_api.event_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(3, len(events)) events = db_api.event_get_all_by_cluster(self.ctx, cluster2.id) self.assertEqual(0, len(events)) # one more events for cluster 2, with one for an orphan node self.create_event(self.ctx, entity=cluster2) self.create_event(self.ctx, entity=node_orphan) events = db_api.event_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(3, len(events)) events = db_api.event_get_all_by_cluster(self.ctx, cluster2.id) self.assertEqual(1, len(events)) # two more events for cluster 2, with one for an orphan node self.create_event(self.ctx, entity=node2_1) self.create_event(self.ctx, entity=node2_1) self.create_event(self.ctx, entity=node_orphan) events = db_api.event_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(3, len(events)) events = db_api.event_get_all_by_cluster(self.ctx, cluster2.id) self.assertEqual(3, len(events)) # two more events for cluster 1, with one for an orphan node self.create_event(self.ctx, entity=cluster1) self.create_event(self.ctx, entity=node1_1) self.create_event(self.ctx, entity=node_orphan) events = db_api.event_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(5, len(events)) events = db_api.event_get_all_by_cluster(self.ctx, cluster2.id) self.assertEqual(3, len(events))
def test_node_get_by_cluster_admin_context(self): shared.create_cluster(self.ctx, self.profile) node1 = shared.create_node(self.ctx, self.cluster, self.profile) node2 = shared.create_node(self.ctx, self.cluster, self.profile) admin_ctx = utils.dummy_context(project='a_different_project', is_admin=True) nodes = db_api.node_get_all_by_cluster(admin_ctx, self.cluster.id) self.assertEqual(2, len(nodes)) self.assertEqual(set([node1.id, node2.id]), set([nodes[0].id, nodes[1].id]))
def test_ids_by_cluster(self): node0 = shared.create_node(self.ctx, None, self.profile) node1 = shared.create_node(self.ctx, self.cluster, self.profile) node2 = shared.create_node(self.ctx, self.cluster, self.profile) results = db_api.node_ids_by_cluster(self.ctx, self.cluster.id) self.assertEqual(2, len(results)) self.assertEqual(set([node1.id, node2.id]), set(results)) # retrieve orphan nodes results = db_api.node_ids_by_cluster(self.ctx, '') self.assertEqual(1, len(results)) self.assertEqual(node0.id, results[0])
def test_node_get_all_with_filters(self): shared.create_node(self.ctx, None, self.profile, name='node1') shared.create_node(self.ctx, None, self.profile, name='node2') filters = {'name': ['node1', 'nodex']} results = db_api.node_get_all(self.ctx, filters=filters) self.assertEqual(1, len(results)) self.assertEqual('node1', results[0]['name']) filters = {'name': 'node1'} results = db_api.node_get_all(self.ctx, filters=filters) self.assertEqual(1, len(results)) self.assertEqual('node1', results[0]['name'])
def test_node_get_by_short_id_diff_project(self): node_id = 'same-part-unique-part' shared.create_node(self.ctx, None, self.profile, id=node_id, name='node-1') res = db_api.node_get_by_short_id(self.ctx, node_id[:11]) self.assertIsNotNone(res) ctx_new = utils.dummy_context(project='a_different_project') res = db_api.node_get_by_short_id(ctx_new, node_id[:11]) self.assertIsNone(res) res = db_api.node_get_by_short_id(ctx_new, node_id[:11], project_safe=False) self.assertIsNotNone(res)
def test_node_count_by_cluster_diff_project(self): ctx_new = utils.dummy_context(project='a_different_project') shared.create_cluster(self.ctx, self.profile) shared.create_node(self.ctx, self.cluster, self.profile) shared.create_node(self.ctx, self.cluster, self.profile) res = db_api.node_count_by_cluster(ctx_new, self.cluster.id) self.assertEqual(0, res) res = db_api.node_count_by_cluster(ctx_new, self.cluster.id, project_safe=False) self.assertEqual(2, res)
def test_node_get_all_with_project_safe(self): shared.create_node(self.ctx, None, self.profile, name='node1') shared.create_node(self.ctx, None, self.profile, name='node2') self.ctx.project = 'a-different-project' results = db_api.node_get_all(self.ctx, project_safe=False) self.assertEqual(2, len(results)) self.ctx.project = 'a-different-project' results = db_api.node_get_all(self.ctx) self.assertEqual(0, len(results)) results = db_api.node_get_all(self.ctx, project_safe=True) self.assertEqual(0, len(results))
def test_node_get_all_used_sort_keys(self, mock_paginate): node_ids = ['node1', 'node2', 'node3'] for v in node_ids: shared.create_node(self.ctx, self.cluster, self.profile, id=v) sort = ','.join(consts.NODE_SORT_KEYS) db_api.node_get_all(self.ctx, sort=sort) args = mock_paginate.call_args[0] used_sort_keys = set(args[3]) sort_keys = consts.NODE_SORT_KEYS sort_keys.append('id') expected_keys = set(sort_keys) self.assertEqual(expected_keys, used_sort_keys)
def test_node_get_by_name(self): shared.create_node(self.ctx, self.cluster, self.profile) node = db_api.node_get_by_name(self.ctx, 'test_node_name') self.assertIsNotNone(node) self.assertEqual('test_node_name', node.name) self.assertEqual(self.cluster.id, node.cluster_id) res = db_api.node_get_by_name(self.ctx, 'BogusName') self.assertIsNone(res) # duplicated name shared.create_node(self.ctx, self.cluster, self.profile) self.assertRaises(exception.MultipleChoices, db_api.node_get_by_name, self.ctx, 'test_node_name')
def test_node_count_by_cluster_with_filters(self): shared.create_cluster(self.ctx, self.profile) shared.create_node(self.ctx, self.cluster, self.profile, status='ACTIVE') shared.create_node(self.ctx, self.cluster, self.profile, status='ERROR') res = db_api.node_count_by_cluster(self.ctx, self.cluster.id, status='ACTIVE') self.assertEqual(1, res) res = db_api.node_count_by_cluster(self.ctx, self.cluster.id, status='ERROR') self.assertEqual(1, res)
def test_node_get_by_physical_id_diff_project(self): shared.create_node(self.ctx, self.cluster, self.profile, physical_id=UUID1) node = db_api.node_get_by_physical_id(self.ctx, UUID1) self.assertIsNotNone(node) self.assertEqual(UUID1, node.physical_id) ctx_new = utils.dummy_context(project='a_different_project') node = db_api.node_get_by_physical_id(ctx_new, UUID1) self.assertIsNone(node) node = db_api.node_get_by_physical_id(ctx_new, UUID1, project_safe=False) self.assertIsNotNone(node)
def test_node_get_all_show_deleted(self): values = [{'id': 'node1'}, {'id': 'node2'}, {'id': 'node3'}] for v in values: shared.create_node(self.ctx, self.cluster, self.profile, **v) db_api.node_delete(self.ctx, 'node2') nodes = db_api.node_get_all(self.ctx) self.assertEqual(2, len(nodes)) nodes = db_api.node_get_all(self.ctx, show_deleted=False) self.assertEqual(2, len(nodes)) nodes = db_api.node_get_all(self.ctx, show_deleted=True) self.assertEqual(3, len(nodes))
def test_node_get_all_used_sort_keys(self): node_ids = ['node1', 'node2', 'node3'] for v in node_ids: shared.create_node(self.ctx, self.cluster, self.profile, id=v) mock_paginate = self.patchobject(db_api.utils, 'paginate_query') sort_keys = ['index', 'name', 'created_time', 'updated_time', 'deleted_time', 'status'] db_api.node_get_all(self.ctx, sort_keys=sort_keys) args = mock_paginate.call_args[0] used_sort_keys = set(args[3]) expected_keys = set(['index', 'name', 'created_time', 'updated_time', 'deleted_time', 'status', 'id']) self.assertEqual(expected_keys, used_sort_keys)
def test_node_get_by_cluster_diff_project(self): shared.create_cluster(self.ctx, self.profile) node1 = shared.create_node(self.ctx, self.cluster, self.profile) node2 = shared.create_node(self.ctx, self.cluster, self.profile) nodes = db_api.node_get_all_by_cluster(self.ctx, self.cluster.id) self.assertEqual(2, len(nodes)) self.assertEqual(set([node1.id, node2.id]), set([nodes[0].id, nodes[1].id])) ctx_new = utils.dummy_context(project='a_different_project') nodes = db_api.node_get_all_by_cluster(ctx_new, self.cluster.id) self.assertEqual(0, len(nodes)) nodes = db_api.node_get_all_by_cluster(ctx_new, self.cluster.id, project_safe=False) self.assertEqual(2, len(nodes))
def test_node_get(self): res = shared.create_node(self.ctx, self.cluster, self.profile) node = db_api.node_get(self.ctx, res.id) self.assertIsNotNone(node) node = db_api.node_get(self.ctx, UUID2) self.assertIsNone(node)
def test_node_get_with_admin_context(self): res = shared.create_node(self.ctx, self.cluster, self.profile) admin_ctx = utils.dummy_context(project='a_different_project', is_admin=True) node = db_api.node_get(admin_ctx, res.id, project_safe=True) self.assertIsNone(node) node = db_api.node_get(admin_ctx, res.id, project_safe=False) self.assertIsNotNone(node)
def test_node_get(self): res = shared.create_node(self.ctx, self.cluster, self.profile) node = db_api.node_get(self.ctx, res.id) self.assertIsNotNone(node) node = db_api.node_get(self.ctx, UUID2) self.assertIsNone(node) nodes = db_api.node_get_all_by_cluster(self.ctx, self.cluster.id) self.assertEqual(1, len(nodes))
def test_event_purge(self): cluster1 = shared.create_cluster(self.ctx, self.profile) node1_1 = shared.create_node(self.ctx, cluster1, self.profile) node1_2 = shared.create_node(self.ctx, cluster1, self.profile) self.create_event(self.ctx, entity=cluster1, status='start') self.create_event(self.ctx, entity=cluster1, status='end') self.create_event(self.ctx, entity=node1_1, status='start') self.create_event(self.ctx, entity=node1_1, status='end') timestamp = tu.utcnow() self.create_event(self.ctx, timestamp=timestamp, entity=node1_2, status='start') res = db_api.event_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(5, len(res)) db_api.event_purge(project=None, granularity='days', age=5) res = db_api.event_get_all_by_cluster(self.ctx, cluster1.id) self.assertEqual(1, len(res))
def test_node_get_by_name_and_cluster(self): node_name = 'test_node_007' shared.create_node(self.ctx, self.cluster, self.profile, name=node_name) node = db_api.node_get_by_name_and_cluster(self.ctx, node_name, self.cluster.id) self.assertIsNotNone(node) self.assertEqual(node_name, node.name) self.assertEqual(self.cluster.id, node.cluster_id) node = db_api.node_get_by_name_and_cluster(self.ctx, 'not-exist', self.cluster.id) self.assertIsNone(node) node = db_api.node_get_by_name_and_cluster(self.ctx, node_name, 'BogusClusterID') self.assertIsNone(node)
def test_node_get_by_name_show_deleted(self): node_name = 'test_node_name' shared.create_node(self.ctx, self.cluster, self.profile, name=node_name) node = db_api.node_get_by_name(self.ctx, node_name) self.assertIsNotNone(node) node_id = node.id db_api.node_delete(self.ctx, node_id) res = db_api.node_get_by_name(self.ctx, node_name) self.assertIsNone(res) res = db_api.node_get_by_name(self.ctx, node_name, show_deleted=False) self.assertIsNone(res) res = db_api.node_get_by_name(self.ctx, node_name, show_deleted=True) self.assertEqual(node_id, res.id)
def test_node_get_all(self): values = [{'name': 'node1'}, {'name': 'node2'}, {'name': 'node3'}] [shared.create_node(self.ctx, None, self.profile, **v) for v in values] nodes = db_api.node_get_all(self.ctx) self.assertEqual(3, len(nodes)) names = [node.name for node in nodes] [self.assertIn(val['name'], names) for val in values]
def test_node_migrate_to_none(self): node = shared.create_node(self.ctx, self.cluster, self.profile) timestamp = tu.utcnow() node_new = db_api.node_migrate(self.ctx, node.id, None, timestamp) self.assertEqual(timestamp, node_new.updated_at) self.assertEqual('', node_new.cluster_id) nodes = db_api.node_get_all_by_cluster(self.ctx, self.cluster.id) self.assertEqual(0, len(nodes))
def test_node_get_diff_project(self): res = shared.create_node(self.ctx, self.cluster, self.profile) node = db_api.node_get(self.ctx, res.id) self.assertIsNotNone(node) ctx_new = utils.dummy_context(project='a_different_project') node = db_api.node_get(ctx_new, res.id) self.assertIsNone(node) node = db_api.node_get(ctx_new, res.id, project_safe=False) self.assertIsNotNone(node)
def test_profile_delete_profile_used_by_node(self): profile = shared.create_profile(self.ctx) node = shared.create_node(self.ctx, None, profile) profile_id = profile.id ex = self.assertRaises(exception.ResourceBusyError, db_api.profile_delete, self.ctx, profile_id) self.assertEqual("The profile (%s) is busy now." % profile_id, six.text_type(ex)) db_api.node_delete(self.ctx, node.id) db_api.profile_delete(self.ctx, profile_id)