mhenc commented on code in PR #27892: URL: https://github.com/apache/airflow/pull/27892#discussion_r1036841925
########## tests/api_internal/test_internal_api_call.py: ########## @@ -0,0 +1,151 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +from __future__ import annotations + +import json +import unittest +from unittest import mock + +import requests + +from airflow.api_internal.internal_api_call import InternalApiConfig, internal_api_call +from airflow.serialization.serialized_objects import BaseSerialization +from tests.test_utils.config import conf_vars + + +class TestInternalApiConfig(unittest.TestCase): + def setUp(self): + InternalApiConfig._initialized = False + + @conf_vars( + { + ("core", "database_access_isolation"): "false", + ("core", "database_api_url"): "http://localhost:8888", + } + ) + def test_get_use_internal_api_disabled(self): + self.assertFalse(InternalApiConfig.get_use_internal_api()) + + @conf_vars( + { + ("core", "database_access_isolation"): "true", + ("core", "database_api_url"): "http://localhost:8888", + } + ) + def test_get_use_internal_api_enabled(self): + self.assertTrue(InternalApiConfig.get_use_internal_api()) + self.assertEqual( + InternalApiConfig.get_internal_api_endpoint(), + "http://localhost:8888/internal/v1/rpcapi", + ) + + +@internal_api_call +def fake_method() -> str: + return "local-call" + + +@internal_api_call +def fake_method_with_params(dag_id: str, task_id: int) -> str: + return f"local-call-with-params-{dag_id}-{task_id}" + + +class TestInternalApiCall(unittest.TestCase): + def setUp(self): + InternalApiConfig._initialized = False + + @conf_vars( + { + ("core", "database_access_isolation"): "false", + ("core", "database_api_url"): "http://localhost:8888", + } + ) + @mock.patch("airflow.api_internal.internal_api_call.requests") + def test_local_call(self, mock_requests): + result = fake_method() + + self.assertEqual(result, "local-call") + mock_requests.post.assert_not_called() + + @conf_vars( + { + ("core", "database_access_isolation"): "true", + ("core", "database_api_url"): "http://localhost:8888", + } + ) + @mock.patch("airflow.api_internal.internal_api_call.requests") + def test_remote_call(self, mock_requests): + response = requests.Response() + response.status_code = 200 + + response._content = json.dumps(BaseSerialization.serialize("remote-call")) + + mock_requests.post.return_value = response + + result = fake_method() + self.assertEqual(result, "remote-call") + expected_data = json.dumps( + { + "jsonrpc": "2.0", + "method": "tests.api_internal.test_internal_api_call.fake_method", + "params": '{"__var": {}, "__type": "dict"}', + } + ) + mock_requests.post.assert_called_once_with( + url="http://localhost:8888/internal/v1/rpcapi", + data=expected_data, + headers={"Content-Type": "application/json"}, + ) + + @conf_vars( + { + ("core", "database_access_isolation"): "true", + ("core", "database_api_url"): "http://localhost:8888", + } + ) + @mock.patch("airflow.api_internal.internal_api_call.requests") + def test_remote_call_with_params(self, mock_requests): Review Comment: Do we really want to do it in this case? We call two different methods fake_method vs fake_method_with_params, ad the assertion there are little big different -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
