This document discusses porting pre-PONR checks from Python scripts to Actions within the Actions framework. The goals are to make each Action more independent and modular. Checks will be ported by creating subclasses of the Action class for each check. The check code will be moved from functions into the Action run() method. Unit tests will be updated to invoke the Actions and check for errors instead of the original functions. Examples of a ported check and tests are provided.
3. 03/14/2023 3
Goals of Action Framework
●
Framework for performing pre-PONR Actions
●
Make each Action more independent
4. 03/14/2023 4
Goals of Action Framework
●
Framework for performing pre-PONR Actions
●
Make each Action more independent
●
One step in any transition to a third party framework (like LEAPP)
7. 03/14/2023 7
Structure
●
Python modules live in convert2rhel/actions/
●
Modules contain subclasses of convert2rhel.actions.Action
●
(*) Each subclass will be discovered and run by
convert2rhel.actions.run_actions()
9. 03/14/2023 9
The Action class
●
Action class that inherits from convert2rhel.actions.Action
●
Several class attributes (set when you define the class)
●
Action.id
●
Action.dependencies
10. 03/14/2023 10
The Action class
●
Action class that inherits from convert2rhel.actions.Action
●
Several class attributes (set when you define the class)
●
Action.id
●
Action.dependencies
●
One required method
●
Action.run()
11. 03/14/2023 11
Porting Checks: Declaration
●
New file for the check
●
Ex: convert2rhel/actions/convert2rhel_latest.py
12. 03/14/2023 12
Porting Checks: Declaration
●
New file for the check
●
Ex: convert2rhel/actions/convert2rhel_latest.py
●
Create a class, name based on the check function name
●
def convert2rhel_latest_check(…)
●
class Convert2rhelLatest(convert2rhel.actions.Action):
13. 03/14/2023 13
Porting Checks: Declaration
●
New file for the check
●
Ex: convert2rhel/actions/convert2rhel_latest.py
●
Create a class, name based on the check function name
●
def convert2rhel_latest_check(…)
●
class Convert2rhelLatest(convert2rhel.actions.Action):
●
Add machine-oriented id
●
Ex: id = “CONVERT2RHEL_LATEST_VERSION”
14. 03/14/2023 14
Porting Checks: Declaration
●
New file for the check
●
Ex: convert2rhel/actions/convert2rhel_latest.py
●
Create a class, name based on the check function name
●
def convert2rhel_latest_check(…)
●
class Convert2rhelLatest(convert2rhel.actions.Action):
●
Add machine-oriented id
●
Ex: id = “CONVERT2RHEL_LATEST_VERSION”
●
Add dependencies if needed
16. 03/14/2023 16
Porting Checks: Function
●
Move code from the check function into the run() function
●
Replace logger.critical calls with code that sets an error
●
Old: logger.critical(“The running convert2rhel is not the latest version”)
●
New:
self.status = actions.STATUS_CODE[“ERROR”]
self.error_id = “UP_TO_DATE”
self.message = “The running convert2rhel is not the latest version”
return
17. 03/14/2023 17
Porting Checks: Function
●
Move code from the check function into the run() function
●
Replace logger.critical calls with code that sets an error
●
Old: logger.critical(“The running convert2rhel is not the latest version”)
●
New:
self.status = actions.STATUS_CODE[“ERROR”]
self.error_id = “UP_TO_DATE”
self.message = “The running convert2rhel is not the latest version”
return
●
If appropriate, set message on successful cases.
18. 03/14/2023 18
Porting Checks: Function
●
Move code from the check function into the run() function
●
Replace logger.critical calls with code that sets an error
●
Old: logger.critical(“The running convert2rhel is not the latest version”)
●
New:
self.status = actions.STATUS_CODE[“ERROR”]
self.error_id = “UP_TO_DATE”
self.message = “The running convert2rhel is not the latest version”
return
●
If appropriate, set message on successful cases.
●
The Action Runner will take care of displaying the message at the
appropriate time.
20. 03/14/2023 20
Unittests: Fixture
●
Create a fixture that returns an instance of the action
●
@pytest.fixture
def convert2rhel_latest_action():
return convert2rhel_latest.Convert2rhelLatest()
21. 03/14/2023 21
Unittests: Invoking the Action
●
In any test where we ran the check function, use the fixture and
the run() method:
●
def test_convert2rhel_latest_offline(self, caplog, […]):
checks.check_convert2rhel_latest()
●
def test_convert2rhel_latest_offline(
self, caplog, convert2rhel_latest_action, […]):
convert2rhel_latest_action.run()
22. 03/14/2023 22
Unittests: Invoking the Action
●
In any test where we ran the check function, use the fixture and
the run() method:
●
def test_convert2rhel_latest_offline(self, caplog, […]):
checks.check_convert2rhel_latest()
●
def test_convert2rhel_latest_offline(
self, caplog, convert2rhel_latest_action, […]):
convert2rhel_latest_action.run()
23. 03/14/2023 23
Unittests: Checking for errors
●
Where we test for critical errors, switch from pytest.raises and
caplog to checking the status
●
with pytest.raises(SystemExit):
checks.check_convert2rhel_latest()
assert expected_log_msg in caplog.text
●
convert2rhel_latest_check.run()
assert convert2rhel_latest_action.error_id == "OUT_OF_DATE"
assert convert2rhel_latest_action.status ==
actions.STATUS_CODE["ERROR"]
assert expected_log_msg == convert2rhel_latest_action.message
24. 03/14/2023 24
More information
●
Pull Request: https://github.com/oamg/convert2rhel/pull/761
●
Example ported check:
●
convert2rhel/actions/convert2rhel_latest.py
●
convert2rhel/unit_tests/actions/convert2rhel_latest_test.py
●
List of checks to port, see Sub Tasks:
●
https://issues.redhat.com/browse/RHELC-928
●
Ask: Toshio Kuratomi, Rodolfo Olivieri, Preston Watson