Repository navigation
Fix Amazon ECS Enums #29871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix Amazon ECS Enums #29871
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| # | ||
| # 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 | ||
|
|
||
| from typing import TypeVar | ||
| from unittest import mock | ||
|
|
||
| import boto3 | ||
| import pytest | ||
| from slugify import slugify | ||
|
|
||
| from airflow.exceptions import AirflowException | ||
| from airflow.providers.amazon.aws.sensors.ecs import ( | ||
| DEFAULT_CONN_ID, | ||
| EcsBaseSensor, | ||
| EcsClusterStates, | ||
| EcsClusterStateSensor, | ||
| EcsHook, | ||
| EcsTaskDefinitionStates, | ||
| EcsTaskDefinitionStateSensor, | ||
| EcsTaskStates, | ||
| EcsTaskStateSensor, | ||
| ) | ||
| from airflow.utils import timezone | ||
| from airflow.utils.types import NOTSET | ||
|
|
||
| _Operator = TypeVar("_Operator") | ||
| TEST_CLUSTER_NAME = "fake-cluster" | ||
| TEST_TASK_ARN = "arn:aws:ecs:us-east-1:012345678910:task/spam-egg" | ||
| TEST_TASK_DEFINITION_ARN = "arn:aws:ecs:us-east-1:012345678910:task-definition/foo-bar:42" | ||
|
|
||
|
|
||
| class EcsBaseTestCase: | ||
| @pytest.fixture(autouse=True) | ||
| def setup_test_cases(self, monkeypatch, request, create_task_instance_of_operator): | ||
| self.dag_id = f"dag-{slugify(request.cls.__name__)}" | ||
| self.task_id = f"task-{slugify(request.node.name, max_length=40)}" | ||
| self.fake_client = boto3.client("ecs", region_name="eu-west-3") | ||
| monkeypatch.setattr(EcsHook, "conn", self.fake_client) | ||
| self.ti_maker = create_task_instance_of_operator | ||
|
|
||
| def create_rendered_task(self, operator_class: type[_Operator], **kwargs) -> _Operator: | ||
| """ | ||
| Create operator from given class and render fields. | ||
|
|
||
| This might help to prevent of unexpected behaviour in Jinja/task field serialisation | ||
| """ | ||
| return self.ti_maker( | ||
| operator_class, | ||
| dag_id=self.dag_id, | ||
| task_id=self.task_id, | ||
| execution_date=timezone.datetime(2021, 12, 21), | ||
| **kwargs, | ||
| ).render_templates() | ||
|
|
||
|
|
||
| class TestEcsBaseSensor(EcsBaseTestCase): | ||
| @pytest.mark.parametrize("aws_conn_id", [None, NOTSET, "aws_test_conn"]) | ||
| @pytest.mark.parametrize("region_name", [None, NOTSET, "ca-central-1"]) | ||
| def test_initialise_operator(self, aws_conn_id, region_name): | ||
| """Test sensor initialize.""" | ||
| op_kw = {"aws_conn_id": aws_conn_id, "region": region_name} | ||
| op_kw = {k: v for k, v in op_kw.items() if v is not NOTSET} | ||
| op = EcsBaseSensor(task_id="test_ecs_base", **op_kw) | ||
|
|
||
| assert op.aws_conn_id == (aws_conn_id if aws_conn_id is not NOTSET else DEFAULT_CONN_ID) | ||
| assert op.region == (region_name if region_name is not NOTSET else None) | ||
|
|
||
| @pytest.mark.parametrize("aws_conn_id", [None, NOTSET, "aws_test_conn"]) | ||
| @pytest.mark.parametrize("region_name", [None, NOTSET, "ca-central-1"]) | ||
| def test_hook_and_client(self, aws_conn_id, region_name): | ||
| """Test initialize ``EcsHook`` and ``boto3.client``.""" | ||
| op_kw = {"aws_conn_id": aws_conn_id, "region": region_name} | ||
| op_kw = {k: v for k, v in op_kw.items() if v is not NOTSET} | ||
| op = EcsBaseSensor(task_id="test_ecs_base_hook_client", **op_kw) | ||
|
|
||
| hook = op.hook | ||
| assert op.hook is hook, "'hook' property should be cached." | ||
| assert isinstance(op.hook, EcsHook) | ||
|
|
||
| client = op.client | ||
| assert op.client is client, "'client' property should be cached." | ||
| assert client is self.fake_client | ||
|
|
||
|
|
||
| class TestEcsClusterStateSensor(EcsBaseTestCase): | ||
| @pytest.mark.parametrize( | ||
| "return_state, expected", [("ACTIVE", True), ("PROVISIONING", False), ("DEPROVISIONING", False)] | ||
| ) | ||
| def test_default_values_poke(self, return_state, expected): | ||
| task = self.create_rendered_task(EcsClusterStateSensor, cluster_name=TEST_CLUSTER_NAME) | ||
| with mock.patch.object(task.hook, "get_cluster_state") as m: | ||
| m.return_value = return_state | ||
| assert task.poke({}) == expected | ||
| m.assert_called_once_with(cluster_name=TEST_CLUSTER_NAME) | ||
|
|
||
| @pytest.mark.parametrize("return_state", ["FAILED", "INACTIVE"]) | ||
| def test_default_values_terminal_state(self, create_task_of_operator, return_state): | ||
| task = self.create_rendered_task(EcsClusterStateSensor, cluster_name=TEST_CLUSTER_NAME) | ||
| with mock.patch.object(task.hook, "get_cluster_state") as m: | ||
| m.return_value = return_state | ||
| with pytest.raises(AirflowException, match="Terminal state reached"): | ||
| task.poke({}) | ||
| m.assert_called_once_with(cluster_name=TEST_CLUSTER_NAME) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "target_state, return_state, expected", | ||
| [ | ||
| (EcsClusterStates.ACTIVE, "ACTIVE", True), | ||
| (EcsClusterStates.ACTIVE, "DEPROVISIONING", False), | ||
| (EcsClusterStates.DEPROVISIONING, "ACTIVE", False), | ||
| (EcsClusterStates.DEPROVISIONING, "DEPROVISIONING", True), | ||
| ], | ||
| ) | ||
| def test_custom_values_poke(self, target_state, return_state, expected): | ||
| task = self.create_rendered_task( | ||
| EcsClusterStateSensor, cluster_name=TEST_CLUSTER_NAME, target_state=target_state | ||
| ) | ||
| with mock.patch.object(task.hook, "get_cluster_state") as m: | ||
| m.return_value = return_state | ||
| assert task.poke({}) == expected | ||
| m.assert_called_once_with(cluster_name=TEST_CLUSTER_NAME) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "failure_states, return_state", | ||
| [ | ||
| ({EcsClusterStates.ACTIVE}, "ACTIVE"), | ||
| ({EcsClusterStates.PROVISIONING, EcsClusterStates.DEPROVISIONING}, "DEPROVISIONING"), | ||
| ({EcsClusterStates.PROVISIONING, EcsClusterStates.DEPROVISIONING}, "PROVISIONING"), | ||
| ], | ||
| ) | ||
| def test_custom_values_terminal_state(self, failure_states, return_state): | ||
| task = self.create_rendered_task( | ||
| EcsClusterStateSensor, | ||
| cluster_name=TEST_CLUSTER_NAME, | ||
| target_state=EcsClusterStates.FAILED, | ||
| failure_states=failure_states, | ||
| ) | ||
| with mock.patch.object(task.hook, "get_cluster_state") as m: | ||
| m.return_value = return_state | ||
| with pytest.raises(AirflowException, match="Terminal state reached"): | ||
| task.poke({}) | ||
| m.assert_called_once_with(cluster_name=TEST_CLUSTER_NAME) | ||
|
|
||
|
|
||
| class TestEcsTaskDefinitionStateSensor(EcsBaseTestCase): | ||
| @pytest.mark.parametrize( | ||
| "return_state, expected", [("ACTIVE", True), ("INACTIVE", False), ("DELETE_IN_PROGRESS", False)] | ||
| ) | ||
| def test_default_values_poke(self, return_state, expected): | ||
| task = self.create_rendered_task( | ||
| EcsTaskDefinitionStateSensor, task_definition=TEST_TASK_DEFINITION_ARN | ||
| ) | ||
| with mock.patch.object(task.hook, "get_task_definition_state") as m: | ||
| m.return_value = return_state | ||
| assert task.poke({}) == expected | ||
| m.assert_called_once_with(task_definition=TEST_TASK_DEFINITION_ARN) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "target_state, return_state, expected", | ||
| [ | ||
| (EcsTaskDefinitionStates.INACTIVE, "ACTIVE", False), | ||
| (EcsTaskDefinitionStates.INACTIVE, "INACTIVE", True), | ||
| (EcsTaskDefinitionStates.ACTIVE, "INACTIVE", False), | ||
| (EcsTaskDefinitionStates.ACTIVE, "ACTIVE", True), | ||
| ], | ||
| ) | ||
| def test_custom_values_poke(self, create_task_of_operator, target_state, return_state, expected): | ||
| task = self.create_rendered_task( | ||
| EcsTaskDefinitionStateSensor, task_definition=TEST_TASK_DEFINITION_ARN, target_state=target_state | ||
| ) | ||
| with mock.patch.object(task.hook, "get_task_definition_state") as m: | ||
| m.return_value = return_state | ||
| assert task.poke({}) == expected | ||
| m.assert_called_once_with(task_definition=TEST_TASK_DEFINITION_ARN) | ||
|
|
||
|
|
||
| class TestEcsTaskStateSensor(EcsBaseTestCase): | ||
| @pytest.mark.parametrize( | ||
| "return_state, expected", | ||
| [ | ||
| ("PROVISIONING", False), | ||
| ("PENDING", False), | ||
| ("ACTIVATING", False), | ||
| ("RUNNING", True), | ||
| ("DEACTIVATING", False), | ||
| ("STOPPING", False), | ||
| ("DEPROVISIONING", False), | ||
| ("NONE", False), | ||
| ], | ||
| ) | ||
| def test_default_values_poke(self, return_state, expected): | ||
| task = self.create_rendered_task(EcsTaskStateSensor, cluster=TEST_CLUSTER_NAME, task=TEST_TASK_ARN) | ||
| with mock.patch.object(task.hook, "get_task_state") as m: | ||
| m.return_value = return_state | ||
| assert task.poke({}) == expected | ||
| m.assert_called_once_with(cluster=TEST_CLUSTER_NAME, task=TEST_TASK_ARN) | ||
|
|
||
| @pytest.mark.parametrize("return_state", ["STOPPED"]) | ||
| def test_default_values_terminal_state(self, return_state): | ||
| task = self.create_rendered_task(EcsTaskStateSensor, cluster=TEST_CLUSTER_NAME, task=TEST_TASK_ARN) | ||
| with mock.patch.object(task.hook, "get_task_state") as m: | ||
| m.return_value = return_state | ||
| with pytest.raises(AirflowException, match="Terminal state reached"): | ||
| task.poke({}) | ||
| m.assert_called_once_with(cluster=TEST_CLUSTER_NAME, task=TEST_TASK_ARN) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "target_state, return_state, expected", | ||
| [ | ||
| (EcsTaskStates.RUNNING, "RUNNING", True), | ||
| (EcsTaskStates.DEACTIVATING, "DEACTIVATING", True), | ||
| (EcsTaskStates.NONE, "PENDING", False), | ||
| (EcsTaskStates.STOPPING, "NONE", False), | ||
| ], | ||
| ) | ||
| def test_custom_values_poke(self, target_state, return_state, expected): | ||
| task = self.create_rendered_task( | ||
| EcsTaskStateSensor, cluster=TEST_CLUSTER_NAME, task=TEST_TASK_ARN, target_state=target_state | ||
| ) | ||
| with mock.patch.object(task.hook, "get_task_state") as m: | ||
| m.return_value = return_state | ||
| assert task.poke({}) == expected | ||
| m.assert_called_once_with(cluster=TEST_CLUSTER_NAME, task=TEST_TASK_ARN) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "failure_states, return_state", | ||
| [ | ||
| ({EcsTaskStates.RUNNING}, "RUNNING"), | ||
| ({EcsTaskStates.RUNNING, EcsTaskStates.DEACTIVATING}, "DEACTIVATING"), | ||
| ({EcsTaskStates.RUNNING, EcsTaskStates.DEACTIVATING}, "RUNNING"), | ||
| ], | ||
| ) | ||
| def test_custom_values_terminal_state(self, failure_states, return_state): | ||
| task = self.create_rendered_task( | ||
| EcsTaskStateSensor, | ||
| cluster=TEST_CLUSTER_NAME, | ||
| task=TEST_TASK_ARN, | ||
| target_state=EcsTaskStates.NONE, | ||
| failure_states=failure_states, | ||
| ) | ||
| with mock.patch.object(task.hook, "get_task_state") as m: | ||
| m.return_value = return_state | ||
| with pytest.raises(AirflowException, match="Terminal state reached"): | ||
| task.poke({}) | ||
| m.assert_called_once_with(cluster=TEST_CLUSTER_NAME, task=TEST_TASK_ARN) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we put this in a more general place so that other modules can make use of it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now it just for fix but in general we could move it later if it useful for other places, that why I make it "private"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the same thing. Seems like something we'll want to reuse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. We might want to do it now? It'd encourage others to use if needed instead of copy pasting it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it is quite trivial to move it to a more generic place (and still keep it private if you'd prefer that). Seems easy to make the right choice now rather than having to fix it later. But I wont press any harder, happy to merge this as is and I can fix it in a follow up PR 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather move it in separate PR when we actually need this functional. Just for avoid spending time to choose better name and better place 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me :)