Internal func _format_url() to verify url fmt 87/13287/3
authorBengt Thuree <bthuree@linuxfoundation.org>
Thu, 1 Nov 2018 23:48:58 +0000 (19:48 -0400)
committerBengt Thuree <bthuree@linuxfoundation.org>
Fri, 2 Nov 2018 00:17:28 +0000 (11:17 +1100)
Adds a internal function _format_url() to verify url formatting for
domain root.

Change-Id: I0020cf5fb6a848c0fdb2fbc3cd79a48f4c79df4c
Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
lftools/deploy.py
tests/test_deploy.py

index d19133d..7dff5d8 100644 (file)
@@ -11,6 +11,7 @@
 
 import logging
 import os
+import re
 import shutil
 
 import glob2  # Switch to glob when Python < 3.5 support is dropped
@@ -18,6 +19,18 @@ import glob2  # Switch to glob when Python < 3.5 support is dropped
 log = logging.getLogger(__name__)
 
 
+def _format_url(url):
+    """Ensure url starts with http and trim trailing '/'s."""
+    start_pattern = re.compile('^(http|https)://')
+    if not start_pattern.match(url):
+        url = 'http://{}'.format(url)
+
+    if url.endswith('/'):
+        url = url.rstrip('/')
+
+    return url
+
+
 def copy_archives(workspace, pattern=None):
     """Copy files matching PATTERN in a WORKSPACE to the current directory.
 
index 9e34635..039984b 100644 (file)
@@ -14,6 +14,7 @@ import os
 import pytest
 
 from lftools import cli
+import lftools.deploy as deploy_sys
 
 FIXTURE_DIR = os.path.join(
     os.path.dirname(os.path.realpath(__file__)),
@@ -21,6 +22,22 @@ FIXTURE_DIR = os.path.join(
     )
 
 
+def test_format_url():
+    """Test url format."""
+    test_url=[["192.168.1.1", "http://192.168.1.1"],
+         ["192.168.1.1:8081", "http://192.168.1.1:8081"],
+         ["192.168.1.1:8081/nexus", "http://192.168.1.1:8081/nexus"],
+         ["192.168.1.1:8081/nexus/", "http://192.168.1.1:8081/nexus"],
+         ["http://192.168.1.1:8081/nexus", "http://192.168.1.1:8081/nexus"],
+         ["https://192.168.1.1:8081/nexus", "https://192.168.1.1:8081/nexus"],
+         ["https://192.168.1.1:8081/nexus/", "https://192.168.1.1:8081/nexus"],
+         ["www.goodnexussite.org:8081", "http://www.goodnexussite.org:8081"],
+         ["192.168.1.1:8081/nexus///", "http://192.168.1.1:8081/nexus"]]
+
+    for url in test_url:
+        assert deploy_sys._format_url(url[0]) == url[1]
+
+
 @pytest.mark.datafiles(
     os.path.join(FIXTURE_DIR, 'deploy'),
     )