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
=== modified file 'curtin/commands/curthooks.py'
--- curtin/commands/curthooks.py 2017-11-13 22:48:29 +0000
+++ curtin/commands/curthooks.py 2017-11-17 21:11:09 +0000
@@ -36,6 +36,8 @@
3636
37from . import populate_one_subcmd37from . import populate_one_subcmd
3838
39write_files = futil._legacy_write_files # LP: #1731709
40
39CMD_ARGUMENTS = (41CMD_ARGUMENTS = (
40 ((('-t', '--target'),42 ((('-t', '--target'),
41 {'help': 'operate on target. default is env[TARGET_MOUNT_POINT]',43 {'help': 'operate on target. default is env[TARGET_MOUNT_POINT]',
@@ -907,26 +909,6 @@
907 return False909 return False
908910
909911
910def write_files(cfg, base_dir=None):
911 """Backwards compatibility for curthooks.write_files (LP: #1731709)
912 It needs to work like:
913 curthooks.write_files(cfg, target)
914 where cfg is a write_files dictionary and target is the
915 base directory under which the files will be written.
916 """
917 # this takes 'write_files' entry in config and writes files in the target
918 # config entry example:
919 # f1:
920 # path: /file1
921 # content: !!binary |
922 # f0VMRgIBAQAAAAAAAAAAAAIAPgABAAAAwARAAAAAAABAAAAAAAAAAJAVAAAAAAA
923 # f2: {path: /file2, content: "foobar", permissions: '0666'}
924 if 'write_files' not in cfg:
925 return
926
927 futil.write_files(cfg.get('write_files'), base_dir=base_dir)
928
929
930def curthooks(args):912def curthooks(args):
931 state = util.load_command_environment()913 state = util.load_command_environment()
932914
933915
=== modified file 'curtin/futil.py'
--- curtin/futil.py 2017-07-28 19:01:38 +0000
+++ curtin/futil.py 2017-11-17 21:11:09 +0000
@@ -18,6 +18,7 @@
18import grp18import grp
19import pwd19import pwd
20import os20import os
21import warnings
2122
22from .util import write_file, target_path23from .util import write_file, target_path
23from .log import LOG24from .log import LOG
@@ -101,3 +102,16 @@
101 content=info.get('content', ''),102 content=info.get('content', ''),
102 owner=info.get('owner', "-1:-1"),103 owner=info.get('owner', "-1:-1"),
103 perms=info.get('permissions', info.get('perms', "0644")))104 perms=info.get('permissions', info.get('perms', "0644")))
105
106
107def _legacy_write_files(cfg, base_dir=None):
108 """Backwards compatibility for curthooks.write_files (LP: #1731709)
109 It needs to work like:
110 curthooks.write_files(cfg, target)
111 cfg is a 'cfg' dictionary with a 'write_files' entry in it.
112 """
113 warnings.warn(
114 "write_files use from curtin.util is deprecated. "
115 "Please use futil.write_files.", DeprecationWarning)
116
117 return write_files(cfg.get('write_files', {}), base_dir=base_dir)
104118
=== removed file 'examples/tests/curthooks_writefiles.yaml'
--- examples/tests/curthooks_writefiles.yaml 2017-11-14 22:24:40 +0000
+++ examples/tests/curthooks_writefiles.yaml 1970-01-01 00:00:00 +0000
@@ -1,40 +0,0 @@
1# LP: #1731709
2# during install use write_files to write out a python program
3# that calls curtin.curthooks.write_files and feeds it yaml
4# config to simulate older maas images which may have called
5# curthooks.write_files(cfg, target) pattern.
6write_files:
7 test_curthooks_input:
8 path: /tmp/writefiles.yaml
9 content: |
10 # this is my input yaml file
11 write_files:
12 testfile1:
13 path: /root/testfile1
14 content: "This is testfile1\n"
15 test_curthooks:
16 path: /tmp/test_curthooks.py
17 permissions: '0755'
18 content: |
19 #!/usr/bin/python3
20 import sys, yaml, os
21 from curtin.commands import curthooks
22 target_path = os.environ['TARGET_MOUNT_POINT']
23 print('Found TARGET_MOUNT_POINT=%s' % target_path)
24 ypath = target_path + '/tmp/writefiles.yaml'
25 cfg = yaml.load(open(ypath))
26 curthooks.write_files(cfg, target_path)
27 print('Wrote %s to %s' % (cfg, target_path))
28
29
30# call python3 in ephemeral to import curtin code use files written to target
31run_curthooks:
32 - &call_python3 |
33 echo $@
34 TP=${TARGET_MOUNT_POINT};
35 FILE=${TP}${1};
36 echo "running $FILE";
37 exec python3 $FILE
38
39late_commands:
40 01_runcurthooks: [sh, -c, *call_python3, 'curthook', '/tmp/test_curthooks.py']
410
=== modified file 'tests/unittests/helpers.py'
--- tests/unittests/helpers.py 2017-08-03 19:31:49 +0000
+++ tests/unittests/helpers.py 2017-11-17 21:11:09 +0000
@@ -79,3 +79,16 @@
79 if _dir is None:79 if _dir is None:
80 _dir = self.tmp_dir()80 _dir = self.tmp_dir()
81 return os.path.normpath(os.path.abspath(os.path.join(_dir, path)))81 return os.path.normpath(os.path.abspath(os.path.join(_dir, path)))
82
83
84def dir2dict(startdir, prefix=None):
85 flist = {}
86 if prefix is None:
87 prefix = startdir
88 for root, dirs, files in os.walk(startdir):
89 for fname in files:
90 fpath = os.path.join(root, fname)
91 key = fpath[len(prefix):]
92 with open(fpath, "r") as fp:
93 flist[key] = fp.read()
94 return flist
8295
=== modified file 'tests/unittests/test_curthooks.py'
--- tests/unittests/test_curthooks.py 2017-11-13 22:48:29 +0000
+++ tests/unittests/test_curthooks.py 2017-11-17 21:11:09 +0000
@@ -5,7 +5,7 @@
5from curtin import util5from curtin import util
6from curtin import config6from curtin import config
7from curtin.reporter import events7from curtin.reporter import events
8from .helpers import CiTestCase8from .helpers import CiTestCase, dir2dict
99
1010
11class TestGetFlashKernelPkgs(CiTestCase):11class TestGetFlashKernelPkgs(CiTestCase):
@@ -762,38 +762,23 @@
762762
763763
764class TestCurthooksWriteFiles(CiTestCase):764class TestCurthooksWriteFiles(CiTestCase):
765 @patch('curtin.commands.curthooks.futil.write_files')765 def test_handle_write_files_empty(self):
766 def test_handle_write_files_empty(self, mock_write_files):
767 """ Test curthooks.write_files returns for empty config """766 """ Test curthooks.write_files returns for empty config """
768 ret = curthooks.write_files({}, "/my/target")767 tmpd = self.tmp_dir()
768 ret = curthooks.write_files({}, tmpd)
769 self.assertEqual({}, dir2dict(tmpd, prefix=tmpd))
769 self.assertIsNone(ret)770 self.assertIsNone(ret)
770 self.assertEqual(0, len(mock_write_files.call_args_list))
771771
772 @patch('curtin.commands.curthooks.futil.write_files')772 def test_handle_write_files(self):
773 def test_handle_write_files(self, mock_write_files):
774 """ Test curthooks.write_files works as it used to """773 """ Test curthooks.write_files works as it used to """
775 cc_target = "tmpXXXX/random/dir/used/by/maas"774 tmpd = self.tmp_dir()
776 cfg = {775 cfg = {'file1': {'path': '/etc/default/hello.txt',
777 'file1': {776 'content': "Hello World!\n"},
778 'path': '/etc/default/hello.txt',777 'foobar': {'path': '/sys/wark', 'content': "Engauge!\n"}}
779 'content': "Hello World!\n",778 ret = curthooks.write_files({'write_files': cfg}, tmpd)
780 },779 self.assertEqual(
781 'foobar': {780 dict((cfg[i]['path'], cfg[i]['content']) for i in cfg.keys()),
782 'path': '/sys/wark',781 dir2dict(tmpd, prefix=tmpd))
783 'content': "Engauge!\n",
784 }
785 }
786
787 expected_cfg = {
788 'file1': {
789 'path': '/etc/default/hello.txt',
790 'content': cfg['file1']['content']},
791 'foobar': {
792 'path': '/sys/wark',
793 'content': cfg['foobar']['content']}
794 }
795 curthooks.write_files({'write_files': cfg}, cc_target)
796 mock_write_files.assert_called_with(expected_cfg, base_dir=cc_target)
797782
798 @patch('curtin.commands.curthooks.futil.target_path')783 @patch('curtin.commands.curthooks.futil.target_path')
799 @patch('curtin.commands.curthooks.futil.write_finfo')784 @patch('curtin.commands.curthooks.futil.write_finfo')
800785
=== removed file 'tests/vmtests/test_curthooks_write_files.py'
--- tests/vmtests/test_curthooks_write_files.py 2017-11-14 22:24:40 +0000
+++ tests/vmtests/test_curthooks_write_files.py 1970-01-01 00:00:00 +0000
@@ -1,36 +0,0 @@
1from . import VMBaseClass
2from .releases import base_vm_classes as relbase
3
4import textwrap
5
6
7class TestCurthooksWriteFiles(VMBaseClass):
8 """ Test curthooks.write_files() legacy call works """
9 conf_file = "examples/tests/curthooks_writefiles.yaml"
10 extra_disks = []
11 extra_nics = []
12 collect_scripts = [textwrap.dedent("""
13 cd OUTPUT_COLLECT_D
14 cp /etc/network/interfaces interfaces
15 if [ -f /var/log/cloud-init-output.log ]; then
16 cp /var/log/cloud-init-output.log .
17 fi
18 cp /var/log/cloud-init.log .
19 find /etc/network/interfaces.d > find_interfacesd
20 """)]
21
22 def test_output_files_exist(self):
23 self.output_files_exist(["interfaces", "cloud-init.log"])
24
25 def test_curthooks_write_files(self):
26 self.output_files_exist(["root/testfile1"])
27 content = self.load_collect_file("root/testfile1")
28 self.assertEqual("This is testfile1", content.strip())
29
30
31class XenialTestCurthooksWriteFiles(relbase.xenial, TestCurthooksWriteFiles):
32 __test__ = True
33
34
35class ArtfulTestCurthooksWriteFiles(relbase.artful, TestCurthooksWriteFiles):
36 __test__ = True

Subscribers

People subscribed via source and target branches

to all changes: