cinder/cinder/tests/unit/test_paginate_query.py
wangxiyuan 6c01699023 Don't use None value for sql query.
Cinder now will raise 500 error if users list resources with
"--sort_key=name" and "--marker=resource_id"(the marker resource's
name should be None here).

This patch add sql "case" sentence to avoid use None value direcetly.

Most of the code is copied from glance[1]

Oslo.db now have the same paginate_query function and the problem
has been solved in a recent patch[2] as well. But it still has some
bugs: with the oslo.db' code,  when the maker resource is not None,
the return value doesn't contain resouces which names are None.

So it's not the time to use oslo.db here. Just fix this bug in Cinder
first.

[1]:
I2187c41a3fab8b5813ba3a5e0f3249ee5dd981ed
Ib3b5f3f57be3683ba274f0122e6314978a79e75f

[2]:
Iea2cd0bb2556b0b15a0baaa76ef522a3097f9928

Co-authored-by: Stuart McLaren<stuart.mclaren@hpe.com>
Closes-bug: #1657331
Change-Id: Ia1052be567de63083cc6c28a2e5b38a7b8470615
2017-02-04 09:31:27 +08:00

42 lines
1.8 KiB
Python

# 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.
from cinder.common import sqlalchemyutils
from cinder import context
from cinder.db.sqlalchemy import api as db_api
from cinder.db.sqlalchemy import models
from cinder import test
from cinder.tests.unit import fake_constants as fake
class TestPaginateQuery(test.TestCase):
def setUp(self):
super(TestPaginateQuery, self).setUp()
self.ctxt = context.RequestContext(fake.USER_ID, fake.PROJECT_ID,
auth_token=True,
is_admin=True)
self.query = db_api._volume_get_query(self.ctxt)
self.model = models.Volume
def test_paginate_query_marker_null(self):
marker_object = self.model()
self.assertIsNone(marker_object.display_name)
self.assertIsNone(marker_object.updated_at)
marker_object.size = 1
# There is no error raised here.
sqlalchemyutils.paginate_query(self.query, self.model, 10,
sort_keys=['display_name',
'updated_at',
'size'],
marker=marker_object,
sort_dirs=['desc', 'asc', 'desc'])