Best Python code snippet using lisa_python
xmlparse.py
Source:xmlparse.py
...131 guest.cpu.add_feature("addfeature")132 check = self._make_checker(guest.numatune)133 check("memory_mode", "interleave", "strict", None)134 check("memory_nodeset", "1-5,^3,7", "2,4,6")135 check = self._make_checker(guest.get_devices("memballoon")[0])136 check("model", "virtio", "none")137 self._alter_compare(guest.get_xml_config(), outfile)138 def testAlterMinimalGuest(self):139 infile = "tests/xmlparse-xml/change-minimal-guest-in.xml"140 outfile = "tests/xmlparse-xml/change-minimal-guest-out.xml"141 guest = virtinst.Guest(conn=conn,142 parsexml=file(infile).read())143 check = self._make_checker(guest.features)144 check("acpi", False, True)145 check("pae", False)146 self.assertTrue(147 guest.features.get_xml_config().startswith("<features"))148 check = self._make_checker(guest.clock)149 check("offset", None, "utc")150 self.assertTrue(guest.clock.get_xml_config().startswith("<clock"))151 check = self._make_checker(guest.seclabel)152 check("model", None, "default")153 check("type", None, "static")154 check("label", None, "frob")155 self.assertTrue(156 guest.seclabel.get_xml_config().startswith("<seclabel"))157 check = self._make_checker(guest.cpu)158 check("model", None, "foobar")159 check("cores", None, 4)160 guest.cpu.add_feature("x2apic", "forbid")161 guest.cpu.set_topology_defaults(guest.vcpus)162 self.assertTrue(guest.cpu.get_xml_config().startswith("<cpu"))163 self.assertTrue(guest.installer.get_xml_config().startswith("<os"))164 self._alter_compare(guest.get_xml_config(), outfile)165 def testAlterBootMulti(self):166 infile = "tests/xmlparse-xml/change-boot-multi-in.xml"167 outfile = "tests/xmlparse-xml/change-boot-multi-out.xml"168 guest = virtinst.Guest(conn=conn,169 parsexml=file(infile).read())170 check = self._make_checker(guest.installer.bootconfig)171 check("bootorder", ['hd', 'fd', 'cdrom', 'network'], ["cdrom"])172 check("enable_bootmenu", False, True)173 check("kernel", None, "foo.img")174 check("initrd", None, "bar.img")175 check("kernel_args", None, "ks=foo.ks")176 self._alter_compare(guest.get_xml_config(), outfile)177 def testAlterBootKernel(self):178 infile = "tests/xmlparse-xml/change-boot-kernel-in.xml"179 outfile = "tests/xmlparse-xml/change-boot-kernel-out.xml"180 guest = virtinst.Guest(conn=conn,181 parsexml=file(infile).read())182 check = self._make_checker(guest.installer.bootconfig)183 check("bootorder", [], ["network", "hd", "fd"])184 check("enable_bootmenu", None)185 check("kernel", "/boot/vmlinuz", None)186 check("initrd", "/boot/initrd", None)187 check("kernel_args", "location", None)188 self._alter_compare(guest.get_xml_config(), outfile)189 def testAlterCpuMode(self):190 infile = "tests/xmlparse-xml/change-cpumode-in.xml"191 outfile = "tests/xmlparse-xml/change-cpumode-out.xml"192 guest = virtinst.Guest(conn=conn,193 parsexml=file(infile).read())194 check = self._make_checker(guest.cpu)195 check("mode", "host-passthrough", "custom")196 check("mode", "custom", "host-model")197 # mode will be "custom"198 check("model", None, "qemu64")199 self._alter_compare(guest.get_xml_config(), outfile)200 def testAlterDisk(self):201 """202 Test changing VirtualDisk() parameters after parsing203 """204 infile = "tests/xmlparse-xml/change-disk-in.xml"205 outfile = "tests/xmlparse-xml/change-disk-out.xml"206 guest = virtinst.Guest(conn=conn,207 parsexml=file(infile).read())208 # Set size up front. VirtualDisk validation is kind of209 # convoluted. If trying to change a non-existing one and size wasn't210 # already specified, we will error out.211 disks = guest.get_devices("disk")212 disk1 = disks[0]213 disk1.size = 1214 disk2 = disks[2]215 disk2.size = 1216 disk3 = disks[5]217 disk3.size = 1218 check = self._make_checker(disk1)219 check("path", "/tmp/test.img", "/dev/loop0")220 check("driver_name", None, "test")221 check("driver_type", None, "raw")222 check("serial", "WD-WMAP9A966149", "frob")223 check = self._make_checker(disk2)224 check("path", "/dev/loop0", None)225 check("device", "cdrom", "floppy")226 check("read_only", True, False)227 check("target", None, "fde")228 check("bus", None, "fdc")229 check("error_policy", "stop", None)230 check = self._make_checker(disk3)231 check("path", None, "/default-pool/default-vol")232 check("shareable", False, True)233 check("driver_cache", None, "writeback")234 check("driver_io", None, "threads")235 check("driver_io", "threads", "native")236 self._alter_compare(guest.get_xml_config(), outfile)237 def testSingleDisk(self):238 xml = ("""<disk type="file" device="disk"><source file="/a.img"/>"""239 """<target dev="hda" bus="ide"/></disk>""")240 d = virtinst.VirtualDisk(parsexml=xml)241 self._set_and_check(d, "target", "hda", "hdb")242 self.assertEquals(xml.replace("hda", "hdb"), d.get_xml_config())243 def testAlterChars(self):244 infile = "tests/xmlparse-xml/change-chars-in.xml"245 outfile = "tests/xmlparse-xml/change-chars-out.xml"246 guest = virtinst.Guest(conn=conn,247 parsexml=file(infile).read())248 serial1 = guest.get_devices("serial")[0]249 serial2 = guest.get_devices("serial")[1]250 parallel1 = guest.get_devices("parallel")[0]251 parallel2 = guest.get_devices("parallel")[1]252 console1 = guest.get_devices("console")[0]253 console2 = guest.get_devices("console")[1]254 channel1 = guest.get_devices("channel")[0]255 channel2 = guest.get_devices("channel")[1]256 check = self._make_checker(serial1)257 check("char_type", "null")258 check = self._make_checker(serial2)259 check("char_type", "tcp")260 check("protocol", "telnet", "raw")261 check("source_mode", "bind", "connect")262 check = self._make_checker(parallel1)263 check("source_mode", "bind")264 check("source_path", "/tmp/foobar", None)265 check("char_type", "unix", "pty")266 check = self._make_checker(parallel2)267 check("char_type", "udp")268 check("bind_port", "1111", "1357")269 check("bind_host", "my.bind.host", "my.foo.host")270 check("source_mode", "connect")271 check("source_port", "2222", "7777")272 check("source_host", "my.source.host", "source.foo.host")273 check = self._make_checker(console1)274 check("char_type", "pty")275 check("target_type", None)276 check = self._make_checker(console2)277 check("char_type", "file")278 check("source_path", "/tmp/foo.img", None)279 check("source_path", None, "/root/foo")280 check("target_type", "virtio")281 check = self._make_checker(channel1)282 check("char_type", "pty")283 check("target_type", "virtio")284 check("target_name", "foo.bar.frob", "test.changed")285 check = self._make_checker(channel2)286 check("char_type", "unix")287 check("target_type", "guestfwd")288 check("target_address", "1.2.3.4", "5.6.7.8")289 check("target_port", "4567", "1199")290 self._alter_compare(guest.get_xml_config(), outfile)291 def testAlterControllers(self):292 infile = "tests/xmlparse-xml/change-controllers-in.xml"293 outfile = "tests/xmlparse-xml/change-controllers-out.xml"294 guest = virtinst.Guest(conn=conn,295 parsexml=file(infile).read())296 dev1 = guest.get_devices("controller")[0]297 dev2 = guest.get_devices("controller")[1]298 dev3 = guest.get_devices("controller")[2]299 dev4 = guest.get_devices("controller")[3]300 check = self._make_checker(dev1)301 check("type", "ide")302 check("index", "3", "1")303 check = self._make_checker(dev2)304 check("type", "virtio-serial")305 check("index", "0", "7")306 check("ports", "32", "5")307 check("vectors", "17", None)308 check = self._make_checker(dev3)309 check("type", "scsi")310 check("index", "1", "2")311 check = self._make_checker(dev4)312 check("type", "usb")313 check("index", "3", "9")314 check("model", "ich9-ehci1")315 check = self._make_checker(dev4.get_master())316 check("startport", "4", "2", None)317 self._alter_compare(guest.get_xml_config(), outfile)318 def testAlterNics(self):319 infile = "tests/xmlparse-xml/change-nics-in.xml"320 outfile = "tests/xmlparse-xml/change-nics-out.xml"321 guest = virtinst.Guest(conn=conn,322 parsexml=file(infile).read())323 dev1 = guest.get_devices("interface")[0]324 dev2 = guest.get_devices("interface")[1]325 dev3 = guest.get_devices("interface")[2]326 dev4 = guest.get_devices("interface")[3]327 dev5 = guest.get_devices("interface")[4]328 check = self._make_checker(dev1)329 check("type", "user")330 check("model", None, "testmodel")331 check("bridge", None, "br0")332 check("network", None, "route")333 check("macaddr", "22:11:11:11:11:11", "AA:AA:AA:AA:AA:AA")334 self.assertEquals(dev1.get_source(), None)335 check = self._make_checker(dev2)336 self.assertEquals(dev2.get_source(), "default")337 check("network", "default", None)338 check("bridge", None, "newbr0")339 check("type", "network", "bridge")340 check("model", "e1000", "virtio")341 check = self._make_checker(dev3)342 check("type", "bridge")343 check("bridge", "foobr0", "newfoo0")344 check("network", None, "default")345 check("macaddr", "22:22:22:22:22:22")346 check("target_dev", None, "test1")347 self.assertEquals(dev3.get_source(), "newfoo0")348 check = self._make_checker(dev4)349 check("type", "ethernet")350 check("source_dev", "eth0", "eth1")351 check("target_dev", "nic02", "nic03")352 check("target_dev", "nic03", None)353 self.assertEquals(dev4.get_source(), "eth1")354 check = self._make_checker(dev5)355 check("type", "direct")356 check("source_dev", "eth0.1")357 check("source_mode", "vepa", "bridge")358 virtualport = dev5.virtualport359 check = self._make_checker(virtualport)360 check("type", "802.1Qbg")361 check("managerid", "12", "11")362 check("typeid", "1193046", "1193047")363 check("typeidversion", "1", "2")364 check("instanceid", "09b11c53-8b5c-4eeb-8f00-d84eaa0aaa3b",365 "09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f")366 self._alter_compare(guest.get_xml_config(), outfile)367 def testAlterInputs(self):368 infile = "tests/xmlparse-xml/change-inputs-in.xml"369 outfile = "tests/xmlparse-xml/change-inputs-out.xml"370 guest = virtinst.Guest(conn=conn,371 parsexml=file(infile).read())372 dev1 = guest.get_devices("input")[0]373 dev2 = guest.get_devices("input")[1]374 check = self._make_checker(dev1)375 check("type", "mouse", "tablet")376 check("bus", "ps2", "usb")377 check = self._make_checker(dev2)378 check("type", "tablet", "mouse")379 check("bus", "usb", "xen")380 check("bus", "xen", "usb")381 self._alter_compare(guest.get_xml_config(), outfile)382 def testAlterGraphics(self):383 infile = "tests/xmlparse-xml/change-graphics-in.xml"384 outfile = "tests/xmlparse-xml/change-graphics-out.xml"385 guest = virtinst.Guest(conn=conn,386 parsexml=file(infile).read())387 dev1 = guest.get_devices("graphics")[0]388 dev2 = guest.get_devices("graphics")[1]389 dev3 = guest.get_devices("graphics")[2]390 dev4 = guest.get_devices("graphics")[3]391 dev5 = guest.get_devices("graphics")[4]392 check = self._make_checker(dev1)393 check("type", "vnc")394 check("passwd", "foobar", "newpass")395 check("port", 100, 6000)396 check("listen", "0.0.0.0", "1.2.3.4")397 check = self._make_checker(dev2)398 check("type", "sdl")399 check("xauth", "/tmp/.Xauthority", "fooauth")400 check("display", "1:2", "6:1")401 check = self._make_checker(dev3)402 check("type", "rdp")403 check = self._make_checker(dev4)404 check("type", "vnc")405 check("port", -1)406 check("socket", "/tmp/foobar", "/var/lib/libvirt/socket/foo")407 check = self._make_checker(dev5)408 check("type", "spice")409 check("passwd", "foobar", "newpass")410 check("port", 100, 6000)411 check("tlsPort", 101, 6001)412 check("listen", "0.0.0.0", "1.2.3.4")413 check("channel_inputs_mode", "insecure", "secure")414 check("channel_main_mode", "secure", "any")415 check("channel_record_mode", "any", "insecure")416 check("passwdValidTo", "2010-04-09T15:51:00", "2011-01-07T19:08:00")417 self._alter_compare(guest.get_xml_config(), outfile)418 def testAlterVideos(self):419 infile = "tests/xmlparse-xml/change-videos-in.xml"420 outfile = "tests/xmlparse-xml/change-videos-out.xml"421 guest = virtinst.Guest(conn=conn,422 parsexml=file(infile).read())423 dev1 = guest.get_devices("video")[0]424 dev2 = guest.get_devices("video")[1]425 dev3 = guest.get_devices("video")[2]426 check = self._make_checker(dev1)427 check("model_type", "vmvga", "vga")428 check("vram", None, "1000")429 check("heads", None, "1")430 check = self._make_checker(dev2)431 check("model_type", "cirrus", "vmvga")432 check("vram", "10240", None)433 check("heads", "3", "5")434 check = self._make_checker(dev3)435 check("model_type", "cirrus", "cirrus")436 self._alter_compare(guest.get_xml_config(), outfile)437 def testAlterHostdevs(self):438 infile = "tests/xmlparse-xml/change-hostdevs-in.xml"439 outfile = "tests/xmlparse-xml/change-hostdevs-out.xml"440 guest = virtinst.Guest(conn=conn,441 parsexml=file(infile).read())442 dev1 = guest.get_devices("hostdev")[0]443 dev2 = guest.get_devices("hostdev")[1]444 dev3 = guest.get_devices("hostdev")[2]445 check = self._make_checker(dev1)446 check("type", "usb")447 check("managed", True, False)448 check("mode", "subsystem", None)449 check("vendor", "0x4321", "0x1111")450 check("product", "0x1234", "0x2222")451 check("bus", None, "1")452 check("device", None, "2")453 check = self._make_checker(dev2)454 check("type", "usb")455 check("managed", False, True)456 check("mode", "capabilities", "subsystem")457 check("bus", "0x12", "0x56")458 check("device", "0x34", "0x78")459 check = self._make_checker(dev3)460 check("type", "pci")461 check("managed", True, True)462 check("mode", "subsystem", "subsystem")463 check("domain", "0x0", "0x4")464 check("bus", "0x1", "0x5")465 check("slot", "0x2", "0x6")466 check("function", "0x3", "0x7")467 self._alter_compare(guest.get_xml_config(), outfile)468 def testAlterWatchdogs(self):469 infile = "tests/xmlparse-xml/change-watchdogs-in.xml"470 outfile = "tests/xmlparse-xml/change-watchdogs-out.xml"471 guest = virtinst.Guest(conn=conn,472 parsexml=file(infile).read())473 dev1 = guest.get_devices("watchdog")[0]474 check = self._make_checker(dev1)475 check("model", "ib700", "i6300esb")476 check("action", "none", "poweroff")477 self._alter_compare(guest.get_xml_config(), outfile)478 def testAlterFilesystems(self):479 devtype = "filesystem"480 infile = "tests/xmlparse-xml/change-%ss-in.xml" % devtype481 outfile = "tests/xmlparse-xml/change-%ss-out.xml" % devtype482 guest = virtinst.Guest(conn=conn,483 parsexml=file(infile).read())484 dev1 = guest.get_devices(devtype)[0]485 dev2 = guest.get_devices(devtype)[1]486 dev3 = guest.get_devices(devtype)[2]487 dev4 = guest.get_devices(devtype)[3]488 check = self._make_checker(dev1)489 check("type", None, "mount")490 check("mode", None, "passthrough")491 check("driver", "handle", None)492 check("wrpolicy", None, None)493 check("source", "/foo/bar", "/new/path")494 check("target", "/bar/baz", "/new/target")495 check = self._make_checker(dev2)496 check("type", "template")497 check("mode", None, "mapped")498 check("source", "template_fedora", "template_new")499 check("target", "/bar/baz")500 check = self._make_checker(dev3)501 check("type", "mount", None)502 check("mode", "squash", None)503 check("driver", "path", "handle")504 check("wrpolicy", "immediate", None)505 check("readonly", False, True)506 check = self._make_checker(dev4)507 check("type", "mount", None)508 check("mode", "mapped", None)509 check("driver", "path", "handle")510 check("wrpolicy", None, "immediate")511 check("readonly", False, True)512 self._alter_compare(guest.get_xml_config(), outfile)513 def testAlterSounds(self):514 infile = "tests/xmlparse-xml/change-sounds-in.xml"515 outfile = "tests/xmlparse-xml/change-sounds-out.xml"516 guest = virtinst.Guest(conn=conn,517 parsexml=file(infile).read())518 dev1 = guest.get_devices("sound")[0]519 dev2 = guest.get_devices("sound")[1]520 dev3 = guest.get_devices("sound")[2]521 check = self._make_checker(dev1)522 check("model", "sb16", "ac97")523 check = self._make_checker(dev2)524 check("model", "es1370", "es1370")525 check = self._make_checker(dev3)526 check("model", "ac97", "sb16")527 self._alter_compare(guest.get_xml_config(), outfile)528 def testAlterAddr(self):529 infile = "tests/xmlparse-xml/change-addr-in.xml"530 outfile = "tests/xmlparse-xml/change-addr-out.xml"531 guest = virtinst.Guest(conn=conn,532 parsexml=file(infile).read())533 dev1 = guest.get_devices("disk")[0]534 dev2 = guest.get_devices("controller")[0]535 dev3 = guest.get_devices("channel")[0]536 check = self._make_checker(dev1.address)537 check("type", "drive", "pci")538 check("type", "pci", "drive")539 check("controller", "3", "1")540 check("bus", "5", "4")541 check("unit", "33", "32")542 check = self._make_checker(dev1.alias)543 check("name", "foo2", None)544 check = self._make_checker(dev2.address)545 check("type", "pci")546 check("domain", "0x0000", "0x0001")547 check("bus", "0x00", "4")548 check("slot", "0x04", "10")549 check("function", "0x7", "0x6")550 check = self._make_checker(dev2.alias)551 check("name", None, "frob")552 check = self._make_checker(dev3.address)553 check("type", "virtio-serial")554 check("controller", "0")555 check("bus", "0")556 check("port", "2", "4")557 check = self._make_checker(dev3.alias)558 check("name", "channel0", "channel1")559 self._alter_compare(guest.get_xml_config(), outfile)560 def testAlterSmartCard(self):561 infile = "tests/xmlparse-xml/change-smartcard-in.xml"562 outfile = "tests/xmlparse-xml/change-smartcard-out.xml"563 guest = virtinst.Guest(conn=conn,564 parsexml=file(infile).read())565 dev1 = guest.get_devices("smartcard")[0]566 dev2 = guest.get_devices("smartcard")[1]567 check = self._make_checker(dev1)568 check("type", None, "tcp")569 check = self._make_checker(dev2)570 check("mode", "passthrough", "host")571 check("type", "spicevmc", None)572 self._alter_compare(guest.get_xml_config(), outfile)573 def testAlterRedirdev(self):574 infile = "tests/xmlparse-xml/change-redirdev-in.xml"575 outfile = "tests/xmlparse-xml/change-redirdev-out.xml"576 guest = virtinst.Guest(conn=conn,577 parsexml=file(infile).read())578 dev1 = guest.get_devices("redirdev")[0]579 dev2 = guest.get_devices("redirdev")[1]580 check = self._make_checker(dev1)581 check("host", "foo", "bar")582 check("service", "12", "42")583 check = self._make_checker(dev2)584 check("type", "spicevmc")585 self._alter_compare(guest.get_xml_config(), outfile)586 def testConsoleCompat(self):587 infile = "tests/xmlparse-xml/console-compat-in.xml"588 outfile = "tests/xmlparse-xml/console-compat-out.xml"589 guest = virtinst.Guest(conn=conn,590 parsexml=file(infile).read())591 dev1 = guest.get_devices("console")[0]592 check = self._make_checker(dev1)593 check("source_path", "/dev/pts/4")594 self._alter_compare(guest.get_xml_config(), outfile)595 def testAddRemoveDevices(self):596 infile = "tests/xmlparse-xml/add-devices-in.xml"597 outfile = "tests/xmlparse-xml/add-devices-out.xml"598 guest = virtinst.Guest(conn=conn,599 parsexml=file(infile).read())600 rmdev = guest.get_devices("disk")[2]601 guest.remove_device(rmdev)602 adddev = virtinst.VirtualNetworkInterface(conn=conn, type="network",603 network="default",604 macaddr="1A:2A:3A:4A:5A:6A")605 guest.add_device(virtinst.VirtualWatchdog(conn))606 guest.add_device(adddev)607 guest.remove_device(adddev)608 guest.add_device(adddev)609 self._alter_compare(guest.get_xml_config(), outfile)610 def testChangeKVMMedia(self):611 infile = "tests/xmlparse-xml/change-media-in.xml"612 outfile = "tests/xmlparse-xml/change-media-out.xml"613 guest = virtinst.Guest(conn=kvmconn,614 parsexml=file(infile).read())615 disk = guest.get_devices("disk")[0]616 check = self._make_checker(disk)617 check("path", None, "/default-pool/default-vol")618 disk = guest.get_devices("disk")[1]619 check = self._make_checker(disk)620 check("path", None, "/default-pool/default-vol")621 check("path", "/default-pool/default-vol", "/disk-pool/diskvol1")622 disk = guest.get_devices("disk")[2]623 check = self._make_checker(disk)624 check("path", None, "/disk-pool/diskvol1")625 disk = guest.get_devices("disk")[3]626 check = self._make_checker(disk)627 check("path", None, "/default-pool/default-vol")628 disk = guest.get_devices("disk")[4]629 check = self._make_checker(disk)630 check("path", None, "/disk-pool/diskvol1")631 self._alter_compare(guest.get_xml_config(), outfile)632if __name__ == "__main__":...
device_monitor_test.py
Source:device_monitor_test.py
1#!/usr/bin/env python2# Copyright 2015 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5import os6import sys7import unittest8if __name__ == '__main__':9 sys.path.append(10 os.path.abspath(os.path.join(os.path.dirname(__file__),11 '..', '..', '..')))12from devil import devil_env13from devil.android import device_errors14from devil.android import device_utils15from devil.android.tools import device_monitor16with devil_env.SysPath(devil_env.PYMOCK_PATH):17 import mock # pylint: disable=import-error18class DeviceMonitorTest(unittest.TestCase):19 def setUp(self):20 self.device = mock.Mock(spec=device_utils.DeviceUtils,21 serial='device_cereal', build_id='abc123', build_product='clownfish')22 self.file_contents = {23 '/proc/meminfo': """24 MemTotal: 1234567 kB25 MemFree: 1000000 kB26 MemUsed: 234567 kB27 """,28 '/sys/class/thermal/thermal_zone0/type': 'CPU-therm',29 '/sys/class/thermal/thermal_zone0/temp': '30',30 '/proc/uptime': '12345 99999',31 }32 self.device.ReadFile = mock.MagicMock(33 side_effect=lambda file_name: self.file_contents[file_name])34 self.cmd_outputs = {35 'ps': ['headers', 'p1', 'p2', 'p3', 'p4', 'p5'],36 'grep': ['/sys/class/thermal/thermal_zone0/type'],37 }38 def mock_run_shell(cmd, **_kwargs):39 args = cmd.split() if isinstance(cmd, basestring) else cmd40 try:41 return self.cmd_outputs[args[0]]42 except KeyError:43 raise device_errors.AdbShellCommandFailedError(cmd, None, None)44 self.device.RunShellCommand = mock.MagicMock(side_effect=mock_run_shell)45 self.battery = mock.Mock()46 self.battery.GetBatteryInfo = mock.MagicMock(47 return_value={'level': '80', 'temperature': '123'})48 self.expected_status = {49 'device_cereal': {50 'processes': 5,51 'temp': {52 'CPU-therm': 30.053 },54 'battery': {55 'temperature': 123,56 'level': 8057 },58 'uptime': 12345.0,59 'mem': {60 'total': 1234567,61 'free': 100000062 },63 'build': {64 'build.id': 'abc123',65 'product.device': 'clownfish',66 },67 'state': 'available',68 }69 }70 @mock.patch('devil.android.battery_utils.BatteryUtils')71 @mock.patch('devil.android.device_utils.DeviceUtils.HealthyDevices')72 def test_getStats(self, get_devices, get_battery):73 get_devices.return_value = [self.device]74 get_battery.return_value = self.battery75 status = device_monitor.get_all_status(None)76 self.assertEquals(self.expected_status, status['devices'])77 @mock.patch('devil.android.battery_utils.BatteryUtils')78 @mock.patch('devil.android.device_utils.DeviceUtils.HealthyDevices')79 def test_getStatsNoBattery(self, get_devices, get_battery):80 get_devices.return_value = [self.device]81 get_battery.return_value = self.battery82 broken_battery_info = mock.Mock()83 broken_battery_info.GetBatteryInfo = mock.MagicMock(84 return_value={'level': '-1', 'temperature': 'not_a_number'})85 get_battery.return_value = broken_battery_info86 # Should be same status dict but without battery stats.87 expected_status_no_battery = self.expected_status.copy()88 expected_status_no_battery['device_cereal'].pop('battery')89 status = device_monitor.get_all_status(None)90 self.assertEquals(expected_status_no_battery, status['devices'])91 @mock.patch('devil.android.battery_utils.BatteryUtils')92 @mock.patch('devil.android.device_utils.DeviceUtils.HealthyDevices')93 def test_getStatsNoPs(self, get_devices, get_battery):94 get_devices.return_value = [self.device]95 get_battery.return_value = self.battery96 del self.cmd_outputs['ps'] # Throw exception on run shell ps command.97 # Should be same status dict but without process stats.98 expected_status_no_ps = self.expected_status.copy()99 expected_status_no_ps['device_cereal'].pop('processes')100 status = device_monitor.get_all_status(None)101 self.assertEquals(expected_status_no_ps, status['devices'])102 @mock.patch('devil.android.battery_utils.BatteryUtils')103 @mock.patch('devil.android.device_utils.DeviceUtils.HealthyDevices')104 def test_getStatsNoSensors(self, get_devices, get_battery):105 get_devices.return_value = [self.device]106 get_battery.return_value = self.battery107 del self.cmd_outputs['grep'] # Throw exception on run shell grep command.108 # Should be same status dict but without temp stats.109 expected_status_no_temp = self.expected_status.copy()110 expected_status_no_temp['device_cereal'].pop('temp')111 status = device_monitor.get_all_status(None)112 self.assertEquals(expected_status_no_temp, status['devices'])113 @mock.patch('devil.android.battery_utils.BatteryUtils')114 @mock.patch('devil.android.device_utils.DeviceUtils.HealthyDevices')115 def test_getStatsWithBlacklist(self, get_devices, get_battery):116 get_devices.return_value = [self.device]117 get_battery.return_value = self.battery118 blacklist = mock.Mock()119 blacklist.Read = mock.MagicMock(120 return_value={'bad_device': {'reason': 'offline'}})121 # Should be same status dict but with extra blacklisted device.122 expected_status = self.expected_status.copy()123 expected_status['bad_device'] = {'state': 'offline'}124 status = device_monitor.get_all_status(blacklist)125 self.assertEquals(expected_status, status['devices'])126 @mock.patch('devil.android.battery_utils.BatteryUtils')127 @mock.patch('devil.android.device_utils.DeviceUtils.HealthyDevices')128 def test_brokenTempValue(self, get_devices, get_battery):129 self.file_contents['/sys/class/thermal/thermal_zone0/temp'] = 'n0t a numb3r'130 get_devices.return_value = [self.device]131 get_battery.return_value = self.battery132 expected_status_no_temp = self.expected_status.copy()133 expected_status_no_temp['device_cereal'].pop('temp')134 status = device_monitor.get_all_status(None)135 self.assertEquals(self.expected_status, status['devices'])136if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!