Exemplo n.º 1
0
    async def test_purge_error_storage_response(self, conf, expected_return):
        """Test that purge_data logs error when storage purge returns an error response"""
        @asyncio.coroutine
        def mock_audit_info():
            return ""

        mockStorageClientAsync = MagicMock(spec=StorageClientAsync)
        mockAuditLogger = AuditLogger(mockStorageClientAsync)

        with patch.object(FledgeProcess, '__init__'):
            with patch.object(mockAuditLogger, "__init__", return_value=None):
                p = Purge()
                p._logger = logger
                p._logger.info = MagicMock()
                p._logger.error = MagicMock()
                p._storage_async = MagicMock(spec=StorageClientAsync)
                p._readings_storage_async = MagicMock(
                    spec=ReadingsStorageClientAsync)
                audit = p._audit
                with patch.object(
                        p._storage_async,
                        "query_tbl_with_payload",
                        return_value=q_result('streams')) as patch_storage:
                    with patch.object(p._readings_storage_async,
                                      'purge',
                                      side_effect=self.store_purge):
                        with patch.object(audit,
                                          'information',
                                          return_value=mock_audit_info()):
                            assert expected_return == await p.purge_data(conf)
                assert patch_storage.called
                assert 1 == patch_storage.call_count
Exemplo n.º 2
0
    async def test_purge_data(self, conf, expected_return, expected_calls):
        """Test that purge_data calls Storage's purge with defined configuration"""
        @asyncio.coroutine
        def mock_audit_info():
            return ""

        mockStorageClientAsync = MagicMock(spec=StorageClientAsync)
        mockAuditLogger = AuditLogger(mockStorageClientAsync)

        with patch.object(FledgeProcess, '__init__'):
            with patch.object(mockAuditLogger, "__init__", return_value=None):
                p = Purge()
                p._logger = logger
                p._logger.info = MagicMock()
                p._logger.error = MagicMock()
                p._storage_async = MagicMock(spec=StorageClientAsync)
                p._readings_storage_async = MagicMock(
                    spec=ReadingsStorageClientAsync)
                audit = p._audit

                with patch.object(
                        p._storage_async,
                        "query_tbl_with_payload",
                        return_value=q_result('streams')) as patch_storage:
                    with patch.object(p._readings_storage_async,
                                      'purge',
                                      side_effect=self.store_purge
                                      ) as mock_storage_purge:
                        with patch.object(
                                audit,
                                'information',
                                return_value=mock_audit_info()) as audit_info:
                            # Test the positive case when all if conditions in purge_data pass
                            assert expected_return == await p.purge_data(conf)
                            assert audit_info.called
                            args, kwargs = mock_storage_purge.call_args
                            assert kwargs == expected_calls
                assert patch_storage.called
                assert 1 == patch_storage.call_count
                args, kwargs = patch_storage.call_args
                assert (
                    'streams',
                    '{"aggregate": {"operation": "min", "column": "last_object"}}'
                ) == args
Exemplo n.º 3
0
    async def test_purge_data_invalid_conf(self, conf, expected_error_key):
        """Test that purge_data raises exception when called with invalid configuration"""
        @asyncio.coroutine
        def mock_audit_info():
            return ""

        mockStorageClientAsync = MagicMock(spec=StorageClientAsync)
        mockAuditLogger = AuditLogger(mockStorageClientAsync)

        with patch.object(FledgeProcess, '__init__'):
            with patch.object(mockAuditLogger, "__init__", return_value=None):
                p = Purge()
                p._logger = logger
                p._logger.info = MagicMock()
                p._logger.error = MagicMock()
                p._storage_async = MagicMock(spec=StorageClientAsync)
                p._readings_storage_async = MagicMock(
                    spec=ReadingsStorageClientAsync)
                audit = p._audit

                with patch.object(
                        p._storage_async,
                        "query_tbl_with_payload",
                        return_value=q_result('streams')) as patch_storage:
                    with patch.object(p._readings_storage_async,
                                      'purge',
                                      side_effect=self.store_purge):
                        with patch.object(audit,
                                          'information',
                                          return_value=mock_audit_info()):
                            # Test the code block when purge failed because of invalid configuration
                            await p.purge_data(conf)
                            p._logger.error.assert_called_with(
                                'Configuration item {} bla should be integer!'.
                                format(expected_error_key))
                assert patch_storage.called
                assert 1 == patch_storage.call_count
Exemplo n.º 4
0
    async def test_write_statistics(self):
        """Test that write_statistics calls update statistics with defined keys and value increments"""
        @asyncio.coroutine
        def mock_s_update():
            return ""

        mockStorageClientAsync = MagicMock(spec=StorageClientAsync)
        mockAuditLogger = AuditLogger(mockStorageClientAsync)
        with patch.object(FledgeProcess, '__init__'):
            with patch.object(Statistics,
                              '_load_keys',
                              return_value=mock_s_update()):
                with patch.object(
                        Statistics, 'update',
                        return_value=mock_s_update()) as mock_stats_update:
                    with patch.object(mockAuditLogger,
                                      "__init__",
                                      return_value=None):
                        p = Purge()
                        p._storage_async = mockStorageClientAsync
                        await p.write_statistics(1, 2)
                        mock_stats_update.assert_has_calls(
                            [call('PURGED', 1),
                             call('UNSNPURGED', 2)])