You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by pi...@apache.org on 2023/03/06 21:47:05 UTC

[airflow] 21/37: Allow URI without authority and host blocks in `airflow connections add` (#28922)

This is an automated email from the ASF dual-hosted git repository.

pierrejeambrun pushed a commit to branch v2-5-test
in repository https://gitbox.apache.org/repos/asf/airflow.git

commit 31bffa87131906f50b8b433bcc3e909dc9ffdd90
Author: Andrey Anshin <An...@taragol.is>
AuthorDate: Sat Jan 14 01:41:11 2023 +0400

    Allow URI without authority and host blocks in `airflow connections add` (#28922)
    
    (cherry picked from commit d8b84ce0e6d36850cd61b1ce37840c80aaec0116)
---
 airflow/cli/commands/connection_command.py    |  5 +-
 tests/cli/commands/test_connection_command.py | 67 +++++++++++++++++++++++----
 2 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/airflow/cli/commands/connection_command.py b/airflow/cli/commands/connection_command.py
index 7737f4fa2c..c19490da93 100644
--- a/airflow/cli/commands/connection_command.py
+++ b/airflow/cli/commands/connection_command.py
@@ -132,9 +132,8 @@ def _is_stdout(fileio: io.TextIOWrapper) -> bool:
 
 
 def _valid_uri(uri: str) -> bool:
-    """Check if a URI is valid, by checking if both scheme and netloc are available."""
-    uri_parts = urlsplit(uri)
-    return uri_parts.scheme != "" and uri_parts.netloc != ""
+    """Check if a URI is valid, by checking if scheme (conn_type) provided."""
+    return urlsplit(uri).scheme != ""
 
 
 @cache
diff --git a/tests/cli/commands/test_connection_command.py b/tests/cli/commands/test_connection_command.py
index 05d8417c16..b5748e7a1d 100644
--- a/tests/cli/commands/test_connection_command.py
+++ b/tests/cli/commands/test_connection_command.py
@@ -19,6 +19,7 @@ from __future__ import annotations
 import io
 import json
 import re
+import shlex
 import warnings
 from contextlib import redirect_stdout
 from unittest import mock
@@ -353,7 +354,7 @@ class TestCliAddConnections:
     @pytest.mark.parametrize(
         "cmd, expected_output, expected_conn",
         [
-            (
+            pytest.param(
                 [
                     "connections",
                     "add",
@@ -371,8 +372,9 @@ class TestCliAddConnections:
                     "port": 5432,
                     "schema": "airflow",
                 },
+                id="json-connection",
             ),
-            (
+            pytest.param(
                 [
                     "connections",
                     "add",
@@ -391,8 +393,9 @@ class TestCliAddConnections:
                     "port": 5432,
                     "schema": "airflow",
                 },
+                id="uri-connection-with-description",
             ),
-            (
+            pytest.param(
                 [
                     "connections",
                     "add",
@@ -411,8 +414,9 @@ class TestCliAddConnections:
                     "port": 5432,
                     "schema": "airflow",
                 },
+                id="uri-connection-with-description-2",
             ),
-            (
+            pytest.param(
                 [
                     "connections",
                     "add",
@@ -432,8 +436,9 @@ class TestCliAddConnections:
                     "port": 5432,
                     "schema": "airflow",
                 },
+                id="uri-connection-with-extra",
             ),
-            (
+            pytest.param(
                 [
                     "connections",
                     "add",
@@ -455,8 +460,9 @@ class TestCliAddConnections:
                     "port": 5432,
                     "schema": "airflow",
                 },
+                id="uri-connection-with-extra-and-description",
             ),
-            (
+            pytest.param(
                 [
                     "connections",
                     "add",
@@ -480,8 +486,9 @@ class TestCliAddConnections:
                     "port": 9083,
                     "schema": "airflow",
                 },
+                id="individual-parts",
             ),
-            (
+            pytest.param(
                 [
                     "connections",
                     "add",
@@ -504,6 +511,37 @@ class TestCliAddConnections:
                     "port": None,
                     "schema": None,
                 },
+                id="empty-uri-with-conn-type-and-extra",
+            ),
+            pytest.param(
+                ["connections", "add", "new6", "--conn-uri", "aws://?region_name=foo-bar-1"],
+                "Successfully added `conn_id`=new6 : aws://?region_name=foo-bar-1",
+                {
+                    "conn_type": "aws",
+                    "description": None,
+                    "host": "",
+                    "is_encrypted": False,
+                    "is_extra_encrypted": True,
+                    "login": None,
+                    "port": None,
+                    "schema": "",
+                },
+                id="uri-without-authority-and-host-blocks",
+            ),
+            pytest.param(
+                ["connections", "add", "new7", "--conn-uri", "aws://@/?region_name=foo-bar-1"],
+                "Successfully added `conn_id`=new7 : aws://@/?region_name=foo-bar-1",
+                {
+                    "conn_type": "aws",
+                    "description": None,
+                    "host": "",
+                    "is_encrypted": False,
+                    "is_extra_encrypted": True,
+                    "login": "",
+                    "port": None,
+                    "schema": "",
+                },
+                id="uri-with-@-instead-authority-and-host-blocks",
             ),
         ],
     )
@@ -572,11 +610,20 @@ class TestCliAddConnections:
                 )
             )
 
-    def test_cli_connections_add_invalid_uri(self):
+    @pytest.mark.parametrize(
+        "invalid_uri",
+        [
+            pytest.param("nonsense_uri", id="word"),
+            pytest.param("://password:type@host:42/schema", id="missing-conn-type"),
+        ],
+    )
+    def test_cli_connections_add_invalid_uri(self, invalid_uri):
         # Attempt to add with invalid uri
-        with pytest.raises(SystemExit, match=r"The URI provided to --conn-uri is invalid: nonsense_uri"):
+        with pytest.raises(SystemExit, match=r"The URI provided to --conn-uri is invalid: .*"):
             connection_command.connections_add(
-                self.parser.parse_args(["connections", "add", "new1", f"--conn-uri={'nonsense_uri'}"])
+                self.parser.parse_args(
+                    ["connections", "add", "new1", f"--conn-uri={shlex.quote(invalid_uri)}"]
+                )
             )
 
     def test_cli_connections_add_invalid_type(self):