Skip to content

Commit

Permalink
Increase unittest coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LiaoU3 committed Apr 1, 2024
1 parent 0ebba5d commit a44a16d
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 18 deletions.
2 changes: 1 addition & 1 deletion providers/base/bin/watchdog_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def main():
print("watchdog.service unit does not report as active")
watchdog_config_ready = False
if watchdog_config_ready:
print("watchdog.service active")
print("watchdog.service is active")

return not watchdog_config_ready

Expand Down
334 changes: 317 additions & 17 deletions providers/base/tests/test_watchdog_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,133 @@ def test_watchdog_service_check_exception(self, mock_subprocess_run):
with self.assertRaises(SystemExit):
watchdog_service_check()

@patch('watchdog_config_test.watchdog_argparse')
@patch('watchdog_config_test.get_series')
@patch('watchdog_config_test.get_systemd_wdt_usec')
@patch('watchdog_config_test.on_ubuntucore')
@patch('watchdog_config_test.watchdog_service_check')
def test_main_check_time(self, mock_watchdog_service_check, mock_on_ubuntucore, mock_get_systemd_wdt_usec, mock_get_series, mock_watchdog_argparse):
@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.get_systemd_wdt_usec")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_time_and_service(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_systemd_wdt_usec,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = True
mock_args.check_service = True
mock_watchdog_argparse.return_value = mock_args

# Mock get_series
mock_get_series.return_value = "20.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = False

# Mock get_systemd_wdt_usec
mock_get_systemd_wdt_usec.return_value = "1000000"

# Mock watchdog_service_check
mock_watchdog_service_check.return_value = False

# Call the function under test
with patch("builtins.print") as mock_print:
main()

# Assert that the expected messages are printed
mock_print.assert_any_call(
"systemd watchdog enabled, reset timeout: 1000000"
)
mock_print.assert_any_call("watchdog.service is not active")

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.get_systemd_wdt_usec")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_time_ubuntucore(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_systemd_wdt_usec,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = True
mock_args.check_service = False
mock_watchdog_argparse.return_value = mock_args

# Mock get_systemd_wdt_usec
mock_get_systemd_wdt_usec.return_value = 1000000

# Mock get_series
mock_get_series.return_value = "18.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = True
# Call the function under test
with patch("builtins.print") as mock_print:
main()

# Assert that the expected messages are printed
mock_print.assert_any_call(
"systemd watchdog enabled, reset timeout: 1000000"
)

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.get_systemd_wdt_usec")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_time_not_active(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_systemd_wdt_usec,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = True
mock_args.check_service = False
mock_watchdog_argparse.return_value = mock_args

# Mock get_series
mock_get_series.return_value = "20.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = False

# Mock get_systemd_wdt_usec
mock_get_systemd_wdt_usec.return_value = "0"

# Call the function under test
with patch("builtins.print") as mock_print:
main()

# Assert that the expected message is printed
mock_print.assert_any_call(
"systemd watchdog should be enabled but reset timeout: 0"
)

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.get_systemd_wdt_usec")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_time_and_systemd_wdt_configured(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_systemd_wdt_usec,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = True
Expand All @@ -130,18 +251,94 @@ def test_main_check_time(self, mock_watchdog_service_check, mock_on_ubuntucore,
mock_get_systemd_wdt_usec.return_value = "1000000"

# Call the function under test
with patch('builtins.print') as mock_print:
with patch("builtins.print") as mock_print:
main()
# Assert that the expected messages are printed
mock_print.assert_any_call(
"systemd watchdog enabled, reset timeout: 1000000"
)

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.get_systemd_wdt_usec")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_time_and_watchdog_config_ready(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_systemd_wdt_usec,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = True
mock_args.check_service = False
mock_watchdog_argparse.return_value = mock_args

# Mock get_series
mock_get_series.return_value = "18.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = False

# Mock get_systemd_wdt_usec
mock_get_systemd_wdt_usec.return_value = "0"

# Call the function under test
with patch("builtins.print") as mock_print:
main()
# Assert that the expected messages are printed
mock_print.assert_any_call("systemd watchdog disabled")

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.get_systemd_wdt_usec")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_time_is_systemd_wdt_configured(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_systemd_wdt_usec,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = True
mock_args.check_service = False
mock_watchdog_argparse.return_value = mock_args

# Mock get_series
mock_get_series.return_value = "18.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = False

# Mock get_systemd_wdt_usec
mock_get_systemd_wdt_usec.return_value = "1000000"

# Call the function under test
with patch("builtins.print") as mock_print:
main()
# Assert that the expected messages are printed
mock_print.assert_any_call("systemd watchdog enabled, reset timeout: 1000000")

@patch('watchdog_config_test.watchdog_argparse')
@patch('watchdog_config_test.get_series')
@patch('watchdog_config_test.get_systemd_wdt_usec')
@patch('watchdog_config_test.on_ubuntucore')
@patch('watchdog_config_test.watchdog_service_check')
def test_main_check_service(self, mock_watchdog_service_check, mock_on_ubuntucore, mock_get_systemd_wdt_usec, mock_get_series, mock_watchdog_argparse):
mock_print.assert_any_call(
"systemd watchdog should not be enabled but reset timeout: 1000000"
)

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_service_ubuntucore_not_active(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = False
Expand All @@ -158,8 +355,111 @@ def test_main_check_service(self, mock_watchdog_service_check, mock_on_ubuntucor
mock_watchdog_service_check.return_value = False

# Call the function under test
with patch('builtins.print') as mock_print:
with patch("builtins.print") as mock_print:
main()

# Assert that the expected messages are printed
# Assert that the expected message is printed
mock_print.assert_any_call("watchdog.service is not active")

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_service_ubuntucore_is_wdt_service_configured(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = False
mock_args.check_service = True
mock_watchdog_argparse.return_value = mock_args

# Mock get_series
mock_get_series.return_value = "20.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = False

# Mock watchdog_service_check
mock_watchdog_service_check.return_value = True

# Call the function under test
with patch("builtins.print") as mock_print:
main()

# Assert that the expected message is printed
mock_print.assert_any_call(
"found unexpected active watchdog.service unit"
)

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_service_active(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = False
mock_args.check_service = True
mock_watchdog_argparse.return_value = mock_args

# Mock get_series
mock_get_series.return_value = "18.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = False

# Mock watchdog_service_check
mock_watchdog_service_check.return_value = True

# Call the function under test
with patch("builtins.print") as mock_print:
main()

# Assert that the expected message is printed
mock_print.assert_any_call("watchdog.service is active")

@patch("watchdog_config_test.watchdog_argparse")
@patch("watchdog_config_test.get_series")
@patch("watchdog_config_test.on_ubuntucore")
@patch("watchdog_config_test.watchdog_service_check")
def test_main_check_service_is_wdt_service_configured(
self,
mock_watchdog_service_check,
mock_on_ubuntucore,
mock_get_series,
mock_watchdog_argparse,
):
# Mock arguments
mock_args = MagicMock()
mock_args.check_time = False
mock_args.check_service = True
mock_watchdog_argparse.return_value = mock_args

# Mock get_series
mock_get_series.return_value = "18.04"

# Mock on_ubuntucore
mock_on_ubuntucore.return_value = False

# Mock watchdog_service_check
mock_watchdog_service_check.return_value = False

# Call the function under test
with patch("builtins.print") as mock_print:
main()

# Assert that the expected message is printed
mock_print.assert_any_call(
"watchdog.service unit does not report as active"
)

0 comments on commit a44a16d

Please sign in to comment.