Fix sort of *list command output
While sorting output of list command ("audittemplate list",
"strategy list", etc) by sort-key that is not belongs
to specific model, this sort-key was passed to db what
caused error (HTTP 500). We added check on such keys and now,
if got one of them, then we make sort on API side
instead of db side.
We removed excess sort and changed all sorting routines
to unified way.
Also added sort tests on every model.
Change-Id: I41faea1622605ee4fa8dc48cd572876d75be8383
Closes-Bug: 1662887
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
import datetime
|
||||
import itertools
|
||||
import mock
|
||||
|
||||
from oslo_config import cfg
|
||||
@@ -267,6 +268,67 @@ class TestListAction(api_base.FunctionalTest):
|
||||
response = self.get_json(url, expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
def test_many_with_sort_key_uuid(self):
|
||||
action_plan = obj_utils.create_test_action_plan(
|
||||
self.context,
|
||||
uuid=utils.generate_uuid(),
|
||||
audit_id=self.audit.id)
|
||||
|
||||
actions_list = []
|
||||
for id_ in range(1, 3):
|
||||
action = obj_utils.create_test_action(
|
||||
self.context, id=id_,
|
||||
action_plan_id=action_plan.id,
|
||||
uuid=utils.generate_uuid())
|
||||
actions_list.append(action)
|
||||
|
||||
response = self.get_json('/actions?sort_key=%s' % 'uuid')
|
||||
names = [s['uuid'] for s in response['actions']]
|
||||
|
||||
self.assertEqual(
|
||||
sorted([a.uuid for a in actions_list]),
|
||||
names)
|
||||
|
||||
def test_many_with_sort_key_action_plan_uuid(self):
|
||||
action_plan_1 = obj_utils.create_test_action_plan(
|
||||
self.context,
|
||||
uuid=utils.generate_uuid(),
|
||||
audit_id=self.audit.id)
|
||||
|
||||
action_plan_2 = obj_utils.create_test_action_plan(
|
||||
self.context,
|
||||
uuid=utils.generate_uuid(),
|
||||
audit_id=self.audit.id)
|
||||
|
||||
action_plans_uuid_list = []
|
||||
for id_, action_plan_id in enumerate(itertools.chain.from_iterable([
|
||||
itertools.repeat(action_plan_1.id, 3),
|
||||
itertools.repeat(action_plan_2.id, 2)]), 1):
|
||||
action = obj_utils.create_test_action(
|
||||
self.context, id=id_,
|
||||
action_plan_id=action_plan_id,
|
||||
uuid=utils.generate_uuid())
|
||||
action_plans_uuid_list.append(action.action_plan.uuid)
|
||||
|
||||
for direction in ['asc', 'desc']:
|
||||
response = self.get_json(
|
||||
'/actions?sort_key={0}&sort_dir={1}'
|
||||
.format('action_plan_uuid', direction))
|
||||
|
||||
action_plan_uuids = \
|
||||
[s['action_plan_uuid'] for s in response['actions']]
|
||||
|
||||
self.assertEqual(
|
||||
sorted(action_plans_uuid_list, reverse=(direction == 'desc')),
|
||||
action_plan_uuids,
|
||||
message='Failed on %s direction' % direction)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/actions?sort_key=%s' % 'bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
def test_many_with_soft_deleted_action_plan_uuid(self):
|
||||
action_plan1 = obj_utils.create_test_action_plan(
|
||||
self.context,
|
||||
|
||||
@@ -256,6 +256,12 @@ class TestListActionPlan(api_base.FunctionalTest):
|
||||
uuids = [s['audit_uuid'] for s in response['action_plans']]
|
||||
self.assertEqual(sorted(audit_list), uuids)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/action_plans?sort_key=%s' % 'bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
def test_links(self):
|
||||
uuid = utils.generate_uuid()
|
||||
obj_utils.create_test_action_plan(self.context, id=1, uuid=uuid)
|
||||
|
||||
@@ -294,6 +294,50 @@ class TestListAuditTemplate(FunctionalTestWithSetup):
|
||||
'/audit_templates?strategy=%s' % self.fake_strategy2.name)
|
||||
self.assertEqual(2, len(response['audit_templates']))
|
||||
|
||||
def test_many_with_sort_key_name(self):
|
||||
audit_template_list = []
|
||||
for id_ in range(1, 6):
|
||||
audit_template = obj_utils.create_test_audit_template(
|
||||
self.context, id=id_, uuid=utils.generate_uuid(),
|
||||
name='My Audit Template {0}'.format(id_))
|
||||
audit_template_list.append(audit_template)
|
||||
|
||||
response = self.get_json('/audit_templates?sort_key=%s' % 'name')
|
||||
|
||||
names = [s['name'] for s in response['audit_templates']]
|
||||
|
||||
self.assertEqual(
|
||||
sorted([at.name for at in audit_template_list]),
|
||||
names)
|
||||
|
||||
def test_many_with_sort_key_goal_name(self):
|
||||
goal_names_list = []
|
||||
for id_, goal_id in enumerate(itertools.chain.from_iterable([
|
||||
itertools.repeat(self.fake_goal1.id, 3),
|
||||
itertools.repeat(self.fake_goal2.id, 2)]), 1):
|
||||
audit_template = obj_utils.create_test_audit_template(
|
||||
self.context, id=id_, uuid=utils.generate_uuid(),
|
||||
name='My Audit Template {0}'.format(id_),
|
||||
goal_id=goal_id)
|
||||
goal_names_list.append(audit_template.goal.name)
|
||||
|
||||
for direction in ['asc', 'desc']:
|
||||
response = self.get_json(
|
||||
'/audit_templates?sort_key={0}&sort_dir={1}'
|
||||
.format('goal_name', direction))
|
||||
|
||||
goal_names = [s['goal_name'] for s in response['audit_templates']]
|
||||
|
||||
self.assertEqual(
|
||||
sorted(goal_names_list, reverse=(direction == 'desc')),
|
||||
goal_names)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/audit_templates?sort_key=%s' % 'goal_bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
|
||||
class TestPatch(FunctionalTestWithSetup):
|
||||
|
||||
|
||||
@@ -217,6 +217,12 @@ class TestListAudit(api_base.FunctionalTest):
|
||||
uuids = [s['goal_uuid'] for s in response['audits']]
|
||||
self.assertEqual(sorted(goal_list), uuids)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/audits?sort_key=%s' % 'bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
def test_links(self):
|
||||
uuid = utils.generate_uuid()
|
||||
obj_utils.create_test_audit(
|
||||
|
||||
@@ -120,6 +120,27 @@ class TestListGoal(api_base.FunctionalTest):
|
||||
response = self.get_json('/goals')
|
||||
self.assertEqual(3, len(response['goals']))
|
||||
|
||||
def test_many_with_sort_key_uuid(self):
|
||||
goal_list = []
|
||||
for idx in range(1, 6):
|
||||
goal = obj_utils.create_test_goal(
|
||||
self.context, id=idx,
|
||||
uuid=utils.generate_uuid(),
|
||||
name='GOAL_{0}'.format(idx))
|
||||
goal_list.append(goal.uuid)
|
||||
|
||||
response = self.get_json('/goals/?sort_key=uuid')
|
||||
|
||||
self.assertEqual(5, len(response['goals']))
|
||||
uuids = [s['uuid'] for s in response['goals']]
|
||||
self.assertEqual(sorted(goal_list), uuids)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/goals?sort_key=%s' % 'bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
|
||||
class TestGoalPolicyEnforcement(api_base.FunctionalTest):
|
||||
|
||||
|
||||
@@ -113,6 +113,26 @@ class TestListScoringEngine(api_base.FunctionalTest):
|
||||
response = self.get_json('/scoring_engines')
|
||||
self.assertEqual(3, len(response['scoring_engines']))
|
||||
|
||||
def test_many_with_sort_key_uuid(self):
|
||||
scoring_engine_list = []
|
||||
for idx in range(1, 6):
|
||||
scoring_engine = obj_utils.create_test_scoring_engine(
|
||||
self.context, id=idx, uuid=utils.generate_uuid(),
|
||||
name=str(idx), description='SE_{0}'.format(idx))
|
||||
scoring_engine_list.append(scoring_engine.uuid)
|
||||
|
||||
response = self.get_json('/scoring_engines/?sort_key=uuid')
|
||||
|
||||
self.assertEqual(5, len(response['scoring_engines']))
|
||||
uuids = [s['uuid'] for s in response['scoring_engines']]
|
||||
self.assertEqual(sorted(scoring_engine_list), uuids)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/goals?sort_key=%s' % 'bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
|
||||
class TestScoringEnginePolicyEnforcement(api_base.FunctionalTest):
|
||||
|
||||
|
||||
@@ -131,6 +131,26 @@ class TestListService(api_base.FunctionalTest):
|
||||
response = self.get_json('/services')
|
||||
self.assertEqual(3, len(response['services']))
|
||||
|
||||
def test_many_with_sort_key_name(self):
|
||||
service_list = []
|
||||
for id_ in range(1, 4):
|
||||
service = obj_utils.create_test_service(
|
||||
self.context, id=id_, host='CONTROLLER',
|
||||
name='SERVICE_{0}'.format(id_))
|
||||
service_list.append(service.name)
|
||||
|
||||
response = self.get_json('/services/?sort_key=name')
|
||||
|
||||
self.assertEqual(3, len(response['services']))
|
||||
names = [s['name'] for s in response['services']]
|
||||
self.assertEqual(sorted(service_list), names)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/services?sort_key=%s' % 'bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
|
||||
class TestServicePolicyEnforcement(api_base.FunctionalTest):
|
||||
|
||||
|
||||
@@ -221,6 +221,27 @@ class TestListStrategy(api_base.FunctionalTest):
|
||||
for strategy in strategies:
|
||||
self.assertEqual(goal1['uuid'], strategy['goal_uuid'])
|
||||
|
||||
def test_many_with_sort_key_goal_uuid(self):
|
||||
goals_uuid_list = []
|
||||
for idx in range(1, 6):
|
||||
strategy = obj_utils.create_test_strategy(
|
||||
self.context, id=idx,
|
||||
uuid=utils.generate_uuid(),
|
||||
name='STRATEGY_{0}'.format(idx))
|
||||
goals_uuid_list.append(strategy.goal.uuid)
|
||||
|
||||
response = self.get_json('/strategies/?sort_key=goal_uuid')
|
||||
|
||||
self.assertEqual(5, len(response['strategies']))
|
||||
goal_uuids = [s['goal_uuid'] for s in response['strategies']]
|
||||
self.assertEqual(sorted(goals_uuid_list), goal_uuids)
|
||||
|
||||
def test_sort_key_validation(self):
|
||||
response = self.get_json(
|
||||
'/strategies?sort_key=%s' % 'bad_name',
|
||||
expect_errors=True)
|
||||
self.assertEqual(400, response.status_int)
|
||||
|
||||
|
||||
class TestStrategyPolicyEnforcement(api_base.FunctionalTest):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user