Exemplo n.º 1
0
def test_handle_openshift_sdn_config(request):
    handler = None
    try:
        bigip = MockBigIp()
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)

        obj = {}
        obj['openshift-sdn'] = {
            'vxlan-name': 'vxlan0',
            'vxlan-node-ips': ['198.162.0.1', '198.162.0.2']
        }

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        r = bigipconfigdriver._parse_config(config_file)
        try:
            bigipconfigdriver._handle_openshift_sdn_config(r)
        except:
            assert 0

    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
Exemplo n.º 2
0
def test_handle_openshift_sdn_config_missing_vxlan_node_ips(request):
    handler = None
    try:
        bigip = MockBigIp()
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)

        obj = {}
        obj['openshift-sdn'] = {'vxlan-name': 'vxlan0'}

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        r = bigipconfigdriver._parse_config(config_file)
        with pytest.raises(bigipconfigdriver.ConfigError):
            bigipconfigdriver._handle_openshift_sdn_config(r)
    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
Exemplo n.º 3
0
def test_handle_global_config_string_verify_interval(request):
    handler = None
    try:
        bigip = MockBigIp()
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)

        obj = {"global": {"log-level": "ERROR", "verify-interval": "hundred"}}

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        r = bigipconfigdriver._parse_config(config_file)
        verify_interval, level = bigipconfigdriver._handle_global_config(r)
        assert verify_interval == bigipconfigdriver.DEFAULT_VERIFY_INTERVAL
        assert level == logging.ERROR

    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
Exemplo n.º 4
0
def test_handle_bigip_config_missing_partitions(request):
    handler = None
    try:
        bigip = MockBigIp()
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)

        obj = {}
        obj['bigip'] = {
            'username': '******',
            'password': '******',
            'url': 'http://10.10.10.10:443',
            'partitions': []
        }

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        r = bigipconfigdriver._parse_config(config_file)
        with pytest.raises(bigipconfigdriver.ConfigError):
            bigipconfigdriver._handle_bigip_config(r)
    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
Exemplo n.º 5
0
def test_handle_global_config(request):
    handler = None
    try:
        bigip = MockBigIp()
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)

        obj = {}
        obj['global'] = {'log-level': 'WARNING', 'verify-interval': 10}

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        r = bigipconfigdriver._parse_config(config_file)
        verify_interval, level = bigipconfigdriver._handle_global_config(r)
        assert verify_interval == 10
        assert level == logging.WARNING

    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
Exemplo n.º 6
0
def test_handle_global_config_number_log_level(request):
    handler = None
    try:
        bigip = MockBigIp()
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)

        obj = {"global": {"log-level": 55, "verify-interval": 100}}

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        r = bigipconfigdriver._parse_config(config_file)
        verify_interval, level = bigipconfigdriver._handle_global_config(r)
        assert verify_interval == 100
        assert level == bigipconfigdriver.DEFAULT_LOG_LEVEL

    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
Exemplo n.º 7
0
def test_confighandler_backoff(request):
    def cb():
        pass

    try:
        handler = bigipconfigdriver.ConfigHandler({}, {}, 0.25)
        backoff = handler.retry_backoff
        handler._backoff_timer = .025
        handler._max_backoff_time = .1

        backoff(cb)
        # first call doubles _backoff_timer
        assert handler._backoff_timer == .05
        backoff(cb)
        # second call doubles _backoff_timer
        assert handler._backoff_timer == .1
        backoff(cb)
        # hit _max_backoff_time so _backoff_timer does not increase
        assert handler._backoff_timer == .1

    finally:
        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
        assert handler._interval.is_running() is False
Exemplo n.º 8
0
def test_confighandler_reset(request):
    handler = None
    try:
        obj = {}
        obj['services'] = []
        obj['services'].append({'field': 8080})
        obj['services'].append({'field': 9090})
        obj['services'].append({'field': 10101})
        obj['global'] = {'verify-interval': 0}

        bigip = MockBigIp(obj)
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)
        # give the thread an opportunity to spin up
        time.sleep(0)

        assert bigip.calls == 0

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        assert handler._thread.is_alive() is True

        handler.notify_reset()
        time.sleep(0.1)
        assert bigip.calls == 1

        handler.notify_reset()
        time.sleep(0.1)
        assert bigip.calls == 2

        handler.notify_reset()
        time.sleep(0.1)
        assert bigip.calls == 3

        # in the failure case we'll respond with a notify_reset to try again
        # therefore, we'll tick twice for this test case
        bigip._fail = True
        # set the backoff_timer for quick testing
        handler._backoff_timer = .01

        handler.notify_reset()
        time.sleep(0.1)
        assert bigip.calls == 5
        # backoff_timer is set to one after a clean run
        assert handler._backoff_timer == 1
    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
Exemplo n.º 9
0
def test_confighandler_checkpointstopafterfailure(request):
    handler = None
    try:
        obj = {}
        obj['services'] = []
        obj['services'].append({'field': 8080})
        obj['services'].append({'field': 9090})
        obj['services'].append({'field': 10101})

        event = threading.Event()
        bigip = MockBigIp(obj, fail=True, notify_event=event, notify_after=5)
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0.25)
        # give the thread an opportunity to spin up
        time.sleep(0)

        assert bigip.calls == 0

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        assert handler._thread.is_alive() is True

        assert handler._interval.is_running() is False

        # get rid of the real notify reset so we only do_reset once in
        # this test
        def p():
            pass

        handler.notify_reset = p
        handler._condition.acquire()
        handler._pending_reset = True
        handler._condition.notify()
        handler._condition.release()
        time.sleep(0.1)

        # should be false here because an invalid config stops the interval
        assert handler._interval.is_running() is False
    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
        assert handler._interval.is_running() is False
Exemplo n.º 10
0
def test_confighandler_checkpoint(request):
    handler = None
    try:
        obj = {}
        obj['services'] = []
        obj['services'].append({'field': 8080})
        obj['services'].append({'field': 9090})
        obj['services'].append({'field': 10101})
        obj['global'] = {'verify-interval': 0.25}

        event = threading.Event()
        bigip = MockBigIp(obj, notify_event=event, notify_after=5)
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0.25)
        # give the thread an opportunity to spin up
        time.sleep(0)

        assert bigip.calls == 0

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        assert handler._thread.is_alive() is True

        assert handler._interval.is_running() is False
        handler.notify_reset()
        time.sleep(0.1)
        assert handler._interval.is_running() is True

        event.wait(30)
        assert event.is_set() is True
    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False
        assert handler._interval.is_running() is False
Exemplo n.º 11
0
def test_confighandler_lifecycle():
    handler = None
    try:
        bigip = MockBigIp()
        handler = bigipconfigdriver.ConfigHandler('/tmp/config', bigip, 0)

        assert handler._thread in threading.enumerate()
        assert handler._thread.is_alive() is True
        assert handler._pending_reset is False
        assert handler._stop is False
        assert handler._bigip == bigip
        assert handler._config_file == '/tmp/config'
    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread not in threading.enumerate()
        assert handler._thread.is_alive() is False
        assert handler._stop is True
Exemplo n.º 12
0
def test_parse_config(request):
    handler = None
    try:
        bigip = MockBigIp()
        config_template = Template('/tmp/config.$pid')
        config_file = config_template.substitute(pid=os.getpid())

        handler = bigipconfigdriver.ConfigHandler(config_file, bigip, 0)

        r = bigipconfigdriver._parse_config(config_file)
        assert r is None

        obj = {}
        obj['field1'] = 'one'
        obj['field_string'] = 'string'
        obj['field_number'] = 10

        with open(config_file, 'w+') as f:

            def fin():
                os.unlink(config_file)

            request.addfinalizer(fin)
            json.dump(obj, f)

        r = bigipconfigdriver._parse_config(config_file)
        assert r is not None
        assert r['field1'] == obj['field1']
        assert r['field_string'] == obj['field_string']
        assert r['field_number'] == obj['field_number']
    finally:
        assert handler is not None

        handler.stop()
        handler._thread.join(30)
        assert handler._thread.is_alive() is False