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 __init__(self, options): |
| 24 """Processes vm-specific options.""" |
| 25 au_worker.AUWorker.__init__(self, options) |
| 26 self.graphics_flag = '' |
| 27 if options.no_graphics: self.graphics_flag = '--no_graphics' |
| 28 if not self.board: cros_lib.Die('Need board to convert base image to vm.') |
| 29 |
| 30 self._AcquireUniquePortAndPidFile() |
| 31 self._KillExistingVM(self._kvm_pid_file) |
| 32 |
| 33 def _KillExistingVM(self, pid_file): |
| 34 """Kills an existing VM specified by the pid_file.""" |
| 35 if os.path.exists(pid_file): |
| 36 cros_lib.Warning('Existing %s found. Deleting and killing process' % |
| 37 pid_file) |
| 38 cros_lib.RunCommand(['./cros_stop_vm', '--kvm_pid=%s' % pid_file], |
| 39 cwd=self.crosutilsbin) |
| 40 |
| 41 assert not os.path.exists(pid_file) |
| 42 |
| 43 def _AcquireUniquePortAndPidFile(self): |
| 44 """Acquires unique ssh port and pid file for VM.""" |
| 45 with VMAUWorker._vm_lock: |
| 46 self._ssh_port = VMAUWorker._next_port |
| 47 self._kvm_pid_file = '/tmp/kvm.%d' % self._ssh_port |
| 48 VMAUWorker._next_port += 1 |
| 49 |
| 50 def CleanUp(self): |
| 51 """Stop the vm after a test.""" |
| 52 self._KillExistingVM(self._kvm_pid_file) |
| 53 |
| 54 def PrepareBase(self, image_path): |
| 55 """Creates an update-able VM based on base image.""" |
| 56 self.PrepareVMBase(image_path) |
| 57 |
| 58 def UpdateImage(self, image_path, src_image_path='', stateful_change='old', |
| 59 proxy_port='', private_key_path=None): |
| 60 """Updates VM image with image_path.""" |
| 61 stateful_change_flag = self.GetStatefulChangeFlag(stateful_change) |
| 62 if src_image_path and self._first_update: |
| 63 src_image_path = self.vm_image_path |
| 64 self._first_update = False |
| 65 |
| 66 cmd = ['%s/cros_run_vm_update' % self.crosutilsbin, |
| 67 '--vm_image_path=%s' % self.vm_image_path, |
| 68 '--snapshot', |
| 69 self.graphics_flag, |
| 70 '--persist', |
| 71 '--kvm_pid=%s' % self._kvm_pid_file, |
| 72 '--ssh_port=%s' % self._ssh_port, |
| 73 stateful_change_flag, |
| 74 ] |
| 75 self.AppendUpdateFlags(cmd, image_path, src_image_path, proxy_port, |
| 76 private_key_path) |
| 77 self.RunUpdateCmd(cmd) |
| 78 |
| 79 def UpdateUsingPayload(self, update_path, stateful_change='old', |
| 80 proxy_port=None): |
| 81 """Updates a vm image using cros_run_vm_update.""" |
| 82 stateful_change_flag = self.GetStatefulChangeFlag(stateful_change) |
| 83 cmd = ['%s/cros_run_vm_update' % self.crosutilsbin, |
| 84 '--payload=%s' % update_path, |
| 85 '--vm_image_path=%s' % self.vm_image_path, |
| 86 '--snapshot', |
| 87 self.graphics_flag, |
| 88 '--persist', |
| 89 '--kvm_pid=%s' % self._kvm_pid_file, |
| 90 '--ssh_port=%s' % self._ssh_port, |
| 91 stateful_change_flag, |
| 92 ] |
| 93 if proxy_port: cmd.append('--proxy_port=%s' % proxy_port) |
| 94 self.RunUpdateCmd(cmd) |
| 95 |
| 96 def VerifyImage(self, unittest, percent_required_to_pass=100): |
| 97 """Runs vm smoke suite to verify image.""" |
| 98 # image_to_live already verifies lsb-release matching. This is just |
| 99 # for additional steps. |
| 100 commandWithArgs = ['%s/cros_run_vm_test' % self.crosutilsbin, |
| 101 '--image_path=%s' % self.vm_image_path, |
| 102 '--snapshot', |
| 103 '--persist', |
| 104 '--kvm_pid=%s' % self._kvm_pid_file, |
| 105 '--ssh_port=%s' % self._ssh_port, |
| 106 self.verify_suite, |
| 107 ] |
| 108 if self.graphics_flag: commandWithArgs.append(self.graphics_flag) |
| 109 output = cros_lib.RunCommand(commandWithArgs, error_ok=True, |
| 110 enter_chroot=False, redirect_stdout=True) |
| 111 return self.AssertEnoughTestsPassed(unittest, output, |
| 112 percent_required_to_pass) |
| 113 |
OLD | NEW |