-
Notifications
You must be signed in to change notification settings - Fork 14.6k
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
Add information about Amazon Elastic MapReduce Connection #26687
Changes from all commits
5d38495
d9d98f1
0451e95
84b6db8
c8c0662
5a22a92
afe3a6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
.. 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. | ||
|
||
.. _howto/connection:emr: | ||
|
||
Amazon Elastic MapReduce (EMR) Connection | ||
========================================= | ||
|
||
.. note:: | ||
This connection type is only used to store parameters to Start EMR Cluster (`run_job_flow` boto3 EMR client method). | ||
|
||
This connection not intend to store any credentials for ``boto3`` client, if you try to pass any | ||
parameters not listed in `RunJobFlow API <https://docs.aws.amazon.com/emr/latest/APIReference/API_RunJobFlow.html>`_ | ||
you will get an error like this. | ||
|
||
.. code-block:: text | ||
|
||
Parameter validation failed: Unknown parameter in input: "region_name", must be one of: | ||
|
||
For Authenticating to AWS please use :ref:`Amazon Web Services Connection <howto/connection:aws>`. | ||
|
||
Configuring the Connection | ||
-------------------------- | ||
|
||
Extra (optional) | ||
Specify the parameters (as a `json` dictionary) that can be used as an initial configuration | ||
in :meth:`airflow.providers.amazon.aws.hooks.emr.EmrHook.create_job_flow` to propagate to | ||
`RunJobFlow API <https://docs.aws.amazon.com/emr/latest/APIReference/API_RunJobFlow.html>`_. | ||
All parameters are optional. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,10 @@ | |
# under the License. | ||
from __future__ import annotations | ||
|
||
import unittest | ||
from unittest import mock | ||
|
||
import boto3 | ||
import pytest | ||
|
||
from airflow.providers.amazon.aws.hooks.emr import EmrHook | ||
|
||
|
@@ -29,8 +30,8 @@ | |
mock_emr = None | ||
|
||
|
||
@unittest.skipIf(mock_emr is None, 'moto package not present') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Love the conversion to pytest here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I try to replace # Total number of files which use `unittest.TestCase `
❯ grep -rl 'unittest.TestCase' ./tests | wc -l
528
# By tests packages
❯ grep -rl 'unittest.TestCase' ./tests | cut -d"/" -f3 | sort | uniq -c | sort -nr
379 providers
36 charts
23 utils
20 cli
10 ti_deps
10 api_connexion
6 sensors
6 operators
6 executors
6 core
5 www
5 always
4 models
3 api
2 task
2 kubernetes
1 security
1 plugins
1 macros
1 hooks
1 dag_processing
# By provider ("apache", "microsoft", "common" has subpackages which is separate provider)
❯ grep -rl 'unittest.TestCase' ./tests/providers/ | cut -d"/" -f5 | sort | uniq -c | sort -nr
113 google
101 amazon
32 apache
26 microsoft
6 databricks
4 redis
4 mysql
4 alibaba
3 trino
3 tableau
3 qubole
3 oracle
3 jenkins
3 http
3 docker
3 atlassian
3 arangodb
3 airbyte
2 yandex
2 vertica
2 telegram
2 sqlite
2 snowflake
2 sftp
2 segment
2 salesforce
2 presto
2 postgres
2 opsgenie
2 neo4j
2 mongo
2 jdbc
2 influxdb
2 imap
2 grpc
2 ftp
2 exasol
2 discord
2 dingding
2 datadog
2 common
2 cncf
2 asana
1 ssh
1 singularity
1 sendgrid
1 samba
1 papermill
1 openfaas
1 elasticsearch
1 cloudant
1 celery |
||
class TestEmrHook(unittest.TestCase): | ||
@pytest.mark.skipif(mock_emr is None, reason='moto package not present') | ||
class TestEmrHook: | ||
@mock_emr | ||
def test_get_conn_returns_a_boto3_connection(self): | ||
hook = EmrHook(aws_conn_id='aws_default', region_name='ap-southeast-2') | ||
|
@@ -59,13 +60,63 @@ def test_create_job_flow_extra_args(self): | |
# AmiVersion is really old and almost no one will use it anymore, but | ||
# it's one of the "optional" request params that moto supports - it's | ||
# coverage of EMR isn't 100% it turns out. | ||
cluster = hook.create_job_flow({'Name': 'test_cluster', 'ReleaseLabel': '', 'AmiVersion': '3.2'}) | ||
|
||
with pytest.warns(None): # Expected no warnings if ``emr_conn_id`` exists with correct conn_type | ||
cluster = hook.create_job_flow({'Name': 'test_cluster', 'ReleaseLabel': '', 'AmiVersion': '3.2'}) | ||
cluster = client.describe_cluster(ClusterId=cluster['JobFlowId'])['Cluster'] | ||
|
||
# The AmiVersion comes back as {Requested,Running}AmiVersion fields. | ||
assert cluster['RequestedAmiVersion'] == '3.2' | ||
|
||
@mock.patch("airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook.get_conn") | ||
def test_empty_emr_conn_id(self, mock_boto3_client): | ||
"""Test empty ``emr_conn_id``.""" | ||
mock_run_job_flow = mock.MagicMock() | ||
mock_boto3_client.return_value.run_job_flow = mock_run_job_flow | ||
job_flow_overrides = {"foo": "bar"} | ||
|
||
hook = EmrHook(aws_conn_id="aws_default", emr_conn_id=None) | ||
hook.create_job_flow(job_flow_overrides) | ||
mock_run_job_flow.assert_called_once_with(**job_flow_overrides) | ||
|
||
@mock.patch("airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook.get_conn") | ||
def test_missing_emr_conn_id(self, mock_boto3_client): | ||
"""Test not exists ``emr_conn_id``.""" | ||
mock_run_job_flow = mock.MagicMock() | ||
mock_boto3_client.return_value.run_job_flow = mock_run_job_flow | ||
job_flow_overrides = {"foo": "bar"} | ||
|
||
hook = EmrHook(aws_conn_id="aws_default", emr_conn_id="not-exists-emr-conn-id") | ||
warning_message = r"Unable to find Amazon Elastic MapReduce Connection ID 'not-exists-emr-conn-id',.*" | ||
with pytest.warns(UserWarning, match=warning_message): | ||
hook.create_job_flow(job_flow_overrides) | ||
mock_run_job_flow.assert_called_once_with(**job_flow_overrides) | ||
|
||
@mock.patch("airflow.providers.amazon.aws.hooks.base_aws.AwsBaseHook.get_conn") | ||
def test_emr_conn_id_wrong_conn_type(self, mock_boto3_client): | ||
"""Test exists ``emr_conn_id`` have unexpected ``conn_type``.""" | ||
mock_run_job_flow = mock.MagicMock() | ||
mock_boto3_client.return_value.run_job_flow = mock_run_job_flow | ||
job_flow_overrides = {"foo": "bar"} | ||
|
||
with mock.patch.dict("os.environ", AIRFLOW_CONN_WRONG_TYPE_CONN="aws://"): | ||
hook = EmrHook(aws_conn_id="aws_default", emr_conn_id="wrong_type_conn") | ||
warning_message = ( | ||
r"Amazon Elastic MapReduce Connection expected connection type 'emr'" | ||
r".* This connection might not work correctly." | ||
) | ||
with pytest.warns(UserWarning, match=warning_message): | ||
hook.create_job_flow(job_flow_overrides) | ||
mock_run_job_flow.assert_called_once_with(**job_flow_overrides) | ||
|
||
@pytest.mark.parametrize("aws_conn_id", ["aws_default", None]) | ||
@pytest.mark.parametrize("emr_conn_id", ["emr_default", None]) | ||
def test_emr_connection(self, aws_conn_id, emr_conn_id): | ||
"""Test that ``EmrHook`` always return False state.""" | ||
hook = EmrHook(aws_conn_id=aws_conn_id, emr_conn_id=emr_conn_id) | ||
result, message = hook.test_connection() | ||
assert not result | ||
assert message.startswith("'Amazon Elastic MapReduce' Airflow Connection cannot be tested") | ||
|
||
@mock_emr | ||
def test_get_cluster_id_by_name(self): | ||
""" | ||
|
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.
Neat-picking: Should we have a separate function ?
IMHO it does increase readability by ditching a few level of indentation.
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.
Actually for this purpose usually use
get_conn()
however we can not overwrite this method because it uses for obtain AWS credentials (aws_conn_id
).We could create this method, but only use in one place. Current implementation not contain any complex logic, so personally I do not see any benefits with this separate method.