import pytest from argparse import Namespace from unittest.mock import call, mock_open, patch import spoc @patch('builtins.open', new_callable=mock_open, read_data='foo\0arg1\0arg2\n') def test_print_lock(cmdline_open, capsys): spoc.print_lock('123') cmdline_open.assert_called_once_with('/proc/123/cmdline') captured = capsys.readouterr() assert captured.out == 'Waiting for lock currently held by process 123 - foo arg1 arg2\n' @patch('spoc.podman.get_apps', return_value={'anotherapp': '0.1', 'someapp': '0.1'}) def test_listing_installed(get_apps, capsys): spoc.listing('installed') get_apps.assert_called_once() # Order is important here captured = capsys.readouterr() assert captured.out == 'anotherapp 0.1\nsomeapp 0.1\n' @patch('spoc.repo.get_apps') def test_listing_online(get_apps): spoc.listing('online') get_apps.assert_called_once() @patch('spoc.repo.get_apps', return_value={'someapp': {'version': '0.2'}, 'anotherapp': {'version': '0.1'}}) @patch('spoc.podman.get_apps', return_value={'someapp': '0.1'}) def test_listing_updates(repo_get_apps, podman_get_apps, capsys): spoc.listing('updates') repo_get_apps.assert_called_once() podman_get_apps.assert_called_once() captured = capsys.readouterr() assert captured.out == 'someapp 0.1 -> 0.2\n' @patch('spoc.repo.get_apps') @patch('spoc.podman.get_apps') def test_listing_invalid(repo_get_apps, podman_get_apps, capsys): spoc.listing('invalid') repo_get_apps.assert_not_called() podman_get_apps.assert_not_called() captured = capsys.readouterr() assert captured.out == '' @patch('spoc.app.install') def test_install(app_install): spoc.install('someapp') app_install.assert_called_once_with('someapp') @patch('spoc.app.update') def test_update(app_update): spoc.update('someapp') app_update.assert_called_once_with('someapp') @patch('spoc.app.uninstall') def test_uninstall(app_uninstall): spoc.uninstall('someapp') app_uninstall.assert_called_once_with('someapp') @patch('spoc.podman.start_pod') def test_start(start_pod): spoc.start('someapp') start_pod.assert_called_once_with('someapp') @patch('spoc.podman.stop_pod') def test_stop(stop_pod): spoc.stop('someapp') stop_pod.assert_called_once_with('someapp') @patch('spoc.podman.get_pod_status', return_value='RESULT') def test_status(get_pod_status, capsys): spoc.status('someapp') get_pod_status.assert_called_once_with('someapp') captured = capsys.readouterr() assert captured.out == 'RESULT\n' @pytest.mark.parametrize('value,expected',[ ('1', True), ('on', True), ('Enable', True), ('TRUE', True), ('whatever', False), ]) @patch('spoc.autostart.set_app') def test_set_autostart(set_app, value, expected): spoc.set_autostart('someapp', value) set_app.assert_called_once_with('someapp', expected) @patch('spoc.autostart.get_apps', return_value={'someapp', 'anotherapp'}) @patch('spoc.podman.start_pod') def test_start_autostarted(start_pod, get_apps): spoc.start_autostarted() get_apps.assert_called_once() start_pod.assert_has_calls([ call('someapp'), call('anotherapp'), ], any_order=True) @patch('spoc.podman.get_apps', return_value={'someapp': '0.1', 'anotherapp': '0.1'}) @patch('spoc.podman.stop_pod') def test_stop_all(stop_pod, get_apps): spoc.stop_all() get_apps.assert_called_once() stop_pod.assert_has_calls([ call('someapp'), call('anotherapp'), ], any_order=True) @patch('spoc.podman.prune') def test_prune(prune): spoc.prune() prune.assert_called_once() @patch('sys.argv', ['foo', 'list']) @patch('spoc.listing') def test_main_listing(listing): spoc.main() listing.assert_called_once_with('installed') @patch('sys.argv', ['foo', 'list', 'online']) @patch('spoc.listing') def test_main_listing_online(listing): spoc.main() listing.assert_called_once_with('online') @patch('sys.argv', ['foo', 'install', 'someapp']) @patch('spoc.install') def test_main_install(install): spoc.main() install.assert_called_once_with('someapp') @patch('sys.argv', ['foo', 'update', 'someapp']) @patch('spoc.update') def test_main_update(update): spoc.main() update.assert_called_once_with('someapp') @patch('sys.argv', ['foo', 'uninstall', 'someapp']) @patch('spoc.uninstall') def test_main_uninstall(uninstall): spoc.main() uninstall.assert_called_once_with('someapp') @patch('sys.argv', ['foo', 'start', 'someapp']) @patch('spoc.start') def test_main_start(start): spoc.main() start.assert_called_once_with('someapp') @patch('sys.argv', ['foo', 'stop', 'someapp']) @patch('spoc.stop') def test_main_stop(stop): spoc.main() stop.assert_called_once_with('someapp') @patch('sys.argv', ['foo', 'status', 'someapp']) @patch('spoc.status') def test_main_status(status): spoc.main() status.assert_called_once_with('someapp') @patch('sys.argv', ['foo', 'status']) @patch('spoc.status') def test_main_status_all(status): spoc.main() status.assert_called_once_with(None) @patch('sys.argv', ['foo', 'autostart', 'someapp', 'on']) @patch('spoc.set_autostart') def test_main_autostart(autostart): spoc.main() autostart.assert_called_once_with('someapp', 'on') @patch('sys.argv', ['foo', 'start-autostarted']) @patch('spoc.start_autostarted') def test_main_start_autostarted(start_autostarted): spoc.main() start_autostarted.assert_called_once() @patch('sys.argv', ['foo', 'stop-all']) @patch('spoc.stop_all') def test_main_stop_all(stop_all): spoc.main() stop_all.assert_called_once() @patch('sys.argv', ['foo', 'prune']) @patch('spoc.prune') def test_main_prune(prune): spoc.main() prune.assert_called_once() @patch('spoc.parse_args', return_value=Namespace(action=None)) @patch('spoc.listing') @patch('spoc.install') @patch('spoc.update') @patch('spoc.uninstall') @patch('spoc.start') @patch('spoc.stop') @patch('spoc.status') @patch('spoc.start_autostarted') @patch('spoc.stop_all') @patch('spoc.prune') def test_main_invalid(prune, stop_all, start_autostarted, status, stop, start, uninstall, update, install, listing, parse_args): spoc.main() parse_args.assert_called_once() listing.assert_not_called() install.assert_not_called() update.assert_not_called() uninstall.assert_not_called() start.assert_not_called() stop.assert_not_called() status.assert_not_called() start_autostarted.assert_not_called() stop_all.assert_not_called() prune.assert_not_called() @pytest.mark.parametrize('argv', [ ['list', 'invalid'], ['install'], ['update'], ['uninstall'], ['start'], ['stop'], ['autostart'], ['autostart', 'someapp'], ['invalid'], ]) def test_main_systemexit(argv): argv.insert(0, 'foo') with patch('sys.argv', argv): with pytest.raises(SystemExit): spoc.main()