diff --git a/changelog b/changelog index 660c7b905d70b45d96006b5582f949aa82000e98..348bc6ac1eaa3a2182fcc12432df42ed9a06a664 100644 --- a/changelog +++ b/changelog @@ -26,3 +26,4 @@ derive active router list from junosspace cache ix public & vpn rr peers use external logging config file + added utilities for the test environment diff --git a/inventory_provider/__init__.py b/inventory_provider/__init__.py index cdfb86083f614d423ef75faa187b17fb76609e07..ed7d1c17db0e309c8e30748575f07bb878ace8fa 100644 --- a/inventory_provider/__init__.py +++ b/inventory_provider/__init__.py @@ -14,6 +14,10 @@ def create_app(): :return: a new flask app instance """ + if "SETTINGS_FILENAME" not in os.environ: + assert False, \ + "environment variable SETTINGS_FILENAME' must be defined" + app = Flask(__name__) app.secret_key = "super secret session key" @@ -35,9 +39,10 @@ def create_app(): from inventory_provider.routes import poller app.register_blueprint(poller.routes, url_prefix='/poller') - if "SETTINGS_FILENAME" not in os.environ: - assert False, \ - "environment variable SETTINGS_FILENAME' must be defined" + if "ENABLE_TESTING_ROUTES" in os.environ: + from inventory_provider.routes import testing + app.register_blueprint(testing.routes, url_prefix='/testing') + logging.warning('DANGER!!! testing routes enabled') logging.info("initializing Flask with config from: %r" % os.environ["SETTINGS_FILENAME"]) diff --git a/inventory_provider/routes/testing.py b/inventory_provider/routes/testing.py new file mode 100644 index 0000000000000000000000000000000000000000..16f1d670265c555f20938d5e2099fe5eb660f0c8 --- /dev/null +++ b/inventory_provider/routes/testing.py @@ -0,0 +1,10 @@ +from flask import Blueprint, Response +from inventory_provider.routes import common + +routes = Blueprint("inventory-data-testing-support-routes", __name__) + + +@routes.route("/flushdb", methods=['GET', 'POST']) +def flushdb(): + common.get_redis().flushdb() + return Response('OK') diff --git a/test/conftest.py b/test/conftest.py index ca7000226686d4739bf587978e1500d831406c6b..30084bf4aa731abdf08d29305c81391b3bbff7ca 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -115,6 +115,10 @@ class MockedRedis(object): k.encode("utf-8") for k in MockedRedis.db.keys() if k.startswith(m.group(1))]) + def flushdb(self): + # only called from testing routes (hopefully) + pass + @pytest.fixture def data_config(): @@ -149,6 +153,7 @@ def client(app_config, mocker): MockedRedis) os.environ["SETTINGS_FILENAME"] = app_config + os.environ["ENABLE_TESTING_ROUTES"] = "1" with inventory_provider.create_app().test_client() as c: yield c diff --git a/test/test_testing_routes.py b/test/test_testing_routes.py new file mode 100644 index 0000000000000000000000000000000000000000..44ae19a006225b9386cebb8eba3d0b71f62a9d72 --- /dev/null +++ b/test/test_testing_routes.py @@ -0,0 +1,4 @@ + +def test_flushdb(client): + rv = client.post("/testing/flushdb") + assert rv.status_code == 200