Skip to content

Commit

Permalink
Changed the way the unit test works
Browse files Browse the repository at this point in the history
* now we use paramatrize for all the test because of
one of the tests has multiple cases so to make the fixture
work for all the other test

* added a return statment to one of the cases in checks.py
  • Loading branch information
Andrew-ang9 committed May 27, 2022
1 parent 9ea4a59 commit bc93c2b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion convert2rhel/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def check_convert2rhel_latest():

if return_code != 0:
logger.warning("Couldn't check if this is the latest version of convert2rhel")

return
PKG_NEVR = r"\b(\S+)-(?:([0-9]+):)?(\S+)-(\S+)\b"
convert2rhel_versions = re.findall(PKG_NEVR, raw_output_convert2rhel_versions, re.MULTILINE)

Expand Down
70 changes: 40 additions & 30 deletions convert2rhel/unit_tests/checks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import logging
import os
import sys

Expand Down Expand Up @@ -174,13 +174,11 @@ def test_pre_ponr_checks(monkeypatch):
{"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6"}
)
def test_offline_version(caplog, monkeypatch, tmpdir, request):
marker = request.node.get_closest_marker("convert2rhel_version")
monkeypatch.setattr(convert2rhel, "__version__", marker.args[0]["local_version"])

run_subprocess_mocked = mock.Mock(spec=run_subprocess, return_value=(marker.args[0]["package_version"], 1))
run_subprocess_mocked = mock.Mock(spec=run_subprocess, return_value=(" ", 1))

monkeypatch.setattr(checks, "run_subprocess", run_subprocess_mocked)
monkeypatch.setattr(system_info, "version", namedtuple("Version", ["major", "minor"])(marker.args[0]["pmajor"], 0))
monkeypatch.setattr(system_info, "version", namedtuple("Version", ["major", "minor"])(6, 0))
monkeypatch.setattr(utils, "TMP_DIR", str(tmpdir))

checks.check_convert2rhel_latest()
Expand All @@ -191,67 +189,79 @@ def test_offline_version(caplog, monkeypatch, tmpdir, request):

@pytest.fixture
def convert2rhel_latest_t(monkeypatch, tmpdir, request):
marker = request.node.get_closest_marker("convert2rhel_version")
monkeypatch.setattr(convert2rhel, "__version__", marker.args[0]["local_version"])
marker = request.param
monkeypatch.setattr(checks, "convert2rhel_version", marker["local_version"])

run_subprocess_mocked = mock.Mock(spec=run_subprocess, return_value=(marker.args[0]["package_version"], 0))
run_subprocess_mocked = mock.Mock(spec=run_subprocess, return_value=(marker["package_version"], 0))

monkeypatch.setattr(checks, "run_subprocess", run_subprocess_mocked)
monkeypatch.setattr(system_info, "version", namedtuple("Version", ["major", "minor"])(marker.args[0]["pmajor"], 0))
monkeypatch.setattr(system_info, "version", namedtuple("Version", ["major", "minor"])(marker["pmajor"], 0))
monkeypatch.setattr(utils, "TMP_DIR", str(tmpdir))

return marker["local_version"], marker["package_version"]
# mock_ver_comp = mock.patch.object(
# checks, 'ver_comp', ver_comp=int(1)
# )


@pytest.mark.convert2rhel_version(
{"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6"}
@pytest.mark.parametrize(
("convert2rhel_latest_t",),
([{"local_version": "0.25", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6"}],),
indirect=True,
)
def test_convert2rhel_latest(caplog, convert2rhel_latest_t):
checks.check_convert2rhel_latest()

local_version, dummy_ = convert2rhel_latest_t
log_msg = (
"You are currently running 0.25 and the latest version of convert2rhel is 0.22.\n"
"You are currently running %s and the latest version of convert2rhel is 0.22.\n"
"Only the latest version is supported for conversion. If you want to ignore"
" this you can set 'CONVERT2RHEL_UNSUPPORTED_VERSION' to continue"
" this you can set 'CONVERT2RHEL_UNSUPPORTED_VERSION' to continue" % local_version
)
assert log_msg in caplog.text
assert log_msg in caplog.clear()


@pytest.mark.convert2rhel_version(
{"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "7"}
@pytest.mark.parametrize(
("convert2rhel_latest_t",),
([{"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "7"}],),
indirect=True,
)
def test_convert2rhel_latest_log_check_exit(caplog, convert2rhel_latest_t):
with pytest.raises(SystemExit):
checks.check_convert2rhel_latest()
local_version, dummy_ = convert2rhel_latest_t

log_msg = "You are currently running 0.25 and the latest version of convert2rhel is 0.22.\n" "change message"
log_msg = (
"You are currently running %s and the latest version of convert2rhel is 0.22.\n"
"change message" % locak_version
)
assert log_msg in caplog.text


@pytest.mark.convert2rhel_version(
{"local_version": "0.18", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6", "enset": "1"}
)
@pytest.mark.convert2rhel_version(
{"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "7", "enset": "1"}
)
@pytest.mark.convert2rhel_version(
{"local_version": "0.05", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "8", "enset": "1"}
@pytest.mark.parametrize(
("convert2rhel_latest_t",),
(
[{"local_version": "0.18", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6", "enset": "1"}],
[{"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "7", "enset": "1"}],
[{"local_version": "0.05", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "8", "enset": "1"}],
),
indirect=True,
)
@unit_tests.mock(os, "environ", {"CONVERT2RHEL_UNSUPPORTED_VERSION": "1"})
def test_convert2rhel_latest_log_check_env(caplog, convert2rhel_latest_t):
checks.check_convert2rhel_latest()

local_version, dummy_ = convert2rhel_latest_t
log_msg = (
"You are currently running 0.25 and the latest version of convert2rhel is 0.22.\n"
"'CONVERT2RHEL_UNSUPPORTED_VERSION' environment detected, continuing conversion"
"You are currently running %s and the latest version of convert2rhel is 0.22.\n"
"'CONVERT2RHEL_UNSUPPORTED_VERSION' environment detected, continuing conversion" % local_version
)
assert log_msg in caplog.text


@pytest.mark.convert2rhel_version(
{"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6", "ver_comp": int(1)}
)
# @pytest.mark.convert2rhel_version(
# {"local_version": "0.21", "package_version": "convert2rhel-0:0.22-1.el7.noarch", "pmajor": "6", "ver_comp": int(1)}
# )
# def test_convert2rhel_latest_ver_error(caplog, monkeypatch, convert2rhel_latest_t):
# monkeypatch.setattr(checks, "ver_comp", ver_comp, 1)
# checks.check_convert2rhel_latest()
Expand Down

0 comments on commit bc93c2b

Please sign in to comment.