def __init__(self, image_path, port=None, tempdir=None, debug_level=logging.DEBUG): """Initializes VMWrapper with a VM image path. Args: image_path: Path to the VM image. port: SSH port of the VM. tempdir: Temporary working directory. debug_level: Debug level for logging. """ self.image_path = image_path self.tempdir = tempdir self._tempdir_obj = None if not self.tempdir: self._tempdir_obj = osutils.TempDir(prefix='vm_wrapper', sudo_rm=True) self.tempdir = self._tempdir_obj.tempdir self.kvm_pid_path = os.path.join(self.tempdir, 'kvm.pid') self.port = (remote_access.GetUnusedPort() if port is None else remote_access.NormalizePort(port)) self.debug_level = debug_level self.ssh_settings = remote_access.CompileSSHConnectSettings( ConnectTimeout=self.SSH_CONNECT_TIMEOUT) self.agent = remote_access.RemoteAccess( remote_access.LOCALHOST, self.tempdir, self.port, debug_level=self.debug_level, interactive=False) self.device_addr = 'ssh://%s:%d' % (remote_access.LOCALHOST, self.port)
def __init__(self, board, gdb_args, inf_cmd, inf_args, remote, pid, remote_process_name, cgdb_flag, ping, binary): self.board = board self.sysroot = None self.prompt = '(gdb) ' self.inf_cmd = inf_cmd self.run_as_root = False self.gdb_args = gdb_args self.inf_args = inf_args self.remote = remote.hostname if remote else None self.pid = pid self.remote_process_name = remote_process_name # Port used for sending ssh commands to DUT. self.remote_port = remote.port if remote else None # Port for communicating between gdb & gdbserver. self.gdbserver_port = remote_access.GetUnusedPort() self.ssh_settings = remote_access.CompileSSHConnectSettings( **self._EXTRA_SSH_SETTINGS) self.cgdb = cgdb_flag self.framework = 'auto' self.qemu = None self.device = None self.cross_gdb = None self.ping = ping self.binary = binary self.in_chroot = None self.chrome_path = None self.sdk_path = None
def SetUp(self): """Creates and starts the VM instance for testing.""" self.port = remote_access.GetUnusedPort() self.device_addr = 'ssh://%s:%d' % (remote_access.LOCALHOST, self.port) vm_path = vm.CreateVMImage(image=self.image_path, board=self.board, updatable=True) vm_cmd = [ './cros_vm', '--ssh-port=%d' % self.port, '--copy-on-write', '--image-path=%s' % vm_path, '--start' ] cros_build_lib.run(vm_cmd, cwd=constants.CHROMITE_BIN_DIR)
def SpawnServer(self, RequestHandler): """Spawn a new http server""" while True: try: port = remote_access.GetUnusedPort() address = ('', port) self.httpd = SymbolServer(address, RequestHandler) break except socket.error as e: if e.errno == errno.EADDRINUSE: continue raise self.server_url = 'http://localhost:%i/post/path' % port self.httpd_pid = os.fork() if self.httpd_pid == 0: self.httpd.serve_forever(poll_interval=0.1) sys.exit(0)
def testWaitForSSHPort(self): """Verify VM correctly waits on the SSH port if it is busy.""" # Assigning an unused port to the VM SSH Port. self._vm.ssh_port = remote_access.GetUnusedPort() sock = socket.socket() sock.bind((remote_access.LOCALHOST_IP, self._vm.ssh_port)) # Look for retry messages in output. with cros_test_lib.LoggingCapturer(log_level=logging.INFO) as logger: self.assertRaises(vm.VMError, self._vm._WaitForSSHPort, sleep=0) in_use_message = 'SSH port %d in use' % self._vm.ssh_port self.assertTrue(logger.LogsMatch((in_use_message + '...\n') * 11 + in_use_message + '\n')) # Verify the VM works correctly when the port is not in use. # There should be no retries after the port is released. # Another process could grab the unused port between closing it # and calling _WaitForSSHPort but this is extremely unlikely. sock.close() with cros_test_lib.LoggingCapturer(log_level=logging.INFO) as logger: self._vm._WaitForSSHPort() self.assertEqual(logger.messages, '')