Skip to content

Commit

Permalink
Skip EDID test if there's no desktop session running (BugFix) (#1404)
Browse files Browse the repository at this point in the history
* Add: desktop_session resource

* Change: EDID job requires a desktop_session

* Fix: test case for main

* Fix: zapper automated testplan name
  • Loading branch information
p-gentili authored Aug 13, 2024
1 parent 975562b commit 4123afe
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
37 changes: 37 additions & 0 deletions providers/base/bin/desktop_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
# Copyright 2024 Canonical Ltd.
# All rights reserved.
#
# Written by:
# Paolo Gentili <[email protected]>

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:])
55 changes: 55 additions & 0 deletions providers/base/tests/test_desktop_session.py
Original file line number Diff line number Diff line change
@@ -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"),
]
)
6 changes: 6 additions & 0 deletions providers/base/units/desktop/resource.pxu
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion providers/base/units/zapper/jobs.pxu
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion providers/base/units/zapper/test-plan.pxu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4123afe

Please sign in to comment.