def test_no_activity(self, mocker) -> None: check = XIdleTime("name", 100, "logind", re.compile(r"a^"), re.compile(r"a^")) mocker.patch.object(check, "_provide_sessions").return_value = [ ("42", "auser"), ] mocker.patch("subprocess.check_output").return_value = "120000" assert check.check() is None
def test_create_no_int(self) -> None: parser = configparser.ConfigParser() parser.read_string( """ [section] timeout = string """ ) with pytest.raises(ConfigurationError): XIdleTime.create("name", parser["section"])
def test_create_unknown_method(self) -> None: parser = configparser.ConfigParser() parser.read_string( """ [section] method = asdfasdf """ ) with pytest.raises(ConfigurationError): XIdleTime.create("name", parser["section"])
def test_create_broken_users_re(self) -> None: parser = configparser.ConfigParser() parser.read_string( """ [section] ignore_users = [[a-9] """ ) with pytest.raises(ConfigurationError): XIdleTime.create("name", parser["section"])
def test_handle_call_error(self, mocker) -> None: check = XIdleTime("name", 100, "logind", re.compile(r"a^"), re.compile(r"a^")) mocker.patch.object(check, "_provide_sessions").return_value = [ ("42", "auser"), ] mocker.patch( "subprocess.check_output", ).side_effect = subprocess.CalledProcessError(2, "foo") with pytest.raises(TemporaryCheckError): check.check()
def test_smoke(self, mocker) -> None: check = XIdleTime("name", 100, "logind", re.compile(r"a^"), re.compile(r"a^")) mocker.patch.object(check, "_provide_sessions").return_value = [ ("42", "auser"), ] co_mock = mocker.patch("subprocess.check_output") co_mock.return_value = "123" res = check.check() assert res is not None assert " 0.123 " in res args, kwargs = co_mock.call_args assert "auser" in args[0] assert kwargs["env"]["DISPLAY"] == ":42" assert "auser" in kwargs["env"]["XAUTHORITY"]
def test_create_default(self) -> None: parser = configparser.ConfigParser() parser.read_string("""[section]""") check = XIdleTime.create("name", parser["section"]) assert check._timeout == 600 assert check._ignore_process_re == re.compile(r"a^") assert check._ignore_users_re == re.compile(r"a^") assert check._provide_sessions == check._list_sessions_sockets
def test_create_default(self): parser = configparser.ConfigParser() parser.read_string('''[section]''') check = XIdleTime.create('name', parser['section']) assert check._timeout == 600 assert check._ignore_process_re == re.compile(r'a^') assert check._ignore_users_re == re.compile(r'a^') assert check._provide_sessions == check._list_sessions_sockets
def test_list_sessions_logind_dbus_error(self, mocker) -> None: mock = mocker.patch("autosuspend.checks.activity.list_logind_sessions") mock.side_effect = LogindDBusException() parser = configparser.ConfigParser() parser.read_string("""[section]""") check = XIdleTime.create("name", parser["section"]) with pytest.raises(TemporaryCheckError): check._list_sessions_logind()
def test_list_sessions_logind(self, mocker): mock = mocker.patch('autosuspend.checks.activity.list_logind_sessions') mock.return_value = [('c1', {'Name': 'foo'}), ('c2', {'Display': 'asdfasf'}), ('c3', {'Name': 'hello', 'Display': 'nonumber'}), ('c4', {'Name': 'hello', 'Display': '3'})] parser = configparser.ConfigParser() parser.read_string('''[section]''') check = XIdleTime.create('name', parser['section']) assert check._list_sessions_logind() == [(3, 'hello')]
def test_create(self): parser = configparser.ConfigParser() parser.read_string('''[section] timeout = 42 ignore_if_process = .*test ignore_users = test.*test method = logind''') check = XIdleTime.create('name', parser['section']) assert check._timeout == 42 assert check._ignore_process_re == re.compile(r'.*test') assert check._ignore_users_re == re.compile(r'test.*test') assert check._provide_sessions == check._list_sessions_logind
def test_list_sessions_logind(self, mocker) -> None: mock = mocker.patch("autosuspend.checks.activity.list_logind_sessions") mock.return_value = [ ("c1", {"Name": "foo"}), ("c2", {"Display": "asdfasf"}), ("c3", {"Name": "hello", "Display": "nonumber"}), ("c4", {"Name": "hello", "Display": "3"}), ] parser = configparser.ConfigParser() parser.read_string("""[section]""") check = XIdleTime.create("name", parser["section"]) assert check._list_sessions_logind() == [(3, "hello")]
def test_multiple_sessions(self, mocker) -> None: check = XIdleTime("name", 100, "logind", re.compile(r"a^"), re.compile(r"a^")) mocker.patch.object(check, "_provide_sessions").return_value = [ ("42", "auser"), ("17", "otheruser"), ] co_mock = mocker.patch("subprocess.check_output") co_mock.side_effect = [ "120000", "123", ] res = check.check() assert res is not None assert " 0.123 " in res assert co_mock.call_count == 2 # check second call for correct values, not checked before args, kwargs = co_mock.call_args_list[1] assert "otheruser" in args[0] assert kwargs["env"]["DISPLAY"] == ":17" assert "otheruser" in kwargs["env"]["XAUTHORITY"]
def test_create(self) -> None: parser = configparser.ConfigParser() parser.read_string( """ [section] timeout = 42 ignore_if_process = .*test ignore_users = test.*test method = logind """ ) check = XIdleTime.create("name", parser["section"]) assert check._timeout == 42 assert check._ignore_process_re == re.compile(r".*test") assert check._ignore_users_re == re.compile(r"test.*test") assert check._provide_sessions == check._list_sessions_logind
def test_list_sessions_socket(self, mocker): mock_glob = mocker.patch('glob.glob') mock_glob.return_value = ['/tmp/.X11-unix/X0', '/tmp/.X11-unix/X42', '/tmp/.X11-unix/Xnum'] stat_return = os.stat(os.path.realpath(__file__)) this_user = pwd.getpwuid(stat_return.st_uid) mock_stat = mocker.patch('os.stat') mock_stat.return_value = stat_return mock_pwd = mocker.patch('pwd.getpwuid') mock_pwd.return_value = this_user parser = configparser.ConfigParser() parser.read_string('''[section]''') check = XIdleTime.create('name', parser['section']) assert check._list_sessions_sockets() == [(0, this_user.pw_name), (42, this_user.pw_name)]
def test_list_sessions_socket(self, mocker) -> None: mock_glob = mocker.patch("glob.glob") mock_glob.return_value = [ "/tmp/.X11-unix/X0", "/tmp/.X11-unix/X42", "/tmp/.X11-unix/Xnum", ] stat_return = os.stat(os.path.realpath(__file__)) this_user = pwd.getpwuid(stat_return.st_uid) mock_stat = mocker.patch("os.stat") mock_stat.return_value = stat_return mock_pwd = mocker.patch("pwd.getpwuid") mock_pwd.return_value = this_user parser = configparser.ConfigParser() parser.read_string("""[section]""") check = XIdleTime.create("name", parser["section"]) assert check._list_sessions_sockets() == [ (0, this_user.pw_name), (42, this_user.pw_name), ]
def test_create_no_int(self): parser = configparser.ConfigParser() parser.read_string('''[section] timeout = string''') with pytest.raises(ConfigurationError): XIdleTime.create('name', parser['section'])
def test_create_broken_users_re(self): parser = configparser.ConfigParser() parser.read_string('''[section] ignore_users = [[a-9]''') with pytest.raises(ConfigurationError): XIdleTime.create('name', parser['section'])
def test_create_unknown_method(self): parser = configparser.ConfigParser() parser.read_string('''[section] method = asdfasdf''') with pytest.raises(ConfigurationError): XIdleTime.create('name', parser['section'])
def create_instance(self, name): return XIdleTime(name, 10, "sockets", None, None)