diff --git a/test/test_opsdb.py b/test/test_opsdb.py index 04a71a2732e007f91a1e8484408a412f7b7cf27b..5685b2a82604e04c0e8d2a1f8b597a301ca7c4ec 100644 --- a/test/test_opsdb.py +++ b/test/test_opsdb.py @@ -1,9 +1,129 @@ import inventory_provider.opsdb -def test_get_circuits_to_monitor(mocker): - mocked_get_cursor = mocker.patch('inventory_provider.opsdbdb.cursor') - mocked_get_cursor.return_value.__enter__.return_value = None +class MockedCrs: + def __enter__(self): + return "hello" + + def __exit__(self): + return "Goodbye" + + def close(self): + pass + + def execute(self): + pass + +# +# def test_get_circuits_to_monitor(mocker): +# mocked_get_cursor = mocker.patch('inventory_provider.opsdb.cursor') +# mocked_get_cursor.return_value.__enter__.return_value = None +# mocked_execute = mocked_get_cursor. \ +# return_value.__enter__.return_value.execute +# pass + + +def test_update_fields(mocker): + mocker.patch("inventory_provider.opsdb._juniper_field_update") + t = {"manufacturer": "juniper"} + inventory_provider.opsdb._update_fields(t) + inventory_provider.opsdb._juniper_field_update.assert_called_once_with(t) + + mocker.patch("inventory_provider.opsdb._infinera_field_update") + t = {"manufacturer": "infinera"} + inventory_provider.opsdb._update_fields(t) + inventory_provider.opsdb._infinera_field_update.assert_called_once_with(t) + + f = {"manufacturer": "non-existent"} + r = inventory_provider.opsdb._update_fields(f) + assert f == r + + +def test_infinera_field_update(): + i = { + "equipment": "AMS01-DTNX10-1-1", + "card_id": "tim-b-5-7", + "port": "1" + } + r = inventory_provider.opsdb._infinera_field_update(i) + assert r["equipment"] == "AMS01-DTNX10-1" + assert r["interface_name"] == "1-B-5-7-1" + + i = { + "equipment": "BUD01_CX_01", + "card_id": "tim-1/2", + "port": "1" + } + r = inventory_provider.opsdb._infinera_field_update(i) + assert r["equipment"] == "BUD01_CX_01" + assert r["interface_name"] == "1/2-1" + + i = { + "equipment": "irrelevant", + "card_id": "tim_1/2", + "port": "1" + } + r = inventory_provider.opsdb._infinera_field_update(i) + assert r["interface_name"] == "TIM_1/2-1" + + +def test_juniper_field_update(): + i = { + "interface_name": "xe-1/2", + "logical_unit": None + } + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-1/2" + + i["interface_name"] = "xe-1/2" + i["logical_unit"] = 101 + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-1/2.101" + + i["interface_name"] = "xe-1/2" + i["logical_unit"] = 0 + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-1/2.0" + + i["interface_name"] = "xe-1/2" + i["logical_unit"] = None + i["port"] = 0 + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-1/2" + + i["interface_name"] = None + i["card_id"] = "xe-2/0" + i["logical_unit"] = None + i["port"] = None + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-2/0" + + i["interface_name"] = None + i["port"] = "0" + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-2/0/0" + + i["interface_name"] = None + i["port"] = 0 + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-2/0/0" + + i["interface_name"] = None + i["logical_unit"] = "123" + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-2/0/0.123" + + i["interface_name"] = None + i["logical_unit"] = 123 + r = inventory_provider.opsdb._juniper_field_update(i) + assert r["interface_name"] == "xe-2/0/0.123" + + +def test_get_circuits(mocker): + mocked_get_cursor = mocker.patch("inventory_provider.opsdb.cursor") mocked_execute = mocked_get_cursor. \ return_value.__enter__.return_value.execute - pass \ No newline at end of file + mocked_convert_to_dict = mocker.patch( + "inventory_provider.opsdb._convert_to_dict") + mocked_convert_to_dict.return_value = "" + pass