From 01950ae88abe56b2a24ef1dba6d79e5ba582148b Mon Sep 17 00:00:00 2001
From: Bjarke Madsen <bjarke.madsen@geant.org>
Date: Wed, 10 Mar 2021 16:16:35 +0100
Subject: [PATCH] Get some coverage to check for bugs

---
 brian_dashboard_manager/templating/render.py |   2 +-
 test/test_aggregrate.py                      | 177 +++++++++++++++++++
 2 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100644 test/test_aggregrate.py

diff --git a/brian_dashboard_manager/templating/render.py b/brian_dashboard_manager/templating/render.py
index cf263de..dede30c 100644
--- a/brian_dashboard_manager/templating/render.py
+++ b/brian_dashboard_manager/templating/render.py
@@ -46,7 +46,7 @@ def create_panel(data):
     with open(file) as f:
         template = jinja2.Template(f.read())
     yaxes = create_yaxes(data.get('y_axis_type', 'bits'))
-    targets = []
+    targets = data.get('targets', [])
     for target in data.get('panel_targets', []):
         targets.append(create_panel_target(target))
     return template.render({**data, 'yaxes': yaxes, 'targets': targets})
diff --git a/test/test_aggregrate.py b/test/test_aggregrate.py
new file mode 100644
index 0000000..ac0df39
--- /dev/null
+++ b/test/test_aggregrate.py
@@ -0,0 +1,177 @@
+from brian_dashboard_manager.grafana.utils.request import TokenRequest
+import responses
+from brian_dashboard_manager.grafana.provision import provision_aggregate, \
+    is_cls_peer
+
+DEFAULT_REQUEST_HEADERS = {
+    "Content-type": "application/json",
+    "Accept": ["application/json"]
+}
+
+
+TEST_INTERFACES = [
+    {
+        "router": "mx1.ath2.gr.geant.net",
+        "name": "xe-1/0/1",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 569,
+        "description": "PHY RESERVED | New OTEGLOBE ATH2-VIE 10Gb LS",
+        "circuits": []
+    },
+    {
+        "router": "mx1.ath2.gr.geant.net",
+        "name": "ge-1/3/7",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 543,
+        "description": "PHY SPARE",
+        "circuits": []
+    },
+    {
+        "router": "mx1.ham.de.geant.net",
+        "name": "xe-2/2/0.13",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 721,
+        "description": "SRV_L2CIRCUIT CUSTOMER WP6T3 WP6T3 #ham_lon2-WP6-GTS_20063 |",  # noqa: E501
+        "circuits": [
+            {
+                "id": 52382,
+                "name": "ham_lon2-WP6-GTS_20063_L2c",
+                "type": "",
+                "status": "operational"
+            }
+        ]
+    },
+    {
+        "router": "mx1.fra.de.geant.net",
+        "name": "ae27",
+        "bundle": [],
+        "bundle-parents": [
+            "xe-10/0/2",
+            "xe-10/3/2",
+            "xe-10/3/3"
+        ],
+        "snmp-index": 760,
+        "description": "LAG CUSTOMER ULAKBIM SRF9940983 |",
+        "circuits": [
+            {
+                "id": 40983,
+                "name": "ULAKBIM AP2 LAG",
+                "type": "",
+                "status": "operational"
+            }
+        ]
+    },
+    {
+        "router": "mx2.zag.hr.geant.net",
+        "name": "xe-2/1/0",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 739,
+        "description": "PHY SPARE",
+        "circuits": []
+    },
+    {
+        "router": "rt1.rig.lv.geant.net",
+        "name": "xe-0/1/5",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 539,
+        "description": "PHY SPARE",
+        "circuits": []
+    },
+    {
+        "router": "srx1.ch.office.geant.net",
+        "name": "ge-0/0/0",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 513,
+        "description": "Reserved for GEANT OC to test Virgin Media link",
+        "circuits": []
+    },
+    {
+        "router": "mx1.par.fr.geant.net",
+        "name": "xe-4/1/4.1",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 1516,
+        "description": "SRV_L2CIRCUIT INFRASTRUCTURE JRA1 JRA1 | #SDX-L2_PILOT-Br52 OF-P3_par   ",  # noqa: E501
+        "circuits": []
+    },
+    {
+        "router": "mx1.lon.uk.geant.net",
+        "name": "lt-1/3/0.61",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 1229,
+        "description": "SRV_IAS INFRASTRUCTURE ACCESS GLOBAL #LON-IAS-RE-Peering | BGP Peering - IAS Side",  # noqa: E501
+        "circuits": []
+    },
+    {
+        "router": "mx1.sof.bg.geant.net",
+        "name": "xe-2/0/5",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 694,
+        "description": "PHY RESERVED | Prime Telecom Sofia-Bucharest 3_4",
+        "circuits": []
+    },
+    {
+        "router": "mx1.sof.bg.geant.net",
+        "name": "xe-2/0/5",
+        "bundle": [],
+        "bundle-parents": [],
+        "snmp-index": 694,
+        "description": "SRV_GLOBAL CUSTOMER HEANET TESTDESCRIPTION |",
+        "circuits": []
+    }
+]
+
+
+def generate_folder(data):
+    return {
+        "id": 555,
+        "uid": data['uid'],
+        "title": data['title'],
+        "url": f"/dashboards/f/{data['uid']}/{data['title'].lower()}",
+        "hasAcl": False,
+        "canSave": True,
+        "canEdit": True,
+        "canAdmin": True,
+        "createdBy": "Anonymous",
+        "created": "2021-02-23T15:33:46Z",
+        "updatedBy": "Anonymous",
+        "updated": "2021-02-23T15:33:46Z",
+        "version": 1
+    }
+
+
+@responses.activate
+def test_provision_aggregate(data_config, mocker, client):
+
+    TEST_DATASOURCE = [{
+        "name": "brian-influx-datasource",
+        "type": "influxdb",
+        "access": "proxy",
+        "url": "http://test-brian-datasource.geant.org:8086",
+        "database": "test-db",
+        "basicAuth": False,
+        "isDefault": True,
+        "readOnly": False
+    }]
+
+    _mocked_create_dashboard = mocker.patch(
+        'brian_dashboard_manager.grafana.provision.create_dashboard')
+    # we dont care about this, tested separately
+    _mocked_create_dashboard.return_value = None
+
+    request = TokenRequest(**data_config, token='test')
+    fake_folder = generate_folder({'uid': 'aggtest', 'title': 'aggtest'})
+    dash = {
+        'predicate': is_cls_peer,
+        'tag': 'cls_peers',
+    }
+    provision_aggregate(request, 'MY FAKE PEERS', fake_folder,
+                        dash, TEST_INTERFACES, TEST_DATASOURCE[0]['name'])
-- 
GitLab