def test_handle_modern_zfsroot(self, mount_info, zpool_info, parse_mount,
                                   is_container):
        devpth = 'zroot/ROOT/default'
        disk = 'da0p3'
        fs_type = 'zfs'
        mount_point = '/'

        mount_info.return_value = (devpth, fs_type, mount_point)
        zpool_info.return_value = disk
        parse_mount.return_value = (devpth, fs_type, mount_point)

        cfg = {'resizefs_enabled': True}

        def fake_stat(devpath):
            if devpath == disk:
                raise OSError("not here")
            FakeStat = namedtuple(
                'FakeStat', ['st_mode', 'st_size', 'st_mtime'])  # minimal stat
            return FakeStat(25008, 0, 1)  # fake char block device

        with mock.patch(
                'cloudinit.config.cc_resizefs_vyos.do_resize') as dresize:
            with mock.patch(
                    'cloudinit.config.cc_resizefs_vyos.os.stat') as m_stat:
                m_stat.side_effect = fake_stat
                handle('cc_resizefs_vyos', cfg, _cloud=None, log=LOG, args=[])

        self.assertEqual(('zpool', 'online', '-e', 'zroot', '/dev/' + disk),
                         dresize.call_args[0][0])
 def test_handle_noops_on_disabled(self):
     """The handle function logs when the configuration disables resize."""
     cfg = {'resizefs_enabled': False}
     handle('cc_resizefs_vyos', cfg, _cloud=None, log=LOG, args=[])
     self.assertIn(
         'DEBUG: Skipping module named cc_resizefs_vyos, resizing disabled\n',
         self.logs.getvalue())
 def test_handle_warns_on_unknown_mount_info(self, m_get_mount_info):
     """handle warns when get_mount_info sees unknown filesystem for /."""
     m_get_mount_info.return_value = None
     cfg = {'resizefs_enabled': True}
     handle('cc_resizefs_vyos', cfg, _cloud=None, log=LOG, args=[])
     logs = self.logs.getvalue()
     self.assertNotIn("WARNING: Invalid config:\nresizefs_enabled:", logs)
     self.assertIn('WARNING: Could not determine filesystem type of /\n',
                   logs)
     self.assertEqual([mock.call('/', LOG)],
                      m_get_mount_info.call_args_list)
    def test_handle_schema_validation_logs_invalid_resize_enabled_value(self):
        """The handle reports json schema violations as a warning.

        Invalid values for resizefs_enabled result in disabling the module.
        """
        cfg = {'resizefs_enabled': 'junk'}
        handle('cc_resizefs_vyos', cfg, _cloud=None, log=LOG, args=[])
        logs = self.logs.getvalue()
        self.assertIn(
            "WARNING: Invalid config:\nresizefs_enabled: 'junk' is not one of"
            " [True, False, 'noblock']", logs)
        self.assertIn(
            'DEBUG: Skipping module named cc_resizefs_vyos, resizing disabled\n',
            logs)
    def test_handle_zfs_root(self, mount_info, zpool_info, parse_mount,
                             is_container):
        devpth = 'vmzroot/ROOT/freebsd'
        disk = 'gpt/system'
        fs_type = 'zfs'
        mount_point = '/'

        mount_info.return_value = (devpth, fs_type, mount_point)
        zpool_info.return_value = disk
        parse_mount.return_value = (devpth, fs_type, mount_point)

        cfg = {'resizefs_enabled': True}

        with mock.patch(
                'cloudinit.config.cc_resizefs_vyos.do_resize') as dresize:
            handle('cc_resizefs_vyos', cfg, _cloud=None, log=LOG, args=[])
            ret = dresize.call_args[0][0]

        self.assertEqual(('zpool', 'online', '-e', 'vmzroot', disk), ret)