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
=== modified file 'landscape/package/tests/helpers.py'
--- landscape/package/tests/helpers.py 2011-10-26 12:55:00 +0000
+++ landscape/package/tests/helpers.py 2011-11-09 09:43:23 +0000
@@ -1,9 +1,14 @@
1import base641import base64
2import os2import os
3import textwrap
4import time
35
4import smart6import smart
57
6from landscape.lib.fs import create_file8import apt_inst
9import apt_pkg
10
11from landscape.lib.fs import append_file, create_file
7from landscape.package.facade import AptFacade12from landscape.package.facade import AptFacade
813
914
@@ -12,10 +17,82 @@
1217
13 def set_up(self, test_case):18 def set_up(self, test_case):
14 test_case.apt_root = test_case.makeDir()19 test_case.apt_root = test_case.makeDir()
15 test_case.dpkg_status = os.path.join(20 self.dpkg_status = os.path.join(
16 test_case.apt_root, "var", "lib", "dpkg", "status")21 test_case.apt_root, "var", "lib", "dpkg", "status")
17 test_case.facade = AptFacade(root=test_case.apt_root)22 test_case.facade = AptFacade(root=test_case.apt_root)
18 test_case.facade.refetch_package_index = True23 test_case.facade.refetch_package_index = True
24 test_case._add_system_package = self._add_system_package
25 test_case._install_deb_file = self._install_deb_file
26 test_case._add_package_to_deb_dir = self._add_package_to_deb_dir
27 test_case._touch_packages_file = self._touch_packages_file
28
29 def _add_package(self, packages_file, name, architecture="all",
30 version="1.0", control_fields=None):
31 if control_fields is None:
32 control_fields = {}
33 package_stanza = textwrap.dedent("""
34 Package: %(name)s
35 Priority: optional
36 Section: misc
37 Installed-Size: 1234
38 Maintainer: Someone
39 Architecture: %(architecture)s
40 Source: source
41 Version: %(version)s
42 Config-Version: 1.0
43 Description: description
44 """ % {"name": name, "version": version,
45 "architecture": architecture})
46 package_stanza = apt_pkg.rewrite_section(
47 apt_pkg.TagSection(package_stanza), apt_pkg.REWRITE_PACKAGE_ORDER,
48 control_fields.items())
49 append_file(packages_file, "\n" + package_stanza + "\n")
50
51 def _add_system_package(self, name, architecture="all", version="1.0",
52 control_fields=None):
53 """Add a package to the dpkg status file."""
54 system_control_fields = {"Status": "install ok installed"}
55 if control_fields is not None:
56 system_control_fields.update(control_fields)
57 self._add_package(
58 self.dpkg_status, name, architecture=architecture, version=version,
59 control_fields=system_control_fields)
60
61 def _install_deb_file(self, path):
62 """Fake the the given deb file is installed in the system."""
63 deb_file = open(path)
64 deb = apt_inst.DebFile(deb_file)
65 control = deb.control.extractdata("control")
66 deb_file.close()
67 lines = control.splitlines()
68 lines.insert(1, "Status: install ok installed")
69 status = "\n".join(lines)
70 append_file(self.dpkg_status, status + "\n\n")
71
72 def _add_package_to_deb_dir(self, path, name, version="1.0",
73 control_fields=None):
74 """Add fake package information to a directory.
75
76 There will only be basic information about the package
77 available, so that get_packages() have something to return.
78 There won't be an actual package in the dir.
79 """
80 if control_fields is None:
81 control_fields = {}
82 self._add_package(
83 os.path.join(path, "Packages"), name, version=version,
84 control_fields=control_fields)
85
86 def _touch_packages_file(self, deb_dir):
87 """Make sure the Packages file get a newer mtime value.
88
89 If we rely on simply writing to the file to update the mtime, we
90 might end up with the same as before, since the resolution is
91 seconds, which causes apt to not reload the file.
92 """
93 packages_path = os.path.join(deb_dir, "Packages")
94 mtime = int(time.time() + 1)
95 os.utime(packages_path, (mtime, mtime))
1996
2097
21class SimpleRepositoryHelper(object):98class SimpleRepositoryHelper(object):
2299
=== modified file 'landscape/package/tests/test_facade.py'
--- landscape/package/tests/test_facade.py 2011-11-09 09:43:23 +0000
+++ landscape/package/tests/test_facade.py 2011-11-09 09:43:23 +0000
@@ -8,7 +8,6 @@
8from smart.cache import Provides8from smart.cache import Provides
9from smart.const import NEVER, ALWAYS9from smart.const import NEVER, ALWAYS
1010
11import apt_inst
12import apt_pkg11import apt_pkg
13from aptsources.sourceslist import SourcesList12from aptsources.sourceslist import SourcesList
1413
@@ -18,7 +17,7 @@
1817
19import smart18import smart
2019
21from landscape.lib.fs import append_file, read_file20from landscape.lib.fs import read_file
22from landscape.package.facade import (21from landscape.package.facade import (
23 TransactionError, DependencyError, ChannelError, SmartError, AptFacade)22 TransactionError, DependencyError, ChannelError, SmartError, AptFacade)
2423
@@ -35,74 +34,6 @@
3534
36 helpers = [AptFacadeHelper]35 helpers = [AptFacadeHelper]
3736
38 def _add_package(self, packages_file, name, architecture="all",
39 version="1.0", control_fields=None):
40 if control_fields is None:
41 control_fields = {}
42 package_stanza = textwrap.dedent("""
43 Package: %(name)s
44 Priority: optional
45 Section: misc
46 Installed-Size: 1234
47 Maintainer: Someone
48 Architecture: %(architecture)s
49 Source: source
50 Version: %(version)s
51 Config-Version: 1.0
52 Description: description
53 """ % {"name": name, "version": version,
54 "architecture": architecture})
55 package_stanza = apt_pkg.rewrite_section(
56 apt_pkg.TagSection(package_stanza), apt_pkg.REWRITE_PACKAGE_ORDER,
57 control_fields.items())
58 append_file(packages_file, package_stanza + "\n")
59
60 def _add_system_package(self, name, architecture="all", version="1.0",
61 control_fields=None):
62 """Add a package to the dpkg status file."""
63 system_control_fields = {"Status": "install ok installed"}
64 if control_fields is not None:
65 system_control_fields.update(control_fields)
66 self._add_package(
67 self.dpkg_status, name, architecture=architecture, version=version,
68 control_fields=system_control_fields)
69
70 def _install_deb_file(self, path):
71 """Fake the the given deb file is installed in the system."""
72 deb_file = open(path)
73 deb = apt_inst.DebFile(deb_file)
74 control = deb.control.extractdata("control")
75 deb_file.close()
76 lines = control.splitlines()
77 lines.insert(1, "Status: install ok installed")
78 status = "\n".join(lines)
79 append_file(self.dpkg_status, status + "\n\n")
80
81 def _add_package_to_deb_dir(self, path, name, version="1.0",
82 control_fields=None):
83 """Add fake package information to a directory.
84
85 There will only be basic information about the package
86 available, so that get_packages() have something to return.
87 There won't be an actual package in the dir.
88 """
89 if control_fields is None:
90 control_fields = {}
91 self._add_package(
92 os.path.join(path, "Packages"), name, version=version,
93 control_fields=control_fields)
94
95 def _touch_packages_file(self, deb_dir):
96 """Make sure the Packages file get a newer mtime value.
97
98 If we rely on simply writing to the file to update the mtime, we
99 might end up with the same as before, since the resolution is
100 seconds, which causes apt to not reload the file.
101 """
102 packages_path = os.path.join(deb_dir, "Packages")
103 mtime = int(time.time() + 1)
104 os.utime(packages_path, (mtime, mtime))
105
106 def test_default_root(self):37 def test_default_root(self):
107 """38 """
108 C{AptFacade} can be created by not providing a root directory,39 C{AptFacade} can be created by not providing a root directory,
10940
=== modified file 'landscape/package/tests/test_reporter.py'
--- landscape/package/tests/test_reporter.py 2011-11-08 10:54:08 +0000
+++ landscape/package/tests/test_reporter.py 2011-11-09 09:43:23 +0000
@@ -2,15 +2,13 @@
2import sys2import sys
3import os3import os
4import unittest4import unittest
5import textwrap
6import time5import time
76
8from twisted.internet.defer import Deferred, succeed7from twisted.internet.defer import Deferred, succeed
9from twisted.internet import reactor8from twisted.internet import reactor
109
11import apt_inst
1210
13from landscape.lib.fs import append_file, create_file11from landscape.lib.fs import create_file
14from landscape.lib.fetch import fetch_async, FetchError12from landscape.lib.fetch import fetch_async, FetchError
15from landscape.lib import bpickle13from landscape.lib import bpickle
16from landscape.package.store import (14from landscape.package.store import (
@@ -1639,41 +1637,6 @@
1639 result = super(PackageReporterAptTest, self).setUp()1637 result = super(PackageReporterAptTest, self).setUp()
1640 return result.addCallback(set_up)1638 return result.addCallback(set_up)
16411639
1642 def _install_deb_file(self, path):
1643 """Fake the the given deb file is installed in the system."""
1644 deb_file = open(path)
1645 deb = apt_inst.DebFile(deb_file)
1646 control = deb.control.extractdata("control")
1647 deb_file.close()
1648 lines = control.splitlines()
1649 lines.insert(1, "Status: install ok installed")
1650 status = "\n".join(lines)
1651 append_file(self.dpkg_status, status + "\n\n")
1652
1653 def _add_package_to_deb_dir(self, path, name, version="1.0"):
1654 """Add fake package information to a directory.
1655
1656 There will only be basic information about the package
1657 available, so that get_packages() have something to return.
1658 There won't be an actual package in the dir.
1659 """
1660 package_stanza = textwrap.dedent("""
1661 Package: %(name)s
1662 Priority: optional
1663 Section: misc
1664 Installed-Size: 1234
1665 Maintainer: Someone
1666 Architecture: all
1667 Source: source
1668 Version: %(version)s
1669 Config-Version: 1.0
1670 Description: description
1671
1672 """)
1673 append_file(
1674 os.path.join(path, "Packages"),
1675 package_stanza % {"name": name, "version": version})
1676
1677 def _clear_repository(self):1640 def _clear_repository(self):
1678 """Remove all packages from self.repository."""1641 """Remove all packages from self.repository."""
1679 create_file(self.repository_dir + "/Packages", "")1642 create_file(self.repository_dir + "/Packages", "")

Subscribers

People subscribed via source and target branches

to all changes: