Skip to content
Snippets Groups Projects
Select Git revision
  • e955e9f574a83f59296e79ad7dae331921b35888
  • develop default protected
  • validate_l2_circuit
  • master protected
  • feature/NAT-1797-netbox-migration
  • authorship-fix-from-develop
  • 1048-service-config-backfilling
  • feature/nat-1211-edgeport-lacp-xmit
  • fix/nat-1120-sdp-validation
  • NAT-1154-import-edge-port-update
  • fix/l3-imports
  • feature/10GGBS-NAT-980
  • fix/NAT-1009/fix-redeploy-base-config-if-there-is-a-vprn
  • 4.27
  • 4.26
  • 4.25
  • 4.24
  • 4.23
  • 4.22
  • 4.21
  • 4.20
  • 4.19
  • 4.18
  • 4.17
  • 4.16
  • 4.15
  • 4.14
  • 4.13
  • 4.12
  • 4.11
  • 4.10
  • 4.8
  • 4.5
33 results

build-docs.sh

Blame
  • test_grafana_dashboard.py 5.56 KiB
    import pytest
    import json
    import requests
    import responses
    from brian_dashboard_manager.grafana import dashboard, provision
    from brian_dashboard_manager.grafana.utils.request import TokenRequest
    
    
    @responses.activate
    def test_get_dashboard(data_config):
    
        UID = 1
    
        request = TokenRequest(**data_config, token='test')
    
        responses.add_callback(
            method=responses.GET,
            url=request.BASE_URL +
            f'api/dashboards/uid/{UID}',
            callback=lambda f: (404, {}, ''))
    
        data = dashboard._get_dashboard(request, UID)
        assert data is None
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID+1}',
            json={'uid': 1})
    
        data = dashboard._get_dashboard(request, UID + 1)
        assert data['uid'] == 1
    
    
    @responses.activate
    def test_delete_dashboards(data_config):
        UID = 1
        dashboards = [{'uid': UID}]
    
        request = TokenRequest(**data_config, token='test')
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json=dashboards[0])
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            json=dashboards)
    
        def delete_callback(request):
            uid = request.path_url.split('/')[-1]
            assert int(uid) == UID
            return 200, {}, json.dumps({'message': 'Dashboard has been deleted.'})
    
        responses.add_callback(
            method=responses.DELETE,
            url=request.BASE_URL +
            f'api/dashboards/uid/{UID}',
            callback=delete_callback)
    
        data = dashboard.delete_dashboards(request)
        assert data is True
    
        responses.add_callback(
            method=responses.DELETE,
            url=request.BASE_URL +
            f'api/dashboards/uid/{UID+1}',
            callback=lambda f: (400, {}, ''))
    
        with pytest.raises(requests.HTTPError):
            data = dashboard._delete_dashboard(request, UID + 1)
    
    
    @responses.activate
    def test_delete_dashboard(data_config):
        UID = 1
        ID = 1
        VERSION = 1
        FOLDER_ID = 1
        TITLE = 'testdashboard'
        dash = {'id': ID, 'uid': UID, 'title': TITLE, 'version': VERSION}
        request = TokenRequest(**data_config, token='test')
    
        responses.add(
            method=responses.DELETE,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json={'message': 'deleted dashboard'})
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json={})
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            json=[dash])
    
        deleted = dashboard.delete_dashboard(request, dash)
        assert deleted
        del dash['uid']
        deleted = dashboard.delete_dashboard(request, dash, FOLDER_ID)
        assert deleted
    
    
    @responses.activate
    def test_search_dashboard(data_config):
        UID = 1
        TITLE = 'testdashboard'
        dashboards = [{'uid': UID, 'title': TITLE}]
    
        request = TokenRequest(**data_config, token='test')
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            json=dashboards)
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json=dashboards[0])
    
        data = dashboard._search_dashboard(
            request, {'title': dashboards[0]['title']})
        assert data['uid'] == UID
    
        data = dashboard._search_dashboard(request, {'title': 'DoesNotExist'})
        assert data is None
    
    
    @responses.activate
    def test_search_dashboard_error(data_config):
        request = TokenRequest(**data_config, token='test')
    
        responses.add_callback(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            callback=lambda f: (400, {}, ''))
    
        data = dashboard._search_dashboard(request, {'title': 'DoesNotExist'})
        assert data is None
    
    
    @responses.activate
    def test_create_dashboard(data_config):
        UID = 1
        ID = 1
        VERSION = 1
        TITLE = 'testdashboard'
        dashboard = {'id': ID, 'uid': UID, 'title': TITLE, 'version': VERSION}
        request = TokenRequest(**data_config, token='test')
    
        responses.add(
            method=responses.GET,
            url=request.BASE_URL + f'api/dashboards/uid/{UID}',
            json={'dashboard': dashboard})
    
        responses.add_callback(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            callback=lambda f: (400, {}, ''))
    
        def post_callback(request):
            body = json.loads(request.body)
            return 200, {}, json.dumps(body['dashboard'])
    
        responses.add_callback(
            method=responses.POST,
            url=request.BASE_URL + 'api/dashboards/db',
            callback=post_callback)
    
        data = provision.create_dashboard(request, dashboard)
        assert data == dashboard
    
    
    @responses.activate
    def test_create_dashboard_no_uid_error(data_config):
        ID = 1
        VERSION = 1
        TITLE = 'testdashboard'
        dashboard = {'id': ID, 'title': TITLE, 'version': VERSION}
        request = TokenRequest(**data_config, token='test')
    
        responses.add_callback(
            method=responses.GET,
            url=request.BASE_URL + 'api/search',
            callback=lambda f: (400, {}, ''))
    
        def post_callback(request):
            body = json.loads(request.body)
            # if a dashboard doesn't have an UID, the ID should not be sent to
            # grafana.
            assert 'id' not in body['dashboard']
    
            # have already tested a successful response, respond with error here.
            return 400, {}, '{}'
    
        responses.add_callback(
            method=responses.POST,
            url=request.BASE_URL + 'api/dashboards/db',
            callback=post_callback)
    
        data = provision.create_dashboard(request, dashboard)
        assert data is None