Skip to content

Commit

Permalink
HTTPS is also valid. Switch to parametrized test.
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
  • Loading branch information
s0undt3ch committed May 4, 2023
1 parent 841f5ca commit 42d3eaa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion changelog/63810.fixed.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Updated source_hash documentation and add it log warning when source_hash is used with non http or ftp source.
Updated `source_hash` documentation and added a log warning when `source_hash` is used with a source other than `http`, `https` and `ftp`.
16 changes: 8 additions & 8 deletions salt/states/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ def run():

def _http_ftp_check(source):
"""
Check if source or sources is http or ftp.
Check if source or sources is http, https or ftp.
"""
if isinstance(source, str):
return source.lower().startswith(("http:", "ftp:"))
return all([s.lower().startswith(("http:", "ftp:")) for s in source])
return source.lower().startswith(("http:", "https:", "ftp:"))
return any([s.lower().startswith(("http:", "https:", "ftp:")) for s in source])


def _get_accumulator_filepath():
Expand Down Expand Up @@ -2423,7 +2423,7 @@ def managed(
- source: https://launchpad.net/tomdroid/beta/0.7.3/+download/tomdroid-src-0.7.3.tar.gz
- source_hash: md5=79eef25f9b0b2c642c62b7f737d4f53f
source_hash is ignored if the file hosted is not on a HTTP or FTP server.
source_hash is ignored if the file hosted is not on a HTTP, HTTPS or FTP server.
Known issues:
If the remote server URL has the hash file as an apparent
Expand Down Expand Up @@ -2958,7 +2958,7 @@ def managed(
)

if source is not None and not _http_ftp_check(source) and source_hash:
log.warning("source_hash is only used with 'http' or 'ftp'")
log.warning("source_hash is only used with 'http', 'https' or 'ftp'")

# If no source is specified, set replace to False, as there is nothing
# with which to replace the file.
Expand Down Expand Up @@ -6013,7 +6013,7 @@ def blockreplace(
return _error(ret, "Must provide name to file.blockreplace")

if source is not None and not _http_ftp_check(source) and source_hash:
log.warning("source_hash is only used with 'http' or 'ftp'")
log.warning("source_hash is only used with 'http', 'https' or 'ftp'")

if sources is None:
sources = []
Expand Down Expand Up @@ -6452,7 +6452,7 @@ def append(
return _error(ret, "Must provide name to file.append")

if source is not None and not _http_ftp_check(source) and source_hash:
log.warning("source_hash is only used with 'http' or 'ftp'")
log.warning("source_hash is only used with 'http', 'https' or 'ftp'")

name = os.path.expanduser(name)

Expand Down Expand Up @@ -6739,7 +6739,7 @@ def prepend(
return _error(ret, "Must provide name to file.prepend")

if source is not None and not _http_ftp_check(source) and source_hash:
log.warning("source_hash is only used with 'http' or 'ftp'")
log.warning("source_hash is only used with 'http', 'https' or 'ftp'")

if sources is None:
sources = []
Expand Down
50 changes: 24 additions & 26 deletions tests/pytests/unit/states/file/test_managed.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,29 +407,27 @@ def test_managed_test_mode_user_group_not_present():
assert "is not available" not in ret["comment"]


def test_http_ftp_check_pass():
assert filestate._http_ftp_check("http://@$@dead_link@$@/src.tar.gz") is True
assert filestate._http_ftp_check("ftp://@$@dead_link@$@/src.tar.gz") is True


def test_http_ftp_check_fail():
assert filestate._http_ftp_check("salt://@$@dead_link@$@/src.tar.gz") is False
assert filestate._http_ftp_check("https://@$@dead_link@$@/src.tar.gz") is False


def test_http_ftp_check_list_pass():
assert (
filestate._http_ftp_check(
["http://@$@dead_link@$@/src.tar.gz", "ftp://@$@dead_link@$@/src.tar.gz"]
)
is True
)


def test_http_ftp_check_list_fail():
assert (
filestate._http_ftp_check(
["salt://@$@dead_link@$@/src.tar.gz", "https://@$@dead_link@$@/src.tar.gz"]
)
is False
)
@pytest.mark.parametrize(
"source,check_result",
[
("http://@$@dead_link@$@/src.tar.gz", True),
("https://@$@dead_link@$@/src.tar.gz", True),
("ftp://@$@dead_link@$@/src.tar.gz", True),
("salt://@$@dead_link@$@/src.tar.gz", False),
("file://@$@dead_link@$@/src.tar.gz", False),
(
["http://@$@dead_link@$@/src.tar.gz", "https://@$@dead_link@$@/src.tar.gz"],
True,
),
(
["salt://@$@dead_link@$@/src.tar.gz", "file://@$@dead_link@$@/src.tar.gz"],
False,
),
(
["http://@$@dead_link@$@/src.tar.gz", "file://@$@dead_link@$@/src.tar.gz"],
True,
),
],
)
def test_sources_source_hash_check(source, check_result):
assert filestate._http_ftp_check(source) is check_result

0 comments on commit 42d3eaa

Please sign in to comment.