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, test_results_root): | |
24 """Processes vm-specific options.""" | |
25 au_worker.AUWorker.__init__(self, options, test_results_root) | |
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 log_directory = self.GetNextResultsPath('update') | |
62 stateful_change_flag = self.GetStatefulChangeFlag(stateful_change) | |
63 if src_image_path and self._first_update: | |
64 src_image_path = self.vm_image_path | |
65 self._first_update = False | |
66 | |
67 cmd = ['%s/cros_run_vm_update' % self.crosutilsbin, | |
68 '--vm_image_path=%s' % self.vm_image_path, | |
69 '--update_log=%s' % os.path.join(log_directory, 'update_engine.log'), | |
70 '--snapshot', | |
71 self.graphics_flag, | |
72 '--persist', | |
73 '--kvm_pid=%s' % self._kvm_pid_file, | |
74 '--ssh_port=%s' % self._ssh_port, | |
75 stateful_change_flag, | |
76 ] | |
77 self.AppendUpdateFlags(cmd, image_path, src_image_path, proxy_port, | |
78 private_key_path) | |
79 self.RunUpdateCmd(cmd, log_directory) | |
80 | |
81 def UpdateUsingPayload(self, update_path, stateful_change='old', | |
82 proxy_port=None): | |
83 """Updates a vm image using cros_run_vm_update.""" | |
84 log_directory = self.GetNextResultsPath('update') | |
85 stateful_change_flag = self.GetStatefulChangeFlag(stateful_change) | |
86 cmd = ['%s/cros_run_vm_update' % self.crosutilsbin, | |
87 '--payload=%s' % update_path, | |
88 '--vm_image_path=%s' % self.vm_image_path, | |
89 '--update_log=%s' % os.path.join(log_directory, 'update_engine.log'), | |
90 '--snapshot', | |
91 self.graphics_flag, | |
92 '--persist', | |
93 '--kvm_pid=%s' % self._kvm_pid_file, | |
94 '--ssh_port=%s' % self._ssh_port, | |
95 stateful_change_flag, | |
96 ] | |
97 if proxy_port: cmd.append('--proxy_port=%s' % proxy_port) | |
98 self.RunUpdateCmd(cmd, log_directory) | |
99 | |
100 def VerifyImage(self, unittest, percent_required_to_pass=100): | |
101 """Runs vm smoke suite to verify image.""" | |
102 log_directory = self.GetNextResultsPath('verify') | |
103 (_, _, log_directory_in_chroot) = log_directory.rpartition('chroot') | |
104 # image_to_live already verifies lsb-release matching. This is just | |
105 # for additional steps. | |
106 commandWithArgs = ['%s/cros_run_vm_test' % self.crosutilsbin, | |
107 '--image_path=%s' % self.vm_image_path, | |
108 '--snapshot', | |
109 '--persist', | |
110 '--kvm_pid=%s' % self._kvm_pid_file, | |
111 '--ssh_port=%s' % self._ssh_port, | |
112 '--results_dir_root=%s' % log_directory_in_chroot, | |
113 self.verify_suite, | |
114 ] | |
115 if self.graphics_flag: commandWithArgs.append(self.graphics_flag) | |
116 output = cros_lib.RunCommand(commandWithArgs, error_ok=True, | |
117 enter_chroot=False, redirect_stdout=True) | |
118 return self.AssertEnoughTestsPassed(unittest, output, | |
119 percent_required_to_pass) | |
120 | |
OLD | NEW |