From 0ef45fb30b7aec3572e7e742654b81692374cf33 Mon Sep 17 00:00:00 2001 From: Paolo Gentili Date: Thu, 8 Aug 2024 15:52:56 +0200 Subject: [PATCH 1/4] Add: desktop_session resource --- providers/base/bin/desktop_session.py | 36 +++++++++++++++ providers/base/tests/test_desktop_session.py | 46 ++++++++++++++++++++ providers/base/units/desktop/resource.pxu | 6 +++ 3 files changed, 88 insertions(+) create mode 100755 providers/base/bin/desktop_session.py create mode 100644 providers/base/tests/test_desktop_session.py create mode 100644 providers/base/units/desktop/resource.pxu diff --git a/providers/base/bin/desktop_session.py b/providers/base/bin/desktop_session.py new file mode 100755 index 0000000000..13535ed1a9 --- /dev/null +++ b/providers/base/bin/desktop_session.py @@ -0,0 +1,36 @@ +#!/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(args=None): + """ + Retrieve information about the current desktop session. + """ + + if not args: + parser = argparse.ArgumentParser() + parser.add_argument("command", choices=["resources"]) + args = parser.parse_args() + + if args.command == "resources": + return resources() + + +if __name__ == "__main__": + main() diff --git a/providers/base/tests/test_desktop_session.py b/providers/base/tests/test_desktop_session.py new file mode 100644 index 0000000000..145afa45ff --- /dev/null +++ b/providers/base/tests/test_desktop_session.py @@ -0,0 +1,46 @@ +"""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("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 0000000000..f89d70155e --- /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 From dbb7d8c616951e5f3aee6bd633a5fdae3d64c364 Mon Sep 17 00:00:00 2001 From: Paolo Gentili Date: Thu, 8 Aug 2024 15:53:14 +0200 Subject: [PATCH 2/4] Change: EDID job requires a desktop_session --- providers/base/units/zapper/jobs.pxu | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/providers/base/units/zapper/jobs.pxu b/providers/base/units/zapper/jobs.pxu index 62537aea70..54859cb4a3 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 From eabe06fcbf4408cd69296a9425fcec9ff9dea31e Mon Sep 17 00:00:00 2001 From: Paolo Gentili Date: Thu, 8 Aug 2024 16:29:02 +0200 Subject: [PATCH 3/4] Fix: test case for main --- providers/base/bin/desktop_session.py | 13 +++++++------ providers/base/tests/test_desktop_session.py | 9 +++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/providers/base/bin/desktop_session.py b/providers/base/bin/desktop_session.py index 13535ed1a9..d1570f2bab 100755 --- a/providers/base/bin/desktop_session.py +++ b/providers/base/bin/desktop_session.py @@ -18,19 +18,20 @@ def resources(): print("session_type: {}".format(os.getenv("XDG_SESSION_TYPE"))) -def main(args=None): +def main(argv): """ Retrieve information about the current desktop session. """ - if not args: - parser = argparse.ArgumentParser() - parser.add_argument("command", choices=["resources"]) - args = parser.parse_args() + parser = argparse.ArgumentParser() + parser.add_argument("command", choices=["resources"]) + args = parser.parse_args(argv) if args.command == "resources": return resources() if __name__ == "__main__": - 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 index 145afa45ff..dd396f296f 100644 --- a/providers/base/tests/test_desktop_session.py +++ b/providers/base/tests/test_desktop_session.py @@ -10,6 +10,15 @@ 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.""" From f4c9725f8433603c2f76fbfd0237ebdecada3299 Mon Sep 17 00:00:00 2001 From: Paolo Gentili Date: Mon, 12 Aug 2024 09:15:33 +0200 Subject: [PATCH 4/4] Fix: zapper automated testplan name --- providers/base/units/zapper/test-plan.pxu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/base/units/zapper/test-plan.pxu b/providers/base/units/zapper/test-plan.pxu index fa77d2dfc9..d6d2fe9846 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