Ejemplo n.º 1
0
def test_plugin_shutdown(mocker):
    # GIVEN
    config = copy.deepcopy(cc2650._DEFAULT_CONFIG)
    config['bluetoothAddress']['value'] = config['bluetoothAddress']['default']
    config['connectionTimeout']['value'] = config['connectionTimeout'][
        'default']
    config['shutdownThreshold']['value'] = config['shutdownThreshold'][
        'default']
    config['pollInterval']['value'] = config['pollInterval']['default']
    log_info = mocker.patch.object(cc2650._LOGGER, "info")

    mocker.patch.object(SensorTagCC2650, "__init__", return_value=None)
    mocker.patch.object(SensorTagCC2650, "is_connected", True)
    mocker.patch.object(SensorTagCC2650,
                        "get_char_handle",
                        return_value=0x0000)
    mocker.patch.object(SensorTagCC2650, "disconnect", return_value=None)

    returned_config = cc2650.plugin_init(config)

    # WHEN
    cc2650.plugin_shutdown(returned_config)

    # THEN
    calls = [
        call('SensorTagCC2650 {} Disconnected.'.format(
            config['bluetoothAddress']['value'])),
        call('CC2650 poll plugin shut down.'),
        call('SensorTagCC2650 {} Polling initialized'.format(
            config['bluetoothAddress']['value']))
    ]
    log_info.assert_has_calls(calls, any_order=True)
Ejemplo n.º 2
0
def test_plugin_poll_data_retrieval_error(mocker):
    # GIVEN
    config = copy.deepcopy(cc2650._DEFAULT_CONFIG)
    config['bluetoothAddress']['value'] = config['bluetoothAddress']['default']
    config['connectionTimeout']['value'] = config['connectionTimeout'][
        'default']
    config['shutdownThreshold']['value'] = config['shutdownThreshold'][
        'default']
    config['pollInterval']['value'] = config['pollInterval']['default']
    mocker.patch.object(SensorTagCC2650, "__init__", return_value=None)
    mocker.patch.object(SensorTagCC2650, "is_connected", True)
    mocker.patch.object(SensorTagCC2650,
                        "get_char_handle",
                        return_value=0x0000)
    mocker.patch.object(SensorTagCC2650, "reading_iterations", 1)
    log_exception = mocker.patch.object(cc2650._LOGGER, "exception")
    log_info = mocker.patch.object(cc2650._LOGGER, "info")

    returned_config = cc2650.plugin_init(config)

    # WHEN
    from foglamp.services.south import exceptions
    with pytest.raises(exceptions.DataRetrievalError):
        data = cc2650.plugin_poll(returned_config)

    # THEN
    log_info.assert_called_once_with(
        'SensorTagCC2650 B0:91:22:EA:79:04 Polling initialized')
    log_exception.assert_called_once_with(
        "SensorTagCC2650 B0:91:22:EA:79:04 exception: 'NoneType' object has no attribute 'sendline'"
    )
Ejemplo n.º 3
0
def test_plugin_init(mocker):
    # GIVEN
    config = copy.deepcopy(cc2650._DEFAULT_CONFIG)
    config['bluetoothAddress']['value'] = config['bluetoothAddress']['default']
    config['connectionTimeout']['value'] = config['connectionTimeout'][
        'default']
    config['shutdownThreshold']['value'] = config['shutdownThreshold'][
        'default']
    config['pollInterval']['value'] = config['pollInterval']['default']
    mocker.patch.object(SensorTagCC2650, "__init__", return_value=None)
    mocker.patch.object(SensorTagCC2650, "is_connected", True)
    mocker.patch.object(SensorTagCC2650,
                        "get_char_handle",
                        return_value=0x0000)
    log_info = mocker.patch.object(cc2650._LOGGER, "info")

    # WHEN
    returned_config = cc2650.plugin_init(config)

    # THEN
    assert "characteristics" in returned_config
    assert "tag" in returned_config
    log_info.assert_called_once_with(
        'SensorTagCC2650 {} Polling initialized'.format(
            config['bluetoothAddress']['value']))
Ejemplo n.º 4
0
def test_plugin_reconfigure(mocker):
    # GIVEN
    config = copy.deepcopy(cc2650._DEFAULT_CONFIG)
    config['bluetoothAddress']['value'] = config['bluetoothAddress']['default']
    config['connectionTimeout']['value'] = config['connectionTimeout'][
        'default']
    config['shutdownThreshold']['value'] = config['shutdownThreshold'][
        'default']
    config['pollInterval']['value'] = config['pollInterval']['default']
    log_info = mocker.patch.object(cc2650._LOGGER, "info")

    mocker.patch.object(SensorTagCC2650, "__init__", return_value=None)
    mocker.patch.object(SensorTagCC2650, "is_connected", True)
    mocker.patch.object(SensorTagCC2650,
                        "get_char_handle",
                        return_value=0x0000)
    mocker.patch.object(SensorTagCC2650, "disconnect", return_value=None)
    returned_old_config = cc2650.plugin_init(config)

    # Only bluetoothAddress is changed
    config2 = copy.deepcopy(_NEW_CONFIG)
    config2['bluetoothAddress']['value'] = config2['bluetoothAddress'][
        'default']
    config2['connectionTimeout']['value'] = config2['connectionTimeout'][
        'default']
    config2['shutdownThreshold']['value'] = config2['shutdownThreshold'][
        'default']
    config2['pollInterval']['value'] = config2['pollInterval']['default']

    pstop = mocker.patch.object(cc2650, '_plugin_stop', return_value=True)

    # WHEN
    new_config = cc2650.plugin_reconfigure(returned_old_config, config2)

    # THEN
    for key, value in new_config.items():
        if key in ('characteristics', 'tag', 'restart', 'bluetoothAddress'):
            continue
        assert returned_old_config[key] == value

    # Confirm the new bluetoothAddress
    assert new_config['bluetoothAddress']['value'] == config2[
        'bluetoothAddress']['value']
    assert new_config["restart"] == "yes"
    assert 1 == pstop.call_count

    log_info_call_args = log_info.call_args_list
    assert log_info_call_args[0] == call(
        'SensorTagCC2650 B0:91:22:EA:79:04 Polling initialized')

    args, kwargs = log_info_call_args[1]
    assert 'new config' in args[
        0]  # Assert to check if 'new config' text exists when reconfigured

    assert log_info_call_args[2] == call(
        'SensorTagCC2650 C0:92:23:EB:80:05 Polling initialized')
    assert log_info_call_args[3] == call(
        'Restarting CC2650POLL plugin due to change in configuration keys [bluetoothAddress]'
    )
Ejemplo n.º 5
0
def test_plugin_poll_exception(mocker):
    # GIVEN
    config = copy.deepcopy(cc2650._DEFAULT_CONFIG)
    config['bluetoothAddress']['value'] = config['bluetoothAddress']['default']
    config['connectionTimeout']['value'] = config['connectionTimeout'][
        'default']
    config['shutdownThreshold']['value'] = config['shutdownThreshold'][
        'default']
    config['pollInterval']['value'] = config['pollInterval']['default']
    mocker.patch.object(SensorTagCC2650, "__init__", return_value=None)
    mocker.patch.object(SensorTagCC2650, "is_connected", False)
    mocker.patch.object(SensorTagCC2650,
                        "get_char_handle",
                        return_value=0x0000)
    mocker.patch.object(SensorTagCC2650, "reading_iterations", 1)

    returned_config = cc2650.plugin_init(config)

    # WHEN
    # THEN
    with pytest.raises(RuntimeError):
        cc2650.plugin_poll(returned_config)
Ejemplo n.º 6
0
def test_plugin_poll(mocker):
    # GIVEN
    config = copy.deepcopy(cc2650._DEFAULT_CONFIG)
    config['bluetoothAddress']['value'] = config['bluetoothAddress']['default']
    config['connectionTimeout']['value'] = config['connectionTimeout'][
        'default']
    config['shutdownThreshold']['value'] = config['shutdownThreshold'][
        'default']
    config['pollInterval']['value'] = config['pollInterval']['default']

    log_info = mocker.patch.object(cc2650._LOGGER, "info")
    log_debug = mocker.patch.object(cc2650._LOGGER, "debug")

    mocker.patch.object(SensorTagCC2650, "__init__", return_value=None)
    mocker.patch.object(SensorTagCC2650, "is_connected", True)
    mocker.patch.object(SensorTagCC2650,
                        "get_char_handle",
                        return_value=0x0000)
    mocker.patch.object(SensorTagCC2650, "reading_iterations", 1)

    returned_config = cc2650.plugin_init(config)

    mocker.patch.object(returned_config['tag'],
                        "char_read_hnd",
                        return_value=None)
    mocker.patch.object(returned_config['tag'],
                        "char_write_cmd",
                        return_value=None)
    mocker.patch.object(returned_config['tag'],
                        "hex_temp_to_celsius",
                        return_value=(None, None))
    mocker.patch.object(returned_config['tag'],
                        "hex_lux_to_lux",
                        return_value=None)
    mocker.patch.object(returned_config['tag'],
                        "hex_humidity_to_rel_humidity",
                        return_value=(None, None))
    mocker.patch.object(returned_config['tag'],
                        "hex_pressure_to_pressure",
                        return_value=None)
    mocker.patch.object(returned_config['tag'],
                        "hex_movement_to_movement",
                        return_value=(None, None, None, None, None, None, None,
                                      None, None, None))
    mocker.patch.object(returned_config['tag'],
                        "get_battery_level",
                        return_value=None)

    expected_readings_keys = [
        'temperature', 'luxometer', 'humidity', 'pressure', 'gyroscope',
        'accelerometer', 'magnetometer', 'battery'
    ]

    # WHEN
    data = cc2650.plugin_poll(returned_config)

    # THEN
    assert "characteristics" in returned_config
    assert "tag" in returned_config
    for item in data:
        assert 'asset' in item
        assert item['asset'][20:] in expected_readings_keys
        assert 'key' in item
        assert 'timestamp' in item
        assert 'readings' in item

    assert 1 == log_debug.call_count
    log_info.assert_called_once_with(
        'SensorTagCC2650 B0:91:22:EA:79:04 Polling initialized')