import json import os import re import shutil import tempfile import pytest import inventory_provider from inventory_provider import config TEST_DATA_DIRNAME = os.path.realpath(os.path.join( inventory_provider.__path__[0], "..", "test", "data")) def data_config_filename(tmp_dir_name): config = { "alarms-db": { "hostname": "xxxxxxx.yyyyy.zzz", "dbname": "xxxxxx", "username": "xxxxxx", "password": "xxxxxxxx" }, "ops-db": { "hostname": "xxxxxxx.yyyyy.zzz", "dbname": "xxxxxx", "username": "xxxxxx", "password": "xxxxxxxx" }, "oid_list.conf": os.path.join( tmp_dir_name, "oid_list.conf"), "ssh": { "username": "uSeR-NaMe", "private-key": "private-key-filename", "known-hosts": "known-hosts=filename" }, "redis": { "hostname": "xxxxxx", "port": 6379 }, "junosspace": { "api": "bogus-url", "username": "bogus-username", "password": "bogus-password" }, "infinera-dna": [ {"name": "name1", "address": "123.456.789.0"}, {"name": "name2", "address": "012.345.678.9"}, {"name": "name3", "address": "111.222.333.000"} ], "coriant-tnms": [ {"name": "name1", "address": "123.456.789.0"}, {"name": "name2", "address": "012.345.678.9"}, {"name": "name3", "address": "111.222.333.000"} ] } shutil.copyfile( os.path.join(TEST_DATA_DIRNAME, 'oid_list.conf'), config['oid_list.conf'] ) filename = os.path.join(tmp_dir_name, "config.json") with open(filename, "w") as f: f.write(json.dumps(config)) return filename def _tmp_data_config(): with tempfile.TemporaryDirectory() as tmpdir: with open(data_config_filename(tmpdir)) as f: return config.load(f) TEST_DATA_DIRNAME = os.path.realpath(os.path.join( inventory_provider.__path__[0], "..", "test", "data")) class MockedRedis(object): db = None def __init__(self, *args, **kwargs): if MockedRedis.db is None: test_data_filename = os.path.join( TEST_DATA_DIRNAME, "router-info.json") with open(test_data_filename) as f: MockedRedis.db = json.loads(f.read()) def set(self, name, value): MockedRedis.db[name] = value def get(self, name): value = MockedRedis.db.get(name, None) if value is None: return None return value.encode('utf-8') def keys(self, glob=None): if not glob: return list([k.encode("utf-8") for k in MockedRedis.db.keys()]) m = re.match(r'^([^*]+)\*$', glob) assert m # all expected global are like this return list([ k.encode("utf-8") for k in MockedRedis.db.keys() if k.startswith(m.group(1))]) @pytest.fixture def data_config(): return _tmp_data_config() @pytest.fixture def cached_test_data(): filename = os.path.join(TEST_DATA_DIRNAME, "router-info.json") with open(filename) as f: return json.loads(f.read()) @pytest.fixture def app_config(): with tempfile.TemporaryDirectory() as tmpdir: app_config_filename = os.path.join(tmpdir, "app.config") with open(app_config_filename, "w") as f: f.write("%s = '%s'\n" % ( "INVENTORY_PROVIDER_CONFIG_FILENAME", data_config_filename(tmpdir))) yield app_config_filename @pytest.fixture def client(app_config, mocker): mocker.patch( 'inventory_provider.routes.common.redis.StrictRedis', MockedRedis) os.environ["SETTINGS_FILENAME"] = app_config with inventory_provider.create_app().test_client() as c: yield c @pytest.fixture def client_with_mocked_data(client, mocker): mocker.patch( 'inventory_provider.routes.common.redis.StrictRedis', MockedRedis) return client