Skip to content
Snippets Groups Projects
Select Git revision
  • 66ab9fb164561f76a4c14dfd38e189fce76125f4
  • python3 default protected
  • feature/exabgp_support2
  • feature/exabgp_support2.bgpextcommunity
  • feature/exabgp_support2.django4.2
  • fix/existingcheck_honor_fragtype
  • feature/python3-authz_netmask
  • feature/authz_netmask
  • fix/wrong_ratelimit_stats
  • feature/requirements_version_update2024-01
  • feature/split_celery
  • feature/improved-warning-mails
  • fix/reenable_expireset_via_restapi
  • feature/admin_user_delete_with_owned_rule_reassigning1
  • feature/admin_user_delete_with_owned_rule_reassigning
  • feature/branded_doc
  • fix/forked_snmp_polling_worker_exit_issue
  • fix/false_user_activation_error
  • feature/exabgp_with_docker-compose
  • fix/prefix_overlap_handling
  • fix/js_security_issues-a
  • save1
  • rpm-1.5-7
  • working1
  • myv1.6
  • t12b1
  • v1.5_newnew2
  • merged_final
  • v1.5_newnew
  • startstop_old
  • myadd2
  • tomas3
  • merge_jra2t6_and_RESTAPI
  • mytomas2
  • mynew1
  • new_jra2t6
  • v1.5_final
  • fod16_ruleroutes-merged_old
  • merged_new
  • v1.6_new_old
  • v1.5_new_old_follower
41 results

tasks.py

Blame
  • test_parse_router.py 2.38 KiB
    import json
    import os
    from unittest.mock import patch, MagicMock
    
    from jsonschema import validate
    import pytest
    
    from resource_management.hardware.router \
        import load_line_cards, LINE_CARDS_LIST_SCHEMA
    
    # another possible approach: try to mock the netconf responses
    # directly and use the code from:
    # https://github.com/GIC-de/Juniper-PyEZ-Unit-Testing
    
    DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
    
    
    def _load_json_data(filename):
        with open(os.path.join(DATA_DIR, filename)) as f:
            return json.loads(f.read())
    
    
    class _namespace(object):
        pass
    
    
    HOSTNAMES = [
        'vmx',
        'mx1.lab.office.geant.net',
        'mx2.lab.office.geant.net',
        'mx3.lab.office.geant.net',
        'mx4.lab.office.geant.net',
        'mx5.lab.office.geant.net',
    ]
    
    
    def pytest_generate_tests(metafunc):
        metafunc.parametrize("router_name", HOSTNAMES)
    
    
    @pytest.fixture
    def mocked_pyez_vmx(router_name):
    
        def _table_data_to_iter(json_data_filename):
            # simple simulation of how pyez renders
            # a table as an iterable
            #
            # datafile should be something formatted as
            # if (or actually captured) from a pyez table
            # object's to_json() method
            for name, data in _load_json_data(json_data_filename).items():
                o = _namespace()
                o.name = o.key = name
                for k, v in data.items():
                    setattr(o, k, v)
                yield o
    
        m = 'resource_management.hardware.router'
        with patch(f'{m}.PhyPortTable') as PhyPortTable:
            with patch(f'{m}.FpcHwTable') as FpcHwTable:
                with patch(f'{m}.Device') as Device:
                    phy_port_table = MagicMock()
                    phy_port_table.__iter__.return_value \
                        = _table_data_to_iter(f'{router_name}-PhyPortTable.json')
                    PhyPortTable.return_value = phy_port_table
    
                    fpc_hw_table = MagicMock()
                    fpc_hw_table.__iter__.return_value \
                        = _table_data_to_iter(f'{router_name}-FpcHwTable.json')
                    FpcHwTable.return_value = fpc_hw_table
    
                    yield  # stay in this block until caller context exits
    
    
    def test_load_line_cards(mocked_pyez_vmx):
    
        fpcs = load_line_cards(
            hostname='bogus', username='bogus', key_filename='no file')
        fpcs = list(fpcs)
        validate(fpcs, LINE_CARDS_LIST_SCHEMA)
        assert len(fpcs) > 0  # because we know the test data is non-empty