~bjornt/landscape-client/apt-facade-changer-fixes

« back to all changes in this revision

Viewing changes to landscape/package/tests/test_facade.py

  • Committer: Bjorn Tillenius
  • Date: 2011-11-09 09:36:24 UTC
  • Revision ID: bjorn@canonical.com-20111109093624-81yvnl02jlmfrwlv
Factor out methods that modify Packages files into AptFacadeHelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from smart.cache import Provides
9
9
from smart.const import NEVER, ALWAYS
10
10
 
11
 
import apt_inst
12
11
import apt_pkg
13
12
from aptsources.sourceslist import SourcesList
14
13
 
18
17
 
19
18
import smart
20
19
 
21
 
from landscape.lib.fs import append_file, read_file
 
20
from landscape.lib.fs import read_file
22
21
from landscape.package.facade import (
23
22
    TransactionError, DependencyError, ChannelError, SmartError, AptFacade)
24
23
 
35
34
 
36
35
    helpers = [AptFacadeHelper]
37
36
 
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
37
    def test_default_root(self):
107
38
        """
108
39
        C{AptFacade} can be created by not providing a root directory,