import responses import json 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": [] } ] 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(data_config, mocker, client): def get_callback(request): return 200, {}, json.dumps(TEST_INTERFACES) responses.add_callback( method=responses.GET, url=f"http://{data_config['inventory_provider']}/poller/interfaces", callback=get_callback) def folder_get(request): return 200, {}, json.dumps([]) responses.add_callback( method=responses.GET, url=f"http://{data_config['hostname']}/api/folders", callback=folder_get) def folder_post(request): data = json.loads(request.body) return 200, {}, json.dumps(generate_folder(data)) responses.add_callback( method=responses.POST, url=f"http://{data_config['hostname']}/api/folders", callback=folder_post) def home_dashboard(request): return 200, {}, json.dumps([]) responses.add_callback( method=responses.GET, url=f"http://{data_config['hostname']}/api/search?query=Home", callback=home_dashboard) 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 }] def datasources(request): return 200, {}, json.dumps(TEST_DATASOURCE) responses.add_callback( method=responses.GET, url=f"http://{data_config['hostname']}/api/datasources", callback=datasources) PROVISIONED_ORGANIZATION = { 'name': data_config['organizations'][0], 'id': 0 } EXISTING_ORGS = [{**org, 'id': i + 1} for i, org in enumerate(data_config['organizations'][1:])] _mocked_get_organizations = mocker.patch( 'brian_dashboard_manager.grafana.provision.get_organizations') # all organizations are provisioned except the first one. _mocked_get_organizations.return_value = EXISTING_ORGS.copy() _mocked_create_organization = mocker.patch( 'brian_dashboard_manager.grafana.provision.create_organization') # spoof creating first organization _mocked_create_organization.return_value = PROVISIONED_ORGANIZATION _mocked_delete_expired_api_tokens = mocker.patch( 'brian_dashboard_manager.grafana.provision.delete_expired_api_tokens') # we dont care about this, , tested separately _mocked_delete_expired_api_tokens.return_value = None _mocked_create_api_token = mocker.patch( 'brian_dashboard_manager.grafana.provision.create_api_token') _mocked_create_api_token.return_value = { 'key': 'testtoken', 'id': 0} # api token _mocked_create_datasource = mocker.patch( 'brian_dashboard_manager.grafana.provision.create_datasource') # we dont care about this, just mark it created _mocked_create_datasource.return_value = True _mocked_get_dashboard_definitions = mocker.patch( 'brian_dashboard_manager.grafana.provision.get_dashboard_definitions') UID = 1 ID = 1 VERSION = 1 TITLE = 'testdashboard' dashboard = {'id': ID, 'uid': UID, 'title': TITLE, 'version': VERSION} _mocked_get_dashboard_definitions.return_value = [ dashboard # test dashboard ] _mocked_create_dashboard = mocker.patch( 'brian_dashboard_manager.grafana.provision.create_dashboard') # we dont care about this, just mark it created # we dont care about this, tested separately _mocked_create_dashboard.return_value = None _mocked_delete_api_token = mocker.patch( 'brian_dashboard_manager.grafana.provision.delete_api_token') # we dont care about this, tested separately _mocked_delete_api_token.return_value = None response = client.get('/update/', headers=DEFAULT_REQUEST_HEADERS) assert response.status_code == 200 data = json.loads(response.data.decode('utf-8'))['data'] assert data == EXISTING_ORGS + [PROVISIONED_ORGANIZATION]