Merge lp:~bjornt/landscape-client/apt-facade-test-helpers into lp:~landscape/landscape-client/trunk

Proposed by Björn Tillenius
Status: Merged
Approved by: Thomas Herve
Approved revision: 429
Merged at revision: 399
Proposed branch: lp:~bjornt/landscape-client/apt-facade-test-helpers
Merge into: lp:~landscape/landscape-client/trunk
Prerequisite: lp:~bjornt/landscape-client/apt-facade-remove-package
Diff against target: 262 lines (+81/-110)
3 files modified
landscape/package/tests/helpers.py (+79/-2)
landscape/package/tests/test_facade.py (+1/-70)
landscape/package/tests/test_reporter.py (+1/-38)
To merge this branch: bzr merge lp:~bjornt/landscape-client/apt-facade-test-helpers
Reviewer Review Type Date Requested Status
Thomas Herve (community) Approve
Alberto Donato (community) Approve
Review via email: mp+81693@code.launchpad.net

Description of the change

Factor out methods that modify Apt Packages files, since we already had
similar methods in two TestCases and we'll be needed the same methods of
the package changer tests as well.

To post a comment you must log in.
Revision history for this message
Alberto Donato (ack) wrote :

Good, +1!

review: Approve
Revision history for this message
Thomas Herve (therve) wrote :

+1!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/package/tests/helpers.py'
2--- landscape/package/tests/helpers.py 2011-10-26 12:55:00 +0000
3+++ landscape/package/tests/helpers.py 2011-11-09 09:43:23 +0000
4@@ -1,9 +1,14 @@
5 import base64
6 import os
7+import textwrap
8+import time
9
10 import smart
11
12-from landscape.lib.fs import create_file
13+import apt_inst
14+import apt_pkg
15+
16+from landscape.lib.fs import append_file, create_file
17 from landscape.package.facade import AptFacade
18
19
20@@ -12,10 +17,82 @@
21
22 def set_up(self, test_case):
23 test_case.apt_root = test_case.makeDir()
24- test_case.dpkg_status = os.path.join(
25+ self.dpkg_status = os.path.join(
26 test_case.apt_root, "var", "lib", "dpkg", "status")
27 test_case.facade = AptFacade(root=test_case.apt_root)
28 test_case.facade.refetch_package_index = True
29+ test_case._add_system_package = self._add_system_package
30+ test_case._install_deb_file = self._install_deb_file
31+ test_case._add_package_to_deb_dir = self._add_package_to_deb_dir
32+ test_case._touch_packages_file = self._touch_packages_file
33+
34+ def _add_package(self, packages_file, name, architecture="all",
35+ version="1.0", control_fields=None):
36+ if control_fields is None:
37+ control_fields = {}
38+ package_stanza = textwrap.dedent("""
39+ Package: %(name)s
40+ Priority: optional
41+ Section: misc
42+ Installed-Size: 1234
43+ Maintainer: Someone
44+ Architecture: %(architecture)s
45+ Source: source
46+ Version: %(version)s
47+ Config-Version: 1.0
48+ Description: description
49+ """ % {"name": name, "version": version,
50+ "architecture": architecture})
51+ package_stanza = apt_pkg.rewrite_section(
52+ apt_pkg.TagSection(package_stanza), apt_pkg.REWRITE_PACKAGE_ORDER,
53+ control_fields.items())
54+ append_file(packages_file, "\n" + package_stanza + "\n")
55+
56+ def _add_system_package(self, name, architecture="all", version="1.0",
57+ control_fields=None):
58+ """Add a package to the dpkg status file."""
59+ system_control_fields = {"Status": "install ok installed"}
60+ if control_fields is not None:
61+ system_control_fields.update(control_fields)
62+ self._add_package(
63+ self.dpkg_status, name, architecture=architecture, version=version,
64+ control_fields=system_control_fields)
65+
66+ def _install_deb_file(self, path):
67+ """Fake the the given deb file is installed in the system."""
68+ deb_file = open(path)
69+ deb = apt_inst.DebFile(deb_file)
70+ control = deb.control.extractdata("control")
71+ deb_file.close()
72+ lines = control.splitlines()
73+ lines.insert(1, "Status: install ok installed")
74+ status = "\n".join(lines)
75+ append_file(self.dpkg_status, status + "\n\n")
76+
77+ def _add_package_to_deb_dir(self, path, name, version="1.0",
78+ control_fields=None):
79+ """Add fake package information to a directory.
80+
81+ There will only be basic information about the package
82+ available, so that get_packages() have something to return.
83+ There won't be an actual package in the dir.
84+ """
85+ if control_fields is None:
86+ control_fields = {}
87+ self._add_package(
88+ os.path.join(path, "Packages"), name, version=version,
89+ control_fields=control_fields)
90+
91+ def _touch_packages_file(self, deb_dir):
92+ """Make sure the Packages file get a newer mtime value.
93+
94+ If we rely on simply writing to the file to update the mtime, we
95+ might end up with the same as before, since the resolution is
96+ seconds, which causes apt to not reload the file.
97+ """
98+ packages_path = os.path.join(deb_dir, "Packages")
99+ mtime = int(time.time() + 1)
100+ os.utime(packages_path, (mtime, mtime))
101
102
103 class SimpleRepositoryHelper(object):
104
105=== modified file 'landscape/package/tests/test_facade.py'
106--- landscape/package/tests/test_facade.py 2011-11-09 09:43:23 +0000
107+++ landscape/package/tests/test_facade.py 2011-11-09 09:43:23 +0000
108@@ -8,7 +8,6 @@
109 from smart.cache import Provides
110 from smart.const import NEVER, ALWAYS
111
112-import apt_inst
113 import apt_pkg
114 from aptsources.sourceslist import SourcesList
115
116@@ -18,7 +17,7 @@
117
118 import smart
119
120-from landscape.lib.fs import append_file, read_file
121+from landscape.lib.fs import read_file
122 from landscape.package.facade import (
123 TransactionError, DependencyError, ChannelError, SmartError, AptFacade)
124
125@@ -35,74 +34,6 @@
126
127 helpers = [AptFacadeHelper]
128
129- def _add_package(self, packages_file, name, architecture="all",
130- version="1.0", control_fields=None):
131- if control_fields is None:
132- control_fields = {}
133- package_stanza = textwrap.dedent("""
134- Package: %(name)s
135- Priority: optional
136- Section: misc
137- Installed-Size: 1234
138- Maintainer: Someone
139- Architecture: %(architecture)s
140- Source: source
141- Version: %(version)s
142- Config-Version: 1.0
143- Description: description
144- """ % {"name": name, "version": version,
145- "architecture": architecture})
146- package_stanza = apt_pkg.rewrite_section(
147- apt_pkg.TagSection(package_stanza), apt_pkg.REWRITE_PACKAGE_ORDER,
148- control_fields.items())
149- append_file(packages_file, package_stanza + "\n")
150-
151- def _add_system_package(self, name, architecture="all", version="1.0",
152- control_fields=None):
153- """Add a package to the dpkg status file."""
154- system_control_fields = {"Status": "install ok installed"}
155- if control_fields is not None:
156- system_control_fields.update(control_fields)
157- self._add_package(
158- self.dpkg_status, name, architecture=architecture, version=version,
159- control_fields=system_control_fields)
160-
161- def _install_deb_file(self, path):
162- """Fake the the given deb file is installed in the system."""
163- deb_file = open(path)
164- deb = apt_inst.DebFile(deb_file)
165- control = deb.control.extractdata("control")
166- deb_file.close()
167- lines = control.splitlines()
168- lines.insert(1, "Status: install ok installed")
169- status = "\n".join(lines)
170- append_file(self.dpkg_status, status + "\n\n")
171-
172- def _add_package_to_deb_dir(self, path, name, version="1.0",
173- control_fields=None):
174- """Add fake package information to a directory.
175-
176- There will only be basic information about the package
177- available, so that get_packages() have something to return.
178- There won't be an actual package in the dir.
179- """
180- if control_fields is None:
181- control_fields = {}
182- self._add_package(
183- os.path.join(path, "Packages"), name, version=version,
184- control_fields=control_fields)
185-
186- def _touch_packages_file(self, deb_dir):
187- """Make sure the Packages file get a newer mtime value.
188-
189- If we rely on simply writing to the file to update the mtime, we
190- might end up with the same as before, since the resolution is
191- seconds, which causes apt to not reload the file.
192- """
193- packages_path = os.path.join(deb_dir, "Packages")
194- mtime = int(time.time() + 1)
195- os.utime(packages_path, (mtime, mtime))
196-
197 def test_default_root(self):
198 """
199 C{AptFacade} can be created by not providing a root directory,
200
201=== modified file 'landscape/package/tests/test_reporter.py'
202--- landscape/package/tests/test_reporter.py 2011-11-08 10:54:08 +0000
203+++ landscape/package/tests/test_reporter.py 2011-11-09 09:43:23 +0000
204@@ -2,15 +2,13 @@
205 import sys
206 import os
207 import unittest
208-import textwrap
209 import time
210
211 from twisted.internet.defer import Deferred, succeed
212 from twisted.internet import reactor
213
214-import apt_inst
215
216-from landscape.lib.fs import append_file, create_file
217+from landscape.lib.fs import create_file
218 from landscape.lib.fetch import fetch_async, FetchError
219 from landscape.lib import bpickle
220 from landscape.package.store import (
221@@ -1639,41 +1637,6 @@
222 result = super(PackageReporterAptTest, self).setUp()
223 return result.addCallback(set_up)
224
225- def _install_deb_file(self, path):
226- """Fake the the given deb file is installed in the system."""
227- deb_file = open(path)
228- deb = apt_inst.DebFile(deb_file)
229- control = deb.control.extractdata("control")
230- deb_file.close()
231- lines = control.splitlines()
232- lines.insert(1, "Status: install ok installed")
233- status = "\n".join(lines)
234- append_file(self.dpkg_status, status + "\n\n")
235-
236- def _add_package_to_deb_dir(self, path, name, version="1.0"):
237- """Add fake package information to a directory.
238-
239- There will only be basic information about the package
240- available, so that get_packages() have something to return.
241- There won't be an actual package in the dir.
242- """
243- package_stanza = textwrap.dedent("""
244- Package: %(name)s
245- Priority: optional
246- Section: misc
247- Installed-Size: 1234
248- Maintainer: Someone
249- Architecture: all
250- Source: source
251- Version: %(version)s
252- Config-Version: 1.0
253- Description: description
254-
255- """)
256- append_file(
257- os.path.join(path, "Packages"),
258- package_stanza % {"name": name, "version": version})
259-
260 def _clear_repository(self):
261 """Remove all packages from self.repository."""
262 create_file(self.repository_dir + "/Packages", "")

Subscribers

People subscribed via source and target branches

to all changes: