Skip to content
Snippets Groups Projects
test_grafana_datasource.py 4.36 KiB
import json
import responses
from brian_dashboard_manager.grafana import datasource, provision
from brian_dashboard_manager.grafana.utils.request import AdminRequest


@responses.activate
def test_get_datasources(data_config):

    BODY = []

    request = AdminRequest(**data_config)

    responses.add(
        method=responses.GET,
        url=request.BASE_URL + 'api/datasources', json=BODY)

    data = datasource.get_datasources(request)
    assert data == BODY


@responses.activate
def test_get_missing_datasource_definitions(data_config):
    # this only retrieves data from the filesystem and checks against
    # what's configured in grafana.. just make sure
    # we cover the part for fetching datasources

    request = AdminRequest(**data_config)

    responses.add(method=responses.GET, url=request.BASE_URL +
                  'api/datasources', json={})

    dir = '/tmp/dirthatreallyshouldnotexistsousealonganduniquestring'
    # it returns a generator, so iterate :)
    for data in datasource.get_missing_datasource_definitions(request, dir):
        pass


def test_datasource_provisioned():
    val = datasource._datasource_provisioned({}, [])
    assert val

    val = datasource._datasource_provisioned({'id': 1}, [])
    assert val is False

    val = datasource._datasource_provisioned({'id': 1, "name": 'testcase2'},
                                             [{'id': -1, 'name': 'testcase1'},
                                              {'id': 1, 'name': 'testcase1'}])
    assert val is False

    val = datasource._datasource_provisioned({'id': 1},
                                             [{'id': -1, 'name': 'testcase1'},
                                              {'id': 1, 'name': 'testcase2'}])
    assert val

    val = datasource._datasource_provisioned({'id': 2, "name": 'testcase2'},
                                             [{'id': -1, 'name': 'testcase1'},
                                              {'id': 1, 'name': 'testcase1'},
                                              {'id': 2, 'name': 'testcase2'}])
    assert val


@responses.activate
def test_create_prod_datasource(data_config):
    ORG_ID = 1

    BODY = {
        "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
    }

    request = AdminRequest(**data_config)

    def post_callback(request):
        body = json.loads(request.body)
        result = {
            'datasource': {
                'id': 1,
                'orgId': ORG_ID,
                'type': 'graphite',
                'typeLogoUrl': '',
                'password': '',
                'user': '',
                'basicAuthUser': '',
                'basicAuthPassword': '',
                'withCredentials': False,
                'jsonData': {},
                'secureJsonFields': {},
                'version': 1
            },
            'id': 1,
            'message': 'Datasource added',
            'name': body['name']
        }
        result['datasource'].update(body)
        return 200, {}, json.dumps(result)

    responses.add_callback(
        method=responses.POST,
        url=request.BASE_URL + 'api/datasources',
        callback=post_callback)

    data = provision.create_datasource(
        request, BODY, datasources=data_config['datasources'])

    datasource_type = data['datasource']['type']
    datasource_config_url = data_config['datasources'][datasource_type]['url']
    assert data['datasource']['url'] == datasource_config_url


@responses.activate
def test_create_prod_datasource_fails(data_config):
    BODY = {
        "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
    }

    request = AdminRequest(**data_config)

    responses.add_callback(
        method=responses.POST,
        url=request.BASE_URL + 'api/datasources',
        callback=lambda f: (400, {}, ''))

    data = provision.create_datasource(
        request, BODY, datasources=data_config['datasources'])

    # if an error occured when provisioning a datasource, we log the response
    # but return None
    assert data is None