• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5import dbus
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros.cros_disks import CrosDisksTester
10
11class CrosDisksAPITester(CrosDisksTester):
12    def __init__(self, test):
13        super(CrosDisksAPITester, self).__init__(test)
14
15    def get_tests(self):
16        return [
17            self.test_is_alive,
18            self.test_enumerate_devices,
19            self.test_enumerate_auto_mountable_devices,
20            self.test_get_device_properties,
21            self.test_get_device_properties_of_nonexistent_device,
22            self.test_enumerate_auto_mountable_devices_are_not_on_boot_device,
23            self.test_enumerate_auto_mountable_devices_are_not_virtual,
24            self.test_mount_nonexistent_device,
25            self.test_unmount_nonexistent_device,
26        ]
27
28    def validate_disk_properties(self, disk):
29        # Disk properties provided by the API
30        disk_properties = (
31            ('DeviceFile', dbus.String),
32            ('DeviceIsDrive', dbus.Boolean),
33            ('DeviceIsMediaAvailable', dbus.Boolean),
34            ('DeviceIsOnBootDevice', dbus.Boolean),
35            ('DeviceIsVirtual', dbus.Boolean),
36            ('DeviceIsMounted', dbus.Boolean),
37            ('DeviceIsReadOnly', dbus.Boolean),
38            ('DeviceMediaType', dbus.UInt32),
39            ('DeviceMountPaths', dbus.Array),
40            ('DevicePresentationHide', dbus.Boolean),
41            ('DeviceSize', dbus.UInt64),
42            ('DriveIsRotational', dbus.Boolean),
43            ('DriveModel', dbus.String),
44            ('IdLabel', dbus.String),
45            ('NativePath', dbus.String),
46        )
47
48        for (prop_name, prop_value_type) in disk_properties:
49            # Check if all disk properties are set.
50            if prop_name not in disk:
51                raise error.TestFail("disk.%s not found" % prop_name)
52
53            # Check if each disk property has the right data type.
54            prop_value = disk[prop_name]
55            if not isinstance(prop_value, prop_value_type):
56                raise error.TestFail(
57                        "disk.%s is %s, but %s expected"
58                        % (prop_name, type(prop_value), prop_value_type))
59
60        # Check if DeviceFile has a proper value.
61        if not disk['DeviceFile']:
62            raise error.TestFail(
63                    "disk.DeviceFile should not be empty")
64
65        # Check if the values of DeviceIsMounted and DeviceMountPaths
66        # are consistent.
67        mount_paths = disk['DeviceMountPaths']
68        if disk['DeviceIsMounted']:
69            if len(mount_paths) == 0:
70                raise error.TestFail(
71                        "disk.DeviceMountPaths should not be empty "
72                        "if disk.DeviceIsMounted is true")
73        else:
74            if len(mount_paths) != 0:
75                raise error.TestFail(
76                        "disk.DeviceMountPaths should be empty "
77                        "if disk.DeviceIsMounted is false")
78
79        if mount_paths.signature != dbus.Signature('s'):
80            raise error.TestFail(
81                    "disk.DeviceMountPaths should contain only strings")
82
83        for mount_path in mount_paths:
84            if not mount_path:
85                raise error.TestFail(
86                        "disk.DeviceMountPaths should not contain any "
87                        "empty string")
88
89    def test_is_alive(self):
90        # Check if CrosDisks server is alive.
91        is_alive = self.cros_disks.is_alive()
92        if not is_alive:
93            raise error.TestFail("Unable to talk to the disk daemon")
94
95    def test_enumerate_devices(self):
96        # Check if EnumerateDevices method returns a list of devices.
97        devices = self.cros_disks.enumerate_devices()
98        for device in devices:
99            if not device or not isinstance(device, dbus.String):
100                raise error.TestFail(
101                        "device returned by EnumerateDevices "
102                        "should be a non-empty string")
103
104    def test_enumerate_auto_mountable_devices(self):
105        # Check if EnumerateAutoMountableDevices method returns a list
106        # of devices.
107        devices = self.cros_disks.enumerate_auto_mountable_devices()
108        for device in devices:
109            if not device or not isinstance(device, dbus.String):
110                raise error.TestFail(
111                        "device returned by EnumerateAutoMountableDevices "
112                        "should be a non-empty string")
113
114    def test_enumerate_auto_mountable_devices_are_not_on_boot_device(self):
115        # Make sure EnumerateAutoMountableDevices method does not return
116        # any device that is on the boot device.
117        devices = self.cros_disks.enumerate_auto_mountable_devices()
118        for device in devices:
119            properties = self.cros_disks.get_device_properties(device)
120            if properties['DeviceIsOnBootDevice']:
121                raise error.TestFail(
122                        "device returned by EnumerateAutoMountableDevices "
123                        "should not be on boot device")
124
125    def test_enumerate_auto_mountable_devices_are_not_virtual(self):
126        # Make sure EnumerateAutoMountableDevices method does not return
127        # any device that is virtual.
128        devices = self.cros_disks.enumerate_auto_mountable_devices()
129        for device in devices:
130            properties = self.cros_disks.get_device_properties(device)
131            if properties['DeviceIsVirtual']:
132                raise error.TestFail(
133                        "device returned by EnumerateAutoMountableDevices "
134                        "should not be virtual")
135
136    def test_get_device_properties(self):
137        # Check if GetDeviceProperties method returns valid properties.
138        devices = self.cros_disks.enumerate_devices()
139        for device in devices:
140            properties = self.cros_disks.get_device_properties(device)
141            self.validate_disk_properties(properties)
142
143    def test_get_device_properties_of_nonexistent_device(self):
144        try:
145            properties = self.cros_disks.get_device_properties('/nonexistent')
146        except dbus.DBusException:
147            return
148        raise error.TestFail(
149            "GetDeviceProperties of a nonexistent device should fail")
150
151    def test_mount_nonexistent_device(self):
152        self.cros_disks.mount('/dev/nonexistent', '', [])
153        self.cros_disks.expect_mount_completion({
154            'source_path': '/dev/nonexistent',
155            'mount_path':  '',
156        })
157
158    def test_unmount_nonexistent_device(self):
159        try:
160            self.cros_disks.unmount('/dev/nonexistent', [])
161        except dbus.DBusException:
162            return
163        raise error.TestFail("Unmounting a nonexistent device should fail")
164
165
166class platform_CrosDisksDBus(test.test):
167    version = 1
168
169    def run_once(self, *args, **kwargs):
170        tester = CrosDisksAPITester(self)
171        tester.run(*args, **kwargs)
172