lightning/tests/tests_app/utilities/test_cli_helpers.py

114 lines
3.9 KiB
Python
Raw Normal View History

from unittest.mock import Mock, patch
[CLI] Adding opportunity to see basic cluster logs (#14334) * pinning starsessions * pinning starsessions * adding strict back to requirements.txt * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Duplicated * Basic implementation * Basic implementation * Basic implementation * Basic implementation * Common things moved to log helpers file * Decomposing logs reader classes for reusing * Setting colors for log levels * Manifest trimming * Changes added to CHANGELOG * Prettifications * Prettifications * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Logs function name change * Logs function name change * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * attempt to fix the pydanitc import * Tests + command name fixes * Extending tests * Adding limit argument * Unmerging CI fix * Unmerging CI fix * Adding fields for errors * Adding log level fixed field width * Adding absent typing + exeptions raising * Adding socket error logging * Addressing comments on cluster list function return value * Addressing comments on adding e2e tests * Adding version range for arrow package in reqs * New unit tests * arrow time parsing callback modified + unit tests * helpers updated * helpers updated * helpers updated * One more test * CMD test fix * CMD test fix * CMD test fix * CMD test fix * CMD test fix * LightningClient mocking * Flaky test removed Co-authored-by: hhsecond <sherin@grid.ai> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
2022-08-23 19:48:22 +00:00
import arrow
import pytest
import lightning_app
from lightning_app.utilities.cli_helpers import _arrow_time_callback, _format_input_env_variables, _get_newer_version
def test_format_input_env_variables():
with pytest.raises(Exception, match="Invalid format of environment variable"):
_format_input_env_variables(("invalid-env",))
with pytest.raises(Exception, match="Invalid format of environment variable"):
_format_input_env_variables(("=invalid",))
with pytest.raises(Exception, match="Invalid format of environment variable"):
_format_input_env_variables(("=invalid=",))
with pytest.raises(Exception, match="is duplicated. Please only include it once."):
_format_input_env_variables(
(
"FOO=bar",
"FOO=bar",
)
)
with pytest.raises(
Exception,
match="is not a valid name. It is only allowed to contain digits 0-9, letters A-Z",
):
_format_input_env_variables(("*FOO#=bar",))
assert _format_input_env_variables(("FOO=bar", "BLA=bloz")) == {"FOO": "bar", "BLA": "bloz"}
[CLI] Adding opportunity to see basic cluster logs (#14334) * pinning starsessions * pinning starsessions * adding strict back to requirements.txt * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Duplicated * Basic implementation * Basic implementation * Basic implementation * Basic implementation * Common things moved to log helpers file * Decomposing logs reader classes for reusing * Setting colors for log levels * Manifest trimming * Changes added to CHANGELOG * Prettifications * Prettifications * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Logs function name change * Logs function name change * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * attempt to fix the pydanitc import * Tests + command name fixes * Extending tests * Adding limit argument * Unmerging CI fix * Unmerging CI fix * Adding fields for errors * Adding log level fixed field width * Adding absent typing + exeptions raising * Adding socket error logging * Addressing comments on cluster list function return value * Addressing comments on adding e2e tests * Adding version range for arrow package in reqs * New unit tests * arrow time parsing callback modified + unit tests * helpers updated * helpers updated * helpers updated * One more test * CMD test fix * CMD test fix * CMD test fix * CMD test fix * CMD test fix * LightningClient mocking * Flaky test removed Co-authored-by: hhsecond <sherin@grid.ai> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
2022-08-23 19:48:22 +00:00
def test_arrow_time_callback():
# Check ISO 8601 variations
assert _arrow_time_callback(Mock(), Mock(), "2022.08.23") == arrow.Arrow(2022, 8, 23)
assert _arrow_time_callback(Mock(), Mock(), "2022.08.23 12:34") == arrow.Arrow(2022, 8, 23, 12, 34)
assert _arrow_time_callback(Mock(), Mock(), "2022-08-23 12:34") == arrow.Arrow(2022, 8, 23, 12, 34)
assert _arrow_time_callback(Mock(), Mock(), "2022-08-23 12:34:00.000") == arrow.Arrow(2022, 8, 23, 12, 34)
# Just check humanized format is parsed
assert type(_arrow_time_callback(Mock(), Mock(), "48 hours ago")) == arrow.Arrow
assert type(_arrow_time_callback(Mock(), Mock(), "60 minutes ago")) == arrow.Arrow
assert type(_arrow_time_callback(Mock(), Mock(), "120 seconds ago")) == arrow.Arrow
# Check raising errors
with pytest.raises(Exception, match="cannot parse time Mon"):
_arrow_time_callback(Mock(), Mock(), "Mon")
with pytest.raises(Exception, match="cannot parse time Mon Sep 08 16:41:45 2022"):
_arrow_time_callback(Mock(), Mock(), "Mon Sep 08 16:41:45 2022")
with pytest.raises(Exception, match="cannot parse time 2022.125.12"):
_arrow_time_callback(Mock(), Mock(), "2022.125.12")
with pytest.raises(Exception, match="cannot parse time 1 time unit ago"):
_arrow_time_callback(Mock(), Mock(), "1 time unit ago")
@pytest.mark.parametrize(
"releases, current_version, newer_version",
[
(
{
"1.0.0": [{"upload_time_iso_8601": "2022-09-10", "yanked": False}],
"2.0.0": [{"upload_time_iso_8601": "2022-11-01", "yanked": False}],
},
"1.0.0",
"2.0.0",
),
(
{
"1.0.0": [{"upload_time_iso_8601": "2022-09-10", "yanked": False}],
"2.0.0": [{"upload_time_iso_8601": "2022-11-01", "yanked": True}],
},
"1.0.0",
None,
),
(
{
"1.0.0": [{"upload_time_iso_8601": "2022-09-10", "yanked": False}],
"2.0.0rc0": [{"upload_time_iso_8601": "2022-11-01", "yanked": False}],
},
"1.0.0",
None,
),
(
{
"2.0.0": [{"upload_time_iso_8601": "2022-11-01", "yanked": False}],
},
"1.0.0dev",
None,
),
({"1.0.0": "this wil trigger an error"}, "1.0.0", "err"),
({}, "1.0.0rc0", None),
],
)
@patch("lightning_app.utilities.cli_helpers.requests")
def test_get_newer_version(mock_requests, releases, current_version, newer_version):
mock_requests.get().json.return_value = {"releases": releases}
lightning_app.utilities.cli_helpers.__version__ = current_version
_get_newer_version.cache_clear()
assert _get_newer_version() == newer_version