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)
@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):
os.environ["SETTINGS_FILENAME"] = app_config
with inventory_provider.create_app().test_client() as c:
yield c