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

« back to all changes in this revision

Viewing changes to landscape/package/tests/helpers.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:
1
1
import base64
2
2
import os
 
3
import textwrap
 
4
import time
3
5
 
4
6
import smart
5
7
 
6
 
from landscape.lib.fs import create_file
 
8
import apt_inst
 
9
import apt_pkg
 
10
 
 
11
from landscape.lib.fs import append_file, create_file
7
12
from landscape.package.facade import AptFacade
8
13
 
9
14
 
12
17
 
13
18
    def set_up(self, test_case):
14
19
        test_case.apt_root = test_case.makeDir()
15
 
        test_case.dpkg_status = os.path.join(
 
20
        self.dpkg_status = os.path.join(
16
21
            test_case.apt_root, "var", "lib", "dpkg", "status")
17
22
        test_case.facade = AptFacade(root=test_case.apt_root)
18
23
        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))
19
96
 
20
97
 
21
98
class SimpleRepositoryHelper(object):