OLD | NEW |
1 """ | 1 """ |
2 KVM test utility functions. | 2 KVM test utility functions. |
3 | 3 |
4 @copyright: 2008-2009 Red Hat Inc. | 4 @copyright: 2008-2009 Red Hat Inc. |
5 """ | 5 """ |
6 | 6 |
7 import time, string, random, socket, os, signal, re, logging, commands, cPickle | 7 import time, string, random, socket, os, signal, re, logging, commands, cPickle |
8 import fcntl, shelve, ConfigParser, rss_file_transfer, threading, sys, UserDict | 8 import fcntl, shelve, ConfigParser, threading, sys, UserDict, inspect |
9 import inspect | |
10 from autotest_lib.client.bin import utils, os_dep | 9 from autotest_lib.client.bin import utils, os_dep |
11 from autotest_lib.client.common_lib import error, logging_config | 10 from autotest_lib.client.common_lib import error, logging_config |
12 import kvm_subprocess | 11 import rss_client, aexpect |
13 try: | 12 try: |
14 import koji | 13 import koji |
15 KOJI_INSTALLED = True | 14 KOJI_INSTALLED = True |
16 except ImportError: | 15 except ImportError: |
17 KOJI_INSTALLED = False | 16 KOJI_INSTALLED = False |
18 | 17 |
19 | 18 |
20 def _lock_file(filename): | 19 def _lock_file(filename): |
21 f = open(filename, "w") | 20 f = open(filename, "w") |
22 fcntl.lockf(f, fcntl.LOCK_EX) | 21 fcntl.lockf(f, fcntl.LOCK_EX) |
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 raise LoginError("Client said 'connection closed'", text) | 588 raise LoginError("Client said 'connection closed'", text) |
590 elif match == 4: # "Connection refused" | 589 elif match == 4: # "Connection refused" |
591 raise LoginError("Client said 'connection refused'", text) | 590 raise LoginError("Client said 'connection refused'", text) |
592 elif match == 5: # "Please wait" | 591 elif match == 5: # "Please wait" |
593 logging.debug("Got 'Please wait'") | 592 logging.debug("Got 'Please wait'") |
594 timeout = 30 | 593 timeout = 30 |
595 continue | 594 continue |
596 elif match == 6: # prompt | 595 elif match == 6: # prompt |
597 logging.debug("Got shell prompt -- logged in") | 596 logging.debug("Got shell prompt -- logged in") |
598 break | 597 break |
599 except kvm_subprocess.ExpectTimeoutError, e: | 598 except aexpect.ExpectTimeoutError, e: |
600 raise LoginTimeoutError(e.output) | 599 raise LoginTimeoutError(e.output) |
601 except kvm_subprocess.ExpectProcessTerminatedError, e: | 600 except aexpect.ExpectProcessTerminatedError, e: |
602 raise LoginProcessTerminatedError(e.status, e.output) | 601 raise LoginProcessTerminatedError(e.status, e.output) |
603 | 602 |
604 | 603 |
605 def remote_login(client, host, port, username, password, prompt, linesep="\n", | 604 def remote_login(client, host, port, username, password, prompt, linesep="\n", |
606 log_filename=None, timeout=10): | 605 log_filename=None, timeout=10): |
607 """ | 606 """ |
608 Log into a remote host (guest) using SSH/Telnet/Netcat. | 607 Log into a remote host (guest) using SSH/Telnet/Netcat. |
609 | 608 |
610 @param client: The client to use ('ssh', 'telnet' or 'nc') | 609 @param client: The client to use ('ssh', 'telnet' or 'nc') |
611 @param host: Hostname or IP address | 610 @param host: Hostname or IP address |
(...skipping 16 matching lines...) Expand all Loading... |
628 "-o PreferredAuthentications=password -p %s %s@%s" % | 627 "-o PreferredAuthentications=password -p %s %s@%s" % |
629 (port, username, host)) | 628 (port, username, host)) |
630 elif client == "telnet": | 629 elif client == "telnet": |
631 cmd = "telnet -l %s %s %s" % (username, host, port) | 630 cmd = "telnet -l %s %s %s" % (username, host, port) |
632 elif client == "nc": | 631 elif client == "nc": |
633 cmd = "nc %s %s" % (host, port) | 632 cmd = "nc %s %s" % (host, port) |
634 else: | 633 else: |
635 raise LoginBadClientError(client) | 634 raise LoginBadClientError(client) |
636 | 635 |
637 logging.debug("Trying to login with command '%s'", cmd) | 636 logging.debug("Trying to login with command '%s'", cmd) |
638 session = kvm_subprocess.ShellSession(cmd, linesep=linesep, prompt=prompt) | 637 session = aexpect.ShellSession(cmd, linesep=linesep, prompt=prompt) |
639 try: | 638 try: |
640 _remote_login(session, username, password, prompt, timeout) | 639 _remote_login(session, username, password, prompt, timeout) |
641 except: | 640 except: |
642 session.close() | 641 session.close() |
643 raise | 642 raise |
644 if log_filename: | 643 if log_filename: |
645 session.set_output_func(log_line) | 644 session.set_output_func(log_line) |
646 session.set_output_params((log_filename,)) | 645 session.set_output_params((log_filename,)) |
647 return session | 646 return session |
648 | 647 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 session.sendline(password_list[password_prompt_count]) | 728 session.sendline(password_list[password_prompt_count]) |
730 password_prompt_count += 1 | 729 password_prompt_count += 1 |
731 timeout = transfer_timeout | 730 timeout = transfer_timeout |
732 authentication_done = True | 731 authentication_done = True |
733 continue | 732 continue |
734 else: | 733 else: |
735 raise SCPAuthenticationError("Got password prompt twice", | 734 raise SCPAuthenticationError("Got password prompt twice", |
736 text) | 735 text) |
737 elif match == 2: # "lost connection" | 736 elif match == 2: # "lost connection" |
738 raise SCPError("SCP client said 'lost connection'", text) | 737 raise SCPError("SCP client said 'lost connection'", text) |
739 except kvm_subprocess.ExpectTimeoutError, e: | 738 except aexpect.ExpectTimeoutError, e: |
740 if authentication_done: | 739 if authentication_done: |
741 raise SCPTransferTimeoutError(e.output) | 740 raise SCPTransferTimeoutError(e.output) |
742 else: | 741 else: |
743 raise SCPAuthenticationTimeoutError(e.output) | 742 raise SCPAuthenticationTimeoutError(e.output) |
744 except kvm_subprocess.ExpectProcessTerminatedError, e: | 743 except aexpect.ExpectProcessTerminatedError, e: |
745 if e.status == 0: | 744 if e.status == 0: |
746 logging.debug("SCP process terminated with status 0") | 745 logging.debug("SCP process terminated with status 0") |
747 break | 746 break |
748 else: | 747 else: |
749 raise SCPTransferFailedError(e.status, e.output) | 748 raise SCPTransferFailedError(e.status, e.output) |
750 | 749 |
751 | 750 |
752 def remote_scp(command, password_list, log_filename=None, transfer_timeout=600, | 751 def remote_scp(command, password_list, log_filename=None, transfer_timeout=600, |
753 login_timeout=10): | 752 login_timeout=10): |
754 """ | 753 """ |
(...skipping 13 matching lines...) Expand all Loading... |
768 @raise: Whatever _remote_scp() raises | 767 @raise: Whatever _remote_scp() raises |
769 """ | 768 """ |
770 logging.debug("Trying to SCP with command '%s', timeout %ss", | 769 logging.debug("Trying to SCP with command '%s', timeout %ss", |
771 command, transfer_timeout) | 770 command, transfer_timeout) |
772 if log_filename: | 771 if log_filename: |
773 output_func = log_line | 772 output_func = log_line |
774 output_params = (log_filename,) | 773 output_params = (log_filename,) |
775 else: | 774 else: |
776 output_func = None | 775 output_func = None |
777 output_params = () | 776 output_params = () |
778 session = kvm_subprocess.Expect(command, | 777 session = aexpect.Expect(command, |
779 output_func=output_func, | 778 output_func=output_func, |
780 output_params=output_params) | 779 output_params=output_params) |
781 try: | 780 try: |
782 _remote_scp(session, password_list, transfer_timeout, login_timeout) | 781 _remote_scp(session, password_list, transfer_timeout, login_timeout) |
783 finally: | 782 finally: |
784 session.close() | 783 session.close() |
785 | 784 |
786 | 785 |
787 def scp_to_remote(host, port, username, password, local_path, remote_path, | 786 def scp_to_remote(host, port, username, password, local_path, remote_path, |
788 log_filename=None, timeout=600): | 787 log_filename=None, timeout=600): |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
873 complete. | 872 complete. |
874 @raise: Whatever remote_scp() raises | 873 @raise: Whatever remote_scp() raises |
875 """ | 874 """ |
876 if client == "scp": | 875 if client == "scp": |
877 scp_to_remote(address, port, username, password, local_path, | 876 scp_to_remote(address, port, username, password, local_path, |
878 remote_path, log_filename, timeout) | 877 remote_path, log_filename, timeout) |
879 elif client == "rss": | 878 elif client == "rss": |
880 log_func = None | 879 log_func = None |
881 if verbose: | 880 if verbose: |
882 log_func = logging.debug | 881 log_func = logging.debug |
883 c = rss_file_transfer.FileUploadClient(address, port, log_func) | 882 c = rss_client.FileUploadClient(address, port, log_func) |
884 c.upload(local_path, remote_path, timeout) | 883 c.upload(local_path, remote_path, timeout) |
885 c.close() | 884 c.close() |
886 | 885 |
887 | 886 |
888 def copy_files_from(address, client, username, password, port, remote_path, | 887 def copy_files_from(address, client, username, password, port, remote_path, |
889 local_path, log_filename=None, verbose=False, timeout=600): | 888 local_path, log_filename=None, verbose=False, timeout=600): |
890 """ | 889 """ |
891 Copy files from a remote host (guest) using the selected client. | 890 Copy files from a remote host (guest) using the selected client. |
892 | 891 |
893 @param client: Type of transfer client | 892 @param client: Type of transfer client |
894 @param username: Username (if required) | 893 @param username: Username (if required) |
895 @param password: Password (if requried) | 894 @param password: Password (if requried) |
896 @param remote_path: Path on the remote machine where we are copying from | 895 @param remote_path: Path on the remote machine where we are copying from |
897 @param local_path: Path on the local machine where we are copying to | 896 @param local_path: Path on the local machine where we are copying to |
898 @param address: Address of remote host(guest) | 897 @param address: Address of remote host(guest) |
899 @param log_filename: If specified, log all output to this file (SCP only) | 898 @param log_filename: If specified, log all output to this file (SCP only) |
900 @param verbose: If True, log some stats using logging.debug (RSS only) | 899 @param verbose: If True, log some stats using logging.debug (RSS only) |
901 @param timeout: The time duration (in seconds) to wait for the transfer to | 900 @param timeout: The time duration (in seconds) to wait for the transfer to |
902 complete. | 901 complete. |
903 @raise: Whatever remote_scp() raises | 902 @raise: Whatever remote_scp() raises |
904 """ | 903 """ |
905 if client == "scp": | 904 if client == "scp": |
906 scp_from_remote(address, port, username, password, remote_path, | 905 scp_from_remote(address, port, username, password, remote_path, |
907 local_path, log_filename, timeout) | 906 local_path, log_filename, timeout) |
908 elif client == "rss": | 907 elif client == "rss": |
909 log_func = None | 908 log_func = None |
910 if verbose: | 909 if verbose: |
911 log_func = logging.debug | 910 log_func = logging.debug |
912 c = rss_file_transfer.FileDownloadClient(address, port, log_func) | 911 c = rss_client.FileDownloadClient(address, port, log_func) |
913 c.download(remote_path, local_path, timeout) | 912 c.download(remote_path, local_path, timeout) |
914 c.close() | 913 c.close() |
915 | 914 |
916 | 915 |
917 # The following are utility functions related to ports. | 916 # The following are utility functions related to ports. |
918 | 917 |
919 def is_port_free(port, address): | 918 def is_port_free(port, address): |
920 """ | 919 """ |
921 Return True if the given port is available for use. | 920 Return True if the given port is available for use. |
922 | 921 |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1316 for target in targets: | 1315 for target in targets: |
1317 if isinstance(target, tuple) or isinstance(target, list): | 1316 if isinstance(target, tuple) or isinstance(target, list): |
1318 t = Thread(*target) | 1317 t = Thread(*target) |
1319 else: | 1318 else: |
1320 t = Thread(target) | 1319 t = Thread(target) |
1321 threads.append(t) | 1320 threads.append(t) |
1322 t.start() | 1321 t.start() |
1323 return [t.join() for t in threads] | 1322 return [t.join() for t in threads] |
1324 | 1323 |
1325 | 1324 |
1326 class KvmLoggingConfig(logging_config.LoggingConfig): | 1325 class VirtLoggingConfig(logging_config.LoggingConfig): |
1327 """ | 1326 """ |
1328 Used with the sole purpose of providing convenient logging setup | 1327 Used with the sole purpose of providing convenient logging setup |
1329 for the KVM test auxiliary programs. | 1328 for the KVM test auxiliary programs. |
1330 """ | 1329 """ |
1331 def configure_logging(self, results_dir=None, verbose=False): | 1330 def configure_logging(self, results_dir=None, verbose=False): |
1332 super(KvmLoggingConfig, self).configure_logging(use_console=True, | 1331 super(VirtLoggingConfig, self).configure_logging(use_console=True, |
1333 verbose=verbose) | 1332 verbose=verbose) |
1334 | 1333 |
1335 | 1334 |
1336 class PciAssignable(object): | 1335 class PciAssignable(object): |
1337 """ | 1336 """ |
1338 Request PCI assignable devices on host. It will check whether to request | 1337 Request PCI assignable devices on host. It will check whether to request |
1339 PF (physical Functions) or VF (Virtual Functions). | 1338 PF (physical Functions) or VF (Virtual Functions). |
1340 """ | 1339 """ |
1341 def __init__(self, type="vf", driver=None, driver_option=None, | 1340 def __init__(self, type="vf", driver=None, driver_option=None, |
1342 names=None, devices_requested=None): | 1341 names=None, devices_requested=None): |
1343 """ | 1342 """ |
(...skipping 964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2308 if patch_list: | 2307 if patch_list: |
2309 host_kernel.patch(patch_list) | 2308 host_kernel.patch(patch_list) |
2310 host_kernel.config(kernel_config) | 2309 host_kernel.config(kernel_config) |
2311 host_kernel.build() | 2310 host_kernel.build() |
2312 host_kernel.install() | 2311 host_kernel.install() |
2313 host_kernel.boot() | 2312 host_kernel.boot() |
2314 | 2313 |
2315 else: | 2314 else: |
2316 logging.info('Chose %s, using the current kernel for the host', | 2315 logging.info('Chose %s, using the current kernel for the host', |
2317 install_type) | 2316 install_type) |
OLD | NEW |