diff --git a/test/test_external_inventory_routes.py b/test/test_external_inventory_routes.py new file mode 100644 index 0000000000000000000000000000000000000000..d17dd4f031b73691c83f810483c0e9dc76bd0be2 --- /dev/null +++ b/test/test_external_inventory_routes.py @@ -0,0 +1,104 @@ +import json +from inventory_provider.storage import external_inventory + +DEFAULT_REQUEST_HEADERS = { + "Content-type": "application/json", + "Accept": ["application/json"] +} + + +def test_get_one_equipment_location(mocker, client): + mocked_redis = mocker.patch( + "inventory_provider.routes.opsdb.db.get_redis") + mocked_hget = mocked_redis.return_value.hget + dummy_data = { + "absid": 1404, + "equipment_name": "pp-cz-e13b", + "pop_name": "Prague", + "pop_abbreviation": "pra", + "pop_site_id": "", + "country": "Czech Republic", + "longitude": 14.391738888889, + "latitude": 50.101847222222 + } + mocked_hget.return_value = json.dumps(dummy_data) + + rv = client.get( + '/opsdb/equipment-location/dummy-equipment', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + assert dummy_data == json.loads(rv.data.decode("utf-8")) + + mocked_hget.assert_called_with( + external_inventory.equipment_locations_key, + "dummy-equipment" + ) + + +def test_get_equipment_location(mocker, client): + mocked_redis = mocker.patch( + "inventory_provider.routes.opsdb.db.get_redis") + mocked_hgetall = mocked_redis.return_value.hgetall + + rv = client.get( + '/opsdb/equipment-location', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + + mocked_hgetall.assert_called_with( + external_inventory.equipment_locations_key + ) + + +def test_get_interface_info(mocker, client): + mocked_redis = mocker.patch( + "inventory_provider.routes.opsdb.db.get_redis") + mocked_hgetall = mocked_redis.return_value.hgetall + + rv = client.get( + '/opsdb/interfaces', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + + mocked_hgetall.assert_called_with( + external_inventory.interfaces_key + ) + + +def test_get_interface_info_for_equipment(mocker, client): + mocked_redis = mocker.patch( + "inventory_provider.routes.opsdb.db.get_redis") + mocked_hscan_iter = mocked_redis.return_value.hscan_iter + + rv = client.get( + '/opsdb/interfaces/dummy-equipment', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + + mocked_hscan_iter.assert_called_with( + external_inventory.interfaces_key, "dummy-equipment::*" + ) + + +def test_get_interface_info_for_equipment_and_interface(mocker, client): + mocked_redis = mocker.patch( + "inventory_provider.routes.opsdb.db.get_redis") + mocked_hget = mocked_redis.return_value.hget + + rv = client.get( + '/opsdb/interfaces/dummy-equipment/xe-2/3/1', + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + assert rv.is_json + + mocked_hget.assert_called_with( + external_inventory.interfaces_key, "dummy-equipment::xe-2/3/1" + ) + + +def test_get_children(mocker, client): + pass # todo - carry on from here