Ejemplo n.º 1
0
def test_section_end():
    data = lib.LineData('}', None, False)
    assert data.type == data.TYPE_SECTION_END
    data = lib.LineData('     }  # trailing comment', 'devices', True)
    assert data.type == data.TYPE_SECTION_END
    data = lib.LineData('     }  JUNK AFTERWARDS', 'defaults', False)
    assert data.type == data.TYPE_SECTION_END
    data = lib.LineData('\t \t}  !!tabs', 'multipaths', True)
    assert data.type == data.TYPE_SECTION_END
Ejemplo n.º 2
0
def test_enabled():
    values = (('yes', True), ('1', True), ('no', False), ('0', False))
    for value, is_enabled in values:
        data = lib.LineData('key ' + value, 'defaults', False)
        assert data.type == data.TYPE_OPTION
        assert data.option == 'key'
        assert data.value == value
        assert data.is_enabled() == is_enabled

    data = lib.LineData('key neither_yes_nor_no', 'defaults', False)
    assert data.type == data.TYPE_OPTION
    assert data.option == 'key'
    assert data.value == 'neither_yes_nor_no'
    assert data.is_enabled() is None
Ejemplo n.º 3
0
def test_option():
    data = lib.LineData('key value', 'defaults', False)
    assert data.type == data.TYPE_OPTION
    assert data.option == 'key'
    assert data.value == 'value'

    data = lib.LineData('key value # comment', 'devices', True)
    assert data.option == 'key'
    assert data.value == 'value'

    data = lib.LineData('    key value "extra string"', 'multipaths', True)
    assert data.option == 'key'
    assert data.value == 'value'

    data = lib.LineData(' \t\tkey "string value" junk', 'blacklist', False)
    assert data.option == 'key'
    assert data.value == 'string value'

    try:
        data = lib.LineData('key  # comment', 'devices', True)
        assert False
    except ValueError:
        pass
Ejemplo n.º 4
0
def test_blank_line():
    data = lib.LineData('', None, False)
    assert data.type == data.TYPE_BLANK
    data = lib.LineData('    ', 'devices', True)
    assert data.type == data.TYPE_BLANK
    data = lib.LineData('# comment at start of line', 'defaults', False)
    assert data.type == data.TYPE_BLANK
    data = lib.LineData('  ! alternate comment method!!!', 'blacklist', True)
    assert data.type == data.TYPE_BLANK
    data = lib.LineData('# } still a comment', None, False)
    assert data.type == data.TYPE_BLANK
    data = lib.LineData('   # "also a comment!"', "multipaths", True)
    assert data.type == data.TYPE_BLANK
    data = lib.LineData(' "ignore lines starting with a string"', None, False)
    assert data.type == data.TYPE_BLANK
Ejemplo n.º 5
0
def test_section_start():
    sections = ('defaults', 'blacklist', 'blacklist_exceptions', 'devices',
                'overrides', 'multipaths')
    subsections = {
        'blacklist': 'device',
        'blacklist_exceptions': 'device',
        'devices': 'device',
        'multipaths': 'multipath'
    }
    for section in sections:
        data = lib.LineData(section + ' {', None, False)
        assert data.type == data.TYPE_SECTION_START
        assert data.section == section

    try:
        data = lib.LineData('not_a_section {', None, False)
        assert False
    except ValueError:
        pass

    data = lib.LineData('defaults # works even without brace', None, False)
    assert data.type == data.TYPE_SECTION_START
    assert data.section == 'defaults'

    data = lib.LineData('\t \tmultipaths { # tabs', None, False)
    assert data.type == data.TYPE_SECTION_START
    assert data.section == 'multipaths'

    data = lib.LineData('devices{ # do not need a space', None, False)
    assert data.type == data.TYPE_SECTION_START
    assert data.section == 'devices'

    for section in subsections:
        data = lib.LineData(subsections[section] + ' {', section, False)
        assert data.type == data.TYPE_SECTION_START
        assert data.section == subsections[section]

    data = lib.LineData('devices { # wrong section', 'multipath', False)
    assert data.type != data.TYPE_SECTION_START

    data = lib.LineData('devices { # already in subsection', 'device', True)
    assert data.type != data.TYPE_SECTION_START
def _update_config(config):
    if _nothing_to_do(config):
        return None
    contents = multipathutil.read_config(config.pathname)
    if contents is None:
        return None
    lines = contents.split('\n')
    section = None
    in_subsection = False
    in_all_devs = False
    subsection_start = None
    all_devs_ranges = []
    overrides_line = None
    qinp_info = None
    has_no_path_retry = False
    qinp_infos = []
    for i, line in enumerate(lines):
        try:
            data = multipathutil.LineData(line, section, in_subsection)
        except ValueError:
            continue
        if data.type == data.TYPE_SECTION_END:
            if qinp_info and not in_all_devs:
                qinp_info.has_no_path_retry = has_no_path_retry
                qinp_infos.append(qinp_info)
            qinp_info = None
            has_no_path_retry = False
            if in_subsection:
                in_subsection = False
                if in_all_devs:
                    all_devs_ranges.append((subsection_start, i + 1))
                in_all_devs = False
                subsection_start = None
            elif section is not None:
                section = None
        elif data.type == data.TYPE_SECTION_START:
            if section is None:
                section = data.section
                if section == 'overrides':
                    overrides_line = line
            elif not in_subsection:
                in_subsection = True
                subsection_start = i
        if data.type != data.TYPE_OPTION:
            continue
        if section == 'defaults':
            if (data.option
                    in ('path_checker', 'checker')) and data.value != 'tur':
                lines[i] = _comment_out_line(line)
                continue
            if data.option in _bool_options and \
                    _bool_options[data.option] != data.is_enabled():
                lines[i] = _comment_out_line(line)
                continue
        elif section == 'overrides' and data.option in _ovr_options:
            lines[i] = _comment_out_line(line)
            continue
        elif section == 'devices' and in_subsection and \
                data.option == 'all_devs' and data.is_enabled():
            in_all_devs = True
            continue
        if data.option in _exist_options:
            lines[i] = _comment_out_line(line)
        elif data.option == 'detect_path_checker':
            lines[i] = _convert_checker_line(line)
        elif data.option == 'no_path_retry' and _valid_npr(data.value):
            has_no_path_retry = True
        elif data.option == 'features' and 'queue_if_no_path' in data.value:
            qinp_info = _QueueIfNoPathInfo(line, data.value)

    if in_subsection:
        lines.append('\t} # line added by Leapp')
        if in_all_devs:
            all_devs_ranges.append((subsection_start, len(lines)))
        elif qinp_info:
            qinp_info.has_no_path_retry = has_no_path_retry
            qinp_infos.append(qinp_info)
            qinp_info = None
    if section is not None:
        lines.append('} # line added by Leapp')
        lines.append('')
        if qinp_info:
            qinp_info.has_no_path_retry = has_no_path_retry
            qinp_infos.append(qinp_info)
    _comment_out_ranges(lines, all_devs_ranges)
    if qinp_infos != []:
        _remove_qinp(lines, qinp_infos)
    if config.all_devs_options != []:
        if overrides_line:
            _update_overrides(lines, overrides_line, config.all_devs_options)
        else:
            _add_overrides(lines, config.all_devs_options)
    contents = '\n'.join(lines)
    return contents
Ejemplo n.º 7
0
def _parse_config(path):
    contents = multipathutil.read_config(path)
    if contents is None:
        return None
    conf = MultipathConfig(pathname=path)
    conf.all_devs_options = []
    section = None
    in_subsection = False
    device_options = []
    overrides_options = []
    in_all_devs = False
    for line in contents.split('\n'):
        try:
            data = multipathutil.LineData(line, section, in_subsection)
        except ValueError:
            continue
        if data.type == data.TYPE_BLANK:
            continue
        if data.type == data.TYPE_SECTION_END:
            if in_subsection:
                in_subsection = False
                if in_all_devs:
                    _add_options(conf.all_devs_options, device_options)
                in_all_devs = False
                device_options = []
            elif section:
                section = None
            continue
        if data.type == data.TYPE_SECTION_START:
            if not section:
                section = data.section
            elif not in_subsection:
                in_subsection = True
            continue
        if data.type != data.TYPE_OPTION:
            continue
        if section == 'defaults':
            if data.option == 'path_checker' or data.option == 'checker':
                conf.default_path_checker = data.value
            elif data.option == 'config_dir':
                conf.config_dir = data.value
            elif data.option == 'retain_attached_hw_handler':
                conf.default_retain_hwhandler = data.is_enabled()
            elif data.option == 'detect_prio':
                conf.default_detect_prio = data.is_enabled()
            elif data.option == 'detect_path_checker':
                conf.default_detect_checker = data.is_enabled()
            elif data.option == 'reassign_maps':
                conf.reassign_maps = data.is_enabled()
            elif data.option == 'hw_str_match':
                conf.hw_str_match_exists = True
            elif data.option == 'ignore_new_boot_devs':
                conf.ignore_new_boot_devs_exists = True
            elif data.option == 'new_bindings_in_boot':
                conf.new_bindings_in_boot_exists = True
        if section == 'devices' and in_subsection:
            if data.option == 'all_devs' and data.is_enabled():
                conf.all_devs_section_exists = True
                in_all_devs = True
            else:
                device_options.append((data.option, data.value))
        if section == 'overrides':
            if data.option == 'hardware_handler':
                conf.overrides_hwhandler_exists = True
            elif data.option == 'pg_timeout':
                conf.overrides_pg_timeout_exists = True
            else:
                overrides_options.append((data.option, data.value))
        if data.option == 'unpriv_sgio':
            conf.unpriv_sgio_exists = True
        if data.option == 'detect_path_checker':
            conf.detect_path_checker_exists = True
        if data.option == 'features' and 'queue_if_no_path' in data.value:
            conf.queue_if_no_path_exists = True

    if in_subsection and in_all_devs:
        _add_options(conf.all_devs_options, device_options)
    _fix_qinp_options(conf.all_devs_options)
    _filter_options(conf.all_devs_options, overrides_options)
    return conf