"Fossies" - the Fresh Open Source Software Archive

Member "magnum-8.1.0/magnum/tests/unit/db/test_cluster_template.py" (1 Oct 2019, 9628 Bytes) of package /linux/misc/openstack/magnum-8.1.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. See also the last Fossies "Diffs" side-by-side code changes report for "test_cluster_template.py": 7.1.0_vs_8.0.0.

    1 # Copyright 2015 OpenStack Foundation
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 """Tests for manipulating ClusterTemplate via the DB API"""
   17 from oslo_utils import uuidutils
   18 import six
   19 
   20 from magnum.common import exception
   21 from magnum.tests.unit.db import base
   22 from magnum.tests.unit.db import utils
   23 
   24 
   25 class DbClusterTemplateTestCase(base.DbTestCase):
   26 
   27     def test_create_cluster_template(self):
   28         utils.create_test_cluster_template()
   29 
   30     def test_get_cluster_template_list(self):
   31         uuids = []
   32         for i in range(1, 6):
   33             ct = utils.create_test_cluster_template(
   34                 id=i, uuid=uuidutils.generate_uuid())
   35             uuids.append(six.text_type(ct['uuid']))
   36         res = self.dbapi.get_cluster_template_list(self.context)
   37         res_uuids = [r.uuid for r in res]
   38         self.assertEqual(sorted(uuids), sorted(res_uuids))
   39 
   40     def test_get_cluster_template_list_sorted(self):
   41         uuids = []
   42         for _ in range(5):
   43             ct = utils.create_test_cluster_template(
   44                 uuid=uuidutils.generate_uuid())
   45             uuids.append(six.text_type(ct['uuid']))
   46         res = self.dbapi.get_cluster_template_list(self.context,
   47                                                    sort_key='uuid')
   48         res_uuids = [r.uuid for r in res]
   49         self.assertEqual(sorted(uuids), res_uuids)
   50 
   51         self.assertRaises(exception.InvalidParameterValue,
   52                           self.dbapi.get_cluster_template_list,
   53                           self.context,
   54                           sort_key='foo')
   55 
   56     def test_get_cluster_template_list_with_filters(self):
   57         ct1 = utils.create_test_cluster_template(
   58             id=1,
   59             name='ct-one',
   60             uuid=uuidutils.generate_uuid(),
   61             image_id='image1')
   62         ct2 = utils.create_test_cluster_template(
   63             id=2,
   64             name='ct-two',
   65             uuid=uuidutils.generate_uuid(),
   66             image_id='image2')
   67 
   68         res = self.dbapi.get_cluster_template_list(self.context,
   69                                                    filters={'name': 'ct-one'})
   70         self.assertEqual([ct1['id']], [r.id for r in res])
   71 
   72         res = self.dbapi.get_cluster_template_list(
   73             self.context, filters={'name': 'bad-name'})
   74         self.assertEqual([], [r.id for r in res])
   75 
   76         res = self.dbapi.get_cluster_template_list(
   77             self.context, filters={'image_id': 'image1'})
   78         self.assertEqual([ct1['id']], [r.id for r in res])
   79 
   80         res = self.dbapi.get_cluster_template_list(
   81             self.context, filters={'image_id': 'image2'})
   82         self.assertEqual([ct2['id']], [r.id for r in res])
   83 
   84     def test_get_cluster_template_by_id(self):
   85         ct = utils.create_test_cluster_template()
   86         cluster_template = self.dbapi.get_cluster_template_by_id(
   87             self.context, ct['id'])
   88         self.assertEqual(ct['uuid'], cluster_template.uuid)
   89 
   90     def test_get_cluster_template_by_id_public(self):
   91         ct = utils.create_test_cluster_template(user_id='not_me', public=True)
   92         cluster_template = self.dbapi.get_cluster_template_by_id(
   93             self.context, ct['id'])
   94         self.assertEqual(ct['uuid'], cluster_template.uuid)
   95 
   96     def test_get_cluster_template_by_id_hidden(self):
   97         ct = utils.create_test_cluster_template(user_id='not_me', hidden=True)
   98         cluster_template = self.dbapi.get_cluster_template_by_id(
   99             self.context, ct['id'])
  100         self.assertEqual(ct['uuid'], cluster_template.uuid)
  101 
  102     def test_get_cluster_template_by_uuid(self):
  103         ct = utils.create_test_cluster_template()
  104         cluster_template = self.dbapi.get_cluster_template_by_uuid(
  105             self.context, ct['uuid'])
  106         self.assertEqual(ct['id'], cluster_template.id)
  107 
  108     def test_get_cluster_template_by_uuid_public(self):
  109         ct = utils.create_test_cluster_template(user_id='not_me', public=True)
  110         cluster_template = self.dbapi.get_cluster_template_by_uuid(
  111             self.context, ct['uuid'])
  112         self.assertEqual(ct['id'], cluster_template.id)
  113 
  114     def test_get_cluster_template_by_uuid_hidden(self):
  115         ct = utils.create_test_cluster_template(user_id='not_me', hidden=True)
  116         cluster_template = self.dbapi.get_cluster_template_by_uuid(
  117             self.context, ct['uuid'])
  118         self.assertEqual(ct['id'], cluster_template.id)
  119 
  120     def test_get_cluster_template_that_does_not_exist(self):
  121         self.assertRaises(exception.ClusterTemplateNotFound,
  122                           self.dbapi.get_cluster_template_by_id,
  123                           self.context, 666)
  124 
  125     def test_get_cluster_template_by_name(self):
  126         ct = utils.create_test_cluster_template()
  127         res = self.dbapi.get_cluster_template_by_name(self.context, ct['name'])
  128         self.assertEqual(ct['id'], res.id)
  129         self.assertEqual(ct['uuid'], res.uuid)
  130 
  131     def test_get_cluster_template_by_name_public(self):
  132         ct = utils.create_test_cluster_template(user_id='not_me', public=True)
  133         res = self.dbapi.get_cluster_template_by_name(self.context, ct['name'])
  134         self.assertEqual(ct['id'], res.id)
  135         self.assertEqual(ct['uuid'], res.uuid)
  136 
  137     def test_get_cluster_template_by_name_hidden(self):
  138         ct = utils.create_test_cluster_template(user_id='not_me', hidden=True)
  139         res = self.dbapi.get_cluster_template_by_name(self.context, ct['name'])
  140         self.assertEqual(ct['id'], res.id)
  141         self.assertEqual(ct['uuid'], res.uuid)
  142 
  143     def test_get_cluster_template_by_name_multiple_cluster_template(self):
  144         utils.create_test_cluster_template(
  145             id=1, name='ct',
  146             uuid=uuidutils.generate_uuid(),
  147             image_id='image1')
  148         utils.create_test_cluster_template(
  149             id=2, name='ct',
  150             uuid=uuidutils.generate_uuid(),
  151             image_id='image2')
  152         self.assertRaises(exception.Conflict,
  153                           self.dbapi.get_cluster_template_by_name,
  154                           self.context, 'ct')
  155 
  156     def test_get_cluster_template_by_name_not_found(self):
  157         self.assertRaises(exception.ClusterTemplateNotFound,
  158                           self.dbapi.get_cluster_template_by_name,
  159                           self.context, 'not_found')
  160 
  161     def test_get_cluster_template_by_uuid_that_does_not_exist(self):
  162         self.assertRaises(exception.ClusterTemplateNotFound,
  163                           self.dbapi.get_cluster_template_by_uuid,
  164                           self.context,
  165                           '12345678-9999-0000-aaaa-123456789012')
  166 
  167     def test_update_cluster_template(self):
  168         ct = utils.create_test_cluster_template()
  169         res = self.dbapi.update_cluster_template(ct['id'],
  170                                                  {'name': 'updated-model'})
  171         self.assertEqual('updated-model', res.name)
  172 
  173     def test_update_cluster_template_that_does_not_exist(self):
  174         self.assertRaises(exception.ClusterTemplateNotFound,
  175                           self.dbapi.update_cluster_template, 666,
  176                           {'name': ''})
  177 
  178     def test_update_cluster_template_uuid(self):
  179         ct = utils.create_test_cluster_template()
  180         self.assertRaises(exception.InvalidParameterValue,
  181                           self.dbapi.update_cluster_template, ct['id'],
  182                           {'uuid': 'hello'})
  183 
  184     def test_destroy_cluster_template(self):
  185         ct = utils.create_test_cluster_template()
  186         self.dbapi.destroy_cluster_template(ct['id'])
  187         self.assertRaises(exception.ClusterTemplateNotFound,
  188                           self.dbapi.get_cluster_template_by_id,
  189                           self.context, ct['id'])
  190 
  191     def test_destroy_cluster_template_by_uuid(self):
  192         uuid = uuidutils.generate_uuid()
  193         utils.create_test_cluster_template(uuid=uuid)
  194         self.assertIsNotNone(self.dbapi.get_cluster_template_by_uuid(
  195             self.context, uuid))
  196         self.dbapi.destroy_cluster_template(uuid)
  197         self.assertRaises(exception.ClusterTemplateNotFound,
  198                           self.dbapi.get_cluster_template_by_uuid,
  199                           self.context, uuid)
  200 
  201     def test_destroy_cluster_template_that_does_not_exist(self):
  202         self.assertRaises(exception.ClusterTemplateNotFound,
  203                           self.dbapi.destroy_cluster_template, 666)
  204 
  205     def test_destroy_cluster_template_that_referenced_by_clusters(self):
  206         ct = utils.create_test_cluster_template()
  207         cluster = utils.create_test_cluster(cluster_template_id=ct['uuid'])
  208         self.assertEqual(ct['uuid'], cluster.cluster_template_id)
  209         self.assertRaises(exception.ClusterTemplateReferenced,
  210                           self.dbapi.destroy_cluster_template, ct['id'])
  211 
  212     def test_create_cluster_template_already_exists(self):
  213         uuid = uuidutils.generate_uuid()
  214         utils.create_test_cluster_template(id=1, uuid=uuid)
  215         self.assertRaises(exception.ClusterTemplateAlreadyExists,
  216                           utils.create_test_cluster_template,
  217                           id=2, uuid=uuid)