Example #1
0
def cleanup_stale_iprules():
    """
    Clean test ip rules that may have been left by the test run.
    They may exists on the system due to some buggy test that ran
    and has not properly cleaned after itself.
    In case any stale entries have been detected, attempt to clean everything
    and raise an error.
    """
    commands = [
        'bash',
        '-c',
        'while ip rule delete prio {} 2>/dev/null; do true; done'.format(
            sourceroute.RULE_PRIORITY)
    ]
    cmd.exec_sync(commands)

    yield

    IPRule = ip_rule.driver(ip_rule.Drivers.IPROUTE2)
    rules = [r for r in IPRule.rules()
             if r.to == IPV4_ADDRESS1 or r.prio == sourceroute.RULE_PRIORITY]
    if rules:
        for rule in rules:
            try:
                IPRule.delete(rule)
                logging.warning('Rule (%s) has been removed', rule)
            except Exception as e:
                logging.error('Error removing rule (%s): %s', rule, e)
        raise StaleIPRulesError()
Example #2
0
def _create_linux_bridge(brname):
    rc, _, err = exec_sync(['ip', 'link', 'add', brname, 'type', 'bridge'])
    if rc != 0:
        pytest.fail('Unable to create bridge. err: {}'.format(err))
    try:
        yield brname
    finally:
        exec_sync(['ip', 'link', 'del', brname])
Example #3
0
def check_brctl():
    try:
        cmd.exec_sync([EXT_BRCTL, 'show'])
    except OSError as e:
        if e.errno == errno.ENOENT:
            raise SkipTest('Cannot run %r: %s\nDo you have bridge-utils '
                           'installed?' % (EXT_BRCTL, e))
        raise
Example #4
0
def _create_tap():
    devname = '_tap99'
    rc, _, err = exec_sync(['ip', 'tuntap', 'add', devname, 'mode', 'tap'])
    if rc != 0:
        pytest.fail('Unable to create tap device. err: {}'.format(err))
    try:
        iface(devname).up()
        yield devname
    finally:
        exec_sync(['ip', 'tuntap', 'del', devname, 'mode', 'tap'])
Example #5
0
File: ifcfg.py Project: EdDev/vdsm
def stop_devices(device_ifcfgs):
    for dev in reversed(_sort_device_ifcfgs(device_ifcfgs)):
        ifdown(dev)
        if os.path.exists('/sys/class/net/%s/bridge' % dev):
            # ifdown is not enough to remove nicless bridges
            cmd.exec_sync([constants.EXT_BRCTL, 'delbr', dev])
        if _is_bond_name(dev):
            if _is_running_bond(dev):
                with open(BONDING_MASTERS, 'w') as f:
                    f.write("-%s\n" % dev)
Example #6
0
File: ifcfg.py Project: EdDev/vdsm
 def removeBridge(self, bridge):
     if not self.owned_device(bridge.name):
         IfcfgAcquire.acquire_device(bridge.name)
     if bridge.ipv4.bootproto == 'dhcp':
         ifacetracking.add(bridge.name)
     ifdown(bridge.name)
     self._removeSourceRoute(bridge)
     cmd.exec_sync([constants.EXT_BRCTL, 'delbr', bridge.name])
     self.configApplier.removeBridge(bridge.name)
     self.net_info.del_bridge(bridge.name)
     if bridge.port:
         bridge.port.remove()
Example #7
0
File: vsctl.py Project: EdDev/vdsm
    def commit(self):
        if not self.commands:
            return

        args = []
        for command in self.commands:
            args += ['--'] + command.cmd
        exec_line = [_ovs_vsctl_cmd()] + ['--oneline', '--format=json'] + args
        logging.debug('Executing commands: %s' % ' '.join(exec_line))

        rc, out, err = netcmd.exec_sync(exec_line)
        if rc != 0:
            err = err.splitlines()
            if OvsDBConnectionError.is_ovs_db_conn_error(err):
                raise OvsDBConnectionError('\n'.join(err))
            else:
                raise ConfigNetworkError(
                    ne.ERR_BAD_PARAMS,
                    'Executing commands failed: %s' % '\n'.join(err))
        if out is None:
            return

        for i, line in enumerate(out.splitlines()):
            self.commands[i].set_raw_result(line)
        return [cmd.result for cmd in self.commands]
Example #8
0
def _exec_cmd(command):
    rc, out, err = cmd.exec_sync(command)

    if rc:
        raise NMCliError(rc, err)

    return out
Example #9
0
def is_lldp_enabled_on_iface(iface):
    rc, out, err = cmd.exec_sync([LLDPTOOL, 'get-lldp', '-i', iface,
                                  'adminStatus'])
    if rc:
        return False
    keyval = out.strip().split('=', 1)
    if len(keyval) == 2 and keyval[0] == 'adminStatus':
        return not keyval[1] == 'disabled'
    return False
Example #10
0
File: dhcp.py Project: nirs/vdsm
    def start(self):
        self._create_conf()
        rc, out, err = cmd.exec_sync(self._cmd)

        if rc:  # == 2
            logging.debug(err)
            raise DhcpError('dhclient failed to obtain a lease: %d', rc)

        with open(self._pid_file) as pid_file:
            self.pid = int(pid_file.readline())
Example #11
0
File: info.py Project: nirs/vdsm
def is_ovs_service_running():
    try:
        rc, _, _ = cmd.exec_sync([OVS_CTL, 'status'])
    except OSError as err:
        # Silently ignore the missing file and consider the service as down.
        if err.errno == errno.ENOENT:
            rc = errno.ENOENT
        else:
            raise
    return rc == 0
Example #12
0
File: dpdk.py Project: nirs/vdsm
def _lshw_command():
    filter_out_hw = ['usb', 'pcmcia', 'isapnp', 'ide', 'scsi', 'dmi', 'memory',
                     'cpuinfo']
    filterout_cmd = list(itertools.chain.from_iterable(('-disable', x)
                                                       for x in filter_out_hw))
    rc, out, err = cmd.exec_sync(['lshw', '-json'] + filterout_cmd)
    if rc != 0:
        raise LshwError(err)

    return _normalize_lshw_result(out)
Example #13
0
 def start(self):
     cmds = [_IPERF3_BINARY.cmd, '--client', self._server_ip,
             '--version4',  # only IPv4
             '--time', str(self._test_time), '--parallel',
             str(self._threads), '--bind', self._bind_to,
             '--zerocopy',  # use less cpu
             '--json']
     rc, self._raw_output, err = cmd.exec_sync(cmds)
     if rc == 1 and 'No route to host' in self.out['error']:
         # it seems that it takes some time for the routes to get updated
         # on the os so that we don't get this error, hence the horrific
         # sleep here.
         # TODO: Investigate, understand, and remove this sleep.
         time.sleep(3)
         rc, self._raw_output, err = cmd.exec_sync(cmds)
     if rc:
         raise Exception('iperf3 client failed: cmd=%s, rc=%s, out=%s, '
                         'err=%s' % (' '.join(cmds), rc, self._raw_output,
                                     err))
Example #14
0
def supports_duid_file():
    """
    On EL7 dhclient doesn't have the -df option (to read the DUID from a bridge
    port's lease file). We must detect if the option is available, by running
    dhclient manually. To support EL7, we should probably fall back to -lf and
    refer dhclient to a new lease file with a device name substituted.
    """
    _, _, err = cmd.exec_sync(
        [DHCLIENT_BINARY.cmd,  # dhclient doesn't have -h/--help
         '-do-you-support-loading-duid-from-lease-files?'])
    return '-df' in err
Example #15
0
File: ifcfg.py Project: EdDev/vdsm
    def _persistentBackup(cls, filename):
        """ Persistently backup ifcfg-* config files """
        if os.path.exists('/usr/libexec/ovirt-functions'):
            cmd.exec_sync([constants.EXT_SH, '/usr/libexec/ovirt-functions',
                           'unmount_config', filename])
            logging.debug("unmounted %s using ovirt", filename)

        (dummy, basename) = os.path.split(filename)
        try:
            with open(filename) as f:
                content = f.read()
        except IOError as e:
            if e.errno == os.errno.ENOENT:
                # For non-exists ifcfg-* file use predefined header
                content = cls.DELETED_HEADER + '\n'
            else:
                raise
        logging.debug("backing up %s: %s", basename, content)

        cls.writeBackupFile(NET_CONF_BACK_DIR, basename, content)
Example #16
0
def _exec_cmd(command):
    returnCode, output, error = cmd.exec_sync(command)

    if returnCode:
        if 'Cannot find device' in error:
            exc = IPRoute2NoDeviceError
        elif 'File exists' in error:
            exc = IPRoute2AlreadyExistsError
        else:
            exc = IPRoute2Error
        raise exc(returnCode, error.splitlines())
    return output.splitlines()
Example #17
0
def get_tlvs(iface):
    """
    Report the specified tlv identifiers.

    :param iface: The interface to query.
    :return: TLV reports in a dict format where the TLV ID/s are the keys.
    """
    rc, stdout, err = cmd.exec_sync([LLDPTOOL, 'get-tlv', '-n', '-i', iface])
    if rc == 0:
        return _parse_tlvs(stdout)
    else:
        raise TlvReportLldpError(rc, stdout, err, iface)
Example #18
0
def supports_duid_file():
    """
    On EL7 dhclient doesn't have the -df option (to read the DUID from a bridge
    port's lease file). We must detect if the option is available, by running
    dhclient manually. To support EL7, we should probably fall back to -lf and
    refer dhclient to a new lease file with a device name substituted.
    """
    _, _, err = cmd.exec_sync([
        DHCLIENT_BINARY.cmd,  # dhclient doesn't have -h/--help
        '-do-you-support-loading-duid-from-lease-files?'
    ])
    return '-df' in err
Example #19
0
def _exec_cmd(command):
    returnCode, output, error = cmd.exec_sync(command)

    if returnCode:
        if 'Cannot find device' in error:
            exc = IPRoute2NoDeviceError
        elif 'File exists' in error:
            exc = IPRoute2AlreadyExistsError
        else:
            exc = IPRoute2Error
        raise exc(returnCode, error.splitlines())
    return output.splitlines()
Example #20
0
def get_tlvs(iface):
    """
    Report the specified tlv identifiers.

    :param iface: The interface to query.
    :return: TLV reports in a dict format where the TLV ID/s are the keys.
    """
    rc, stdout, err = cmd.exec_sync([LLDPTOOL, 'get-tlv', '-n', '-i', iface])
    if rc == 0:
        return _parse_tlvs(stdout)
    else:
        raise TlvReportLldpError(rc, stdout, err, iface)
Example #21
0
def process_request(command):
    command.insert(0, EXT_TC)
    retcode, out, err = cmd.exec_sync(command)
    if retcode != 0:
        if retcode == 2 and err:
            for err_line in err.splitlines():
                if err_line.startswith(_TC_ERR_PREFIX):
                    err = err_line
                    retcode = _errno_trans.get(
                        err[len(_TC_ERR_PREFIX):].strip())
                    break
        raise TrafficControlException(retcode, err, command)
    return out
Example #22
0
def process_request(command):
    command.insert(0, EXT_TC)
    retcode, out, err = cmd.exec_sync(command)
    if retcode != 0:
        if retcode == 2 and err:
            for err_line in err.splitlines():
                if err_line.startswith(_TC_ERR_PREFIX):
                    err = err_line
                    retcode = _errno_trans.get(
                        err[len(_TC_ERR_PREFIX):].strip())
                    break
        raise TrafficControlException(retcode, err, command)
    return out
Example #23
0
    def _persistentBackup(cls, filename):
        """ Persistently backup ifcfg-* config files """
        if os.path.exists('/usr/libexec/ovirt-functions'):
            cmd.exec_sync([
                constants.EXT_SH, '/usr/libexec/ovirt-functions',
                'unmount_config', filename
            ])
            logging.debug("unmounted %s using ovirt", filename)

        (dummy, basename) = os.path.split(filename)
        try:
            with open(filename) as f:
                content = f.read()
        except IOError as e:
            if e.errno == os.errno.ENOENT:
                # For non-exists ifcfg-* file use predefined header
                content = cls.DELETED_HEADER + '\n'
            else:
                raise
        logging.debug("backing up %s: %s", basename, content)

        cls.writeBackupFile(NET_CONF_BACK_DIR, basename, content)
Example #24
0
def _exec_ifup_by_name(iface_name, cgroup=dhclient.DHCLIENT_CGROUP):
    """
    Actually bring up an interface (by name).
    """
    cmds = [EXT_IFUP, iface_name]

    if cgroup:
        rc, out, err = cmd.exec_systemd_new_unit(cmds, slice_name=cgroup)
    else:
        rc, out, err = cmd.exec_sync(cmds)

    if rc != 0:
        # In /etc/sysconfig/network-scripts/ifup* the last line usually
        # contains the error reason.
        raise ConfigNetworkError(ERR_FAILED_IFUP, out[-1] if out else '')
Example #25
0
File: ifcfg.py Project: EdDev/vdsm
def _exec_ifup_by_name(iface_name, cgroup=dhclient.DHCLIENT_CGROUP):
    """
    Actually bring up an interface (by name).
    """
    cmds = [constants.EXT_IFUP, iface_name]

    if cgroup:
        rc, out, err = cmd.exec_systemd_new_unit(cmds, slice_name=cgroup)
    else:
        rc, out, err = cmd.exec_sync(cmds)

    if rc != 0:
        # In /etc/sysconfig/network-scripts/ifup* the last line usually
        # contains the error reason.
        raise ConfigNetworkError(ERR_FAILED_IFUP, out[-1] if out else '')
Example #26
0
def _lshw_command():
    filter_out_hw = [
        'usb',
        'pcmcia',
        'isapnp',
        'ide',
        'scsi',
        'dmi',
        'memory',
        'cpuinfo',
    ]
    filterout_cmd = list(
        itertools.chain.from_iterable(('-disable', x) for x in filter_out_hw)
    )
    rc, out, err = cmd.exec_sync(['lshw', '-json'] + filterout_cmd)
    if rc != 0:
        raise LshwError(err)

    return _normalize_lshw_result(out)
Example #27
0
def _setup_ovs_bridge_mappings(running_networks, networks):
    # All networks are already saved in the running_network that's why
    # we can use the split only over running_networks
    ovs_nets, _ = util.split_switch_type(running_networks, running_config={})
    if networks and ovs_nets:
        mappings = (','.join(
            nmstate.prepare_ovs_bridge_mappings(running_networks)) or '""')
        cmds = [
            'ovs-vsctl',
            'set',
            'open',
            '.',
            f'external-ids:ovn-bridge-mappings={mappings}',
        ]
        rc, out, err = cmd.exec_sync(cmds)
        if rc:
            raise Exception(
                'Failed to set external mappings for OvS: '
                f'cmd={" ".join(cmds)}, rc={rc}, out={out}, err={err}')
Example #28
0
def dhcp_nm_client(iface, family=4):
    cmd.exec_sync([
        'nmcli',
        'con',
        'modify',
        iface,
        'ipv{}.method'.format(family),
        'auto',
    ])
    cmd.exec_sync(['nmcli', 'con', 'up', iface])
    try:
        yield
    finally:
        cmd.exec_sync([
            'nmcli',
            'con',
            'modify',
            iface,
            'ipv{}.method'.format(family),
            'disabled',
        ])
        cmd.exec_sync(['nmcli', 'con', 'up', iface])
Example #29
0
def veth_pair(prefix='veth_', max_length=15):
    """
    Yield a pair of veth devices. This assumes root privileges (currently
    required by all tests anyway).

    Both sides of the pair have a pseudo-random suffix (e.g. veth_m6Lz7uMK9c).
    """
    left_side = random_iface_name(prefix, max_length)
    right_side = random_iface_name(prefix, max_length)
    try:
        linkAdd(left_side, linkType='veth', args=('peer', 'name', right_side))
        cmd.exec_sync(['nmcli', 'dev', 'set', left_side, 'managed', 'yes'])
        cmd.exec_sync(['nmcli', 'dev', 'set', right_side, 'managed', 'yes'])
    except IPRoute2Error as e:
        pytest.skip('Failed to create a veth pair: %s', e)
    try:
        yield left_side, right_side
    finally:
        # the peer device is removed by the kernel
        linkDel(left_side)
        cmd.exec_sync(['nmcli', 'con', 'del', left_side])
        cmd.exec_sync(['nmcli', 'con', 'del', right_side])
Example #30
0
 def __init__(self):
     rc, out, err = cmd.exec_sync([OVS_CTL, 'status'])
     self.ovs_init_state_is_up = (rc == 0)
Example #31
0
def is_lldptool_functional():
    rc, _, _ = cmd.exec_sync([LLDPTOOL, '-ping'])
    return rc == 0
Example #32
0
def enable_lldp_on_iface(iface, rx_only=True):
    rc, out, err = cmd.exec_sync(
        [LLDPTOOL, 'set-lldp', '-i', iface,
         'adminStatus=' + ('rx' if rx_only else 'rxtx')])
    if rc:
        raise EnableLldpError(rc, out, err, iface)
Example #33
0
 def setup(self):
     if not self.ovs_init_state_is_up:
         cmd.exec_sync([OVS_CTL, 'start'])
Example #34
0
 def teardown(self):
     if not self.ovs_init_state_is_up:
         cmd.exec_sync([OVS_CTL, 'stop'])
Example #35
0
def is_lldpad_service_running():
    rc, _, _ = cmd.exec_sync([SYSTEMCTL, 'status', LLDPAD_SERVICE])
    return rc == 0
Example #36
0
def _attach_dev_to_bridge(tapdev, bridge):
    rc, _, err = exec_sync(['ip', 'link', 'set', tapdev, 'master', bridge])
    if rc != 0:
        pytest.fail('Filed to add {} to {}. err: {}'.format(
            tapdev, bridge, err))
Example #37
0
 def _showQdisc(self):
     _, out, _ = cmd.exec_sync(
         [EXT_TC, "qdisc", "show", "dev", self._bridge.devName])
     return out
Example #38
0
def _serviceRunning(name):
    ret, _, _ = cmd.exec_sync([_SYSTEMCTL_BINARY.cmd, name, 'status'])
    # return code 0 means service is running
    return not ret
Example #39
0
def is_lldpad_service_running():
    rc, _, _ = cmd.exec_sync([SYSTEMCTL, 'status', LLDPAD_SERVICE])
    return rc == 0
Example #40
0
 def is_service_running(self):
     rc, out, err = cmd.exec_sync([OVS_CTL, 'status'])
     return rc == 0
Example #41
0
File: ifcfg.py Project: EdDev/vdsm
def ifdown(iface):
    "Bring down an interface"
    rc, _, _ = cmd.exec_sync([constants.EXT_IFDOWN, iface])
    return rc
Example #42
0
 def setup(self):
     if not self.ovs_init_state_is_up:
         cmd.exec_sync([OVS_CTL, '--system-id=random', 'start'])
     assert self.is_service_running()
Example #43
0
def _exec_cmd_checker(command):
    ret, out, err = cmd.exec_sync(command)
    if ret:
        raise FirewallError('Command {0} failed with {1}; {2}'.format(
            command, out, err))
Example #44
0
def disable_lldp_on_iface(iface):
    rc, out, err = cmd.exec_sync(
        [LLDPTOOL, 'set-lldp', '-i', iface, 'adminStatus=disabled'])
    if rc:
        raise DisableLldpError(rc, out, err, iface)
Example #45
0
def is_running():
    rc, out, err = cmd.exec_sync([SYSTEMCTL.cmd, 'status', NM_SERVICE])
    return rc == 0
Example #46
0
 def test_exec_sync_with_no_output(self):
     rc, out, err = cmd.exec_sync(('true',))
     self.assertEqual(out, '')
     self.assertEqual(err, '')
Example #47
0
 def setup(self):
     if not self.ovs_init_state_is_up:
         cmd.exec_sync([OVS_CTL, '--system-id=random', 'start'])
     assert self.is_service_running()
Example #48
0
 def test_exec_sync_with_success_output(self):
     rc, out, err = cmd.exec_sync(('echo', 'hello world'))
     self.assertEqual(rc, 0, err)
     self.assertEqual(out, 'hello world\n')
     self.assertEqual(err, '')
Example #49
0
 def is_service_running(self):
     rc, out, err = cmd.exec_sync([OVS_CTL, 'status'])
     return rc == 0
Example #50
0
 def test_exec_sync_with_error_output(self):
     rc, out, err = cmd.exec_sync(('ls', 'no such prog'))
     self.assertNotEqual(rc, 0)
     self.assertIn('No such file or directory', err)
     self.assertEqual(out, '')
Example #51
0
def is_lldptool_functional():
    rc, _, _ = cmd.exec_sync([LLDPTOOL, '-ping'])
    return rc == 0
Example #52
0
def ifdown(iface):
    "Bring down an interface"
    rc, _, _ = cmd.exec_sync([EXT_IFDOWN, iface])
    return rc
Example #53
0
def disable_lldp_on_iface(iface):
    rc, out, err = cmd.exec_sync([LLDPTOOL, 'set-lldp', '-i', iface,
                                  'adminStatus=disabled'])
    if rc:
        raise DisableLldpError(rc, out, err, iface)
Example #54
0
 def test_exec_sync_with_no_output(self):
     _, out, err = cmd.exec_sync(('true', ))
     assert out == ''
     assert err == ''
Example #55
0
 def test_exec_sync_with_error_output(self):
     rc, out, err = cmd.exec_sync(('ls', 'no such prog'))
     assert rc != 0
     assert 'No such file or directory' in err
     assert out == ''
Example #56
0
 def delDevice(self):
     self._down()
     linkDel(self.devName)
     if nmstate.is_nmstate_backend():
         cmd.exec_sync(['nmcli', 'con', 'del', self.devName])
Example #57
0
 def test_exec_sync_with_success_output(self):
     rc, out, err = cmd.exec_sync(('echo', 'hello world'))
     assert rc == 0, err
     assert out == 'hello world\n'
     assert err == ''
Example #58
0
def _requires_systemctl():
    rc, _, err = cmd.exec_sync([_SYSTEMCTL.cmd, 'status', 'foo'])
    run_chroot_err = 'Running in chroot, ignoring request'
    if rc == 1 or run_chroot_err in err:
        raise SkipTest('systemctl is not available')
Example #59
0
 def teardown(self):
     if not self.ovs_init_state_is_up:
         cmd.exec_sync([OVS_CTL, 'stop'])
Example #60
0
def check_call(cmds):
    rc, out, err = cmd.exec_sync(cmds)
    if rc != 0:
        raise ExecError(
            'Command %s returned non-zero exit status %s.' % (cmds, rc), out,
            err)