"Fossies" - the Fresh Open Source Software Archive

Member "zun-4.0.0/zun/tests/unit/db/test_registry.py" (16 Oct 2019, 8191 Bytes) of package /linux/misc/openstack/zun-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 from oslo_utils import uuidutils
   14 import six
   15 
   16 from zun.common import exception
   17 import zun.conf
   18 from zun.db import api as dbapi
   19 from zun.tests.unit.db import base
   20 from zun.tests.unit.db import utils
   21 
   22 
   23 CONF = zun.conf.CONF
   24 
   25 
   26 class DbRegistryTestCase(base.DbTestCase):
   27 
   28     def setUp(self):
   29         super(DbRegistryTestCase, self).setUp()
   30 
   31     def test_create_registry(self):
   32         username = 'fake-user'
   33         password = 'fake-pass'
   34         registry = utils.create_test_registry(context=self.context,
   35                                               username=username,
   36                                               password=password)
   37         self.assertEqual(username, registry.username)
   38         self.assertEqual(password, registry.password)
   39 
   40     def test_create_registry_already_exists(self):
   41         utils.create_test_registry(context=self.context,
   42                                    uuid='123')
   43         with self.assertRaisesRegex(exception.RegistryAlreadyExists,
   44                                     'A registry with UUID 123.*'):
   45             utils.create_test_registry(context=self.context,
   46                                        uuid='123')
   47 
   48     def test_get_registry_by_uuid(self):
   49         username = 'fake-user'
   50         password = 'fake-pass'
   51         registry = utils.create_test_registry(context=self.context,
   52                                               username=username,
   53                                               password=password)
   54         res = dbapi.get_registry_by_uuid(self.context,
   55                                          registry.uuid)
   56         self.assertEqual(registry.id, res.id)
   57         self.assertEqual(registry.uuid, res.uuid)
   58         self.assertEqual(username, res.username)
   59         self.assertEqual(password, res.password)
   60 
   61     def test_get_registry_by_name(self):
   62         username = 'fake-user'
   63         password = 'fake-pass'
   64         registry = utils.create_test_registry(context=self.context,
   65                                               username=username,
   66                                               password=password)
   67         res = dbapi.get_registry_by_name(
   68             self.context, registry.name)
   69         self.assertEqual(registry.id, res.id)
   70         self.assertEqual(registry.uuid, res.uuid)
   71         self.assertEqual(username, res.username)
   72         self.assertEqual(password, res.password)
   73 
   74     def test_get_registry_that_does_not_exist(self):
   75         self.assertRaises(exception.RegistryNotFound,
   76                           dbapi.get_registry_by_uuid,
   77                           self.context,
   78                           uuidutils.generate_uuid())
   79 
   80     def test_list_registries(self):
   81         uuids = []
   82         passwords = []
   83         for i in range(1, 6):
   84             password = 'pass' + str(i)
   85             passwords.append(password)
   86             registry = utils.create_test_registry(
   87                 uuid=uuidutils.generate_uuid(),
   88                 context=self.context,
   89                 name='registry' + str(i),
   90                 password=password)
   91             uuids.append(six.text_type(registry['uuid']))
   92         res = dbapi.list_registries(self.context)
   93         res_uuids = [r.uuid for r in res]
   94         self.assertEqual(sorted(uuids), sorted(res_uuids))
   95         res_passwords = [r.password for r in res]
   96         self.assertEqual(sorted(passwords), sorted(res_passwords))
   97 
   98     def test_list_registries_sorted(self):
   99         uuids = []
  100         for i in range(5):
  101             registry = utils.create_test_registry(
  102                 uuid=uuidutils.generate_uuid(),
  103                 context=self.context,
  104                 name='registry' + str(i))
  105             uuids.append(six.text_type(registry.uuid))
  106         res = dbapi.list_registries(self.context, sort_key='uuid')
  107         res_uuids = [r.uuid for r in res]
  108         self.assertEqual(sorted(uuids), res_uuids)
  109 
  110         self.assertRaises(exception.InvalidParameterValue,
  111                           dbapi.list_registries,
  112                           self.context,
  113                           sort_key='foo')
  114 
  115     def test_list_registries_with_filters(self):
  116         registry1 = utils.create_test_registry(
  117             name='registry-one',
  118             uuid=uuidutils.generate_uuid(),
  119             context=self.context)
  120         registry2 = utils.create_test_registry(
  121             name='registry-two',
  122             uuid=uuidutils.generate_uuid(),
  123             context=self.context)
  124 
  125         res = dbapi.list_registries(
  126             self.context, filters={'name': 'registry-one'})
  127         self.assertEqual([registry1.id], [r.id for r in res])
  128 
  129         res = dbapi.list_registries(
  130             self.context, filters={'name': 'registry-two'})
  131         self.assertEqual([registry2.id], [r.id for r in res])
  132 
  133         res = dbapi.list_registries(
  134             self.context, filters={'name': 'bad-registry'})
  135         self.assertEqual([], [r.id for r in res])
  136 
  137         res = dbapi.list_registries(
  138             self.context,
  139             filters={'name': registry1.name})
  140         self.assertEqual([registry1.id], [r.id for r in res])
  141 
  142     def test_list_registries_with_list_filters(self):
  143         registry1 = utils.create_test_registry(
  144             name='registry-one',
  145             uuid=uuidutils.generate_uuid(),
  146             context=self.context)
  147         registry2 = utils.create_test_registry(
  148             name='registry-two',
  149             uuid=uuidutils.generate_uuid(),
  150             context=self.context)
  151 
  152         res = dbapi.list_registries(
  153             self.context, filters={'name': ['registry-one', 'registry-two']})
  154         uuids = sorted([registry1.uuid, registry2.uuid])
  155         self.assertEqual(uuids, sorted([r.uuid for r in res]))
  156 
  157     def test_destroy_registry(self):
  158         registry = utils.create_test_registry(context=self.context)
  159         dbapi.destroy_registry(self.context, registry.id)
  160         self.assertRaises(exception.RegistryNotFound,
  161                           dbapi.get_registry_by_uuid,
  162                           self.context, registry.uuid)
  163 
  164     def test_destroy_registry_by_uuid(self):
  165         registry = utils.create_test_registry(context=self.context)
  166         dbapi.destroy_registry(self.context, registry.uuid)
  167         self.assertRaises(exception.RegistryNotFound,
  168                           dbapi.get_registry_by_uuid,
  169                           self.context, registry.uuid)
  170 
  171     def test_destroy_registry_that_does_not_exist(self):
  172         self.assertRaises(exception.RegistryNotFound,
  173                           dbapi.destroy_registry, self.context,
  174                           uuidutils.generate_uuid())
  175 
  176     def test_update_registry(self):
  177         registry = utils.create_test_registry(context=self.context)
  178         old_name = registry.name
  179         new_name = 'new-name'
  180         new_password = 'new-pass'
  181         self.assertNotEqual(old_name, new_name)
  182 
  183         res = dbapi.update_registry(self.context, registry.id,
  184                                     {'name': new_name,
  185                                      'password': new_password})
  186         self.assertEqual(new_name, res.name)
  187         self.assertEqual(new_password, res.password)
  188 
  189     def test_update_registry_not_found(self):
  190         registry_uuid = uuidutils.generate_uuid()
  191         new_name = 'new-name'
  192         self.assertRaises(exception.RegistryNotFound,
  193                           dbapi.update_registry, self.context,
  194                           registry_uuid, {'name': new_name})
  195 
  196     def test_update_registry_uuid(self):
  197         registry = utils.create_test_registry(context=self.context)
  198         self.assertRaises(exception.InvalidParameterValue,
  199                           dbapi.update_registry, self.context,
  200                           registry.id, {'uuid': ''})