Merge lp:~smoser/curtin/trunk-lp1731709-curthooks-write-files into lp:~raharper/curtin/trunk-lp1731709-curthooks-write-files

Proposed by Scott Moser
Status: Merged
Merged at revision: 545
Proposed branch: lp:~smoser/curtin/trunk-lp1731709-curthooks-write-files
Merge into: lp:~raharper/curtin/trunk-lp1731709-curthooks-write-files
Diff against target: 240 lines (+43/-125)
6 files modified
curtin/commands/curthooks.py (+2/-20)
curtin/futil.py (+14/-0)
examples/tests/curthooks_writefiles.yaml (+0/-40)
tests/unittests/helpers.py (+13/-0)
tests/unittests/test_curthooks.py (+14/-29)
tests/vmtests/test_curthooks_write_files.py (+0/-36)
To merge this branch: bzr merge lp:~smoser/curtin/trunk-lp1731709-curthooks-write-files
Reviewer Review Type Date Requested Status
Ryan Harper Pending
Review via email: mp+333920@code.launchpad.net

Commit message

suggested changes

a.) put only the import into curthooks.write_files, not a definition.
    move the legacy definition to futil.
b.) drop the vmtests
c.) drop the mocks in the added unit tests, actually read the files
    that are written and verify they have expected content.
d.) raise a DeprecatedWarning if the function is called.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'curtin/commands/curthooks.py'
2--- curtin/commands/curthooks.py 2017-11-13 22:48:29 +0000
3+++ curtin/commands/curthooks.py 2017-11-17 21:11:09 +0000
4@@ -36,6 +36,8 @@
5
6 from . import populate_one_subcmd
7
8+write_files = futil._legacy_write_files # LP: #1731709
9+
10 CMD_ARGUMENTS = (
11 ((('-t', '--target'),
12 {'help': 'operate on target. default is env[TARGET_MOUNT_POINT]',
13@@ -907,26 +909,6 @@
14 return False
15
16
17-def write_files(cfg, base_dir=None):
18- """Backwards compatibility for curthooks.write_files (LP: #1731709)
19- It needs to work like:
20- curthooks.write_files(cfg, target)
21- where cfg is a write_files dictionary and target is the
22- base directory under which the files will be written.
23- """
24- # this takes 'write_files' entry in config and writes files in the target
25- # config entry example:
26- # f1:
27- # path: /file1
28- # content: !!binary |
29- # f0VMRgIBAQAAAAAAAAAAAAIAPgABAAAAwARAAAAAAABAAAAAAAAAAJAVAAAAAAA
30- # f2: {path: /file2, content: "foobar", permissions: '0666'}
31- if 'write_files' not in cfg:
32- return
33-
34- futil.write_files(cfg.get('write_files'), base_dir=base_dir)
35-
36-
37 def curthooks(args):
38 state = util.load_command_environment()
39
40
41=== modified file 'curtin/futil.py'
42--- curtin/futil.py 2017-07-28 19:01:38 +0000
43+++ curtin/futil.py 2017-11-17 21:11:09 +0000
44@@ -18,6 +18,7 @@
45 import grp
46 import pwd
47 import os
48+import warnings
49
50 from .util import write_file, target_path
51 from .log import LOG
52@@ -101,3 +102,16 @@
53 content=info.get('content', ''),
54 owner=info.get('owner', "-1:-1"),
55 perms=info.get('permissions', info.get('perms', "0644")))
56+
57+
58+def _legacy_write_files(cfg, base_dir=None):
59+ """Backwards compatibility for curthooks.write_files (LP: #1731709)
60+ It needs to work like:
61+ curthooks.write_files(cfg, target)
62+ cfg is a 'cfg' dictionary with a 'write_files' entry in it.
63+ """
64+ warnings.warn(
65+ "write_files use from curtin.util is deprecated. "
66+ "Please use futil.write_files.", DeprecationWarning)
67+
68+ return write_files(cfg.get('write_files', {}), base_dir=base_dir)
69
70=== removed file 'examples/tests/curthooks_writefiles.yaml'
71--- examples/tests/curthooks_writefiles.yaml 2017-11-14 22:24:40 +0000
72+++ examples/tests/curthooks_writefiles.yaml 1970-01-01 00:00:00 +0000
73@@ -1,40 +0,0 @@
74-# LP: #1731709
75-# during install use write_files to write out a python program
76-# that calls curtin.curthooks.write_files and feeds it yaml
77-# config to simulate older maas images which may have called
78-# curthooks.write_files(cfg, target) pattern.
79-write_files:
80- test_curthooks_input:
81- path: /tmp/writefiles.yaml
82- content: |
83- # this is my input yaml file
84- write_files:
85- testfile1:
86- path: /root/testfile1
87- content: "This is testfile1\n"
88- test_curthooks:
89- path: /tmp/test_curthooks.py
90- permissions: '0755'
91- content: |
92- #!/usr/bin/python3
93- import sys, yaml, os
94- from curtin.commands import curthooks
95- target_path = os.environ['TARGET_MOUNT_POINT']
96- print('Found TARGET_MOUNT_POINT=%s' % target_path)
97- ypath = target_path + '/tmp/writefiles.yaml'
98- cfg = yaml.load(open(ypath))
99- curthooks.write_files(cfg, target_path)
100- print('Wrote %s to %s' % (cfg, target_path))
101-
102-
103-# call python3 in ephemeral to import curtin code use files written to target
104-run_curthooks:
105- - &call_python3 |
106- echo $@
107- TP=${TARGET_MOUNT_POINT};
108- FILE=${TP}${1};
109- echo "running $FILE";
110- exec python3 $FILE
111-
112-late_commands:
113- 01_runcurthooks: [sh, -c, *call_python3, 'curthook', '/tmp/test_curthooks.py']
114
115=== modified file 'tests/unittests/helpers.py'
116--- tests/unittests/helpers.py 2017-08-03 19:31:49 +0000
117+++ tests/unittests/helpers.py 2017-11-17 21:11:09 +0000
118@@ -79,3 +79,16 @@
119 if _dir is None:
120 _dir = self.tmp_dir()
121 return os.path.normpath(os.path.abspath(os.path.join(_dir, path)))
122+
123+
124+def dir2dict(startdir, prefix=None):
125+ flist = {}
126+ if prefix is None:
127+ prefix = startdir
128+ for root, dirs, files in os.walk(startdir):
129+ for fname in files:
130+ fpath = os.path.join(root, fname)
131+ key = fpath[len(prefix):]
132+ with open(fpath, "r") as fp:
133+ flist[key] = fp.read()
134+ return flist
135
136=== modified file 'tests/unittests/test_curthooks.py'
137--- tests/unittests/test_curthooks.py 2017-11-13 22:48:29 +0000
138+++ tests/unittests/test_curthooks.py 2017-11-17 21:11:09 +0000
139@@ -5,7 +5,7 @@
140 from curtin import util
141 from curtin import config
142 from curtin.reporter import events
143-from .helpers import CiTestCase
144+from .helpers import CiTestCase, dir2dict
145
146
147 class TestGetFlashKernelPkgs(CiTestCase):
148@@ -762,38 +762,23 @@
149
150
151 class TestCurthooksWriteFiles(CiTestCase):
152- @patch('curtin.commands.curthooks.futil.write_files')
153- def test_handle_write_files_empty(self, mock_write_files):
154+ def test_handle_write_files_empty(self):
155 """ Test curthooks.write_files returns for empty config """
156- ret = curthooks.write_files({}, "/my/target")
157+ tmpd = self.tmp_dir()
158+ ret = curthooks.write_files({}, tmpd)
159+ self.assertEqual({}, dir2dict(tmpd, prefix=tmpd))
160 self.assertIsNone(ret)
161- self.assertEqual(0, len(mock_write_files.call_args_list))
162
163- @patch('curtin.commands.curthooks.futil.write_files')
164- def test_handle_write_files(self, mock_write_files):
165+ def test_handle_write_files(self):
166 """ Test curthooks.write_files works as it used to """
167- cc_target = "tmpXXXX/random/dir/used/by/maas"
168- cfg = {
169- 'file1': {
170- 'path': '/etc/default/hello.txt',
171- 'content': "Hello World!\n",
172- },
173- 'foobar': {
174- 'path': '/sys/wark',
175- 'content': "Engauge!\n",
176- }
177- }
178-
179- expected_cfg = {
180- 'file1': {
181- 'path': '/etc/default/hello.txt',
182- 'content': cfg['file1']['content']},
183- 'foobar': {
184- 'path': '/sys/wark',
185- 'content': cfg['foobar']['content']}
186- }
187- curthooks.write_files({'write_files': cfg}, cc_target)
188- mock_write_files.assert_called_with(expected_cfg, base_dir=cc_target)
189+ tmpd = self.tmp_dir()
190+ cfg = {'file1': {'path': '/etc/default/hello.txt',
191+ 'content': "Hello World!\n"},
192+ 'foobar': {'path': '/sys/wark', 'content': "Engauge!\n"}}
193+ ret = curthooks.write_files({'write_files': cfg}, tmpd)
194+ self.assertEqual(
195+ dict((cfg[i]['path'], cfg[i]['content']) for i in cfg.keys()),
196+ dir2dict(tmpd, prefix=tmpd))
197
198 @patch('curtin.commands.curthooks.futil.target_path')
199 @patch('curtin.commands.curthooks.futil.write_finfo')
200
201=== removed file 'tests/vmtests/test_curthooks_write_files.py'
202--- tests/vmtests/test_curthooks_write_files.py 2017-11-14 22:24:40 +0000
203+++ tests/vmtests/test_curthooks_write_files.py 1970-01-01 00:00:00 +0000
204@@ -1,36 +0,0 @@
205-from . import VMBaseClass
206-from .releases import base_vm_classes as relbase
207-
208-import textwrap
209-
210-
211-class TestCurthooksWriteFiles(VMBaseClass):
212- """ Test curthooks.write_files() legacy call works """
213- conf_file = "examples/tests/curthooks_writefiles.yaml"
214- extra_disks = []
215- extra_nics = []
216- collect_scripts = [textwrap.dedent("""
217- cd OUTPUT_COLLECT_D
218- cp /etc/network/interfaces interfaces
219- if [ -f /var/log/cloud-init-output.log ]; then
220- cp /var/log/cloud-init-output.log .
221- fi
222- cp /var/log/cloud-init.log .
223- find /etc/network/interfaces.d > find_interfacesd
224- """)]
225-
226- def test_output_files_exist(self):
227- self.output_files_exist(["interfaces", "cloud-init.log"])
228-
229- def test_curthooks_write_files(self):
230- self.output_files_exist(["root/testfile1"])
231- content = self.load_collect_file("root/testfile1")
232- self.assertEqual("This is testfile1", content.strip())
233-
234-
235-class XenialTestCurthooksWriteFiles(relbase.xenial, TestCurthooksWriteFiles):
236- __test__ = True
237-
238-
239-class ArtfulTestCurthooksWriteFiles(relbase.artful, TestCurthooksWriteFiles):
240- __test__ = True

Subscribers

People subscribed via source and target branches

to all changes: