async def test_load_balancer_cb_open(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: set_settings(mock_client_app_config, retries=1, circuit_breaker_open_failures=10, circuit_breaker_failure_reset_seconds=60, circuit_breaker_open_seconds=60) await init_mock_client_app(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-event-get", "ok") context = create_test_context(mock_client_app_config, "mock_client_event") client = app_client("test_app_connection", context) MockClientSession.set_failure("http://test-host1", 500) for _ in range(20): result = await client.call("test_event_get", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") assert result == [ MockResponseData(value="ok", param="test_param_value", host="http://test-host2", log={ "http://test-host1": 10, "http://test-host2": 20 }) ]
async def test_app_call_invalid_app_connection( monkeypatch, mocker, mock_client_app_config): # noqa: F811 await register_app_connections(mock_client_app_config) context = create_test_context(mock_client_app_config, "mock_client_event") with pytest.raises(AppConnectionNotFound): await app_call("bad_app_connection", event="test_event", datatype=dict, payload="payload", context=context)
async def test_client_session_lifecycle(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: await init_mock_client_app(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-event-get", "ok") assert MockClientSession.session_open context = create_test_context(mock_client_app_config, "mock_client_event") client = app_client("test_app_connection", context) await client.stop() assert MockClientSession.session_open is False
async def test_app_call_list(monkeypatch, mocker, mock_client_app_config): # noqa: F811 await register_app_connections(mock_client_app_config) context = create_test_context(mock_client_app_config, "mock_client_event") result = await app_call_list("test_app_connection", event="test_event", datatype=dict, payload="payload", context=context) assert result == [{ "app_connection": "test_app_connection", "event": "test_event", "payload": "payload" }]
def test_create_test_context(mock_app_config): # noqa: F811 result = create_test_context(mock_app_config, 'mock_event') assert isinstance(result, EventContext) assert result.app == mock_app_config.app assert result.event_name == 'mock_event' assert result.track_ids == { 'track.operation_id': 'test_operation_id', 'track.request_id': 'test_request_id', 'track.request_ts': result.track_ids['track.request_ts'], 'track.session_id': '' } assert result.auth_info == { 'auth_type': AuthType.UNSECURED, 'allowed': 'true' }
async def test_register_app_connections(monkeypatch, mocker, mock_client_app_config): # noqa: F811 await register_app_connections(mock_client_app_config) context = create_test_context(mock_client_app_config, "mock_client_event") client = app_client("test_app_connection", context) assert client.app_config == mock_client_app_config assert client.app_connection == "test_app_connection" assert client.started await stop_app_connections(mock_client_app_config.app_key()) assert client.stopped with pytest.raises(AppConnectionNotFound): client = app_client("test_app_connection", context)
async def test_client_connection_not_found(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: await init_mock_client_app(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-event-get", "ok") context = create_test_context(mock_client_app_config, "mock_client_event") with pytest.raises(AppConnectionNotFound): await app_call("test_app_connection", event="invalid_event", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value")
async def test_load_balancer_next_host(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: await init_mock_client_app(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-event-get", "ok") context = create_test_context(mock_client_app_config, "mock_client_event") result = await app_call("test_app_connection", event="test_event_get", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") assert result == MockResponseData(value="ok", param="test_param_value", host="http://test-host1", log={"http://test-host1": 1}) result = await app_call("test_app_connection", event="test_event_get", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") assert result == MockResponseData(value="ok", param="test_param_value", host="http://test-host2", log={ "http://test-host1": 1, "http://test-host2": 1 }) result = await app_call("test_app_connection", event="test_event_get", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") assert result == MockResponseData(value="ok", param="test_param_value", host="http://test-host1", log={ "http://test-host1": 2, "http://test-host2": 1 })
async def test_client_post(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: await init_mock_client_app(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-event-post", "ok") context = create_test_context(mock_client_app_config, "mock_client_event") result = await app_call_list("test_app_connection", event="test_event_post", datatype=MockResponseData, payload=MockPayloadData("payload"), context=context, test_param="test_param_value") assert result == [ MockResponseData(value="payload ok", param="test_param_value", host="http://test-host1", log={"http://test-host1": 1}) ]
async def test_load_balancer_no_hosts_available(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: set_settings(mock_client_app_config, retries=1, circuit_breaker_open_failures=10, circuit_breaker_failure_reset_seconds=60, circuit_breaker_open_seconds=60) await init_mock_client_app(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-event-get", "ok") context = create_test_context(mock_client_app_config, "mock_client_event") MockClientSession.set_failure("http://test-host1", 500) MockClientSession.set_failure("http://test-host2", 500) client = app_client("test_app_connection", context) for _ in range(10): try: await client.call("test_event_get", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") except: # noqa: E722 pass # Opening circuit breakers assert MockClientSession.call_log == { "http://test-host1": 10, "http://test-host2": 10 } with pytest.raises(ClientLoadBalancerException): await client.call("test_event_get", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") assert MockClientSession.call_log == { "http://test-host1": 10, "http://test-host2": 10 }
async def test_login_response_not_recognized(monkeypatch, client_app_config): # noqa: F811 def mock_client_calls(module, context: EventContext): monkeypatch.setattr(module, "app_call", custom_app_call) monkeypatch.setattr(module, "app_call_list", custom_app_call_list) monkeypatch.setattr(module.auth, "validate_token", MockAuthInfo.mock_invalid_token) context = create_test_context( client_app_config, "count_and_save", auth_info={'payload': b64encode(b'test-user:test-password').decode()} ) with pytest.raises(Unauthorized): await execute_event( app_config=client_app_config, event_name='count_and_save', payload=None, context=context, mocks=[mock_client_calls])
async def test_load_balancer_unauthorized(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: set_settings(mock_client_app_config, retries=0) await init_mock_client_app(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-event-get", "ok") context = create_test_context(mock_client_app_config, "mock_client_event") MockClientSession.set_failure("http://test-host1", 401) with pytest.raises(Unauthorized): await app_call("test_app_connection", event="test_event_get", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") assert MockClientSession.call_log == {"http://test-host1": 1}
async def test_query_item(monkeypatch, client_app_config): # noqa: F811 def mock_client_calls(module, context: EventContext): monkeypatch.setattr(module, "app_call", custom_app_call) monkeypatch.setattr(module, "app_call_list", custom_app_call_list) monkeypatch.setattr(module.auth, "validate_token", MockAuthInfo.mock_validate_token) context = create_test_context( client_app_config, "count_and_save", auth_info={'payload': b64encode(b'test-user:test-password').decode()} ) result = await execute_event( app_config=client_app_config, event_name='count_and_save', payload=None, context=context, mocks=[mock_client_calls]) assert result == CountAndSaveResult(count=2, save_path='test_path')
async def test_client_app_plugin(monkeypatch, mock_client_app_config, mock_auth): async with MockClientSession.lock: await init_mock_client_app_plugin(apps_client_module, monkeypatch, mock_auth, mock_client_app_config, "test-plugin", "test-event-plugin", "ok") context = create_test_context(mock_client_app_config, "mock_client_event") context.auth_info = { "auth_type": AuthType.BASIC, "payload": "user:pass" } result = await app_call("test_app_plugin_connection", event="test_event_plugin", datatype=MockResponseData, payload=None, context=context, test_param="test_param_value") assert result == MockResponseData(value="ok", param="test_param_value", host="http://test-host1", log={"http://test-host1": 1})
def context(): app_config = config('plugins/ops/log-streamer/config/plugin-config.json') return create_test_context(app_config, "log_reader")