From 0b4c35948cf3a800a513c92dd3b8ff2a71b15e81 Mon Sep 17 00:00:00 2001 From: Bengt Thuree Date: Thu, 1 Nov 2018 19:48:58 -0400 Subject: [PATCH] Internal func _format_url() to verify url fmt Adds a internal function _format_url() to verify url formatting for domain root. Change-Id: I0020cf5fb6a848c0fdb2fbc3cd79a48f4c79df4c Signed-off-by: Bengt Thuree --- lftools/deploy.py | 13 +++++++++++++ tests/test_deploy.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lftools/deploy.py b/lftools/deploy.py index d19133d8..7dff5d88 100644 --- a/lftools/deploy.py +++ b/lftools/deploy.py @@ -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. diff --git a/tests/test_deploy.py b/tests/test_deploy.py index 9e34635f..039984b8 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -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'), ) -- 2.16.6