Пример #1
0
def simple_device():
    """Fixture to return a new ``synse_grpc.api.V3Device`` instance for testing."""

    return api.V3Device(
        timestamp='2019-04-22T13:30:00Z',
        id='test-device-1',
        type='temperature',
        plugin='123',
        info='device for unit tests',
        metadata={
            'foo': 'bar',
        },
        capabilities=api.V3DeviceCapability(mode='r', ),
        tags=[
            api.V3Tag(
                namespace='vapor',
                annotation='unit',
                label='test',
            ),
        ],
        outputs=[
            api.V3DeviceOutput(
                name='temperature',
                type='temperature',
                precision=3,
                scalingFactor=1,
                unit=api.V3OutputUnit(
                    name='celsius',
                    symbol='C',
                ),
            ),
        ],
        sortIndex=0,
    )
Пример #2
0
async def test_scan_sort_ok():
    with asynctest.patch('synse_server.cache.update_device_cache') as mock_update:
        with asynctest.patch('synse_server.cache.get_devices') as mock_get:
            mock_get.return_value = [
                api.V3Device(
                    id='1',
                    type='foo',
                    plugin='abc',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
                api.V3Device(
                    id='2',
                    type='foo',
                    plugin='def',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
                api.V3Device(
                    id='3',
                    type='bar',
                    plugin='abc',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
            ]

            resp = await cmd.scan('default', [['foo']], 'type,plugin,id', force=True)
            assert len(resp) == 3
            assert resp[0]['id'] == '3'
            assert resp[1]['id'] == '1'
            assert resp[2]['id'] == '2'

    mock_update.assert_called_once()
    mock_get.assert_called_once()
    mock_get.assert_called_with('default/foo')
Пример #3
0
    async def test_get_devices_ok(self, mocker, tags, expected):
        dev1 = api.V3Device(id='dev-1')
        dev2 = api.V3Device(id='dev-2')
        dev3 = api.V3Device(id='dev-3')

        # Mock test data
        mocker.patch.dict(
            'aiocache.SimpleMemoryCache._cache', {
                f'{cache.NS_DEVICE}system/id:{dev1.id}': [dev1],
                f'{cache.NS_DEVICE}system/id:{dev2.id}': [dev2],
                f'{cache.NS_DEVICE}system/id:{dev3.id}': [dev3],
                f'{cache.NS_DEVICE}system/type:temperature': [dev1, dev2],
                f'{cache.NS_DEVICE}system/type:humidity': [dev3],
                f'{cache.NS_DEVICE}foo': [dev1],
                f'{cache.NS_DEVICE}default/bar': [dev2],
                f'{cache.NS_DEVICE}default/baz': [dev3],
                f'{cache.NS_DEVICE}vapor/baz': [dev1, dev3],
                f'{cache.NS_DEVICE}vapor/type:fun': [dev2, dev3],
            })

        # --- Test case -----------------------------
        devices = await cache.get_devices(*tags)
        assert len(devices) == expected
Пример #4
0
async def test_scan_invalid_keys():
    with asynctest.patch('synse_server.cache.update_device_cache') as mock_update:
        with asynctest.patch('synse_server.cache.get_devices') as mock_get:
            mock_get.return_value = [
                api.V3Device(
                    id='1',
                    type='foo',
                    plugin='abc',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
                api.V3Device(
                    id='2',
                    type='foo',
                    plugin='def',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
                api.V3Device(
                    id='3',
                    type='bar',
                    plugin='abc',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
            ]

            with pytest.raises(errors.InvalidUsage):
                await cmd.scan('default', [['foo']], 'not-a-key,tags', force=False)

    mock_update.assert_not_called()
    mock_get.assert_called_once()
    mock_get.assert_called_with('default/foo')
Пример #5
0
async def test_info_device_found():
    with asynctest.patch('synse_server.cache.get_device') as mock_get:
        mock_get.return_value = api.V3Device(id='123',
                                             info='foo',
                                             metadata={'a': 'b'},
                                             plugin='xyz',
                                             type='test',
                                             tags=[
                                                 api.V3Tag(
                                                     namespace='default',
                                                     annotation='foo',
                                                     label='bar',
                                                 ),
                                                 api.V3Tag(
                                                     namespace='default',
                                                     label='test',
                                                 ),
                                                 api.V3Tag(label='example', ),
                                             ])

        resp = await cmd.info('123')
        assert resp == {
            'id': '123',
            'alias': '',
            'type': 'test',
            'info': 'foo',
            'metadata': {
                'a': 'b',
            },
            'plugin': 'xyz',
            'tags': [
                'default/foo:bar',
                'default/test',
                'example',
            ],
            'outputs': [],
            'sort_index': 0,
            'timestamp': '',
        }

    mock_get.assert_called_once()
    mock_get.assert_called_with('123')
Пример #6
0
    async def test_update_device_cache_ok(self, mocker, simple_plugin):
        # Mock test data
        mocker.patch.dict('synse_server.plugin.PluginManager.plugins', {
            '123': simple_plugin,
            '456': simple_plugin,
        })

        mock_devices = mocker.patch(
            'synse_grpc.client.PluginClientV3.devices',
            return_value=[
                api.V3Device(tags=[
                    api.V3Tag(namespace='system', annotation='id', label='1'),
                    api.V3Tag(namespace='system',
                              annotation='type',
                              label='temperature'),
                    api.V3Tag(label='foo'),
                    api.V3Tag(namespace='default', label='foo'),
                    api.V3Tag(namespace='default', label='foo'),
                    api.V3Tag(namespace='vapor', label='bar'),
                    api.V3Tag(annotation='unit', label='test'),
                ], ),
                api.V3Device(tags=[
                    api.V3Tag(namespace='system', annotation='id', label='2'),
                    api.V3Tag(namespace='system',
                              annotation='type',
                              label='temperature'),
                    api.V3Tag(label='foo'),
                    api.V3Tag(namespace='default', label='bar'),
                    api.V3Tag(namespace='default', label='foo'),
                ], ),
                api.V3Device(tags=[
                    api.V3Tag(namespace='system', annotation='id', label='3'),
                    api.V3Tag(namespace='system',
                              annotation='type',
                              label='humidity'),
                    api.V3Tag(label='bar'),
                    api.V3Tag(namespace='vapor', label='bar'),
                    api.V3Tag(namespace='vapor', label='test'),
                    api.V3Tag(annotation='unit', label='test'),
                    api.V3Tag(annotation='integration', label='test'),
                ], ),
            ],
        )

        # --- Test case -----------------------------
        assert len(cache.device_cache._cache) == 0

        await cache.update_device_cache()

        assert len(cache.device_cache._cache) == 13
        assert f'{cache.NS_DEVICE}system/id:1' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}system/id:2' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}system/id:3' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}system/type:temperature' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}system/type:humidity' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}foo' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}bar' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}default/foo' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}default/bar' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}vapor/bar' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}vapor/test' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}unit:test' in cache.device_cache._cache
        assert f'{cache.NS_DEVICE}integration:test' in cache.device_cache._cache

        mock_devices.assert_has_calls([
            mocker.call(),
            mocker.call(),
        ])
Пример #7
0
async def test_scan_ok():
    with asynctest.patch('synse_server.cache.update_device_cache') as mock_update:
        with asynctest.patch('synse_server.cache.get_devices') as mock_get:
            mock_get.return_value = [
                api.V3Device(
                    id='1',
                    type='foo',
                    plugin='abc',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ],
                    metadata={
                        'foo': 'bar',
                    },
                ),
                api.V3Device(
                    id='2',
                    type='foo',
                    plugin='def',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
                api.V3Device(
                    id='3',
                    type='bar',
                    plugin='abc',
                    tags=[
                        api.V3Tag(namespace='default', label='foo')
                    ]
                ),
            ]

            resp = await cmd.scan('default', [['foo']], 'plugin,sortIndex,id', force=True)
            assert len(resp) == 3
            assert resp[0]['id'] == '1'
            assert resp[1]['id'] == '3'
            assert resp[2]['id'] == '2'

            assert resp == [
                {
                    'id': '1',
                    'alias': '',
                    'info': '',
                    'type': 'foo',
                    'plugin': 'abc',
                    'tags': [
                        'default/foo',
                    ],
                    'metadata': {
                        'foo': 'bar'
                    }
                },
                {
                    'id': '3',
                    'alias': '',
                    'info': '',
                    'type': 'bar',
                    'plugin': 'abc',
                    'tags': [
                        'default/foo',
                    ],
                    'metadata': {}
                },
                {
                    'id': '2',
                    'alias': '',
                    'info': '',
                    'type': 'foo',
                    'plugin': 'def',
                    'tags': [
                        'default/foo',
                    ],
                    'metadata': {}
                },
            ]

    mock_update.assert_called_once()
    mock_get.assert_called_once()
    mock_get.assert_called_with('default/foo')