Merge lp:~canonical-platform-qa/ubuntu-ui-toolkit/systemd into lp:ubuntu-ui-toolkit/staging

Proposed by Leo Arias
Status: Work in progress
Proposed branch: lp:~canonical-platform-qa/ubuntu-ui-toolkit/systemd
Merge into: lp:ubuntu-ui-toolkit/staging
Diff against target: 375 lines (+219/-43)
6 files modified
tests/autopilot/ubuntuuitoolkit/environment.py (+15/-34)
tests/autopilot/ubuntuuitoolkit/init_system/__init__.py (+42/-0)
tests/autopilot/ubuntuuitoolkit/init_system/systemd.py (+76/-0)
tests/autopilot/ubuntuuitoolkit/init_system/upstart.py (+68/-0)
tests/autopilot/ubuntuuitoolkit/tests/__init__.py (+1/-1)
tests/autopilot/ubuntuuitoolkit/tests/test_environment.py (+17/-8)
To merge this branch: bzr merge lp:~canonical-platform-qa/ubuntu-ui-toolkit/systemd
Reviewer Review Type Date Requested Status
Vincent Ladeuil (community) Needs Information
Ubuntu SDK team Pending
Review via email: mp+259210@code.launchpad.net

This proposal supersedes a proposal from 2015-05-15.

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

Wow, the duplication between upstart.py and systemd.py is worrying, can't you factor more ?

Using classes may help to at least define only the diverging methods in the daughter classes (you're more or less using modules as classes here with 'init = init_system.get_init_system_module()' so the transition shouldn't be too hard) ?

review: Needs Information

Unmerged revisions

1506. By Leo Arias

Removed pdb.

1505. By Leo Arias

Added support for systemd. environment variables.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/autopilot/ubuntuuitoolkit/environment.py'
2--- tests/autopilot/ubuntuuitoolkit/environment.py 2014-08-28 19:05:23 +0000
3+++ tests/autopilot/ubuntuuitoolkit/environment.py 2015-05-15 08:59:52 +0000
4@@ -1,6 +1,6 @@
5 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
6 #
7-# Copyright (C) 2014 Canonical Ltd.
8+# Copyright (C) 2014, 2015 Canonical Ltd.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU Lesser General Public License as published by
12@@ -14,13 +14,8 @@
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16-import logging
17-import subprocess
18-
19-from autopilot import logging as autopilot_logging
20-
21-
22-logger = logging.getLogger(__name__)
23+
24+from ubuntuuitoolkit import init_system
25
26
27 def is_initctl_env_var_set(variable, global_=False):
28@@ -32,37 +27,23 @@
29 :return: True if the variable is set. False otherwise.
30
31 """
32- try:
33- get_initctl_env_var(variable, global_)
34- return True
35- except subprocess.CalledProcessError:
36- return False
37+ init = init_system.get_init_system_module()
38+ return init.is_initctl_env_var_set(variable, global_)
39
40
41 def get_initctl_env_var(variable, global_=False):
42 """Return the value of an initctl environment variable."""
43- command = ['/sbin/initctl', 'get-env', variable]
44- if global_:
45- command += ['--global']
46- output = subprocess.check_output(
47- command, stderr=subprocess.STDOUT, universal_newlines=True)
48- return output.rstrip()
49-
50-
51-@autopilot_logging.log_action(logger.info)
52+ init = init_system.get_init_system_module()
53+ return init.get_initctl_env_var(variable, global_)
54+
55+
56 def set_initctl_env_var(variable, value, global_=False):
57 """Set the value of an initctl environment variable."""
58- command = ['/sbin/initctl', 'set-env', '%s=%s' % (variable, value)]
59- if global_:
60- command += ['--global']
61- subprocess.call(command, stderr=subprocess.STDOUT, universal_newlines=True)
62-
63-
64-@autopilot_logging.log_action(logger.info)
65+ init = init_system.get_init_system_module()
66+ init.set_initctl_env_var(variable, value, global_)
67+
68+
69 def unset_initctl_env_var(variable, global_=False):
70 """Remove an initctl environment variable."""
71- command = ['/sbin/initctl', 'unset-env', variable]
72- if global_:
73- command += ['--global']
74- subprocess.call(
75- command, stderr=subprocess.STDOUT, universal_newlines=True)
76+ init = init_system.get_init_system_module()
77+ init.unset_initctl_env_var(variable, global_)
78
79=== added directory 'tests/autopilot/ubuntuuitoolkit/init_system'
80=== added file 'tests/autopilot/ubuntuuitoolkit/init_system/__init__.py'
81--- tests/autopilot/ubuntuuitoolkit/init_system/__init__.py 1970-01-01 00:00:00 +0000
82+++ tests/autopilot/ubuntuuitoolkit/init_system/__init__.py 2015-05-15 08:59:52 +0000
83@@ -0,0 +1,42 @@
84+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
85+#
86+# Copyright (C) 2015 Canonical Ltd.
87+#
88+# This program is free software; you can redistribute it and/or modify
89+# it under the terms of the GNU Lesser General Public License as published by
90+# the Free Software Foundation; version 3.
91+#
92+# This program is distributed in the hope that it will be useful,
93+# but WITHOUT ANY WARRANTY; without even the implied warranty of
94+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95+# GNU Lesser General Public License for more details.
96+#
97+# You should have received a copy of the GNU Lesser General Public License
98+# along with this program. If not, see <http://www.gnu.org/licenses/>.
99+
100+import subprocess
101+
102+
103+_init_system = None
104+
105+
106+def get_init_system_module():
107+ if get_init_system() == 'systemd':
108+ from ubuntuuitoolkit.init_system import systemd
109+ return systemd
110+ else:
111+ from ubuntuuitoolkit.init_system import upstart
112+ return upstart
113+
114+
115+def get_init_system():
116+ global _init_system
117+ if _init_system is None:
118+ # Based on http://ur1.ca/lw3d0
119+ p1_name = subprocess.check_output(
120+ 'ps -p1 -o comm=', shell=True, universal_newlines=True).strip()
121+ if p1_name == 'systemd':
122+ _init_system = p1_name
123+ else:
124+ _init_system = 'upstart'
125+ return _init_system
126
127=== added file 'tests/autopilot/ubuntuuitoolkit/init_system/systemd.py'
128--- tests/autopilot/ubuntuuitoolkit/init_system/systemd.py 1970-01-01 00:00:00 +0000
129+++ tests/autopilot/ubuntuuitoolkit/init_system/systemd.py 2015-05-15 08:59:52 +0000
130@@ -0,0 +1,76 @@
131+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
132+#
133+# Copyright (C) 2015 Canonical Ltd.
134+#
135+# This program is free software; you can redistribute it and/or modify
136+# it under the terms of the GNU Lesser General Public License as published by
137+# the Free Software Foundation; version 3.
138+#
139+# This program is distributed in the hope that it will be useful,
140+# but WITHOUT ANY WARRANTY; without even the implied warranty of
141+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
142+# GNU Lesser General Public License for more details.
143+#
144+# You should have received a copy of the GNU Lesser General Public License
145+# along with this program. If not, see <http://www.gnu.org/licenses/>.
146+
147+import logging
148+import re
149+import subprocess
150+
151+from autopilot import logging as autopilot_logging
152+
153+
154+logger = logging.getLogger(__name__)
155+
156+
157+def is_initctl_env_var_set(variable, global_=False):
158+ """Check True if an initctl environment variable is set.
159+
160+ :param variable: The name of the variable to check.
161+ :param global: if True, the method will operate on the global environment
162+ table. Default is False.
163+ :return: True if the variable is set. False otherwise.
164+
165+ """
166+ variables = _get_initctl_env_vars_dictionary(global_)
167+ return variable in variables
168+
169+
170+def _get_initctl_env_vars_dictionary(global_):
171+ command = ['systemctl', 'show-environment']
172+ if not global_:
173+ command += ['--user']
174+ output = subprocess.check_output(
175+ command, stderr=subprocess.STDOUT, universal_newlines=True).strip()
176+ lines = output.split('\n')
177+ dictionary = {}
178+ for line in lines:
179+ match = re.match('(?P<name>[^=]+)=(?P<value>.*)', line)
180+ dictionary[match.group('name')] = match.group('value')
181+ return dictionary
182+
183+
184+def get_initctl_env_var(variable, global_=False):
185+ """Return the value of an initctl environment variable."""
186+ variables = _get_initctl_env_vars_dictionary(global_)
187+ return variables[variable]
188+
189+
190+@autopilot_logging.log_action(logger.info)
191+def set_initctl_env_var(variable, value, global_=False):
192+ """Set the value of an initctl environment variable."""
193+ command = ['systemctl', 'set-environment', '%s=%s' % (variable, value)]
194+ if not global_:
195+ command += ['--user']
196+ subprocess.call(command, stderr=subprocess.STDOUT, universal_newlines=True)
197+
198+
199+@autopilot_logging.log_action(logger.info)
200+def unset_initctl_env_var(variable, global_=False):
201+ """Remove an initctl environment variable."""
202+ command = ['systemctl', 'unset-environment', variable]
203+ if not global_:
204+ command += ['--user']
205+ subprocess.call(
206+ command, stderr=subprocess.STDOUT, universal_newlines=True)
207
208=== added file 'tests/autopilot/ubuntuuitoolkit/init_system/upstart.py'
209--- tests/autopilot/ubuntuuitoolkit/init_system/upstart.py 1970-01-01 00:00:00 +0000
210+++ tests/autopilot/ubuntuuitoolkit/init_system/upstart.py 2015-05-15 08:59:52 +0000
211@@ -0,0 +1,68 @@
212+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
213+#
214+# Copyright (C) 2014, 2015 Canonical Ltd.
215+#
216+# This program is free software; you can redistribute it and/or modify
217+# it under the terms of the GNU Lesser General Public License as published by
218+# the Free Software Foundation; version 3.
219+#
220+# This program is distributed in the hope that it will be useful,
221+# but WITHOUT ANY WARRANTY; without even the implied warranty of
222+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
223+# GNU Lesser General Public License for more details.
224+#
225+# You should have received a copy of the GNU Lesser General Public License
226+# along with this program. If not, see <http://www.gnu.org/licenses/>.
227+
228+import logging
229+import subprocess
230+
231+from autopilot import logging as autopilot_logging
232+
233+
234+logger = logging.getLogger(__name__)
235+
236+
237+def is_initctl_env_var_set(variable, global_=False):
238+ """Check True if an initctl environment variable is set.
239+
240+ :param variable: The name of the variable to check.
241+ :param global: if True, the method will operate on the global environment
242+ table. Default is False.
243+ :return: True if the variable is set. False otherwise.
244+
245+ """
246+ try:
247+ get_initctl_env_var(variable, global_)
248+ return True
249+ except subprocess.CalledProcessError:
250+ return False
251+
252+
253+def get_initctl_env_var(variable, global_=False):
254+ """Return the value of an initctl environment variable."""
255+ command = ['/sbin/initctl', 'get-env', variable]
256+ if global_:
257+ command += ['--global']
258+ output = subprocess.check_output(
259+ command, stderr=subprocess.STDOUT, universal_newlines=True)
260+ return output.rstrip()
261+
262+
263+@autopilot_logging.log_action(logger.info)
264+def set_initctl_env_var(variable, value, global_=False):
265+ """Set the value of an initctl environment variable."""
266+ command = ['/sbin/initctl', 'set-env', '%s=%s' % (variable, value)]
267+ if global_:
268+ command += ['--global']
269+ subprocess.call(command, stderr=subprocess.STDOUT, universal_newlines=True)
270+
271+
272+@autopilot_logging.log_action(logger.info)
273+def unset_initctl_env_var(variable, global_=False):
274+ """Remove an initctl environment variable."""
275+ command = ['/sbin/initctl', 'unset-env', variable]
276+ if global_:
277+ command += ['--global']
278+ subprocess.call(
279+ command, stderr=subprocess.STDOUT, universal_newlines=True)
280
281=== modified file 'tests/autopilot/ubuntuuitoolkit/tests/__init__.py'
282--- tests/autopilot/ubuntuuitoolkit/tests/__init__.py 2015-04-14 21:02:06 +0000
283+++ tests/autopilot/ubuntuuitoolkit/tests/__init__.py 2015-05-15 08:59:52 +0000
284@@ -116,7 +116,7 @@
285 'QML2_IMPORT_PATH',
286 'UBUNTU_UI_TOOLKIT_THEMES_PATH'
287 ]
288- kwargs = {'global_': True}
289+ kwargs = {'global_': False}
290 for env in env_vars:
291 kwargs[env] = local_modules_path
292 self.useFixture(fixture_setup.InitctlEnvironmentVariable(**kwargs))
293
294=== modified file 'tests/autopilot/ubuntuuitoolkit/tests/test_environment.py'
295--- tests/autopilot/ubuntuuitoolkit/tests/test_environment.py 2014-08-29 16:12:02 +0000
296+++ tests/autopilot/ubuntuuitoolkit/tests/test_environment.py 2015-05-15 08:59:52 +0000
297@@ -23,14 +23,18 @@
298
299 class InitctlEnvironmentVariableTestCase(testtools.TestCase):
300
301+ def get_unique_identifier(self):
302+ return str(uuid.uuid1()).replace('-', '_')
303+
304 def test_is_environment_variable_set_with_unset_variable(self):
305 """Test that is_initctl_env_var_set returns False for unset vars."""
306- variable = 'I do not exist {}'.format(uuid.uuid1())
307+ variable = 'I_do_not_exist_{}'.format(self.get_unique_identifier())
308 self.assertFalse(environment.is_initctl_env_var_set(variable))
309
310 def test_is_environment_variable_set_with_set_variable(self):
311 """Test that is_initctl_env_var_set returns True for existing vars."""
312- variable = 'Test variable to set {}'.format(uuid.uuid1())
313+ variable = 'Test_variable_to_set_{}'.format(
314+ self.get_unique_identifier())
315 self.addCleanup(environment.unset_initctl_env_var, variable)
316
317 environment.set_initctl_env_var(variable, 'dummy')
318@@ -39,7 +43,8 @@
319
320 def test_get_environment_variable(self):
321 """Test that get_initctl_env_var returns the right value."""
322- variable = 'Test variable to get {}'.format(uuid.uuid1())
323+ variable = 'Test_variable_to_get_{}'.format(
324+ self.get_unique_identifier())
325 self.addCleanup(environment.unset_initctl_env_var, variable)
326 environment.set_initctl_env_var(variable, 'test value')
327
328@@ -48,7 +53,8 @@
329
330 def test_unset_environment_variable(self):
331 """Test that unset_initctl_env_var removes the variable."""
332- variable = 'Test variable to unset {}'.format(uuid.uuid1())
333+ variable = 'Test_variable_to_unset_{}'.format(
334+ self.get_unique_identifier())
335 environment.set_initctl_env_var(variable, 'dummy')
336
337 environment.unset_initctl_env_var(variable)
338@@ -57,7 +63,7 @@
339
340 def test_unset_environment_variable_with_unset_variable(self):
341 """Test that unset_initctl_env_var does nothing with unset var."""
342- variable = 'I do not exist {}'.format(uuid.uuid1())
343+ variable = 'I_do_not_exist_{}'.format(self.get_unique_identifier())
344
345 environment.unset_initctl_env_var(variable)
346
347@@ -65,14 +71,16 @@
348
349 def test_is_global_environment_variable_set_with_unset_variable(self):
350 """Test is_initctl_env_var_set returns False for unset global vars."""
351- variable = 'I do not exist global {}'.format(uuid.uuid1())
352+ variable = 'I_do_not_exist_global_{}'.format(
353+ self.get_unique_identifier())
354
355 self.assertFalse(environment.is_initctl_env_var_set(
356 variable, global_=True))
357
358 def test_get_global_environment_variable(self):
359 """Test that get_initctl_env_var returns the right global value."""
360- variable = 'Test variable to get {}'.format(uuid.uuid1())
361+ variable = 'Test_variable_to_get_{}'.format(
362+ self.get_unique_identifier())
363 self.addCleanup(
364 environment.unset_initctl_env_var, variable, global_=True)
365 environment.set_initctl_env_var(variable, 'test value', global_=True)
366@@ -83,7 +91,8 @@
367
368 def test_unset_global_environment_variable(self):
369 """Test that unset_initctl_env_var removes the global variable."""
370- variable = 'Test variable to unset {}'.format(uuid.uuid1())
371+ variable = 'Test_variable_to_unset_{}'.format(
372+ self.get_unique_identifier())
373
374 environment.set_initctl_env_var(variable, 'dummy', global_=True)
375 environment.unset_initctl_env_var(variable, global_=True)

Subscribers

People subscribed via source and target branches