o-nikolas commented on code in PR #77626: URL: https://github.com/apache/airflow/pull/77626#discussion_r4705569406
########## providers/amazon/tests/unit/amazon/aws/executors/aws_lambda/docker/test_app.py: ########## @@ -0,0 +1,197 @@ +# 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 +from unittest import mock + +import pytest + +from airflow.providers.amazon.aws.executors.aws_lambda.docker import app + + +class TestGetQueueUrl: + def test_prefers_airflow_queue_url_env_var(self, monkeypatch): + monkeypatch.setenv( + "AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/airflow-queue", + ) + monkeypatch.setenv( + "QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/fallback-queue", + ) + assert ( Review Comment: Static checks will fail once CI runs: `ruff format` wants this assert (and the one at L48) on one line, and `ruff check` flags `datetime.timezone.utc` in `test_utils.py` (UP017, use `datetime.UTC`). Running `prek run --from-ref main` should fix all of it. --- Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting ########## providers/amazon/tests/unit/amazon/aws/executors/aws_lambda/docker/test_app.py: ########## @@ -0,0 +1,197 @@ +# 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 +from unittest import mock + +import pytest + +from airflow.providers.amazon.aws.executors.aws_lambda.docker import app + + +class TestGetQueueUrl: + def test_prefers_airflow_queue_url_env_var(self, monkeypatch): + monkeypatch.setenv( + "AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/airflow-queue", + ) + monkeypatch.setenv( + "QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/fallback-queue", + ) + assert ( + app.get_queue_url() + == "https://sqs.us-east-1.amazonaws.com/123456789012/airflow-queue" + ) + + def test_falls_back_to_queue_url_env_var(self, monkeypatch): + monkeypatch.delenv("AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", raising=False) + monkeypatch.setenv( + "QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/fallback-queue", + ) + assert ( + app.get_queue_url() + == "https://sqs.us-east-1.amazonaws.com/123456789012/fallback-queue" + ) + + def test_raises_runtime_error_when_no_queue_url_set(self, monkeypatch): + monkeypatch.delenv("AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", raising=False) + monkeypatch.delenv("QUEUE_URL", raising=False) + with pytest.raises(RuntimeError, match="No Queue URL detected"): + app.get_queue_url() + + +class TestGetSqsClient: + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.boto3.client") + def test_creates_boto3_sqs_client(self, mock_boto3_client): + mock_client = mock.MagicMock() + mock_boto3_client.return_value = mock_client + + client = app.get_sqs_client() + + mock_boto3_client.assert_called_once_with("sqs") + assert client == mock_client + + +class TestFetchDagsFromS3: + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.boto3.resource") + def test_downloads_dags_and_skips_directories(self, mock_boto3_resource, monkeypatch): + mock_s3 = mock.MagicMock() + mock_bucket = mock.MagicMock() + mock_boto3_resource.return_value = mock_s3 + mock_s3.Bucket.return_value = mock_bucket + + obj_file = mock.MagicMock() + obj_file.key = "dags/my_dag.py" + + obj_dir = mock.MagicMock() + obj_dir.key = "dags/subdir/" + + mock_bucket.objects.filter.return_value = [obj_file, obj_dir] + + app.fetch_dags_from_s3("s3://test-bucket/dags/") + + mock_s3.Bucket.assert_called_once_with("test-bucket") + mock_bucket.objects.filter.assert_called_once_with(Prefix="dags/") + assert mock_bucket.download_file.call_count == 1 + call_key = mock_bucket.download_file.call_args[0][0] + assert call_key == "dags/my_dag.py" + assert "AIRFLOW__CORE__DAGS_FOLDER" in app.os.environ Review Comment: This one leaks into the rest of the test run: `fetch_dags_from_s3` writes `AIRFLOW__CORE__DAGS_FOLDER` straight into `os.environ`, so every later test in the same worker gets an empty `/tmp/airflow_dags_*` dir as its Dags folder. The assert also can't fail, because the `tests_common` pytest plugin already sets that variable at startup. Could you call `monkeypatch.setenv("AIRFLOW__CORE__DAGS_FOLDER", ...)` before the call (the fixture is already in the signature) so it gets restored, patch `mkdtemp` to return `tmp_path`, and then assert the exact value plus `download_file.assert_called_once_with("dags/my_dag.py", str(tmp_path / "my_dag.py"))`? --- Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting ########## providers/amazon/tests/unit/amazon/aws/executors/aws_lambda/docker/test_app.py: ########## @@ -0,0 +1,197 @@ +# 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 +from unittest import mock + +import pytest + +from airflow.providers.amazon.aws.executors.aws_lambda.docker import app + + +class TestGetQueueUrl: + def test_prefers_airflow_queue_url_env_var(self, monkeypatch): + monkeypatch.setenv( + "AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/airflow-queue", + ) + monkeypatch.setenv( + "QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/fallback-queue", + ) + assert ( + app.get_queue_url() + == "https://sqs.us-east-1.amazonaws.com/123456789012/airflow-queue" + ) + + def test_falls_back_to_queue_url_env_var(self, monkeypatch): + monkeypatch.delenv("AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", raising=False) + monkeypatch.setenv( + "QUEUE_URL", + "https://sqs.us-east-1.amazonaws.com/123456789012/fallback-queue", + ) + assert ( + app.get_queue_url() + == "https://sqs.us-east-1.amazonaws.com/123456789012/fallback-queue" + ) + + def test_raises_runtime_error_when_no_queue_url_set(self, monkeypatch): + monkeypatch.delenv("AIRFLOW__AWS_LAMBDA_EXECUTOR__QUEUE_URL", raising=False) + monkeypatch.delenv("QUEUE_URL", raising=False) + with pytest.raises(RuntimeError, match="No Queue URL detected"): + app.get_queue_url() + + +class TestGetSqsClient: + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.boto3.client") + def test_creates_boto3_sqs_client(self, mock_boto3_client): + mock_client = mock.MagicMock() + mock_boto3_client.return_value = mock_client + + client = app.get_sqs_client() + + mock_boto3_client.assert_called_once_with("sqs") + assert client == mock_client + + +class TestFetchDagsFromS3: + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.boto3.resource") + def test_downloads_dags_and_skips_directories(self, mock_boto3_resource, monkeypatch): + mock_s3 = mock.MagicMock() + mock_bucket = mock.MagicMock() + mock_boto3_resource.return_value = mock_s3 + mock_s3.Bucket.return_value = mock_bucket + + obj_file = mock.MagicMock() + obj_file.key = "dags/my_dag.py" + + obj_dir = mock.MagicMock() + obj_dir.key = "dags/subdir/" + + mock_bucket.objects.filter.return_value = [obj_file, obj_dir] + + app.fetch_dags_from_s3("s3://test-bucket/dags/") + + mock_s3.Bucket.assert_called_once_with("test-bucket") + mock_bucket.objects.filter.assert_called_once_with(Prefix="dags/") + assert mock_bucket.download_file.call_count == 1 + call_key = mock_bucket.download_file.call_args[0][0] + assert call_key == "dags/my_dag.py" + assert "AIRFLOW__CORE__DAGS_FOLDER" in app.os.environ + + +class TestRunAndReport: + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.get_sqs_client") + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.get_queue_url") + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.subprocess.run") + def test_successful_execution_sends_return_code_zero( + self, mock_subprocess_run, mock_get_queue_url, mock_get_sqs_client + ): + mock_proc = mock.MagicMock() + mock_proc.returncode = 0 + mock_proc.stdout = b"Task completed successfully" + mock_subprocess_run.return_value = mock_proc + + mock_get_queue_url.return_value = "https://sqs.us-east-1.amazonaws.com/123/queue" + mock_sqs = mock.MagicMock() + mock_get_sqs_client.return_value = mock_sqs + + app.run_and_report(["airflow", "tasks", "run"], "test_task_key") + + mock_subprocess_run.assert_called_once_with( + ["airflow", "tasks", "run"], + check=False, + shell=False, + stdout=app.subprocess.PIPE, + stderr=app.subprocess.STDOUT, + ) + mock_sqs.send_message.assert_called_once() + sent_body = json.loads(mock_sqs.send_message.call_args[1]["MessageBody"]) + assert sent_body == {"task_key": "test_task_key", "return_code": 0} + + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.get_sqs_client") + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.get_queue_url") + @mock.patch("airflow.providers.amazon.aws.executors.aws_lambda.docker.app.subprocess.run") + def test_subprocess_exception_reports_return_code_one( + self, mock_subprocess_run, mock_get_queue_url, mock_get_sqs_client + ): + mock_subprocess_run.side_effect = OSError("Command not found") + mock_get_queue_url.return_value = "https://sqs.us-east-1.amazonaws.com/123/queue" + mock_sqs = mock.MagicMock() + mock_get_sqs_client.return_value = mock_sqs + + app.run_and_report("echo test", "task_fail_key") Review Comment: nit: this is the only test with a string command, but it doesn't check that it ran with `shell=True`. Since `shell=isinstance(command, str)` is the one bit of logic in that call, could you add `mock_subprocess_run.assert_called_once_with("echo test", check=False, shell=True, stdout=..., stderr=...)` here? --- Drafted-by: Kiro (claude-opus-5.5); reviewed by @o-nikolas before posting ########## providers/amazon/tests/unit/amazon/aws/executors/aws_lambda/test_utils.py: ########## @@ -0,0 +1,116 @@ +# 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 datetime +from dataclasses import is_dataclass + +from airflow.providers.amazon.aws.executors.aws_lambda.utils import ( + CONFIG_GROUP_NAME, + INVALID_CREDENTIALS_EXCEPTIONS, + AllLambdaConfigKeys, + InvokeLambdaKwargsConfigKeys, + LambdaQueuedTask, +) +from airflow.providers.amazon.aws.executors.utils.base_config_keys import BaseConfigKeys + + +class TestAwsLambdaUtilsConstants: + def test_config_group_name(self): + assert CONFIG_GROUP_NAME == "aws_lambda_executor" + + def test_invalid_credentials_exceptions(self): + assert INVALID_CREDENTIALS_EXCEPTIONS == [ + "ExpiredTokenException", + "InvalidClientTokenId", + "UnrecognizedClientException", + ] + + +class TestConfigKeys: + def test_invoke_lambda_kwargs_config_keys(self): + assert issubclass(InvokeLambdaKwargsConfigKeys, BaseConfigKeys) + assert InvokeLambdaKwargsConfigKeys.FUNCTION_NAME == "function_name" + assert InvokeLambdaKwargsConfigKeys.QUALIFIER == "function_qualifier" + + def test_all_lambda_config_keys_inheritance(self): + assert issubclass(AllLambdaConfigKeys, InvokeLambdaKwargsConfigKeys) + assert issubclass(AllLambdaConfigKeys, BaseConfigKeys) + + def test_all_lambda_config_keys_values(self): + assert AllLambdaConfigKeys.AWS_CONN_ID == "conn_id" + assert AllLambdaConfigKeys.CHECK_HEALTH_ON_STARTUP == "check_health_on_startup" + assert AllLambdaConfigKeys.MAX_INVOKE_ATTEMPTS == "max_invoke_attempts" + assert AllLambdaConfigKeys.REGION_NAME == "region_name" + assert AllLambdaConfigKeys.QUEUE_URL == "queue_url" + assert AllLambdaConfigKeys.DLQ_URL == "dead_letter_queue_url" + assert AllLambdaConfigKeys.END_WAIT_TIMEOUT == "end_wait_timeout" + assert AllLambdaConfigKeys.FUNCTION_NAME == "function_name" + assert AllLambdaConfigKeys.QUALIFIER == "function_qualifier" + + +class TestLambdaQueuedTask: Review Comment: `TestLambdaQueuedTask` only tests what `@dataclass` already guarantees: `is_dataclass`, that the fields hold what was passed in, and `__eq__`. `utils.py` has no logic of its own, and `test_lambda_executor.py` already exercises `LambdaQueuedTask` and these config keys through the executor. Could you remove `TestLambdaQueuedTask`, along with `TestAwsLambdaUtilsConstants` at L32, which only repeats the literals from `utils.py`? `TestConfigKeys` is worth keeping, since those values are the user-facing option names in `[aws_lambda_executor]`. -- 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]
