diff --git a/providers/base/bin/desktop_session.py b/providers/base/bin/desktop_session.py new file mode 100755 index 000000000..d1570f2ba --- /dev/null +++ b/providers/base/bin/desktop_session.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# Copyright 2024 Canonical Ltd. +# All rights reserved. +# +# Written by: +# Paolo Gentili + +import argparse +import os + + +def resources(): + """ + Return whether there's a Desktop session and its type. + """ + is_desktop_session = os.getenv("XDG_CURRENT_DESKTOP") is not None + print("desktop_session: {}".format(is_desktop_session)) + print("session_type: {}".format(os.getenv("XDG_SESSION_TYPE"))) + + +def main(argv): + """ + Retrieve information about the current desktop session. + """ + + parser = argparse.ArgumentParser() + parser.add_argument("command", choices=["resources"]) + args = parser.parse_args(argv) + + if args.command == "resources": + return resources() + + +if __name__ == "__main__": + import sys + + main(sys.argv[1:]) diff --git a/providers/base/tests/test_desktop_session.py b/providers/base/tests/test_desktop_session.py new file mode 100644 index 000000000..dd396f296 --- /dev/null +++ b/providers/base/tests/test_desktop_session.py @@ -0,0 +1,55 @@ +"""This module provides test cases for the desktop_session module.""" + +import os +import unittest +from unittest.mock import call, patch + +import desktop_session + + +class DesktopSessionTests(unittest.TestCase): + """Tests for the desktop_session module.""" + + @patch("desktop_session.resources") + def test_main(self, mock_resources): + """ + Test whether the main function calls the resources + function when requested via CLI. + """ + desktop_session.main(["resources"]) + mock_resources.assert_called_once_with() + + @patch("builtins.print") + def test_resources_server(self, mock_print): + """Test the result faking a server session.""" + + server_session = { + "XDG_SESSION_TYPE": "tty", + } + with patch.dict(os.environ, server_session, clear=True): + desktop_session.resources() + + mock_print.assert_has_calls( + [ + call("desktop_session: False"), + call("session_type: tty"), + ] + ) + + @patch("builtins.print") + def test_resources_desktop(self, mock_print): + """Test the result faking a desktop session.""" + + server_session = { + "XDG_SESSION_TYPE": "wayland", + "XDG_CURRENT_DESKTOP": "hyprland", + } + with patch.dict(os.environ, server_session, clear=True): + desktop_session.resources() + + mock_print.assert_has_calls( + [ + call("desktop_session: True"), + call("session_type: wayland"), + ] + ) diff --git a/providers/base/units/desktop/resource.pxu b/providers/base/units/desktop/resource.pxu new file mode 100644 index 000000000..f89d70155 --- /dev/null +++ b/providers/base/units/desktop/resource.pxu @@ -0,0 +1,6 @@ +id: desktop_session +plugin: resource +category_id: com.canonical.plainbox::info +_summary: Check whether a desktop session is available and of which type. +environ: XDG_SESSION_TYPE XDG_CURRENT_DESKTOP +command: desktop_session.py resources diff --git a/providers/base/units/zapper/jobs.pxu b/providers/base/units/zapper/jobs.pxu index 62537aea7..54859cb4a 100644 --- a/providers/base/units/zapper/jobs.pxu +++ b/providers/base/units/zapper/jobs.pxu @@ -1,5 +1,10 @@ id: monitor/zapper-edid -requires: zapper_capabilities.capability == 'hdmi-capture' and zapper_capabilities.edid_cycling == 'True' +template-resource: zapper_capabilities desktop_session +requires: + zapper_capabilities.capability == 'hdmi-capture' + zapper_capabilities.edid_cycling == 'True' + desktop_session.desktop_session == 'True' + desktop_session.session_type in ['x11', 'wayland'] category_id: com.canonical.plainbox::monitor plugin: shell estimated_duration: 60 diff --git a/providers/base/units/zapper/test-plan.pxu b/providers/base/units/zapper/test-plan.pxu index fa77d2dfc..d6d2fe984 100644 --- a/providers/base/units/zapper/test-plan.pxu +++ b/providers/base/units/zapper/test-plan.pxu @@ -8,7 +8,7 @@ nested_part: id: zapper-enabled-automated unit: test plan -_name: Tests using Zapper +_name: Tests using Zapper (automated) _description: Tests using Zapper include: bluetooth/zapper-a2dp