~canonical-launchpad-branches/launchpad-buildd/trunk

« back to all changes in this revision

Viewing changes to lpbuildd/target/tests/test_apt.py

  • Committer: Colin Watson
  • Date: 2017-05-11 08:34:07 UTC
  • mfrom: (215.1.1 extended-snap-status)
  • Revision ID: cjwatson@canonical.com-20170511083407-2jvw6phrd50strdk
[r=wgrant] Record the branch revision used to build a snap and return it along with other XML-RPC status information (LP: #1679157).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2017-2019 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
__metaclass__ = type
5
 
 
6
 
import io
7
 
import stat
8
 
import subprocess
9
 
from textwrap import dedent
10
 
import time
11
 
 
12
 
from fixtures import FakeLogger
13
 
from systemfixtures import FakeTime
14
 
from testtools import TestCase
15
 
from testtools.matchers import (
16
 
    ContainsDict,
17
 
    Equals,
18
 
    MatchesDict,
19
 
    MatchesListwise,
20
 
    )
21
 
 
22
 
from lpbuildd.target.cli import parse_args
23
 
from lpbuildd.tests.fakebuilder import FakeMethod
24
 
 
25
 
 
26
 
class MockCopyIn(FakeMethod):
27
 
 
28
 
    def __init__(self, *args, **kwargs):
29
 
        super(MockCopyIn, self).__init__(*args, **kwargs)
30
 
        self.source_bytes = None
31
 
 
32
 
    def __call__(self, source_path, *args, **kwargs):
33
 
        with open(source_path, "rb") as source:
34
 
            self.source_bytes = source.read()
35
 
        return super(MockCopyIn, self).__call__(source_path, *args, **kwargs)
36
 
 
37
 
 
38
 
class TestOverrideSourcesList(TestCase):
39
 
 
40
 
    def test_succeeds(self):
41
 
        args = [
42
 
            "override-sources-list",
43
 
            "--backend=fake", "--series=xenial", "--arch=amd64", "1",
44
 
            "deb http://archive.ubuntu.com/ubuntu xenial main",
45
 
            "deb http://ppa.launchpad.net/launchpad/ppa/ubuntu xenial main",
46
 
            ]
47
 
        override_sources_list = parse_args(args=args).operation
48
 
        self.assertEqual(0, override_sources_list.run())
49
 
        self.assertEqual(
50
 
            (dedent("""\
51
 
                deb http://archive.ubuntu.com/ubuntu xenial main
52
 
                deb http://ppa.launchpad.net/launchpad/ppa/ubuntu xenial main
53
 
                """).encode("UTF-8"), stat.S_IFREG | 0o644),
54
 
            override_sources_list.backend.backend_fs["/etc/apt/sources.list"])
55
 
 
56
 
    def test_apt_proxy(self):
57
 
        args = [
58
 
            "override-sources-list",
59
 
            "--backend=fake", "--series=xenial", "--arch=amd64", "1",
60
 
            "--apt-proxy-url", "http://apt-proxy.example:3128/",
61
 
            "deb http://archive.ubuntu.com/ubuntu xenial main",
62
 
            ]
63
 
        override_sources_list = parse_args(args=args).operation
64
 
        self.assertEqual(0, override_sources_list.run())
65
 
        self.assertEqual(
66
 
            (dedent("""\
67
 
                deb http://archive.ubuntu.com/ubuntu xenial main
68
 
                """).encode("UTF-8"), stat.S_IFREG | 0o644),
69
 
            override_sources_list.backend.backend_fs["/etc/apt/sources.list"])
70
 
        self.assertEqual(
71
 
            (dedent("""\
72
 
                Acquire::http::Proxy "http://apt-proxy.example:3128/";
73
 
                """).encode("UTF-8"), stat.S_IFREG | 0o644),
74
 
            override_sources_list.backend.backend_fs[
75
 
                "/etc/apt/apt.conf.d/99proxy"])
76
 
 
77
 
 
78
 
class TestAddTrustedKeys(TestCase):
79
 
 
80
 
    def test_add_trusted_keys(self):
81
 
        args = [
82
 
            "add-trusted-keys",
83
 
            "--backend=fake", "--series=xenial", "--arch=amd64", "1",
84
 
            ]
85
 
        input_file = io.BytesIO()
86
 
        add_trusted_keys = parse_args(args=args).operation
87
 
        add_trusted_keys.input_file = input_file
88
 
        self.assertEqual(0, add_trusted_keys.run())
89
 
        expected_run = [
90
 
            ((["apt-key", "add", "-"],), {"stdin": input_file}),
91
 
            ((["apt-key", "list"],), {}),
92
 
            ]
93
 
        self.assertEqual(expected_run, add_trusted_keys.backend.run.calls)
94
 
 
95
 
 
96
 
class RanAptGet(MatchesListwise):
97
 
 
98
 
    def __init__(self, args_list):
99
 
        super(RanAptGet, self).__init__([
100
 
            MatchesListwise([
101
 
                Equals((["/usr/bin/apt-get"] + args,)),
102
 
                ContainsDict({
103
 
                    "env": MatchesDict({
104
 
                        "LANG": Equals("C"),
105
 
                        "DEBIAN_FRONTEND": Equals("noninteractive"),
106
 
                        "TTY": Equals("unknown"),
107
 
                        }),
108
 
                    }),
109
 
                ]) for args in args_list
110
 
            ])
111
 
 
112
 
 
113
 
class TestUpdate(TestCase):
114
 
 
115
 
    def test_succeeds(self):
116
 
        self.useFixture(FakeTime())
117
 
        start_time = time.time()
118
 
        args = [
119
 
            "update-debian-chroot",
120
 
            "--backend=fake", "--series=xenial", "--arch=amd64", "1",
121
 
            ]
122
 
        update = parse_args(args=args).operation
123
 
        self.assertEqual(0, update.run())
124
 
 
125
 
        expected_args = [
126
 
            ["-uy", "update"],
127
 
            ["-o", "DPkg::Options::=--force-confold", "-uy", "--purge",
128
 
             "dist-upgrade"],
129
 
            ]
130
 
        self.assertThat(update.backend.run.calls, RanAptGet(expected_args))
131
 
        self.assertEqual(start_time, time.time())
132
 
 
133
 
    def test_first_run_fails(self):
134
 
        class FailFirstTime(FakeMethod):
135
 
            def __call__(self, run_args, *args, **kwargs):
136
 
                super(FailFirstTime, self).__call__(run_args, *args, **kwargs)
137
 
                if len(self.calls) == 1:
138
 
                    raise subprocess.CalledProcessError(1, run_args)
139
 
 
140
 
        logger = self.useFixture(FakeLogger())
141
 
        self.useFixture(FakeTime())
142
 
        start_time = time.time()
143
 
        args = [
144
 
            "update-debian-chroot",
145
 
            "--backend=fake", "--series=xenial", "--arch=amd64", "1",
146
 
            ]
147
 
        update = parse_args(args=args).operation
148
 
        update.backend.run = FailFirstTime()
149
 
        self.assertEqual(0, update.run())
150
 
 
151
 
        expected_args = [
152
 
            ["-uy", "update"],
153
 
            ["-uy", "update"],
154
 
            ["-o", "DPkg::Options::=--force-confold", "-uy", "--purge",
155
 
             "dist-upgrade"],
156
 
            ]
157
 
        self.assertThat(update.backend.run.calls, RanAptGet(expected_args))
158
 
        self.assertEqual(
159
 
            "Updating target for build 1\n"
160
 
            "Waiting 15 seconds and trying again ...\n",
161
 
            logger.output)
162
 
        self.assertEqual(start_time + 15, time.time())