Unity 8
 All Classes Functions Properties
test_upstart.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2013 Canonical
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 
20 """Tests for upstart integration."""
21 
22 import os
23 import signal
24 import subprocess
25 import time
26 
27 from testtools.matchers._basic import Equals
28 from autopilot.matchers import Eventually
29 from autopilot.introspection import get_proxy_object_for_existing_process
30 
31 from unity8 import get_binary_path
32 from unity8.shell.emulators import UnityEmulatorBase
33 from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
34 
35 import logging
36 
37 logger = logging.getLogger(__name__)
38 
39 # from __future__ import range
40 # (python3's range, is same as python2's xrange)
41 import sys
42 if sys.version_info < (3,):
43  range = xrange
44 
45 class UpstartIntegrationTests(UnityTestCase):
46 
47  scenarios = _get_device_emulation_scenarios()
48 
49  def _get_status(self):
50  pid, status = os.waitpid(self.process.pid, os.WUNTRACED | os.WCONTINUED | os.WNOHANG)
51  logger.debug("Got status: {0}; stopped: {1}; continued: {2}"\
52  .format(status, os.WIFSTOPPED(status), os.WIFCONTINUED(status)))
53  return status
54 
55  def _launch_unity(self):
56  self.patch_environment("QT_LOAD_TESTABILITY", "1")
57  self.process = subprocess.Popen([get_binary_path()] + self.unity_geometry_args)
58  def ensure_stopped():
59  self.process.terminate()
60  for i in range(10):
61  try:
62  self._get_status()
63  except OSError:
64  break
65  else:
66  time.sleep(1)
67  try:
68  self._get_status()
69  except OSError:
70  pass
71  else:
72  self.process.kill()
73 
74  self.process.wait()
75 
76  self.addCleanup(ensure_stopped)
77 
78  def _set_proxy(self):
79  super(UpstartIntegrationTests, self)._set_proxy(get_proxy_object_for_existing_process(
80  pid=self.process.pid,
81  emulator_base=UnityEmulatorBase,
82  ))
83 
84  def test_no_sigstop(self):
85  self.patch_environment("UPSTART_JOB", "foo")
86  self._launch_unity()
87  self._set_proxy()
88 
89  logger.debug("Unity started, waiting for it to be ready.")
90  self.assertUnityReady()
91  logger.debug("Unity loaded and ready.")
92 
93  def test_expect_sigstop(self):
94  self.patch_environment("UPSTART_JOB", "unity8")
95  self._launch_unity()
96  self.assertThat(lambda: os.WIFSTOPPED(self._get_status()), Eventually(Equals(True)), "Unity8 should raise SIGSTOP when ready")
97 
98  self.process.send_signal(signal.SIGCONT)
99  self.assertThat(lambda: os.WIFCONTINUED(self._get_status()), Eventually(Equals(True)), "Unity8 should have resumed")
100 
101  logger.debug("Unity started, waiting for it to be ready.")
102  self._set_proxy()
103  self.assertUnityReady()
104  logger.debug("Unity loaded and ready.")