1 # Copyright (C) 2022-2024 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # pylint: disable=redefined-outer-name
20 from pathlib import Path
25 from flask import Flask
26 from flask_restx import Api
27 from sqlalchemy import create_engine
28 from sqlalchemy.orm import sessionmaker, clear_mappers
29 from tenacity import retry, stop_after_delay
30 from unittest.mock import MagicMock
31 from mock_alchemy.mocking import UnifiedAlchemyMagicMock
33 # Mock cgtsclient, dcmanagerclient, fmclient
39 'dcmanagerclient.api',
40 'dcmanagerclient.api.client',
44 'fmclient.common.exceptions'
47 for module_name in modules_to_mock:
48 sys.modules[module_name] = MagicMock()
50 from o2app.bootstrap import bootstrap
51 from o2ims.views import configure_namespace
52 from o2app.adapter import unit_of_work
53 from o2ims.adapter.orm import metadata, start_o2ims_mappers
54 from o2common.config import config
57 # os.environ['O2APP_CONFIG'] = 'configs/o2app.conf'
58 # os.environ['ALARM_YAML'] = 'configs/alarm.yaml'
64 uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory=session)
69 def mock_alchemy_uow():
70 session = UnifiedAlchemyMagicMock()
71 uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory=session)
76 def mock_flask_uow(mock_uow):
77 session, uow = mock_uow
79 app.config["TESTING"] = True
82 configure_namespace(api)
87 def in_memory_sqlite_db():
88 engine = create_engine("sqlite:///:memory:")
89 # engine = create_engine("sqlite:///:memory:", echo=True)
90 metadata.create_all(engine)
95 def sqlite_session_factory(in_memory_sqlite_db):
96 yield sessionmaker(bind=in_memory_sqlite_db)
100 def sqlite_uow(sqlite_session_factory):
101 uow = unit_of_work.SqlAlchemyUnitOfWork(
102 session_factory=sqlite_session_factory)
104 # start_o2ims_mappers(uow.session.get_bind())
109 engine = uow.session.get_bind()
110 metadata.drop_all(engine)
114 def sqlite_flask_uow(sqlite_uow):
115 app = Flask(__name__)
116 app.config["TESTING"] = True
118 bootstrap(False, sqlite_uow)
119 configure_namespace(api)
120 yield sqlite_uow, app
125 start_o2ims_mappers()
126 # start_o2ims_stx_mappers()
131 @retry(stop=stop_after_delay(10))
132 def wait_for_postgres_to_come_up(engine):
133 return engine.connect()
136 @retry(stop=stop_after_delay(10))
137 def wait_for_webapp_to_come_up():
138 return requests.get(config.get_api_url())
141 @retry(stop=stop_after_delay(10))
142 def wait_for_redis_to_come_up():
143 r = redis.Redis(**config.get_redis_host_and_port())
147 @pytest.fixture(scope="session")
149 engine = create_engine(config.get_postgres_uri(),
150 isolation_level="SERIALIZABLE")
151 wait_for_postgres_to_come_up(engine)
152 metadata.create_all(engine)
157 def postgres_session_factory(postgres_db):
158 yield sessionmaker(bind=postgres_db)
162 def postgres_session(postgres_session_factory):
163 return postgres_session_factory()
167 def postgres_uow(postgres_session_factory):
168 uow = unit_of_work.SqlAlchemyUnitOfWork(
169 session_factory=postgres_session_factory)
174 def postgres_flask_uow(postgres_uow):
175 app = Flask(__name__)
176 app.config["TESTING"] = True
178 bootstrap(False, postgres_uow)
179 configure_namespace(api)
180 yield postgres_uow, app
185 (Path(__file__).parent / "../src/o2ims/entrypoints/flask_application.py")\
188 wait_for_webapp_to_come_up()
192 def restart_redis_pubsub():
193 wait_for_redis_to_come_up()
194 if not shutil.which("docker-compose"):
195 print("skipping restart, assumes running in container")
198 ["docker-compose", "restart", "-t", "0", "redis_pubsub"],