• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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
5import contextlib
6import dbus
7import logging
8import os
9import subprocess
10
11from autotest_lib.client.bin import test, utils
12from autotest_lib.client.common_lib import autotemp, error
13from autotest_lib.client.common_lib.cros import dbus_send
14
15
16class vm_CrosVmStart(test.test):
17    """Tests crosvm."""
18
19    version = 1
20    BUS_NAME = 'org.chromium.ComponentUpdaterService'
21    BUS_PATH = '/org/chromium/ComponentUpdaterService'
22    BUS_INTERFACE = 'org.chromium.ComponentUpdaterService'
23    LOAD_COMPONENT = 'LoadComponent'
24    TERMINA_COMPONENT_NAME = 'cros-termina'
25    USER = 'chronos'
26
27
28    def _load_component(self, name):
29        args = [dbus.String(name)]
30        return dbus_send.dbus_send(
31            self.BUS_NAME,
32            self.BUS_INTERFACE,
33            self.BUS_PATH,
34            self.LOAD_COMPONENT,
35            timeout_seconds=60,
36            user=self.USER,
37            args=args).response
38
39
40    def run_once(self):
41        """
42        Runs a basic test to see if crosvm starts.
43        """
44        mnt_path = self._load_component(self.TERMINA_COMPONENT_NAME)
45        if not mnt_path:
46            raise error.TestError('Component Updater LoadComponent failed')
47        kernel_path = mnt_path + '/vm_kernel'
48        rootfs_path = mnt_path + '/vm_rootfs.img'
49        crosvm_socket_path = '/tmp/vm_CrosVmStart.sock'
50
51        # Running /bin/ls as init causes the VM to exit immediately, crosvm
52        # will have a successful exit code.
53        cmd = ['/usr/bin/crosvm', 'run', '-c', '1', '-m', '1024',
54                '--disk', rootfs_path,
55                '--socket',  crosvm_socket_path,
56                '-p', 'init=/bin/bash root=/dev/vda ro',
57                kernel_path]
58        proc = subprocess.Popen(cmd)
59        if proc.pid <= 0:
60            raise error.TestFail('Failed: crosvm did not start.')
61
62        # Tell the VM to stop.
63        stop_cmd = ['/usr/bin/crosvm', 'stop', '--socket', crosvm_socket_path]
64        stop_proc = subprocess.Popen(cmd)
65        if stop_proc.pid <= 0:
66            raise error.TestFail('Failed: crosvm stop command failed.')
67        stop_proc.wait();
68        if stop_proc.returncode == None:
69            raise error.TestFail('Failed: crosvm stop did not exit.')
70        if stop_proc.returncode != 0:
71            raise error.TestFail('Failed: crosvm stop returned an error %d.' %
72                                 stop_proc.returncode)
73
74        # Wait for the VM to exit.
75        proc.wait()
76        if proc.returncode == None:
77            raise error.TestFail('Failed: crosvm did not exit.')
78        if proc.returncode != 0:
79            raise error.TestFail('Failed: crosvm returned an error %d.' %
80                                 proc.returncode)
81