1
# Copyright 2017-2019 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
9
from textwrap import dedent
12
from fixtures import FakeLogger
13
from systemfixtures import FakeTime
14
from testtools import TestCase
15
from testtools.matchers import (
22
from lpbuildd.target.cli import parse_args
23
from lpbuildd.tests.fakebuilder import FakeMethod
26
class MockCopyIn(FakeMethod):
28
def __init__(self, *args, **kwargs):
29
super(MockCopyIn, self).__init__(*args, **kwargs)
30
self.source_bytes = None
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)
38
class TestOverrideSourcesList(TestCase):
40
def test_succeeds(self):
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",
47
override_sources_list = parse_args(args=args).operation
48
self.assertEqual(0, override_sources_list.run())
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"])
56
def test_apt_proxy(self):
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",
63
override_sources_list = parse_args(args=args).operation
64
self.assertEqual(0, override_sources_list.run())
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"])
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"])
78
class TestAddTrustedKeys(TestCase):
80
def test_add_trusted_keys(self):
83
"--backend=fake", "--series=xenial", "--arch=amd64", "1",
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())
90
((["apt-key", "add", "-"],), {"stdin": input_file}),
91
((["apt-key", "list"],), {}),
93
self.assertEqual(expected_run, add_trusted_keys.backend.run.calls)
96
class RanAptGet(MatchesListwise):
98
def __init__(self, args_list):
99
super(RanAptGet, self).__init__([
101
Equals((["/usr/bin/apt-get"] + args,)),
105
"DEBIAN_FRONTEND": Equals("noninteractive"),
106
"TTY": Equals("unknown"),
109
]) for args in args_list
113
class TestUpdate(TestCase):
115
def test_succeeds(self):
116
self.useFixture(FakeTime())
117
start_time = time.time()
119
"update-debian-chroot",
120
"--backend=fake", "--series=xenial", "--arch=amd64", "1",
122
update = parse_args(args=args).operation
123
self.assertEqual(0, update.run())
127
["-o", "DPkg::Options::=--force-confold", "-uy", "--purge",
130
self.assertThat(update.backend.run.calls, RanAptGet(expected_args))
131
self.assertEqual(start_time, time.time())
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)
140
logger = self.useFixture(FakeLogger())
141
self.useFixture(FakeTime())
142
start_time = time.time()
144
"update-debian-chroot",
145
"--backend=fake", "--series=xenial", "--arch=amd64", "1",
147
update = parse_args(args=args).operation
148
update.backend.run = FailFirstTime()
149
self.assertEqual(0, update.run())
154
["-o", "DPkg::Options::=--force-confold", "-uy", "--purge",
157
self.assertThat(update.backend.run.calls, RanAptGet(expected_args))
159
"Updating target for build 1\n"
160
"Waiting 15 seconds and trying again ...\n",
162
self.assertEqual(start_time + 15, time.time())