Skip to content
Snippets Groups Projects
Commit e8d0fa6d authored by Erik Reid's avatar Erik Reid
Browse files

update to use new config passing method

parent 33ffc213
No related branches found
No related tags found
No related merge requests found
...@@ -104,6 +104,6 @@ def load(f): ...@@ -104,6 +104,6 @@ def load(f):
""" """
config = json.loads(f.read()) config = json.loads(f.read())
jsonschema.validate(config, CONFIG_SCHEMA) jsonschema.validate(config, CONFIG_SCHEMA)
with open(config["oid_list.conf"]) as f: # with open(config["oid_list.conf"]) as f:
config["oids"] = _load_oids(f) # config["oids"] = _load_oids(f)
return config return config
...@@ -63,15 +63,17 @@ def _save_value_etree(key, xml_doc): ...@@ -63,15 +63,17 @@ def _save_value_etree(key, xml_doc):
class LoadConfig(bootsteps.Step): class LoadConfig(bootsteps.Step):
assert os.path.isfile(os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']), (
'config file %r not found' %
os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME'])
with open(os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']) as f: def __init__(self):
logging.info( assert os.path.isfile(os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']), (
"Initializing worker with config from: %r" % 'config file %r not found' %
os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']) os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME'])
InventoryTask.config = config.load(f)
with open(os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME']) as f:
logging.info(
"Initializing worker with config from: %r" %
os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME'])
InventoryTask.config = config.load(f)
app.steps['worker'].add(LoadConfig) app.steps['worker'].add(LoadConfig)
......
...@@ -10,8 +10,8 @@ from lxml import etree ...@@ -10,8 +10,8 @@ from lxml import etree
import pytest import pytest
import inventory_provider import inventory_provider
from inventory_provider import config
from inventory_provider.tasks import worker from inventory_provider.tasks import worker
from inventory_provider import config
TEST_DATA_DIRNAME = os.path.realpath(os.path.join( TEST_DATA_DIRNAME = os.path.realpath(os.path.join(
inventory_provider.__path__[0], inventory_provider.__path__[0],
...@@ -20,49 +20,53 @@ TEST_DATA_DIRNAME = os.path.realpath(os.path.join( ...@@ -20,49 +20,53 @@ TEST_DATA_DIRNAME = os.path.realpath(os.path.join(
"data")) "data"))
def data_config_filename(tmp_dir_name): @pytest.fixture
config = { def data_config_filename():
"ops-db": {
"hostname": "xxxxxxx.yyyyy.zzz", with tempfile.NamedTemporaryFile() as f:
"dbname": "xxxxxx", config = {
"username": "xxxxxx", "ops-db": {
"password": "xxxxxxxx" "hostname": "xxxxxxx.yyyyy.zzz",
}, "dbname": "xxxxxx",
"oid_list.conf": os.path.join( "username": "xxxxxx",
tmp_dir_name, "password": "xxxxxxxx"
"oid_list.conf"), },
"ssh": { # TODO: remove oid_list.conf
"username": "uSeR-NaMe", "oid_list.conf": os.path.join(
"private-key": "private-key-filename", '',
"known-hosts": "known-hosts=filename" "oid_list.conf"),
}, "ssh": {
"redis": { "username": "uSeR-NaMe",
"hostname": "xxxxxx", "private-key": "private-key-filename",
"port": 6379 "known-hosts": "known-hosts=filename"
}, },
"junosspace": { "redis": {
"api": "bogus-url", "hostname": "xxxxxx",
"username": "bogus-username", "port": 6379
"password": "bogus-password" },
"junosspace": {
"api": "bogus-url",
"username": "bogus-username",
"password": "bogus-password"
}
} }
}
shutil.copyfile( # shutil.copyfile(
os.path.join(TEST_DATA_DIRNAME, 'oid_list.conf'), # os.path.join(TEST_DATA_DIRNAME, 'oid_list.conf'),
config['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).encode('utf-8'))
f.flush()
yield f.name
filename = os.path.join(tmp_dir_name, "config.json")
with open(filename, "w") as f:
f.write(json.dumps(config))
return filename @pytest.fixture
def data_config(data_config_filename):
with open(data_config_filename) as f:
def _tmp_data_config(): return config.load(f)
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( TEST_DATA_DIRNAME = os.path.realpath(os.path.join(
...@@ -117,9 +121,6 @@ class MockedRedis(object): ...@@ -117,9 +121,6 @@ class MockedRedis(object):
pass pass
@pytest.fixture
def data_config():
return _tmp_data_config()
@pytest.fixture @pytest.fixture
...@@ -130,29 +131,12 @@ def cached_test_data(): ...@@ -130,29 +131,12 @@ def cached_test_data():
@pytest.fixture @pytest.fixture
def app_config(): def flask_config_filename():
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)))
f.write('ENABLE_TESTING_ROUTES = True\n')
yield app_config_filename with tempfile.NamedTemporaryFile() as f:
f.write('ENABLE_TESTING_ROUTES = True\n'.encode('utf-8'))
f.flush()
@pytest.fixture yield f.name
def client(app_config, mocker):
mocker.patch(
'inventory_provider.tasks.common.redis.StrictRedis',
MockedRedis)
os.environ["SETTINGS_FILENAME"] = app_config
with inventory_provider.create_app().test_client() as c:
yield c
@pytest.fixture @pytest.fixture
...@@ -163,8 +147,16 @@ def mocked_redis(mocker): ...@@ -163,8 +147,16 @@ def mocked_redis(mocker):
@pytest.fixture @pytest.fixture
def client_with_mocked_data(client, mocked_redis): def client(flask_config_filename, data_config_filename, mocked_redis):
return client os.environ['FLASK_SETTINGS_FILENAME'] = flask_config_filename
os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME'] = data_config_filename
with inventory_provider.create_app().test_client() as c:
yield c
#
# @pytest.fixture
# def client(client, mocked_redis):
# return client
NETIFACES_TEST_DATA_STRING = """{ NETIFACES_TEST_DATA_STRING = """{
...@@ -194,15 +186,13 @@ def mocked_netifaces(mocker): ...@@ -194,15 +186,13 @@ def mocked_netifaces(mocker):
@pytest.fixture @pytest.fixture
def mocked_worker_module( def mocked_worker_module(
mocker, mocked_redis, data_config, mocker, mocked_redis, data_config_filename,
cached_test_data, mocked_netifaces): cached_test_data, mocked_netifaces):
os.environ['BROKER_HOSTNAME'] = 'a.b.c' os.environ['INVENTORY_PROVIDER_CONFIG_FILENAME'] = data_config_filename
os.environ['BROKER_PORT'] = '123'
os.environ['CELERY_DB_INDEX'] = '456'
os.environ['BROKER_SCHEME'] = 'redis'
worker.InventoryTask.config = data_config with open(data_config_filename) as f:
worker.InventoryTask.config = config.load(f)
def _mocked_snmp_interfaces(hostname, community): def _mocked_snmp_interfaces(hostname, community):
return json.loads(cached_test_data['snmp-interfaces:' + hostname]) return json.loads(cached_test_data['snmp-interfaces:' + hostname])
......
...@@ -8,7 +8,7 @@ DEFAULT_REQUEST_HEADERS = { ...@@ -8,7 +8,7 @@ DEFAULT_REQUEST_HEADERS = {
} }
def test_router_interfaces(router, client_with_mocked_data): def test_router_interfaces(router, client):
interfaces_list_schema = { interfaces_list_schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
...@@ -36,7 +36,7 @@ def test_router_interfaces(router, client_with_mocked_data): ...@@ -36,7 +36,7 @@ def test_router_interfaces(router, client_with_mocked_data):
} }
} }
rv = client_with_mocked_data.post( rv = client.post(
"/data/interfaces/" + router, "/data/interfaces/" + router,
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
...@@ -46,7 +46,7 @@ def test_router_interfaces(router, client_with_mocked_data): ...@@ -46,7 +46,7 @@ def test_router_interfaces(router, client_with_mocked_data):
assert response # at least shouldn't be empty assert response # at least shouldn't be empty
def test_snmp_ids(router, client_with_mocked_data): def test_snmp_ids(router, client):
snmp_id_list_schema = { snmp_id_list_schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
...@@ -62,7 +62,7 @@ def test_snmp_ids(router, client_with_mocked_data): ...@@ -62,7 +62,7 @@ def test_snmp_ids(router, client_with_mocked_data):
} }
} }
rv = client_with_mocked_data.post( rv = client.post(
"/testing/snmp/" + router, "/testing/snmp/" + router,
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
...@@ -71,7 +71,7 @@ def test_snmp_ids(router, client_with_mocked_data): ...@@ -71,7 +71,7 @@ def test_snmp_ids(router, client_with_mocked_data):
assert response # at least shouldn't be empty assert response # at least shouldn't be empty
def test_router_bgp_routes(router, client_with_mocked_data): def test_router_bgp_routes(router, client):
ROUTERS_WITH_BGP_CONFIG = [ ROUTERS_WITH_BGP_CONFIG = [
"mx1.bud.hu.geant.net", "mx1.bud.hu.geant.net",
...@@ -114,7 +114,7 @@ def test_router_bgp_routes(router, client_with_mocked_data): ...@@ -114,7 +114,7 @@ def test_router_bgp_routes(router, client_with_mocked_data):
} }
} }
rv = client_with_mocked_data.post( rv = client.post(
"/testing/bgp/" + router, "/testing/bgp/" + router,
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
......
...@@ -7,7 +7,7 @@ DEFAULT_REQUEST_HEADERS = { ...@@ -7,7 +7,7 @@ DEFAULT_REQUEST_HEADERS = {
} }
def test_router_interfaces(router, client_with_mocked_data): def test_router_interfaces(router, client):
interfaces_list_schema = { interfaces_list_schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
...@@ -56,7 +56,7 @@ def test_router_interfaces(router, client_with_mocked_data): ...@@ -56,7 +56,7 @@ def test_router_interfaces(router, client_with_mocked_data):
} }
} }
rv = client_with_mocked_data.post( rv = client.post(
"/poller/interfaces/" + router, "/poller/interfaces/" + router,
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
......
...@@ -5,6 +5,7 @@ and some data ends up in the right place ... otherwise not very detailed ...@@ -5,6 +5,7 @@ and some data ends up in the right place ... otherwise not very detailed
import contextlib import contextlib
from inventory_provider.tasks import worker from inventory_provider.tasks import worker
from inventory_provider.tasks.common import get_redis from inventory_provider.tasks.common import get_redis
......
...@@ -130,8 +130,8 @@ JUNIPER_LINK_METADATA = { ...@@ -130,8 +130,8 @@ JUNIPER_LINK_METADATA = {
} }
def test_juniper_link_info(client_with_mocked_data): def test_juniper_link_info(client):
rv = client_with_mocked_data.get( rv = client.get(
'/classifier/juniper-link-info/mx1.ams.nl.geant.net/ae15.1500', '/classifier/juniper-link-info/mx1.ams.nl.geant.net/ae15.1500',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
...@@ -152,7 +152,7 @@ IX_PUBLIC_PEER_INFO_KEYS = {'ix-public-peer-info', 'interfaces'} ...@@ -152,7 +152,7 @@ IX_PUBLIC_PEER_INFO_KEYS = {'ix-public-peer-info', 'interfaces'}
] ]
) )
def test_peer_info( def test_peer_info(
client_with_mocked_data, peer_address, expected_response_keys): client, peer_address, expected_response_keys):
response_schema = { response_schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"type": "object", "type": "object",
...@@ -257,7 +257,7 @@ def test_peer_info( ...@@ -257,7 +257,7 @@ def test_peer_info(
"additionalProperties": False "additionalProperties": False
} }
rv = client_with_mocked_data.get( rv = client.get(
'/classifier/peer-info/%s' % peer_address, '/classifier/peer-info/%s' % peer_address,
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
...@@ -268,15 +268,15 @@ def test_peer_info( ...@@ -268,15 +268,15 @@ def test_peer_info(
assert set(response_data.keys()) == expected_response_keys assert set(response_data.keys()) == expected_response_keys
def test_peer_invalid_address(client_with_mocked_data): def test_peer_invalid_address(client):
rv = client_with_mocked_data.get( rv = client.get(
'/classifier/peer-info/1.2.3.4.5', '/classifier/peer-info/1.2.3.4.5',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 422 assert rv.status_code == 422
def test_peer_not_found(client_with_mocked_data): def test_peer_not_found(client):
rv = client_with_mocked_data.get( rv = client.get(
'/classifier/peer-info/1.2.3.4', '/classifier/peer-info/1.2.3.4',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 404 assert rv.status_code == 404
......
...@@ -89,8 +89,8 @@ INFINERA_LINK_METADATA = { ...@@ -89,8 +89,8 @@ INFINERA_LINK_METADATA = {
} }
def test_trap_metadata(client_with_mocked_data): def test_trap_metadata(client):
rv = client_with_mocked_data.get( rv = client.get(
'/classifier/infinera-lambda-info/' '/classifier/infinera-lambda-info/'
'LON2-DTNX10-1/1-A-2-1-4/gen-lon3_LHC_CERN-JANET_09013', 'LON2-DTNX10-1/1-A-2-1-4/gen-lon3_LHC_CERN-JANET_09013',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
......
...@@ -28,8 +28,8 @@ def test_juniper_addresses(client): ...@@ -28,8 +28,8 @@ def test_juniper_addresses(client):
assert len(response_data) > 0 # test data is not empty assert len(response_data) > 0 # test data is not empty
def test_get_equipment_location(client_with_mocked_data): def test_get_equipment_location(client):
rv = client_with_mocked_data.get( rv = client.get(
'/testing/opsdb/equipment-location', '/testing/opsdb/equipment-location',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
...@@ -37,8 +37,8 @@ def test_get_equipment_location(client_with_mocked_data): ...@@ -37,8 +37,8 @@ def test_get_equipment_location(client_with_mocked_data):
# TODO: validate against schema # TODO: validate against schema
def test_get_interface_info(client_with_mocked_data): def test_get_interface_info(client):
rv = client_with_mocked_data.get( rv = client.get(
'/testing/opsdb/interfaces', '/testing/opsdb/interfaces',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
...@@ -46,8 +46,8 @@ def test_get_interface_info(client_with_mocked_data): ...@@ -46,8 +46,8 @@ def test_get_interface_info(client_with_mocked_data):
# TODO: validate against schema # TODO: validate against schema
def test_get_interface_info_for_equipment(client_with_mocked_data): def test_get_interface_info_for_equipment(client):
rv = client_with_mocked_data.get( rv = client.get(
'/testing/opsdb/interfaces/mx1.ams.nl.geant.net', '/testing/opsdb/interfaces/mx1.ams.nl.geant.net',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
...@@ -56,8 +56,8 @@ def test_get_interface_info_for_equipment(client_with_mocked_data): ...@@ -56,8 +56,8 @@ def test_get_interface_info_for_equipment(client_with_mocked_data):
def test_get_interface_info_for_equipment_and_interface( def test_get_interface_info_for_equipment_and_interface(
client_with_mocked_data): client):
rv = client_with_mocked_data.get( rv = client.get(
'/testing/opsdb/interfaces/mx1.ams.nl.geant.net/ae3.0', '/testing/opsdb/interfaces/mx1.ams.nl.geant.net/ae3.0',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
...@@ -65,8 +65,8 @@ def test_get_interface_info_for_equipment_and_interface( ...@@ -65,8 +65,8 @@ def test_get_interface_info_for_equipment_and_interface(
# TODO: validate against schema # TODO: validate against schema
def test_get_children(client_with_mocked_data): def test_get_children(client):
rv = client_with_mocked_data.get( rv = client.get(
'/testing/opsdb/circuit-hierarchy/children/12363', '/testing/opsdb/circuit-hierarchy/children/12363',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
...@@ -74,8 +74,8 @@ def test_get_children(client_with_mocked_data): ...@@ -74,8 +74,8 @@ def test_get_children(client_with_mocked_data):
# TODO: validate against schema # TODO: validate against schema
def test_get_parents(client_with_mocked_data): def test_get_parents(client):
rv = client_with_mocked_data.get( rv = client.get(
'/testing/opsdb/circuit-hierarchy/parents/11725', '/testing/opsdb/circuit-hierarchy/parents/11725',
headers=DEFAULT_REQUEST_HEADERS) headers=DEFAULT_REQUEST_HEADERS)
assert rv.status_code == 200 assert rv.status_code == 200
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment