Newer
Older
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"),
"routers_community.conf": os.path.join(
tmp_dir_name,
"routers_community.conf"),
"ssh": {
"private-key": "private-key-filename",
"known-hosts": "known-hosts=filename"
},
"redis": {
"hostname": "xxxxxx",
"port": 6379
},
"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']
)
shutil.copyfile(
os.path.join(TEST_DATA_DIRNAME, 'routers_community.conf'),
config['routers_community.conf']
)
filename = os.path.join(tmp_dir_name, "config.json")
with open(filename, "w") as f:
f.write(json.dumps(config))
return filename
with tempfile.TemporaryDirectory() as tmpdir:
with open(data_config_filename(tmpdir)) as f:
return config.load(f)
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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('^([^\*]+)\*$', 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