Debian Codegraph @

neutron_fwaas/tests/functional/db/test_migrations.py

raw symbols SCIP
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 alembic import script as alembic_script
14 from neutron.db.migration.alembic_migrations import external
15 from neutron.db.migration import cli as migration
16 from neutron.tests.functional.db import test_migrations
17 from neutron.tests.unit import testlib_api
18 from oslo_config import cfg
19 import sqlalchemy
20
21 from neutron_fwaas.db.models import head
22
23 # EXTERNAL_TABLES should contain all names of tables that are not related to
24 # current repo.
25 EXTERNAL_TABLES = set(external.TABLES)
26 # Model moved to vendor repo
27 EXTERNAL_TABLES.update({'cisco_firewall_associations'})
28 EXTERNAL_TABLES.update({'firewall_router_associations'})
29
30 VERSION_TABLE = 'alembic_version_fwaas'
31
32
33 class TestModelsMigrationsFWaaS(test_migrations.TestModelsMigrations,
34 testlib_api.MySQLTestCaseMixin,
35 testlib_api.SqlTestCaseLight):
36
37 def db_sync(self, engine):
38 cfg.CONF.set_override(
39 'connection',
40 self.engine.url.render_as_string(hide_password=False),
41 group='database')
42 for conf in migration.get_alembic_configs():
43 self.alembic_config = conf
44 self.alembic_config.neutron_config = cfg.CONF
45 migration.do_alembic_command(conf, 'upgrade', 'heads')
46
47 def get_metadata(self):
48 return head.get_metadata()
49
50 def include_object(self, object_, name, type_, reflected, compare_to):
51 if type_ == 'table' and (name.startswith('alembic') or
52 name == VERSION_TABLE or
53 name in EXTERNAL_TABLES):
54 return False
55 if type_ == 'index' and reflected and name.startswith("idx_autoinc_"):
56 return False
57 return True
58
59
60 class TestSanityCheck(testlib_api.SqlTestCaseLight):
61 BUILD_SCHEMA = False
62
63 def setUp(self):
64 super().setUp()
65
66 for conf in migration.get_alembic_configs():
67 self.alembic_config = conf
68 self.alembic_config.neutron_config = cfg.CONF
69
70 def _drop_table(self, table):
71 with self.engine.begin() as conn:
72 table.drop(conn)
73
74 def test_check_sanity_f24e0d5e5bff(self):
75 current_revision = "f24e0d5e5bff"
76 fwg_port_association = sqlalchemy.Table(
77 'firewall_group_port_associations_v2', sqlalchemy.MetaData(),
78 sqlalchemy.Column('firewall_group_id', sqlalchemy.String(36)),
79 sqlalchemy.Column('port_id', sqlalchemy.String(36)))
80
81 with self.engine.connect() as conn:
82 fwg_port_association.create(conn)
83 self.addCleanup(self._drop_table, fwg_port_association)
84 conn.execute(fwg_port_association.insert(), [
85 {'firewall_group_id': '1234', 'port_id': '12345'},
86 {'firewall_group_id': '12343', 'port_id': '12345'}
87 ])
88 script_dir = alembic_script.ScriptDirectory.from_config(
89 self.alembic_config)
90 script = script_dir.get_revision(current_revision).module
91 self.assertRaises(
92 script.DuplicatePortRecordinFirewallGroupPortAssociation,
93 script.check_sanity, conn)