Merge lp:~canonical-platform-qa/ubuntu-system-tests/mtp-backup-local-media into lp:ubuntu-system-tests

Proposed by Richard Huddie
Status: Merged
Approved by: Sergio Cazzolato
Approved revision: 161
Merged at revision: 161
Proposed branch: lp:~canonical-platform-qa/ubuntu-system-tests/mtp-backup-local-media
Merge into: lp:ubuntu-system-tests
Diff against target: 233 lines (+103/-30)
5 files modified
ubuntu_system_tests/helpers/backup_restore_fixture.py (+3/-1)
ubuntu_system_tests/helpers/file_system.py (+19/-0)
ubuntu_system_tests/helpers/media.py (+67/-0)
ubuntu_system_tests/tests/test_mtp.py (+8/-1)
ubuntu_system_tests/tests/test_scopes.py (+6/-28)
To merge this branch: bzr merge lp:~canonical-platform-qa/ubuntu-system-tests/mtp-backup-local-media
Reviewer Review Type Date Requested Status
Sergio Cazzolato Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+265945@code.launchpad.net

Commit message

Add BackupRestoreLocalMediaFixture and update MTP test_copy_music to backup and restore any local media before running the test.

Description of the change

Update MTP test_copy_music to backup and restore any local media before running the test.

Create a new fixture for backing up and restoring all local media files on the device. Update the backup and restore helper so that when it moves a folder, it leaves the top-level folder in place. This is required for MTP where it can cause problems if the Music, Videos or Pictures folders get deleted and re-added.

This mp also creates a helper to restart the media scanner after moving the local media files to ensure the scopes are updated.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Sergio Cazzolato (sergio-j-cazzolato) wrote :

I added a card to make a Fixture that makes the media backup, do you want to include this Fixture as part of this branch?

The card is:
https://trello.com/c/DtVWrbMj/112-update-mtp-copy-music-test-to-backup-all-the-music-media-on-setup

159. By Richard Huddie

Create and use a fixture for backing up and restoring local media files during testing.

Revision history for this message
Richard Huddie (rhuddie) wrote :

> I added a card to make a Fixture that makes the media backup, do you want to
> include this Fixture as part of this branch?
>
> The card is:
> https://trello.com/c/DtVWrbMj/112-update-mtp-copy-music-test-to-backup-all-
> the-music-media-on-setup

Thanks Sergio for suggestion. I've now added the BackupRestoreLocalMediaFixture to this.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Sergio Cazzolato (sergio-j-cazzolato) wrote :

Getting this error when I execute the test http://paste.ubuntu.com/11953459/

Revision history for this message
Sergio Cazzolato (sergio-j-cazzolato) :
review: Needs Fixing
160. By Richard Huddie

Merge from trunk.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Sergio Cazzolato (sergio-j-cazzolato) wrote :

still failing, this is the media dir that I have

phablet@ubuntu-phablet:~$ ll /media/phablet/5AC0-A72A/Music/
total 288
drwxr-xr-x 3 phablet phablet 32768 jul 28 12:52 ./
drwxr-xr-x 8 phablet phablet 32768 jul 29 18:27 ../
-rw-r--r-- 1 phablet phablet 11977 jul 28 12:21 aaa1111.mp3
-rw-r--r-- 1 phablet phablet 18358 jul 28 12:21 aaaaaa.ogg
-rw-r--r-- 1 phablet phablet 18358 jul 28 12:21 hello (copy).ogg
-rw-r--r-- 1 phablet phablet 11977 jul 28 12:21 hello.mp3
drwxr-xr-x 2 phablet phablet 32768 jul 28 12:52 mymusic/
-rw-r--r-- 1 phablet phablet 18358 jul 28 12:21 sssaaa.ogg
-rw-r--r-- 1 phablet phablet 10240 jul 17 20:06 XATTKXKDJV.mp3

161. By Richard Huddie

Create backup destination folder if path does not exist.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Richard Huddie (rhuddie) wrote :

Ok, I've made an update that should fix this. Please could you re-test.

Revision history for this message
Sergio Cazzolato (sergio-j-cazzolato) wrote :

The Fixture is working for me. The test test_copy_music is failing in my device I'll take a look to that.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_system_tests/helpers/backup_restore_fixture.py'
2--- ubuntu_system_tests/helpers/backup_restore_fixture.py 2015-05-21 18:15:40 +0000
3+++ ubuntu_system_tests/helpers/backup_restore_fixture.py 2015-07-30 09:41:10 +0000
4@@ -21,6 +21,8 @@
5 import os
6 import shutil
7
8+from ubuntu_system_tests.helpers.file_system import move_folder_contents
9+
10 DEFAULT_STORE_ROOT = os.path.expanduser('~/.tmp_backup')
11
12
13@@ -48,7 +50,7 @@
14 shutil.rmtree(self.storage_dir.path)
15 if os.path.exists(self.backup_dir):
16 if self.start_clean:
17- shutil.move(self.backup_dir, self.storage_dir.path)
18+ move_folder_contents(self.backup_dir, self.storage_dir.path)
19 else:
20 shutil.copytree(self.backup_dir, self.storage_dir.path)
21
22
23=== modified file 'ubuntu_system_tests/helpers/file_system.py'
24--- ubuntu_system_tests/helpers/file_system.py 2015-07-24 11:13:22 +0000
25+++ ubuntu_system_tests/helpers/file_system.py 2015-07-30 09:41:10 +0000
26@@ -110,6 +110,25 @@
27 remove_dir(obj_path)
28
29
30+def move_folder_contents(src_root, dst_root):
31+ """
32+ Move each top-level item individually from src folder to dest folder
33+ leaving the top-level folder in place. Result will be an empty src folder
34+ with all items moved to dest folder.
35+
36+ :param src_folder: The folder containing content to move
37+ :parap dst_folder: The destination folder for all content
38+
39+ """
40+ for item in os.listdir(src_root):
41+ src = os.path.join(src_root, item)
42+ dst = os.path.join(dst_root, item)
43+ dst_folder = os.path.dirname(dst)
44+ if not os.path.exists(dst_folder):
45+ os.makedirs(dst_folder)
46+ shutil.move(src, dst)
47+
48+
49 def calculate_file_sha1(file_path):
50 """Calculates the sha1 digest for the file
51 :param file_path: The path to the file to calculate the sha1 digest
52
53=== added file 'ubuntu_system_tests/helpers/media.py'
54--- ubuntu_system_tests/helpers/media.py 1970-01-01 00:00:00 +0000
55+++ ubuntu_system_tests/helpers/media.py 2015-07-30 09:41:10 +0000
56@@ -0,0 +1,67 @@
57+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
58+#
59+# Ubuntu System Tests
60+# Copyright (C) 2015 Canonical
61+#
62+# This program is free software: you can redistribute it and/or modify
63+# it under the terms of the GNU General Public License as published by
64+# the Free Software Foundation, either version 3 of the License, or
65+# (at your option) any later version.
66+#
67+# This program is distributed in the hope that it will be useful,
68+# but WITHOUT ANY WARRANTY; without even the implied warranty of
69+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
70+# GNU General Public License for more details.
71+#
72+# You should have received a copy of the GNU General Public License
73+# along with this program. If not, see <http://www.gnu.org/licenses/>.
74+#
75+
76+import fixtures
77+import os
78+
79+from unity8.process_helpers import start_job, stop_job, is_job_running
80+
81+from ubuntu_system_tests.helpers import backup_restore_fixture as brf
82+from ubuntu_system_tests.helpers.file_system import get_media_folder_list
83+from ubuntu_system_tests.helpers.unity8.dash import get_dash
84+
85+MEDIA_SCANNER = 'mediascanner-2.0'
86+
87+
88+def restart_media_scanner():
89+ """Restart the media scanner process."""
90+ if is_job_running(MEDIA_SCANNER):
91+ stop_job(MEDIA_SCANNER)
92+ start_job(MEDIA_SCANNER)
93+ dash = get_dash()
94+ dash.wait_for_dash_loaded()
95+ dash.wait_for_processing_to_complete()
96+
97+
98+class BackupRestoreLocalMediaFixture(fixtures.Fixture):
99+ """Generic fixture class to backup and restore all local media files."""
100+
101+ def __init__(self, start_clean=True):
102+ """
103+ :param start_clean: Whether to clean media directories after backup.
104+
105+ """
106+ self.start_clean = start_clean
107+
108+ def setUp(self):
109+ """Backup the required directory and register restore actions."""
110+ super().setUp()
111+ self._backup_local_media()
112+
113+ def _backup_local_media(self):
114+ """Backup the local media files which are removed during testing."""
115+ for folder in get_media_folder_list():
116+ if folder.startswith('/media/'):
117+ backup_path = os.path.join(
118+ os.path.dirname(folder), '.tmp_backup')
119+ else:
120+ backup_path = os.path.expanduser('~/.tmp_backup')
121+ self.useFixture(brf.BackupRestoreFixture(
122+ folder, storage_root_dir=backup_path,
123+ start_clean=self.start_clean))
124
125=== modified file 'ubuntu_system_tests/tests/test_mtp.py'
126--- ubuntu_system_tests/tests/test_mtp.py 2015-07-23 15:03:58 +0000
127+++ ubuntu_system_tests/tests/test_mtp.py 2015-07-30 09:41:10 +0000
128@@ -26,6 +26,7 @@
129 from ubuntu_system_tests.helpers import file_system as fs
130 from ubuntu_system_tests.helpers.mtp import MTPObjFactory, MTPHelper
131 from ubuntu_system_tests.helpers import ssh
132+from ubuntu_system_tests.helpers import media
133 from ubuntu_system_tests.helpers.scopes import music
134 from ubuntu_system_tests.helpers.unity8.dash import get_dash
135
136@@ -115,6 +116,11 @@
137
138 return albums_dir, album_mp3_dir, album_ogg_dir
139
140+ def backup_local_media(self):
141+ """Backup the local media files which are removed during testing."""
142+ self.useFixture(media.BackupRestoreLocalMediaFixture())
143+ media.restart_media_scanner()
144+
145 def verify_copied_files(self, seed_file, original_files, copied_dir):
146 """ Verify all the files have been copied correctly to the dst
147 :param seed_file: this is the test data file that is used to generate
148@@ -147,7 +153,8 @@
149 albums have been copied properly to the device and the music appears
150 in the music scope.
151 """
152-
153+ # Remove any existing local media from device
154+ self.backup_local_media()
155 # Prepare the albums structure in the remote machine previous to be
156 # copied to the device
157 mp3_file = self.factory.create_file(
158
159=== modified file 'ubuntu_system_tests/tests/test_scopes.py'
160--- ubuntu_system_tests/tests/test_scopes.py 2015-07-21 21:02:07 +0000
161+++ ubuntu_system_tests/tests/test_scopes.py 2015-07-30 09:41:10 +0000
162@@ -26,11 +26,9 @@
163 from ubuntu_system_tests.helpers import autopilot
164 from ubuntu_system_tests.helpers import file_system
165 from ubuntu_system_tests.helpers import scopes
166-from ubuntu_system_tests.helpers.backup_restore_fixture import (
167- BackupRestoreFixture)
168+from ubuntu_system_tests.helpers import media
169 from ubuntu_system_tests.helpers.unity8.dash import get_dash
170
171-from unity8.process_helpers import start_job, stop_job, is_job_running
172 from unity8 import UnityException
173
174 SAMPLE_VIDEO_FILE = 'video1.ogv'
175@@ -78,7 +76,7 @@
176 len(expected_scopes) > 0,
177 'No scopes defined for channel: {c} device: {d}'.format(
178 c=channel, d=device))
179- self._backup_local_media()
180+ self.useFixture(media.BackupRestoreLocalMediaFixture())
181 self._dismiss_today_scope_setup()
182 self.addCleanup(scopes.go_to_scope, expected_scopes[0].url)
183
184@@ -89,45 +87,25 @@
185 except UnityException:
186 pass
187
188- def _backup_local_media(self):
189- """Backup the local media files which are removed during testing."""
190- for folder in file_system.get_media_folder_list():
191- if folder.startswith('/media/'):
192- backup_path = os.path.join(
193- os.path.dirname(folder), '.tmp_backup')
194- else:
195- backup_path = os.path.expanduser('~/.tmp_backup')
196- self.useFixture(BackupRestoreFixture(
197- folder, storage_root_dir=backup_path, start_clean=True))
198- os.makedirs(folder)
199-
200- def restart_media_scanner(self):
201- MEDIA_SCANNER = 'mediascanner-2.0'
202- if is_job_running(MEDIA_SCANNER):
203- stop_job(MEDIA_SCANNER)
204- start_job(MEDIA_SCANNER)
205- self.dash.wait_for_dash_loaded()
206- self.dash.wait_for_processing_to_complete()
207-
208 def _apply_condition_local_video(self):
209 """Method to apply 'local_video' condition."""
210 shutil.copy2(SRC_VIDEO_FILE, LOCAL_VIDEO_FILE)
211- self.restart_media_scanner()
212+ media.restart_media_scanner()
213
214 def _apply_condition_no_local_video(self):
215 """Method to apply 'no_local_video' condition."""
216 file_system.clean_dir(file_system.DIR_HOME_VIDEOS)
217- self.restart_media_scanner()
218+ media.restart_media_scanner()
219
220 def _apply_condition_local_music(self):
221 """Method to apply 'local_music' condition."""
222 shutil.copy2(SRC_AUDIO_FILE, LOCAL_AUDIO_FILE)
223- self.restart_media_scanner()
224+ media.restart_media_scanner()
225
226 def _apply_condition_no_local_music(self):
227 """Method to apply 'no_local_music' condition."""
228 file_system.clean_dir(file_system.DIR_HOME_MUSIC)
229- self.restart_media_scanner()
230+ media.restart_media_scanner()
231
232 def _apply_condition_none(self):
233 """Method to apply 'none' condition."""

Subscribers

People subscribed via source and target branches

to all changes: