← Back to team overview

cloud-init-dev team mailing list archive

Re: [Merge] ~larsks/cloud-init:bz/1542578 into cloud-init:master

 

Lars, thank you, and prefectly fine to poke me.
I'll try to take a look at this on monday.

one comment in line about some infrastructure that can make this easier.

Thank you for adding some tests!

Scott

Diff comments:

> diff --git a/tests/unittests/test_handler/test_handler_mounts.py b/tests/unittests/test_handler/test_handler_mounts.py
> index fe492d4..1d58ae9 100644
> --- a/tests/unittests/test_handler/test_handler_mounts.py
> +++ b/tests/unittests/test_handler/test_handler_mounts.py
> @@ -134,4 +134,97 @@ class TestSanitizeDevname(test_helpers.FilesystemMockingTestCase):
>              cc_mounts.sanitize_devname(
>                  'ephemeral0.1', lambda x: disk_path, mock.Mock()))
>  
> +
> +class TestFstabHandling(test_helpers.FilesystemMockingTestCase):
> +
> +    swap_path = '/dev/sdb1'
> +
> +    def setUp(self):
> +        super(TestFstabHandling, self).setUp()
> +        self.new_root = tempfile.mkdtemp()
> +        self.addCleanup(shutil.rmtree, self.new_root)
> +        self.patchOS(self.new_root)
> +
> +        self.fstab_path = os.path.join(self.new_root, 'etc/fstab')
> +        self._makedirs('/etc')
> +
> +        patch_fstab_path = mock.patch(
> +            'cloudinit.config.cc_mounts.FSTAB_PATH', self.fstab_path)
> +        patch_fstab_path.start()

these are all made (i hope) easier with 'add_patch'
 
self.add_patch('cloudinit.config.cc_mounts.FSTAB_PATH', 'm_fstab_path', new=self.fstab)

first argument is what to patch, second, where to store that in this class (self.m_fstab_path will get the return of patch_fstab_path).  then kwargs go through to mock.patch

> +
> +        patch_is_block_device = mock.patch(
> +            'cloudinit.config.cc_mounts._is_block_device', return_value=True)
> +        patch_is_block_device.start()
> +
> +        patch_util_subp = mock.patch('cloudinit.config.cc_mounts.util.subp')
> +        patch_util_subp.start()
> +
> +        patchers = [patch_fstab_path, patch_is_block_device, patch_util_subp]
> +        for patcher in patchers:
> +            self.addCleanup(patcher.stop)
> +
> +        self.mock_cloud = mock.Mock()
> +        self.mock_log = mock.Mock()
> +        self.mock_cloud.device_name_to_device = self.device_name_to_device
> +
> +    def _makedirs(self, directory):
> +        directory = os.path.join(self.new_root, directory.lstrip('/'))
> +        if not os.path.exists(directory):
> +            os.makedirs(directory)
> +
> +    def device_name_to_device(self, path):
> +        if path == 'swap':
> +            return self.swap_path
> +        else:
> +            dev = None
> +
> +        return dev
> +
> +    def test_fstab_no_swap_device(self):
> +        fstab_original_content = ''
> +        fstab_expected_content = (
> +            '%s\tnone\tswap\tsw,comment=cloudconfig\t'
> +            '0\t0\n' % (self.swap_path,)
> +        )
> +
> +        with open(cc_mounts.FSTAB_PATH, 'w') as fd:
> +            fd.write(fstab_original_content)
> +
> +        cc_mounts.handle(None, {}, self.mock_cloud, self.mock_log, [])
> +
> +        with open(cc_mounts.FSTAB_PATH, 'r') as fd:
> +            fstab_new_content = fd.read()
> +            self.assertEqual(fstab_expected_content, fstab_new_content)
> +
> +    def test_fstab_same_swap_device_already_configured(self):
> +        fstab_original_content = '%s swap swap defaults 0 0\n' % (
> +            self.swap_path,)
> +        fstab_expected_content = fstab_original_content
> +
> +        with open(cc_mounts.FSTAB_PATH, 'w') as fd:
> +            fd.write(fstab_original_content)
> +
> +        cc_mounts.handle(None, {}, self.mock_cloud, self.mock_log, [])
> +
> +        with open(cc_mounts.FSTAB_PATH, 'r') as fd:
> +            fstab_new_content = fd.read()
> +            self.assertEqual(fstab_expected_content, fstab_new_content)
> +
> +    def test_fstab_alternate_swap_device_already_configured(self):
> +        fstab_original_content = '/dev/sdc1 swap swap defaults 0 0\n'
> +        fstab_expected_content = (
> +            fstab_original_content +
> +            '%s\tnone\tswap\tsw,comment=cloudconfig\t'
> +            '0\t0\n' % (self.swap_path,)
> +        )
> +
> +        with open(cc_mounts.FSTAB_PATH, 'w') as fd:
> +            fd.write(fstab_original_content)
> +
> +        cc_mounts.handle(None, {}, self.mock_cloud, self.mock_log, [])
> +
> +        with open(cc_mounts.FSTAB_PATH, 'r') as fd:
> +            fstab_new_content = fd.read()
> +            self.assertEqual(fstab_expected_content, fstab_new_content)
> +
>  # vi: ts=4 expandtab


-- 
https://code.launchpad.net/~larsks/cloud-init/+git/cloud-init/+merge/345113
Your team cloud-init commiters is requested to review the proposed merge of ~larsks/cloud-init:bz/1542578 into cloud-init:master.


References