Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Module containing implementation of an au_worker for virtual machines.""" | |
| 6 | |
| 7 import os | |
| 8 import threading | |
| 9 import unittest | |
| 10 | |
| 11 import cros_build_lib as cros_lib | |
| 12 | |
| 13 import au_worker | |
| 14 | |
| 15 | |
| 16 class VMAUWorker(au_worker.AUWorker): | |
| 17 """Test harness for updating virtual machines.""" | |
| 18 | |
| 19 # Class variables used to acquire individual VM variables per test. | |
| 20 _vm_lock = threading.Lock() | |
| 21 _next_port = 9222 | |
| 22 | |
| 23 def _KillExistingVM(self, pid_file): | |
| 24 """Kills an existing VM specified by the pid_file.""" | |
| 25 if os.path.exists(pid_file): | |
| 26 cros_lib.Warning('Existing %s found. Deleting and killing process' % | |
| 27 pid_file) | |
| 28 cros_lib.RunCommand(['./cros_stop_vm', '--kvm_pid=%s' % pid_file], | |
| 29 cwd=self.crosutilsbin) | |
| 30 | |
| 31 assert not os.path.exists(pid_file) | |
| 32 | |
| 33 def _AcquireUniquePortAndPidFile(self): | |
| 34 """Acquires unique ssh port and pid file for VM.""" | |
| 35 with VMAUWorker._vm_lock: | |
| 36 self._ssh_port = VMAUWorker._next_port | |
| 37 self._kvm_pid_file = '/tmp/kvm.%d' % self._ssh_port | |
| 38 VMAUWorker._next_port += 1 | |
| 39 | |
| 40 def __init__(self, options): | |
|
dgarrett
2011/03/03 02:17:21
I thought init was supposed to be the first method
sosa
2011/03/03 03:11:41
Wasn't sure. I put the helper functions it used a
| |
| 41 """Processes vm-specific options.""" | |
| 42 au_worker.AUWorker.__init__(self, options) | |
| 43 self.graphics_flag = '' | |
| 44 if options.no_graphics: self.graphics_flag = '--no_graphics' | |
| 45 if not self.board: cros_lib.Die('Need board to convert base image to vm.') | |
| 46 | |
| 47 self._AcquireUniquePortAndPidFile() | |
| 48 self._KillExistingVM(self._kvm_pid_file) | |
| 49 | |
| 50 def PrepareBase(self, image_path): | |
| 51 """Creates an update-able VM based on base image.""" | |
| 52 self.PrepareVMBase(image_path) | |
| 53 | |
| 54 def UpdateImage(self, image_path, src_image_path='', stateful_change='old', | |
| 55 proxy_port='', private_key_path=None): | |
| 56 """Updates VM image with image_path.""" | |
| 57 stateful_change_flag = self.GetStatefulChangeFlag(stateful_change) | |
| 58 if src_image_path and self._first_update: | |
| 59 src_image_path = self.vm_image_path | |
| 60 self._first_update = False | |
| 61 | |
| 62 cmd = ['%s/cros_run_vm_update' % self.crosutilsbin, | |
| 63 '--vm_image_path=%s' % self.vm_image_path, | |
| 64 '--snapshot', | |
| 65 self.graphics_flag, | |
| 66 '--persist', | |
| 67 '--kvm_pid=%s' % self._kvm_pid_file, | |
| 68 '--ssh_port=%s' % self._ssh_port, | |
| 69 stateful_change_flag, | |
| 70 ] | |
| 71 self.AppendUpdateFlags(cmd, image_path, src_image_path, proxy_port, | |
| 72 private_key_path) | |
| 73 self.RunUpdateCmd(cmd) | |
| 74 | |
| 75 def UpdateUsingPayload(self, update_path, stateful_change='old', | |
| 76 proxy_port=None): | |
| 77 """Updates a vm image using cros_run_vm_update.""" | |
| 78 stateful_change_flag = self.GetStatefulChangeFlag(stateful_change) | |
| 79 cmd = ['%s/cros_run_vm_update' % self.crosutilsbin, | |
| 80 '--payload=%s' % update_path, | |
| 81 '--vm_image_path=%s' % self.vm_image_path, | |
| 82 '--snapshot', | |
| 83 self.graphics_flag, | |
| 84 '--persist', | |
| 85 '--kvm_pid=%s' % self._kvm_pid_file, | |
| 86 '--ssh_port=%s' % self._ssh_port, | |
| 87 stateful_change_flag, | |
| 88 ] | |
| 89 if proxy_port: cmd.append('--proxy_port=%s' % proxy_port) | |
| 90 self.RunUpdateCmd(cmd) | |
| 91 | |
| 92 def VerifyImage(self, unittest, percent_required_to_pass=100): | |
| 93 """Runs vm smoke suite to verify image.""" | |
| 94 # image_to_live already verifies lsb-release matching. This is just | |
| 95 # for additional steps. | |
| 96 commandWithArgs = ['%s/cros_run_vm_test' % self.crosutilsbin, | |
| 97 '--image_path=%s' % self.vm_image_path, | |
| 98 '--snapshot', | |
| 99 '--persist', | |
| 100 '--kvm_pid=%s' % self._kvm_pid_file, | |
| 101 '--ssh_port=%s' % self._ssh_port, | |
| 102 self.verify_suite, | |
| 103 ] | |
| 104 if self.graphics_flag: commandWithArgs.append(self.graphics_flag) | |
| 105 output = cros_lib.RunCommand(commandWithArgs, error_ok=True, | |
| 106 enter_chroot=False, redirect_stdout=True) | |
| 107 return self.AssertEnoughTestsPassed(unittest, output, | |
| 108 percent_required_to_pass) | |
| 109 | |
| OLD | NEW |