@@ -409,7 +409,7 @@ def __call__(self, pkg, dest, reposdir=None):
409409 (False , False ),
410410 ),
411411)
412- def test_unregister_system_successfully (submgr_installed , unregistration_failure , monkeypatch , caplog ):
412+ def test_unregister_system (submgr_installed , unregistration_failure , monkeypatch , caplog ):
413413 if unregistration_failure :
414414 monkeypatch .setattr (utils , "run_subprocess" , mock .Mock (return_value = ("output" , 1 )))
415415 else :
@@ -426,11 +426,63 @@ def test_unregister_system_successfully(submgr_installed, unregistration_failure
426426
427427 if submgr_installed :
428428 utils .run_subprocess .assert_called_once ()
429-
430- if submgr_installed :
431429 if unregistration_failure :
432430 assert "System unregistration failed" in caplog .text
433431 else :
434432 assert "System unregistered successfully." in caplog .text
435433 else :
436434 assert "The subscription-manager package is not installed." in caplog .text
435+
436+
437+ @mock .patch ("convert2rhel.toolopts.tool_opts.keep_rhsm" , True )
438+ def test_unregister_system_skipped (monkeypatch , caplog ):
439+ monkeypatch .setattr (pkghandler , "get_installed_pkg_objects" , mock .Mock ())
440+ subscription .unregister_system ()
441+ assert "Skipping due to the use of --keep-rhsm." in caplog .text
442+ pkghandler .get_installed_pkg_objects .assert_not_called ()
443+
444+
445+ @mock .patch ("convert2rhel.toolopts.tool_opts.keep_rhsm" , True )
446+ def test_replace_subscription_manager_skipped (monkeypatch , caplog ):
447+ monkeypatch .setattr (subscription , "unregister_system" , mock .Mock ())
448+ subscription .replace_subscription_manager ()
449+ assert "Skipping due to the use of --keep-rhsm." in caplog .text
450+ subscription .unregister_system .assert_not_called ()
451+
452+
453+ @mock .patch ("convert2rhel.toolopts.tool_opts.keep_rhsm" , True )
454+ def test_download_rhsm_pkgs_skipped (monkeypatch , caplog ):
455+ monkeypatch .setattr (subscription , "_download_rhsm_pkgs" , mock .Mock ())
456+ subscription .download_rhsm_pkgs ()
457+ assert "Skipping due to the use of --keep-rhsm." in caplog .text
458+ subscription ._download_rhsm_pkgs .assert_not_called ()
459+
460+
461+ @pytest .mark .parametrize (
462+ ("submgr_installed" , "keep_rhsm" , "critical_string" ),
463+ (
464+ (True , None , None ),
465+ (False , True , "the subscription-manager needs to be installed" ),
466+ (False , False , "The subscription-manager package is not installed correctly." ),
467+ ),
468+ )
469+ def test_verify_rhsm_installed (submgr_installed , keep_rhsm , critical_string , monkeypatch , caplog ):
470+ if keep_rhsm :
471+ monkeypatch .setattr (tool_opts , "keep_rhsm" , keep_rhsm )
472+
473+ if submgr_installed :
474+ monkeypatch .setattr (
475+ pkghandler , "get_installed_pkg_objects" , lambda _ : [namedtuple ("Pkg" , ["name" ])("subscription-manager" )]
476+ )
477+
478+ subscription .verify_rhsm_installed ()
479+
480+ assert "subscription-manager installed correctly." in caplog .text
481+
482+ else :
483+ monkeypatch .setattr (pkghandler , "get_installed_pkg_objects" , lambda _ : None )
484+
485+ with pytest .raises (SystemExit ):
486+ subscription .verify_rhsm_installed ()
487+
488+ assert critical_string in caplog .text
0 commit comments