Esempio n. 1
0
def read_boot_time(dut, build):
    boot_times = None
    print 'INFO: sending reboot'
    # The first boot time after a fresh install is much slower than the following
    wait_to_come_up(dut)
    run(['reboot'], host=dut)
    with time_limit(600, 'wait for sync up event'):
        while 1:
            try:
                print 'INFO: checking if', dut, 'is still up'
                check_up(dut)
            except Exception, exc:
                print 'INFO:', dut, 'is down', exc
                break
            else:
                print 'INFO:', dut, 'still up'
                sleep(1)
                print
        while 1:
            wait_to_come_up(dut, timeout=1200)
            log = grep_dut_log(dut, '/var/log/messages', UI_READY)
            
            boot_times = parse_sys_log(log)
            print 'INFO: boot times:', ' '.join(['%.2f' % t for t in boot_times])
            if len(boot_times) >= 2:
                break
            sleep(5)
Esempio n. 2
0
File: launch.py Progetto: OpenXT/bvt
def is_test_available(case, dut, current_install, domlist, build):
    """Can we run case on dut? Return boolean and a reason why not"""
    unsuitable = case.get('unsuitable_duts', list())
    if dut in unsuitable:
        return False, dut+' unsuitable'
    if current_install and case['trigger'] in ['first', 'platform install']:
        return False, 'restricted to current install'
    guest = case.get('guest')
    if guest is not None:
        guest_os_name, _ = name_split(guest)
    else:
        guest_os_name = None
    if guest and case.get('trigger') != 'VM install':
        if not [dom for dom in domlist if dom['name'] == guest
                and dom['status'] == 'running']:
            return False, "requires "+guest+" VM"
    if 'futile' not in case:
        futile = ''
    else:
        try:
            with time_limit(10, 'futility check'):
                futile = case['futile'](dut, guest, guest_os_name, build, domlist) 
        except Exception, exc:
            print_exc()
            futile = 'futility check failed %r' % exc
Esempio n. 3
0
def call_with_timeout(function, description, timeout=60, 
                      timeout_callback = None):
    """Call function and return its result. But if that
    takes more than timeout seconds, invoke timeout_callback with the
    actual delay, or if timeout_callback is None, raise TimeoutError with 
    description"""
    with time_limit(timeout, description):
        return function()
Esempio n. 4
0
def wait_for_windows_to_come_up(vm_address, timeout=120, pace=1.0):
    """Return once windows is ip; or through a timeout exception"""
    with time_limit(timeout, description='wait for Windows VM at '+
                    vm_address+' to go down'):
        while 1:
            if is_windows_up(vm_address):
                return
            sleep(pace)
Esempio n. 5
0
def wait_for_vm_to_stop(dut, guest, timeout=120, pace=5.0):
    """Wait for guest to be marked stopped"""
    with time_limit(timeout, 
                    description='Wait for '+guest+' to be marked as stopped'):
        while 1:
            status = find_domain(dut, guest)
            if status['status'] == 'stopped':
                return
            sleep(pace)
Esempio n. 6
0
def wait_for_guest_state(dut, guest, predicate, timeout=120, pace=1):
    """Wait for guest state to fulfill predicate"""
    with time_limit(timeout, description='wait for '+guest+' to go down'):
        while 1:
            status = guest_status(dut, guest)
            print 'INFO: VM', guest, 'status', status
            if predicate(status):
                break
            sleep(pace)
Esempio n. 7
0
def wait_for_windows_to_come_up(vm_address, timeout=120, pace=1.0):
    """Return once windows is ip; or through a timeout exception"""
    with time_limit(timeout,
                    description='wait for Windows VM at ' + vm_address +
                    ' to go down'):
        while 1:
            if is_windows_up(vm_address):
                return
            sleep(pace)
Esempio n. 8
0
def wait_for_guest_state(dut, guest, predicate, timeout=120, pace=1):
    """Wait for guest state to fulfill predicate"""
    with time_limit(timeout, description='wait for ' + guest + ' to go down'):
        while 1:
            status = guest_status(dut, guest)
            print 'INFO: VM', guest, 'status', status
            if predicate(status):
                break
            sleep(pace)
Esempio n. 9
0
def wait_for_vm_to_stop(dut, guest, timeout=120, pace=5.0):
    """Wait for guest to be marked stopped"""
    with time_limit(timeout,
                    description='Wait for ' + guest +
                    ' to be marked as stopped'):
        while 1:
            status = find_domain(dut, guest)
            if status['status'] == 'stopped':
                return
            sleep(pace)
Esempio n. 10
0
def wait_for_domid_change(dut, guest, old_dom_id, timeout=300, pace=5.0):
    """Wait for guest to change from old_dom_id"""
    with time_limit(timeout,
                    description='Wait for '+guest +
                    ' to change domain ID from '+str(old_dom_id)):
        while 1:
            status = find_domain(dut, guest)
            print 'DOMAINS:', guest, 'dom ID', status['dom_id'], 'c/w', old_dom_id
            if status['dom_id'] != old_dom_id:
                return
            sleep(pace)
Esempio n. 11
0
def accelerate_graphics(dut, domain):
    """Turn domain into a PVM"""
    print 'HEADLINE: ensuring acceleration enabled to', dut, domain
    start_vm_if_not_running(dut, domain)
    vm_address = wait_for_windows(dut, domain)
    if have_accelerated_graphics(dut, domain, vm_address):
        print 'HEADLINE: already have accelerated graphics on', dut, domain
        test_graphics(vm_address, dut, domain)
        return
    print 'HEADLINE: no accelerated graphics on', dut, domain, \
        'so will enable acceleration'
    with time_limit(3600, 'enable PVM on ' + domain + ' on ' + dut):
        vm_address = wait_for_windows(dut, domain)
        run(['xec-vm', '-n', domain, 'set', 'pv-addons', 'true'],
            timeout=600,
            host=dut)
        run(['xec-vm', '-n', domain, 'shutdown'], timeout=600, host=dut)
        while is_windows_up(vm_address):
            print 'INFO: windows still up'
            sleep(1)
        gpu_bdf = find_gpu_bdf(dut)
        run(['xec-vm', '-n', domain, 'set', 'gpu', gpu_bdf],
            timeout=600,
            host=dut)
        run(['xec-vm', '-n', domain, 'start'], timeout=600, host=dut)
        with time_limit(1200, 'wait for windows driver model'):
            while 1:
                uuid_ex = run(['xec-vm', '-n', domain, 'get', 'uuid'],
                              host=dut)
                uuid = uuid_ex.split()[0]
                print 'INFO:', 'uuid', uuid
                out = run(['xenvm-cmd', uuid, 'pci-list'], host=dut)
                print 'PCI pass through status:'
                print out
                ready = have_accelerated_graphics(dut, domain, vm_address)
                print 'HEADLINE','accelerated graphics installed' if \
                  ready else  'accelerated graphics not ready'
                if ready:
                    break
                sleep(10)
    test_graphics(vm_address, dut, domain)
Esempio n. 12
0
def wait_for_domid_change(dut, guest, old_dom_id, timeout=300, pace=5.0):
    """Wait for guest to change from old_dom_id"""
    with time_limit(timeout,
                    description='Wait for ' + guest +
                    ' to change domain ID from ' + str(old_dom_id)):
        while 1:
            status = find_domain(dut, guest)
            print 'DOMAINS:', guest, 'dom ID', status[
                'dom_id'], 'c/w', old_dom_id
            if status['dom_id'] != old_dom_id:
                return
            sleep(pace)
Esempio n. 13
0
def wait_to_go_down(host, timeout=120, pace=1):
    """Check that host goes down within timeout"""
    with time_limit(timeout, description='wait for '+host+' to go down'):
        while 1:
            try:
                check_up(host)
            except Exception, exc:
                print 'CHECK_DOWN: got exception', repr(exc), \
                    'so assuming host down'
                break
            else:
                print 'CHECK_DOWN:', host, 'is still up'
            sleep(pace)
Esempio n. 14
0
def wait_to_go_down(host, timeout=120, pace=1):
    """Check that host goes down within timeout"""
    with time_limit(timeout, description='wait for ' + host + ' to go down'):
        while 1:
            try:
                check_up(host)
            except Exception, exc:
                print 'CHECK_DOWN: got exception', repr(exc), \
                    'so assuming host down'
                break
            else:
                print 'CHECK_DOWN:', host, 'is still up'
            sleep(pace)
Esempio n. 15
0
def accelerate_graphics(dut, domain):
    """Turn domain into a PVM"""
    print 'HEADLINE: ensuring acceleration enabled to', dut, domain
    start_vm_if_not_running(dut, domain)
    vm_address = wait_for_windows(dut, domain)
    if have_accelerated_graphics(dut, domain, vm_address):
        print 'HEADLINE: already have accelerated graphics on', dut, domain
        test_graphics(vm_address, dut, domain)
        return
    print 'HEADLINE: no accelerated graphics on', dut, domain, \
        'so will enable acceleration'
    with time_limit(3600, 'enable PVM on '+domain+' on '+dut):
        vm_address = wait_for_windows(dut, domain)
        run(['xec-vm', '-n', domain, 'set', 'pv-addons', 'true'], timeout=600, 
            host=dut)
        run(['xec-vm', '-n', domain, 'shutdown'], timeout=600, host=dut)
        while is_windows_up(vm_address):
            print 'INFO: windows still up'
            sleep(1)
        gpu_bdf = find_gpu_bdf(dut)
        run(['xec-vm', '-n', domain, 'set', 'gpu', gpu_bdf], timeout=600, 
            host=dut)
        run(['xec-vm', '-n', domain, 'start'], timeout=600, host=dut)
        with time_limit(1200, 'wait for windows driver model'):
            while 1:
                uuid_ex = run(['xec-vm', '-n', domain, 'get', 'uuid'], host=dut)
                uuid = uuid_ex.split()[0]
                print 'INFO:', 'uuid', uuid 
                out = run(['xenvm-cmd', uuid, 'pci-list'], host=dut)
                print 'PCI pass through status:'
                print out
                ready = have_accelerated_graphics(dut, domain, vm_address)
                print 'HEADLINE','accelerated graphics installed' if \
                  ready else  'accelerated graphics not ready'
                if ready:
                    break
                sleep(10)
    test_graphics(vm_address, dut, domain)
Esempio n. 16
0
def run_via_exec_daemon(args, host, timeout=60, ignore_failure=False,
                        split=False, word_split=False,line_split=False, echo=True, wait=True):
    """Run args on host.

    Fail after timeout.

    Unless ignore_failure is True, if the command fails raise SubprocessFailure.

    If split is set, split up to list of words per line.

    Intended to be similar to src.bvtlib.run.run"""
    process = call_exec_daemon('run', [' '.join(args)], host=host, 
                               timeout=timeout)
    if echo:
        print 'EXEC_DAEMON: launched', args, 'as', process, 'on', host
    if not wait:
        return
    with time_limit(timeout, 'launch '+' '.join(args)+' on '+host):
        """Has process finished?"""
        while 1:
            result = call_exec_daemon('poll', [process], 
                                      timeout=timeout, host=host,
                                      why=' to complete '+ ' '.join(args))
            if echo:
                print 'EXEC_DAEMON: poll', process, 'returned', result
            if result == 'DONE':
                break
            sleep(1)
        output = call_exec_daemon('log', [process], 
                                  timeout=timeout, host=host), ''
        if echo:
            print 'EXEC_DAEMON: read', len(output), 'bytes of output for', \
                process, 'on', host
            for line in output[0].split('\n'):
                print 'EXEC_DAEMON_OUTPUT:', process, 'on', host, ':', line
        exit_code = call_exec_daemon('returncode', [process], 
                                     timeout=timeout, host=host)
        if echo:
            print 'EXEC_DAEMON: completed', args, 'id', process, \
                'exit code', exit_code
        try:
            call_exec_daemon('cleanup', [process], timeout=timeout, host=host)
        except Fault:
            if echo:
                print 'EXEC_DAEMON: unable to cleanup for', process, 'on', host
        else:
            if echo:
                print 'EXEC_DEAMON: cleaned up', process, 'on', host
    if exit_code != 0 and not ignore_failure:
        raise SubprocessError(exit_code, output[0], output[1])
    
    if word_split:
        output2 = [x.split() for x in output]
    elif split: 
        output2 = [[line.split() for line in x.split('\n')] for 
               x in output]
    elif line_split:
        output2 = [x.split('\n') for x in output]
    else:
        output2 = output
    outv = output2[0]
    return (exit_code, outv) if ignore_failure else outv
Esempio n. 17
0
def set_power_state(dut, state, args):
    """Set AMT state"""
    try:
        pcontrol = get_power_control_type(dut)
    except NoMongoHost:
        pcontrol = 'AMT'
    print 'INFO: ', pcontrol
    APC_match = match('snmp-apc:(.+?):(.+)', pcontrol)
    if APC_match is not None:
        switcher = APC_match.group(1)
        host = int(APC_match.group(2))
        pdu = get_PDU(switcher)
        outlet = pdu.get_outlet_by_number(host)
        if state == 's5':
            outlet.off()
        else:
            outlet.on()
    elif pcontrol == 'AMT':
        #Wake machine out of M-Off state if idle timer was reached.
        run([AMTTOOL, dut + '-amt'], env=AMTENV, timeout=60, stdin_push='y\n')
        sleep(5)
        run([AMTTOOL, dut + '-amt'] + args,
            env=AMTENV,
            timeout=60,
            stdin_push='y\n')
        sleep(5)
        verify_power_state(dut, state)
    elif pcontrol.startswith('franken'):
        run([('/bvt-device/hard_turn_off.pl'
              if state == 's5' else '/bvt-device/turn_on.pl'), pcontrol[-1]],
            host='igor.cam.xci-test.com',
            cwd='/bvt-device')
    elif pcontrol == 'statedb':
        sdb = open_database('statedb')
        config = open_database('configdb')
        asset = open_database('assetdb').select1_field('tag',
                                                       'assets',
                                                       name=dut)
        if 0:
            # sometimes (e.g. fozzie in July 2011)
            asset2 = config.select1_field('asset',
                                          'ips',
                                          reverse_dns=dut +
                                          '.cam.xci-test.com')
            print 'from configdb', asset2
            print 'from assetdb', asset
            assert asset == asset2

        with time_limit(300, description='set power state'):
            while 1:
                current = sdb.select1_field('current_power',
                                            'control',
                                            asset=asset)
                print 'POWER_CONTROL: statedb reports power state', current, \
                    'for', dut, asset,
                statep = 's1' if state == 's0' else state
                if current == statep:
                    print '... happy'
                    break
                sdb.execute(
                    'UPDATE control SET desired_power=%s '
                    'WHERE asset=%s', (statep, asset))
                print '... requesting', statep
                sleep(2)
    elif pcontrol == 'wsman':
        if dut + '-amt' not in run(['amtctrl list'], shell=True):
            run([
                'amtctrl add ' + dut + '-amt ' + dut + '-amt ' +
                AMTENV['AMT_PASSWORD']
            ],
                shell=True)
        if 'powerdown' in args:
            run(['amtctrl ' + dut + '-amt off'], shell=True)
        elif 'pxe' in args:
            run(['amtctrl ' + dut + '-amt on'], shell=True)
            run(['amtctrl ' + dut + '-amt pxeboot'], shell=True)
        elif 'powerup' in args:
            run(['amtctrl ' + dut + '-amt on'], shell=True)
        else:
            print "wsman power operation not supported."
    else:  # grrr: no "if (a=foo()) bar(a);" in python!
        raise NoPowerControl(dut, pcontrol, state)
Esempio n. 18
0
File: run.py Progetto: OpenXT/bvt
def run(args, timeout=60, host=None, split=False, word_split=False, 
        line_split=False,
        ignore_failure=False, verify=True,
        cwd=None, user=None, env={}, shell=False, stderr=False,  echo=False, 
        verbose=True, announce_interval = 20, wait=True,
        stdin_push='', output_callback=None, 
        error_callback=None, check_host_key=False, log_file=None):
    """Run command with args, or raise SubprocessTimeout after timeout seconds.

    If host is specified, ssh to that machine. Let's hope your ssh configuration
    works.

    If split is true, convert output to a list of lines, where each
    line is a list of words.
    
    If word_split is true, convert output to a list of whitespace separated words.

    If line_split is true, convert output to a list of lines.

    If ignore_failure is true, do not raise exceptions for non-zero exit codes.
    
    If cwd is true, run commands in that working directory using a shell.

    If env is a dictionary, set those environment variables.

    If shell is true, run through a shell (implied by cwd or env).

    If stderr is true, return stderr as well as stdout. Otherwise or by default
    return just stdout.

    If echo is true, echo stdout/stderr through to sys.stdout/sys.stderr

    If verbose is true, print arguments timing and exit code.
    See http://stackoverflow.com/questions/1191374/subprocess-with-timeout

    If verify and host are set, then make sure the connection to the host
    works before trying it.

    If wait is false, then simply launch the command and return straight away.
    """
    description = ' '.join(args)
    if host and verify:
        verify_connection(host, user, timeout=timeout, check_host_key=check_host_key)
    spargs = space_escape(args)    
    if host:
        shell_prefixes = []
        if cwd:
            shell_prefixes.extend(['cd', cwd, '&&'])
            cwd = None
        for key, value in env.iteritems():
            shell_prefixes.append("%s=%s" % (key, (space_escape([value])[0])))
        env = None
        if '.' not in host: 
            host += TEST_MACHINE_DOMAIN_POSTFIX
        args = ['ssh', '-oPasswordAuthentication=no', '-l' + (user if user else 'root'), host]
        if not check_host_key:
            args.extend(['-oStrictHostKeyChecking=no', '-oUserKnownHostsFile=/dev/null'])
        args += shell_prefixes + spargs
        description += ' on '+host
    if verbose:
        print 'RUN:', repr(args)
    process = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=shell,
                    env=env, cwd=cwd)

    fd2output = {}
    fd2file = {}
    poller = poll()

    def register_and_append(file_obj, eventmask):
        """Record file_obj for poll operations"""
        poller.register(file_obj.fileno(), eventmask)
        fd2file[file_obj.fileno()] = file_obj
        out = fd2output[file_obj.fileno()] = []
        return out
    def close_unregister_and_remove(fdes):
        """fdes is finished"""
        poller.unregister(fdes)
        fd2file[fdes].close()
        fd2file.pop(fdes)


    stdout_list = register_and_append(process.stdout, POLLIN | POLLPRI)
    stderr_list = register_and_append(process.stderr, POLLIN | POLLPRI)

    def throw_timeout(delay):
        """Throw exception for after delay"""
        try:
            out = run(['ps', '--no-headers', '-o', 'pid', '--ppid', 
                       str(process.pid)])
        except SubprocessError:
            out = ''
        pids = [process.pid] + [int(p) for p in out.split()]
        for pid in pids:
            try:
                kill(pid, SIGKILL)
            except OSError:
                print 'WARNING: unable to kill subprocess', pid
        raise TimeoutError(description, timeout, delay,
                           ''.join(stdout_list),
                           ''.join(stderr_list))

    if not wait:
        return

    start = time()
    with time_limit(timeout, 'launch '+' '.join(args), 
                    timeout_callback=throw_timeout):
        if stdin_push:
            process.stdin.write(stdin_push)
            process.stdin.flush()
            process.stdin.close()
        announce = time() + announce_interval
        while fd2file:
            if time() > announce:
                announce = time() + announce_interval
                print 'NOTE: waiting', time() - start, 'of', timeout, \
                    'seconds for', ' '.join(args)
            try:
                ready = poller.poll(20)
            except error, eparam:
                if eparam.args[0] == EINTR:
                    continue
                raise
            for fdes, mode in ready:
                if not mode & (POLLIN | POLLPRI):
                    close_unregister_and_remove(fdes)
                    continue
                if fdes not in fd2file:
                    print 'operation on unexpected FD', fdes
                    continue
                data = read(fdes, 4096)
                if not data:
                    close_unregister_and_remove(fdes)
                fd2output[fdes].append(data)
                fileobj = fd2file[fdes]
                if fileobj == process.stdout:
                    if echo:
                        for line in data.splitlines():
                            print 'STDOUT:', line
                    if output_callback:
                        output_callback(data)
                if fileobj == process.stderr:
                    if echo:
                        for line in data.splitlines():
                            print 'STDERR:', line
                    if error_callback:
                        error_callback(data)
            
        process.wait()
        output = ''.join(stdout_list), ''.join(stderr_list)
        exit_code = process.returncode
Esempio n. 19
0
def run(args,
        timeout=60,
        host=None,
        split=False,
        word_split=False,
        line_split=False,
        ignore_failure=False,
        verify=True,
        cwd=None,
        user=None,
        env={},
        shell=False,
        stderr=False,
        echo=False,
        verbose=True,
        announce_interval=20,
        wait=True,
        stdin_push='',
        output_callback=None,
        error_callback=None,
        check_host_key=False,
        log_file=None):
    """Run command with args, or raise SubprocessTimeout after timeout seconds.

    If host is specified, ssh to that machine. Let's hope your ssh configuration
    works.

    If split is true, convert output to a list of lines, where each
    line is a list of words.
    
    If word_split is true, convert output to a list of whitespace separated words.

    If line_split is true, convert output to a list of lines.

    If ignore_failure is true, do not raise exceptions for non-zero exit codes.
    
    If cwd is true, run commands in that working directory using a shell.

    If env is a dictionary, set those environment variables.

    If shell is true, run through a shell (implied by cwd or env).

    If stderr is true, return stderr as well as stdout. Otherwise or by default
    return just stdout.

    If echo is true, echo stdout/stderr through to sys.stdout/sys.stderr

    If verbose is true, print arguments timing and exit code.
    See http://stackoverflow.com/questions/1191374/subprocess-with-timeout

    If verify and host are set, then make sure the connection to the host
    works before trying it.

    If wait is false, then simply launch the command and return straight away.
    """
    description = ' '.join(args)
    if host and verify:
        verify_connection(host,
                          user,
                          timeout=timeout,
                          check_host_key=check_host_key)
    spargs = space_escape(args)
    if host:
        shell_prefixes = []
        if cwd:
            shell_prefixes.extend(['cd', cwd, '&&'])
            cwd = None
        for key, value in env.iteritems():
            shell_prefixes.append("%s=%s" % (key, (space_escape([value])[0])))
        env = None
        if '.' not in host:
            host += TEST_MACHINE_DOMAIN_POSTFIX
        args = [
            'ssh', '-oPasswordAuthentication=no',
            '-l' + (user if user else 'root'), host
        ]
        if not check_host_key:
            args.extend([
                '-oStrictHostKeyChecking=no', '-oUserKnownHostsFile=/dev/null'
            ])
        args += shell_prefixes + spargs
        description += ' on ' + host
    if verbose:
        print 'RUN:', repr(args)
    process = Popen(args,
                    stdout=PIPE,
                    stderr=PIPE,
                    stdin=PIPE,
                    shell=shell,
                    env=env,
                    cwd=cwd)

    fd2output = {}
    fd2file = {}
    poller = poll()

    def register_and_append(file_obj, eventmask):
        """Record file_obj for poll operations"""
        poller.register(file_obj.fileno(), eventmask)
        fd2file[file_obj.fileno()] = file_obj
        out = fd2output[file_obj.fileno()] = []
        return out

    def close_unregister_and_remove(fdes):
        """fdes is finished"""
        poller.unregister(fdes)
        fd2file[fdes].close()
        fd2file.pop(fdes)

    stdout_list = register_and_append(process.stdout, POLLIN | POLLPRI)
    stderr_list = register_and_append(process.stderr, POLLIN | POLLPRI)

    def throw_timeout(delay):
        """Throw exception for after delay"""
        try:
            out = run([
                'ps', '--no-headers', '-o', 'pid', '--ppid',
                str(process.pid)
            ])
        except SubprocessError:
            out = ''
        pids = [process.pid] + [int(p) for p in out.split()]
        for pid in pids:
            try:
                kill(pid, SIGKILL)
            except OSError:
                print 'WARNING: unable to kill subprocess', pid
        raise TimeoutError(description, timeout, delay, ''.join(stdout_list),
                           ''.join(stderr_list))

    if not wait:
        return

    start = time()
    with time_limit(timeout,
                    'launch ' + ' '.join(args),
                    timeout_callback=throw_timeout):
        if stdin_push:
            process.stdin.write(stdin_push)
            process.stdin.flush()
            process.stdin.close()
        announce = time() + announce_interval
        while fd2file:
            if time() > announce:
                announce = time() + announce_interval
                print 'NOTE: waiting', time() - start, 'of', timeout, \
                    'seconds for', ' '.join(args)
            try:
                ready = poller.poll(20)
            except error, eparam:
                if eparam.args[0] == EINTR:
                    continue
                raise
            for fdes, mode in ready:
                if not mode & (POLLIN | POLLPRI):
                    close_unregister_and_remove(fdes)
                    continue
                if fdes not in fd2file:
                    print 'operation on unexpected FD', fdes
                    continue
                data = read(fdes, 4096)
                if not data:
                    close_unregister_and_remove(fdes)
                fd2output[fdes].append(data)
                fileobj = fd2file[fdes]
                if fileobj == process.stdout:
                    if echo:
                        for line in data.splitlines():
                            print 'STDOUT:', line
                    if output_callback:
                        output_callback(data)
                if fileobj == process.stderr:
                    if echo:
                        for line in data.splitlines():
                            print 'STDERR:', line
                    if error_callback:
                        error_callback(data)

        process.wait()
        output = ''.join(stdout_list), ''.join(stderr_list)
        exit_code = process.returncode
Esempio n. 20
0
 def inner():
     with time_limit(timeout,
                     'call %s%r on %s %s' % (command, args, host, why)):
         print 'RUN: calling', command, 'arguments', args, 'on', host
         proxy = ServerProxy(EXEC_DAEMON_URL_FORMAT % (host))
         return getattr(proxy, command)(*args)
Esempio n. 21
0
    guest = case.get('guest')
    if guest is not None:
        guest_os_name, _ = name_split(guest)
    else:
        guest_os_name = None
    if guest and case.get('trigger') != 'VM install':
        if not [
                dom for dom in domlist
                if dom['name'] == guest and dom['status'] == 'running'
        ]:
            return False, "requires " + guest + " VM"
    if 'futile' not in case:
        futile = ''
    else:
        try:
            with time_limit(10, 'futility check'):
                futile = case['futile'](dut, guest, guest_os_name, build,
                                        domlist)
        except Exception, exc:
            print_exc()
            futile = 'futility check failed %r' % exc
    if futile:
        return False, futile
    return True, ''


def choose_test(dut, current_install=False, test_case_regexp=None):
    """select a test for dut"""
    mdb = mongodb.get_autotest()
    minimum_n = minimum = None
    current_build = get_build(dut, timeout=20)
Esempio n. 22
0
 def inner():
     with time_limit(timeout, 
                     'call %s%r on %s %s' % (command, args, host, why)):
         print 'RUN: calling', command, 'arguments', args, 'on', host
         proxy = ServerProxy(EXEC_DAEMON_URL_FORMAT % (host))
         return getattr(proxy, command)(*args)
Esempio n. 23
0
def set_power_state(dut, state, args):
    """Set AMT state"""
    try:
        pcontrol = get_power_control_type(dut)
    except NoMongoHost:
        pcontrol = 'AMT'
    print 'INFO: ', pcontrol
    APC_match = match('snmp-apc:(.+?):(.+)', pcontrol)
    if APC_match is not None:
        switcher = APC_match.group(1)
        host = int(APC_match.group(2))
        pdu = get_PDU(switcher)
        outlet = pdu.get_outlet_by_number(host)
        if state == 's5':
            outlet.off()
        else:
            outlet.on()
    elif pcontrol == 'AMT':
        #Wake machine out of M-Off state if idle timer was reached.
        run([AMTTOOL, dut+'-amt'], env=AMTENV, timeout=60, stdin_push='y\n')
        sleep(5)
        run([AMTTOOL, dut+'-amt']+args, env=AMTENV, timeout=60, stdin_push='y\n')
        sleep(5)
        verify_power_state(dut, state)
    elif pcontrol.startswith('franken'):
        run([('/bvt-device/hard_turn_off.pl' if state == 's5' else 
             '/bvt-device/turn_on.pl'), pcontrol[-1]], 
            host='igor.cam.xci-test.com', cwd='/bvt-device')
    elif pcontrol == 'statedb':
        sdb = open_database('statedb')
        config = open_database('configdb')
        asset = open_database('assetdb').select1_field(
            'tag', 'assets', name=dut)
        if 0:
            # sometimes (e.g. fozzie in July 2011)
            asset2 = config.select1_field('asset', 'ips', 
                                          reverse_dns=dut+'.cam.xci-test.com')
            print 'from configdb', asset2
            print 'from assetdb', asset
            assert asset == asset2

        with time_limit(300, description='set power state'):
            while 1:
                current = sdb.select1_field('current_power', 'control', 
                                            asset=asset)
                print 'POWER_CONTROL: statedb reports power state', current, \
                    'for', dut, asset,
                statep = 's1' if state == 's0' else state
                if current == statep:
                    print '... happy'
                    break
                sdb.execute('UPDATE control SET desired_power=%s '
                            'WHERE asset=%s', (statep, asset))
                print '... requesting', statep
                sleep(2)
    elif pcontrol == 'wsman':
        if dut+'-amt' not in run(['amtctrl list'], shell=True):
            run(['amtctrl add '+dut+'-amt '+dut+'-amt ' + AMTENV['AMT_PASSWORD']], shell=True)
        if 'powerdown' in args:
            run(['amtctrl '+dut+'-amt off'], shell=True)
        elif 'pxe' in args:
            run(['amtctrl '+dut+'-amt on'], shell=True)
            run(['amtctrl '+dut+'-amt pxeboot'], shell=True, timeout=30, wait=False)
        elif 'powerup' in args:
            run(['amtctrl '+dut+'-amt on'], shell=True)
        else:
            print "wsman power operation not supported."
            print "wsman power operation not supported."
    elif pcontrol == 'ssh':
        target = get_ssh_target(dut)
        relay = get_ssh_relay(dut)
        if 'powerdown' in args:
            run(['ssh ' + target + ' "echo 0 > /proc/power/relay' + relay + '"'], shell=True)
        elif 'pxe' in args:
            run(['ssh ' + target + ' "echo 0 > /proc/power/relay' + relay + '"'], shell=True)
            run(['ssh ' + target + ' "echo 1 > /proc/power/relay' + relay + '"'], shell=True)
        elif 'powerup' in args:
            run(['ssh ' + target + ' "echo 0 > /proc/power/relay' + relay + '"'], shell=True)
            run(['ssh ' + target + ' "echo 1 > /proc/power/relay' + relay + '"'], shell=True)
    else:
        raise NoPowerControl(dut, pcontrol, state)
Esempio n. 24
0
def run_via_exec_daemon(args,
                        host,
                        timeout=60,
                        ignore_failure=False,
                        split=False,
                        word_split=False,
                        line_split=False,
                        echo=True,
                        wait=True):
    """Run args on host.

    Fail after timeout.

    Unless ignore_failure is True, if the command fails raise SubprocessFailure.

    If split is set, split up to list of words per line.

    Intended to be similar to src.bvtlib.run.run"""
    process = call_exec_daemon('run', [' '.join(args)],
                               host=host,
                               timeout=timeout)
    if echo:
        print 'EXEC_DAEMON: launched', args, 'as', process, 'on', host
    if not wait:
        return
    with time_limit(timeout, 'launch ' + ' '.join(args) + ' on ' + host):
        """Has process finished?"""
        while 1:
            result = call_exec_daemon('poll', [process],
                                      timeout=timeout,
                                      host=host,
                                      why=' to complete ' + ' '.join(args))
            if echo:
                print 'EXEC_DAEMON: poll', process, 'returned', result
            if result == 'DONE':
                break
            sleep(1)
        output = call_exec_daemon('log', [process], timeout=timeout,
                                  host=host), ''
        if echo:
            print 'EXEC_DAEMON: read', len(output), 'bytes of output for', \
                process, 'on', host
            for line in output[0].split('\n'):
                print 'EXEC_DAEMON_OUTPUT:', process, 'on', host, ':', line
        exit_code = call_exec_daemon('returncode', [process],
                                     timeout=timeout,
                                     host=host)
        if echo:
            print 'EXEC_DAEMON: completed', args, 'id', process, \
                'exit code', exit_code
        try:
            call_exec_daemon('cleanup', [process], timeout=timeout, host=host)
        except Fault:
            if echo:
                print 'EXEC_DAEMON: unable to cleanup for', process, 'on', host
        else:
            if echo:
                print 'EXEC_DEAMON: cleaned up', process, 'on', host
    if exit_code != 0 and not ignore_failure:
        raise SubprocessError(exit_code, output[0], output[1])

    if word_split:
        output2 = [x.split() for x in output]
    elif split:
        output2 = [[line.split() for line in x.split('\n')] for x in output]
    elif line_split:
        output2 = [x.split('\n') for x in output]
    else:
        output2 = output
    outv = output2[0]
    return (exit_code, outv) if ignore_failure else outv