Merge lp:~cjwatson/launchpad-buildd/add-trusted-keys-python into lp:launchpad-buildd

Proposed by Colin Watson
Status: Merged
Merged at revision: 249
Proposed branch: lp:~cjwatson/launchpad-buildd/add-trusted-keys-python
Merge into: lp:launchpad-buildd
Prerequisite: lp:~cjwatson/launchpad-buildd/override-sources-list-python
Diff against target: 125 lines (+56/-20)
4 files modified
bin/add-trusted-keys (+31/-20)
debian/changelog (+1/-0)
lpbuildd/target/chroot.py (+7/-0)
lpbuildd/target/tests/test_chroot.py (+17/-0)
To merge this branch: bzr merge lp:~cjwatson/launchpad-buildd/add-trusted-keys-python
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+328264@code.launchpad.net

Commit message

Rewrite add-trusted-keys in Python, allowing it to have unit tests.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/add-trusted-keys'
2--- bin/add-trusted-keys 2017-07-25 22:11:19 +0000
3+++ bin/add-trusted-keys 2017-07-28 23:16:43 +0000
4@@ -1,24 +1,35 @@
5-#!/bin/sh
6+#! /usr/bin/python -u
7 #
8 # Copyright 2017 Canonical Ltd. This software is licensed under the
9 # GNU Affero General Public License version 3 (see the file LICENSE).
10
11-# Write out new trusted keys in the chroot.
12-
13-# Expects the build id as the first argument, and key material on stdin.
14-
15-set -e
16-
17-SUDO=/usr/bin/sudo
18-CHROOT=/usr/sbin/chroot
19-
20-BUILDID="$1"
21-ROOT="$HOME/build-$BUILDID/chroot-autobuild"
22-
23-cd $HOME
24-cd "$ROOT/etc/apt"
25-
26-echo "Adding trusted keys to build-$BUILDID"
27-
28-$SUDO $CHROOT $ROOT apt-key add -
29-$SUDO $CHROOT $ROOT apt-key list
30+"""Write out new trusted keys in the chroot.
31+
32+Expects key material on stdin.
33+"""
34+
35+from __future__ import print_function
36+
37+__metaclass__ = type
38+
39+from argparse import ArgumentParser
40+import sys
41+
42+from lpbuildd.target.chroot import ChrootSetup
43+
44+
45+def main():
46+ parser = ArgumentParser(
47+ description="Write out new trusted keys in the chroot.")
48+ parser.add_argument(
49+ "build_id", metavar="ID", help="write trusted keys for build ID")
50+ args = parser.parse_args()
51+
52+ print("Adding trusted keys to build-%s" % args.build_id)
53+ setup = ChrootSetup(args.build_id)
54+ setup.add_trusted_keys(sys.stdin)
55+ return 0
56+
57+
58+if __name__ == "__main__":
59+ sys.exit(main())
60
61=== modified file 'debian/changelog'
62--- debian/changelog 2017-07-28 23:16:43 +0000
63+++ debian/changelog 2017-07-28 23:16:43 +0000
64@@ -11,6 +11,7 @@
65 * Rewrite update-debian-chroot in Python, allowing it to use lpbuildd.util
66 and to have unit tests.
67 * Rewrite override-sources-list in Python, allowing it to have unit tests.
68+ * Rewrite add-trusted-keys in Python, allowing it to have unit tests.
69
70 -- Colin Watson <cjwatson@ubuntu.com> Tue, 25 Jul 2017 23:07:58 +0100
71
72
73=== modified file 'lpbuildd/target/chroot.py'
74--- lpbuildd/target/chroot.py 2017-07-28 23:16:43 +0000
75+++ lpbuildd/target/chroot.py 2017-07-28 23:16:43 +0000
76@@ -39,6 +39,8 @@
77 if self.arch is not None:
78 args = set_personality(args, self.arch, series=self.series)
79 cmd = ["/usr/bin/sudo", "/usr/sbin/chroot", self.chroot_path] + args
80+ if "cwd" not in kwargs:
81+ kwargs["cwd"] = self.chroot_path
82 if input_text is None:
83 subprocess.check_call(cmd, **kwargs)
84 else:
85@@ -91,3 +93,8 @@
86 print(archive, file=sources_list)
87 sources_list.flush()
88 self.insert_file(sources_list.name, "/etc/apt/sources.list")
89+
90+ def add_trusted_keys(self, input_file):
91+ """Add trusted keys from an input file."""
92+ self.chroot(["apt-key", "add", "-"], stdin=input_file)
93+ self.chroot(["apt-key", "list"])
94
95=== modified file 'lpbuildd/target/tests/test_chroot.py'
96--- lpbuildd/target/tests/test_chroot.py 2017-07-28 23:16:43 +0000
97+++ lpbuildd/target/tests/test_chroot.py 2017-07-28 23:16:43 +0000
98@@ -3,6 +3,7 @@
99
100 __metaclass__ = type
101
102+import io
103 import sys
104 import time
105 from textwrap import dedent
106@@ -154,3 +155,19 @@
107 self.assertEqual(
108 ("/etc/apt/sources.list",),
109 mock_insert_file.extract_args()[0][1:])
110+
111+ def test_add_trusted_keys(self):
112+ self.useFixture(EnvironmentVariable("HOME", "/expected/home"))
113+ setup = ChrootSetup("1")
114+ # XXX cjwatson 2017-07-29: With a newer version of fixtures we could
115+ # mock this at the subprocess level instead, but at the moment doing
116+ # that wouldn't allow us to test stdin.
117+ mock_chroot = self.useFixture(MockPatchObject(setup, "chroot")).mock
118+ input_file = io.BytesIO()
119+ setup.add_trusted_keys(input_file)
120+
121+ self.assertEqual(2, len(mock_chroot.mock_calls))
122+ mock_chroot.assert_has_calls([
123+ ((["apt-key", "add", "-"],), {"stdin": input_file}),
124+ ((["apt-key", "list"],), {}),
125+ ])

Subscribers

People subscribed via source and target branches

to all changes: