1 # Copyright (C) 2022 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
19 from pathlib import Path
24 from flask import Flask
25 from flask_restx import Api
26 from sqlalchemy import create_engine
27 from sqlalchemy.orm import sessionmaker, clear_mappers
28 from tenacity import retry, stop_after_delay
29 from unittest.mock import MagicMock
30 from mock_alchemy.mocking import UnifiedAlchemyMagicMock
32 from o2app.bootstrap import bootstrap
33 from o2ims.views import configure_namespace
34 from o2app.adapter import unit_of_work
35 from o2ims.adapter.orm import metadata, start_o2ims_mappers
36 from o2common.config import config
39 # os.environ['O2APP_CONFIG'] = 'configs/o2app.conf'
40 # os.environ['ALARM_YAML'] = 'configs/alarm.yaml'
46 uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory=session)
51 def mock_alchemy_uow():
52 session = UnifiedAlchemyMagicMock()
53 uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory=session)
58 def mock_flask_uow(mock_uow):
59 session, uow = mock_uow
61 app.config["TESTING"] = True
64 configure_namespace(api)
69 def in_memory_sqlite_db():
70 engine = create_engine("sqlite:///:memory:")
71 # engine = create_engine("sqlite:///:memory:", echo=True)
72 metadata.create_all(engine)
77 def sqlite_session_factory(in_memory_sqlite_db):
78 yield sessionmaker(bind=in_memory_sqlite_db)
82 def sqlite_uow(sqlite_session_factory):
83 uow = unit_of_work.SqlAlchemyUnitOfWork(
84 session_factory=sqlite_session_factory)
86 # start_o2ims_mappers(uow.session.get_bind())
91 engine = uow.session.get_bind()
92 metadata.drop_all(engine)
96 def sqlite_flask_uow(sqlite_uow):
98 app.config["TESTING"] = True
100 bootstrap(False, sqlite_uow)
101 configure_namespace(api)
102 yield sqlite_uow, app
107 start_o2ims_mappers()
108 # start_o2ims_stx_mappers()
113 @retry(stop=stop_after_delay(10))
114 def wait_for_postgres_to_come_up(engine):
115 return engine.connect()
118 @retry(stop=stop_after_delay(10))
119 def wait_for_webapp_to_come_up():
120 return requests.get(config.get_api_url())
123 @retry(stop=stop_after_delay(10))
124 def wait_for_redis_to_come_up():
125 r = redis.Redis(**config.get_redis_host_and_port())
129 @pytest.fixture(scope="session")
131 engine = create_engine(config.get_postgres_uri(),
132 isolation_level="SERIALIZABLE")
133 wait_for_postgres_to_come_up(engine)
134 metadata.create_all(engine)
139 def postgres_session_factory(postgres_db):
140 yield sessionmaker(bind=postgres_db)
144 def postgres_session(postgres_session_factory):
145 return postgres_session_factory()
149 def postgres_uow(postgres_session_factory):
150 uow = unit_of_work.SqlAlchemyUnitOfWork(
151 session_factory=postgres_session_factory)
156 def postgres_flask_uow(postgres_uow):
157 app = Flask(__name__)
158 app.config["TESTING"] = True
160 bootstrap(False, postgres_uow)
161 configure_namespace(api)
162 yield postgres_uow, app
167 (Path(__file__).parent / "../src/o2ims/entrypoints/flask_application.py")\
170 wait_for_webapp_to_come_up()
174 def restart_redis_pubsub():
175 wait_for_redis_to_come_up()
176 if not shutil.which("docker-compose"):
177 print("skipping restart, assumes running in container")
180 ["docker-compose", "restart", "-t", "0", "redis_pubsub"],