Merge lp:~nskaggs/music-app/fix-1292044 into lp:music-app/trusty

Proposed by Nicholas Skaggs
Status: Merged
Approved by: Nicholas Skaggs
Approved revision: 386
Merged at revision: 441
Proposed branch: lp:~nskaggs/music-app/fix-1292044
Merge into: lp:music-app/trusty
Diff against target: 191 lines (+25/-81)
1 file modified
tests/autopilot/music_app/tests/__init__.py (+25/-81)
To merge this branch: bzr merge lp:~nskaggs/music-app/fix-1292044
Reviewer Review Type Date Requested Status
Victor Thompson Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Review via email: mp+211637@code.launchpad.net

Commit message

Fix phablet device test runs not using mocked home

Description of the change

Fix phablet device test runs not using mocked home

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Victor Thompson (vthompson) wrote :

I ran a click test against this change. I think there's instance(s) of calls to 'temp_move_sqlite_db()'--which you are removing.

======================================================================
ERROR: music_app.tests.test_music.TestMainWindow.test_create_playlist_from_songs_tab(with touch)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/phablet/autopilot/music_app/tests/test_music.py", line 26, in setUp
super(TestMainWindow, self).setUp()
File "/home/phablet/autopilot/music_app/tests/__init__.py", line 68, in setUp
self.temp_move_sqlite_db()
AttributeError: 'TestMainWindow' object has no attribute 'temp_move_sqlite_db'

review: Needs Fixing
Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

Yes, sorry, this is still WIP :-)

Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

So this simply removes not using the mocking process on devices. It seems like mediascanner doesn't take kindly to the changes on the device and although everything is lined up, music doesn't see any music when run.

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Victor Thompson (vthompson) wrote :

Here's a comment from Sergio about getting the click home directory mocking to work [1]:

"You can't mock the env with click as it's launched with upstart; in any case you could inject stuff in upstart with initctl set-env"

[1] https://code.launchpad.net/~vthompson/music-app/fix-1293488/+merge/211435/comments/499800

Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

Sounds like Leo has a proper solution for us here:

https://code.launchpad.net/~elopio/ubuntu-ui-toolkit/initctl_env_var/+merge/208612

I guess we can deal with the pain till it lands and use it eh?

Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

Setting to WIP until then.

Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

initctl_env has been added to the sdk autopilot helper, so going to update the MP using this.

lp:~nskaggs/music-app/fix-1292044 updated
383. By Nicholas Skaggs

change str replace func

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
lp:~nskaggs/music-app/fix-1292044 updated
384. By Nicholas Skaggs

more tweaks with str replace ;-)

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

Python3, 1; me, 0.

I <3 UnicodeDecodeError.

lp:~nskaggs/music-app/fix-1292044 updated
385. By Nicholas Skaggs

files are binary, d'oh

Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

This properly runs on my device now :-) Due note jenkins is still running using fake env, so the review really needs to happen on a real device so you can confirm the bug is fixed.

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Victor Thompson (vthompson) wrote :

Do we need to add another dependency to the app? Or do I need a certain version of the toolkit? When I run I get the following error from phablet-test-run. Note this is on Trusty image r303:

======================================================================
ERROR: unittest.loader.ModuleImportFailure.music_app.tests.test_music
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/autopilot/run.py", line 420, in _filter_tests
test.debug()
File "/usr/lib/python3.4/unittest/case.py", line 627, in debug
getattr(self, self._testMethodName)()
File "/usr/lib/python3.4/unittest/loader.py", line 32, in testFailure
raise exception
ImportError: Failed to import test module: music_app.tests.test_music
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python3.4/unittest/loader.py", line 290, in _get_module_from_name
__import__(name)
File "/home/phablet/autopilot/music_app/tests/__init__.py", line 28, in <module>
from ubuntuuitoolkit import (
ImportError: cannot import name 'environment'

Installed ubuntu-ui-toolkit-plugin version:

$apt-cache policy qtdeclarative5-ubuntu-ui-toolkit-plugin
qtdeclarative5-ubuntu-ui-toolkit-plugin:
  Installed: 0.1.46+14.04.20140408.1-0ubuntu1
  Candidate: 0.1.46+14.04.20140408.1-0ubuntu1
  Version table:
 *** 0.1.46+14.04.20140408.1-0ubuntu1 0
        500 http://ports.ubuntu.com/ubuntu-ports/ trusty/main armhf Packages
        100 /var/lib/dpkg/status

review: Needs Information
Revision history for this message
Victor Thompson (vthompson) wrote :

Tests pass on the device. However, after running the tests the next launch of the app says that no music can be found. I'll dig a bit more, but the ~/Music directory is still intact and ~/.cache/mediascanner also seems to still be in tact.

Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

I'll have a look.. I'm assuming you are saying the tests aren't cleaning up well after running.

Revision history for this message
Victor Thompson (vthompson) wrote :

Correct, and IIRC everything went back to normal after a reboot.

Revision history for this message
Victor Thompson (vthompson) wrote :

I verified again and a reboot fixed it. The contents of the mediascanner db was identical before and after the reboot... So I don't know what the issue is. Perhaps the environment faking is becoming permanent until we reboot? If we truly can't figure out what's going on, I'm perfectly fine approving this so we only need to reboot to return to normalcy. I'll start reviewing the code changes.

Revision history for this message
Victor Thompson (vthompson) wrote :

I reviewed the code changes and I don't see any issues. To fix the cleanup issue, this seems to be all that is needed:

=== modified file 'tests/autopilot/music_app/tests/__init__.py'
--- tests/autopilot/music_app/tests/__init__.py 2014-04-22 19:54:51 +0000
+++ tests/autopilot/music_app/tests/__init__.py 2014-04-30 12:01:14 +0000
@@ -118,6 +118,7 @@
         #click can use initctl env (upstart), but desktop still requires mock
         if self.test_type == 'click':
             environment.set_initctl_env_var('HOME', temp_dir)
+ self.addCleanup(environment.unset_initctl_env_var, 'HOME')
         else:
             patcher = mock.patch.dict('os.environ', {'HOME': temp_dir})
             patcher.start()

Once that is in place, I think we can merge this branch.

Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

vthompson, sorry didn't get to this, but yes that finishes it. Need to tell the env to cleanup as well, hah!

lp:~nskaggs/music-app/fix-1292044 updated
386. By Nicholas Skaggs

ty Victor; adding cleanup step so env cleans up after too!

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Victor Thompson (vthompson) wrote :

Looks good! Thanks for doing this--I will no longer shudder at the thought of testing the app!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/autopilot/music_app/tests/__init__.py'
2--- tests/autopilot/music_app/tests/__init__.py 2014-03-07 20:10:34 +0000
3+++ tests/autopilot/music_app/tests/__init__.py 2014-04-30 15:25:17 +0000
4@@ -8,7 +8,6 @@
5 """Music app autopilot tests."""
6
7 import tempfile
8-
9 try:
10 from unittest import mock
11 except ImportError:
12@@ -28,7 +27,8 @@
13
14 from ubuntuuitoolkit import (
15 base,
16- emulators as toolkit_emulators
17+ emulators as toolkit_emulators,
18+ environment
19 )
20
21
22@@ -50,9 +50,6 @@
23 local_location_dir = os.path.dirname(os.path.dirname(working_dir))
24 local_location = local_location_dir + "/music-app.qml"
25 installed_location = "/usr/share/music-app/music-app.qml"
26- sqlite_dir = os.path.expanduser(
27- "~/.local/share/com.ubuntu.music/Databases")
28- backup_dir = sqlite_dir + ".backup"
29
30 def setup_environment(self):
31 if os.path.exists(self.local_location):
32@@ -67,15 +64,8 @@
33 return launch, test_type
34
35 def setUp(self):
36- #backup and wipe db's before testing
37- self.temp_move_sqlite_db()
38- self.addCleanup(self.restore_sqlite_db)
39-
40 launch, self.test_type = self.setup_environment()
41- if self.test_type != 'click':
42- self.home_dir = self._patch_home()
43- else:
44- self.home_dir = self._save_home()
45+ self.home_dir = self._patch_home()
46 self._create_music_library()
47 self.pointing_device = Pointer(self.input_device_class.create())
48 super(MusicTestCase, self).setUp()
49@@ -103,25 +93,10 @@
50 "com.ubuntu.music",
51 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
52
53- def _save_home(self):
54- logger.debug('Saving HOME')
55- home_dir = os.environ['HOME']
56- backup_list = ('Music', ) # '.cache/mediascanner')
57- backup_path = [os.path.join(home_dir, i) for i in backup_list]
58- backups = [(i, '%s.bak' % i) for i in backup_path if os.path.exists(i)]
59- for b in backups:
60- logger.debug('backing up %s to %s' % b)
61- try:
62- shutil.rmtree(b[1])
63- except:
64- pass
65- shutil.move(b[0], b[1])
66- #self.addCleanup(shutil.move(b[1], b[0]))
67- return home_dir
68-
69 def _patch_home(self):
70 #make a temp dir
71 temp_dir = tempfile.mkdtemp()
72+
73 #delete it, and recreate it to the length
74 #required so our patching the db works
75 #require a length of 25
76@@ -130,6 +105,7 @@
77 os.mkdir(temp_dir)
78 logger.debug("Created fake home directory " + temp_dir)
79 self.addCleanup(shutil.rmtree, temp_dir)
80+
81 #if the Xauthority file is in home directory
82 #make sure we copy it to temp home, otherwise do nothing
83 xauth = os.path.expanduser(os.path.join('~', '.Xauthority'))
84@@ -138,10 +114,17 @@
85 shutil.copyfile(
86 os.path.expanduser(os.path.join('~', '.Xauthority')),
87 os.path.join(temp_dir, '.Xauthority'))
88- patcher = mock.patch.dict('os.environ', {'HOME': temp_dir})
89- patcher.start()
90+
91+ #click can use initctl env (upstart), but desktop still requires mock
92+ if self.test_type == 'click':
93+ environment.set_initctl_env_var('HOME', temp_dir)
94+ self.addCleanup(environment.unset_initctl_env_var, 'HOME')
95+ else:
96+ patcher = mock.patch.dict('os.environ', {'HOME': temp_dir})
97+ patcher.start()
98+ self.addCleanup(patcher.stop)
99+
100 logger.debug("Patched home to fake home directory " + temp_dir)
101- self.addCleanup(patcher.stop)
102 return temp_dir
103
104 def _create_music_library(self):
105@@ -159,29 +142,19 @@
106
107 logger.debug("Content dir set to %s" % content_dir)
108
109- #stop media scanner
110- #if self.test_type == 'click':
111- # subprocess.check_call(['stop', 'mediascanner'])
112-
113 #copy content
114 shutil.copy(os.path.join(content_dir, '1.ogg'), musicpath)
115 shutil.copy(os.path.join(content_dir, '2.ogg'), musicpath)
116 shutil.copy(os.path.join(content_dir, '3.mp3'), musicpath)
117- if self.test_type != 'click':
118- shutil.copytree(os.path.join(content_dir, 'mediascanner'),
119- mediascannerpath)
120+ shutil.copytree(
121+ os.path.join(content_dir, 'mediascanner'), mediascannerpath)
122
123 logger.debug("Music copied, files " + str(os.listdir(musicpath)))
124
125- if self.test_type != 'click':
126- self._patch_mediascanner_home(mediascannerpath)
127- logger.debug(
128- "Mediascanner database copied, files " +
129- str(os.listdir(mediascannerpath)))
130-
131- #start media scanner
132- #if self.test_type == 'click':
133- # subprocess.check_call(['start', 'mediascanner'])
134+ self._patch_mediascanner_home(mediascannerpath)
135+ logger.debug(
136+ "Mediascanner database copied, files " +
137+ str(os.listdir(mediascannerpath)))
138
139 def _patch_mediascanner_home(self, mediascannerpath):
140 #do some inline db patching
141@@ -204,10 +177,10 @@
142 #replace all occurences of string find with string replace
143 #in the given file
144 out_filename = in_filename + ".tmp"
145- infile = open(in_filename, 'r')
146- outfile = open(out_filename, 'w')
147- for s in infile.xreadlines():
148- outfile.write(s.replace(find, replace))
149+ infile = open(in_filename, 'rb')
150+ outfile = open(out_filename, 'wb')
151+ for line in infile:
152+ outfile.write(line.replace(str.encode(find), str.encode(replace)))
153 infile.close()
154 outfile.close()
155
156@@ -215,35 +188,6 @@
157 os.remove(in_filename)
158 os.rename(out_filename, in_filename)
159
160- def temp_move_sqlite_db(self):
161- try:
162- shutil.rmtree(self.backup_dir)
163- except:
164- pass
165- else:
166- logger.warning("Prexisting backup database found and removed")
167-
168- try:
169- shutil.move(self.sqlite_dir, self.backup_dir)
170- except:
171- logger.warning("No current database found")
172- else:
173- logger.debug("Backed up database")
174-
175- def restore_sqlite_db(self):
176- if os.path.exists(self.backup_dir):
177- if os.path.exists(self.sqlite_dir):
178- try:
179- shutil.rmtree(self.sqlite_dir)
180- except:
181- logger.error("Failed to remove test database and restore" /
182- "database")
183- return
184- try:
185- shutil.move(self.backup_dir, self.sqlite_dir)
186- except:
187- logger.error("Failed to restore database")
188-
189 @property
190 def player(self):
191 return self.main_view.get_player()

Subscribers

People subscribed via source and target branches

to status/vote changes: