Unity 8
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, 2014, 2015 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 stat
24 import signal
25 import subprocess
26 import time
27 
28 import fixtures
29 import ubuntuuitoolkit
30 from autopilot.matchers import Eventually
31 from autopilot.introspection import get_proxy_object_for_existing_process
32 from testtools.matchers import Equals, MismatchError
33 from ubuntuuitoolkit import ubuntu_scenarios
34 
35 from unity8 import get_binary_path
36 from unity8.shell.tests import UnityTestCase
37 
38 import logging
39 
40 logger = logging.getLogger(__name__)
41 
42 
43 class UpstartIntegrationTests(UnityTestCase):
44 
45  scenarios = ubuntu_scenarios.get_device_simulation_scenarios()
46 
47  def _get_status(self):
48  pid, status = os.waitpid(
49  self.process.pid, os.WUNTRACED | os.WCONTINUED | os.WNOHANG)
50  logger.debug(
51  "Got status: {0}; stopped: {1}; continued: {2}".format(
52  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 
58  try:
59  host_socket = os.getenv("MIR_SOCKET", "/run/mir_socket")
60  if stat.S_ISSOCK(os.stat(host_socket).st_mode):
61  self.patch_environment("MIR_SERVER_HOST_SOCKET",
62  host_socket)
63  socket = os.path.join(os.getenv("XDG_RUNTIME_DIR", "/tmp"),
64  "mir_socket")
65  self.patch_environment("MIR_SERVER_FILE", socket)
66  except OSError:
67  pass
68 
69  self.process = subprocess.Popen(
70  [get_binary_path()] + self.unity_geometry_args)
71 
72  def ensure_stopped():
73  self.process.terminate()
74  for i in range(10):
75  try:
76  self._get_status()
77  except OSError:
78  break
79  else:
80  time.sleep(1)
81  try:
82  self._get_status()
83  except OSError:
84  pass
85  else:
86  self.process.kill()
87 
88  self.process.wait()
89 
90  self.addCleanup(ensure_stopped)
91 
92  def _set_proxy(self):
93  proxy_base = ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase
94  super()._set_proxy(
95  get_proxy_object_for_existing_process(
96  pid=self.process.pid,
97  emulator_base=proxy_base))
98 
99  def test_no_sigstop(self):
100  self.useFixture(
101  fixtures.EnvironmentVariable(
102  'UNITY_MIR_EMITS_SIGSTOP', newvalue=None))
103  self._launch_unity()
104 
105  try:
106  self.assertThat(
107  lambda: os.WIFSTOPPED(self._get_status()),
108  Eventually(Equals(True)))
109  except MismatchError:
110  pass
111  else:
112  self.process.send_signal(signal.SIGCONT)
113  self.fail('Unity8 raised SIGSTOP')
114 
115  self._set_proxy()
116 
117  logger.debug("Unity started, waiting for it to be ready.")
118  self.wait_for_unity()
119  logger.debug("Unity loaded and ready.")
120 
121  def test_expect_sigstop(self):
122  self.useFixture(
123  fixtures.EnvironmentVariable('UNITY_MIR_EMITS_SIGSTOP', '1'))
124  self._launch_unity()
125  self.assertThat(
126  lambda: os.WIFSTOPPED(self._get_status()),
127  Eventually(Equals(True)), "Unity8 should raise SIGSTOP when ready")
128 
129  self.process.send_signal(signal.SIGCONT)
130  self.assertThat(
131  lambda: os.WIFCONTINUED(self._get_status()),
132  Eventually(Equals(True)), "Unity8 should have resumed")
133 
134  logger.debug("Unity started, waiting for it to be ready.")
135  self._set_proxy()
136  self.wait_for_unity()
137  logger.debug("Unity loaded and ready.")