Skip to content
Snippets Groups Projects
Select Git revision
  • e23c01c68bbd3375c919c5b49a56ab9d34e1c03a
  • develop default
  • master protected
  • NGM-30-hover-popup
  • svg-test
  • master-conflicted
  • 0.8
  • 0.7
  • 0.6
  • 0.5
  • 0.4
  • 0.3
  • 0.2
  • 0.1
14 results

conftest.py

Blame
  • conftest.py 2.10 KiB
    import json
    import os
    import tempfile
    from unittest.mock import patch
    
    import pytest
    from fastapi.testclient import TestClient
    
    from mapping_provider import create_app
    from mapping_provider.backends import cache, inventory, correlator
    from .common import load_test_data
    
    
    @pytest.fixture
    def dummy_config():
        return {
            'sentry': {
                'dsn': 'https://token@hostname.geant.org:1111/a/b',
                'environment': 'unit tests'
            },
            'inventory': 'https://inventory.bogus.domain',
            'reporting': 'http://reporting.another-bogus.domain',
            # no rmq param to skip correlator thread
            # 'rmq': {
            #     'brokers': [
            #         'test-noc-alarms01.geant.org',
            #         'test-noc-alarms02.geant.org',
            #         'test-noc-alarms03.geant.org'
            #     ],
            #     'username': 'guest',
            #     'password': 'guest',
            #     'vhost': '/'
            # }
        }
    
    
    @pytest.fixture
    def dummy_config_filename(dummy_config):
        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(json.dumps(dummy_config).encode('utf-8'))
            f.flush()
            yield f.name
    
    
    @pytest.fixture
    def client(dummy_config_filename):
        os.environ['SETTINGS_FILENAME'] = dummy_config_filename
    
        with tempfile.TemporaryDirectory() as tmp_dir:
            cache.init(tmp_dir)  # there's no rmq in config, so workers won't start
    
            cache.set(inventory.INPROV_MAP_SERVICES_CACHE_FILENAME, load_test_data('inprov-services.json'))
            cache.set(inventory.REPORTING_SCID_CURRENT_CACHE_FILENAME, load_test_data('scid-current.json'))
            cache.set(inventory.INPROV_POLLER_INTERFACES_CACHE_FILENAME, load_test_data('poller-interfaces.json'))
            cache.set(correlator.CACHED_CORRELATOR_STATE_FILENAME, load_test_data('correlator-state.json'))
     
            with patch('sentry_sdk.init') as _mock_sentry_init:
                yield TestClient(create_app())
    
    
    @pytest.fixture(autouse=True)
    def run_around_tests():
        assert cache._cache_dir is None  # test env sanity check
        yield
    
        # make sure cache is set to unused before the next test
        cache._cache_dir = None