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

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new d17ae60  Handle leading slash in samba path (#18847)
d17ae60 is described below

commit d17ae60be4d83e0bdfa6cb3c2c41e2bb1fcbf1b6
Author: Fred Thomsen <[email protected]>
AuthorDate: Sat Oct 9 11:38:16 2021 -0400

    Handle leading slash in samba path (#18847)
    
    Fix issue that occurs when the path to a file on a samba share has a
    slash prepended to it, then the `SambaHook` will treat the path as the
    host instead likely resulting trying to connect to the wrong samba host.
---
 airflow/providers/samba/hooks/samba.py    |  2 +-
 tests/providers/samba/hooks/test_samba.py | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/airflow/providers/samba/hooks/samba.py 
b/airflow/providers/samba/hooks/samba.py
index 7f846e3..df46c0c 100644
--- a/airflow/providers/samba/hooks/samba.py
+++ b/airflow/providers/samba/hooks/samba.py
@@ -80,7 +80,7 @@ class SambaHook(BaseHook):
         self._connection_cache.clear()
 
     def _join_path(self, path):
-        return f"//{posixpath.join(self._host, self._share, path)}"
+        return f"//{posixpath.join(self._host, self._share, path.lstrip('/'))}"
 
     @wraps(smbclient.link)
     def link(self, src, dst, follow_symlinks=True):
diff --git a/tests/providers/samba/hooks/test_samba.py 
b/tests/providers/samba/hooks/test_samba.py
index 4fb36d8..a2c45a8 100644
--- a/tests/providers/samba/hooks/test_samba.py
+++ b/tests/providers/samba/hooks/test_samba.py
@@ -130,3 +130,15 @@ class TestSambaHook(unittest.TestCase):
 
             # We expect keyword arguments to include the connection settings.
             assert dict(kwargs, **connection_settings) == p_kwargs
+
+    @parameterized.expand(
+        [
+            ("/start/path/with/slash", "//ip/share/start/path/with/slash"),
+            ("start/path/without/slash", 
"//ip/share/start/path/without/slash"),
+        ],
+    )
+    @mock.patch('airflow.hooks.base.BaseHook.get_connection')
+    def test__join_path(self, path, full_path, get_conn_mock):
+        get_conn_mock.return_value = CONNECTION
+        hook = SambaHook('samba_default')
+        assert hook._join_path(path) == full_path

Reply via email to