Best Python code snippet using fMBT_python
integration-test
Source:integration-test
...100 sys.stderr.write('------------------------------\n')101 #102 # Daemon control and D-BUS I/O103 #104 def start_daemon(self):105 '''Start daemon and create DBus proxy.106 Do this after adding the devices you want to test with. At the moment107 this only works with coldplugging, as we do not currently have a way to108 inject simulated uevents.109 When done, this sets self.proxy a the Gio.DBusProxy for upowerd.110 '''111 env = os.environ.copy()112 env['G_DEBUG'] = 'fatal-criticals'113 # note: Python doesn't propagate the setenv from Testbed.new(), so we114 # have to do that ourselves115 env['UMOCKDEV_DIR'] = self.testbed.get_root_dir()116 self.log = tempfile.NamedTemporaryFile()117 self.daemon = subprocess.Popen([self.daemon_path, '-v'],118 env=env, stdout=self.log,119 stderr=subprocess.STDOUT)120 # wait until the daemon gets online121 timeout = 100122 while timeout > 0:123 time.sleep(0.1)124 timeout -= 1125 try:126 self.get_dbus_property('DaemonVersion')127 break128 except GLib.GError:129 pass130 else:131 self.fail('daemon did not start in 10 seconds')132 self.proxy = Gio.DBusProxy.new_sync(133 self.dbus, Gio.DBusProxyFlags.DO_NOT_AUTO_START, None, UP,134 '/org/freedesktop/UPower', UP, None)135 self.assertEqual(self.daemon.poll(), None, 'daemon crashed')136 def stop_daemon(self):137 '''Stop the daemon if it is running.'''138 if self.daemon:139 try:140 self.daemon.kill()141 except OSError:142 pass143 self.daemon.wait()144 self.daemon = None145 self.proxy = None146 def get_dbus_property(self, name):147 '''Get property value from daemon D-Bus interface.'''148 proxy = Gio.DBusProxy.new_sync(149 self.dbus, Gio.DBusProxyFlags.DO_NOT_AUTO_START, None, UP,150 '/org/freedesktop/UPower', 'org.freedesktop.DBus.Properties', None)151 return proxy.Get('(ss)', UP, name)152 def get_dbus_display_property(self, name):153 '''Get property value from display device D-Bus interface.'''154 proxy = Gio.DBusProxy.new_sync(155 self.dbus, Gio.DBusProxyFlags.DO_NOT_AUTO_START, None, UP,156 UP_DISPLAY_OBJECT_PATH, 'org.freedesktop.DBus.Properties', None)157 return proxy.Get('(ss)', UP, name)158 def get_dbus_dev_property(self, device, name):159 '''Get property value from an upower device D-Bus path.'''160 proxy = Gio.DBusProxy.new_sync(161 self.dbus, Gio.DBusProxyFlags.DO_NOT_AUTO_START, None, UP, device,162 'org.freedesktop.DBus.Properties', None)163 return proxy.Get('(ss)', UP + '.Device', name)164 #165 # Actual test cases166 #167 def test_daemon_version(self):168 '''DaemonVersion property'''169 self.start_daemon()170 self.assertEqual(self.proxy.EnumerateDevices(), [])171 self.assertRegex(self.get_dbus_property('DaemonVersion'), '^[0-9.]+$')172 # without any devices we should assume AC173 self.assertEqual(self.get_dbus_property('OnBattery'), False)174 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)175 def test_no_devices(self):176 '''no devices'''177 # without any devices we should assume AC178 self.start_daemon()179 self.assertEqual(self.get_dbus_property('OnBattery'), False)180 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)181 def test_props_online_ac(self):182 '''properties with online AC'''183 ac = self.testbed.add_device('power_supply', 'AC', None,184 ['type', 'Mains', 'online', '1'], [])185 self.start_daemon()186 devs = self.proxy.EnumerateDevices()187 self.assertEqual(len(devs), 1)188 ac_up = devs[0]189 self.assertTrue('line_power_AC' in ac_up)190 self.assertEqual(self.get_dbus_property('OnBattery'), False)191 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)192 self.assertEqual(self.get_dbus_dev_property(ac_up, 'PowerSupply'), True)193 self.assertEqual(self.get_dbus_dev_property(ac_up, 'Type'), 1)194 self.assertEqual(self.get_dbus_dev_property(ac_up, 'Online'), True)195 self.assertEqual(self.get_dbus_dev_property(ac_up, 'NativePath'), 'AC')196 def test_props_offline_ac(self):197 '''properties with offline AC'''198 ac = self.testbed.add_device('power_supply', 'AC', None,199 ['type', 'Mains', 'online', '0'], [])200 self.start_daemon()201 devs = self.proxy.EnumerateDevices()202 self.assertEqual(len(devs), 1)203 # we don't have any known online power device now, but still no battery204 self.assertEqual(self.get_dbus_property('OnBattery'), False)205 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)206 self.assertEqual(self.get_dbus_dev_property(devs[0], 'Online'), False)207 self.stop_daemon()208 def test_battery_ac(self):209 '''properties with dynamic battery/AC'''210 # offline AC + discharging battery211 ac = self.testbed.add_device('power_supply', 'AC', None,212 ['type', 'Mains', 'online', '0'], [])213 bat0 = self.testbed.add_device('power_supply', 'BAT0', None,214 ['type', 'Battery',215 'present', '1',216 'status', 'Discharging',217 'energy_full', '60000000',218 'energy_full_design', '80000000',219 'energy_now', '48000000',220 'voltage_now', '12000000'], [])221 self.start_daemon()222 devs = self.proxy.EnumerateDevices()223 self.assertEqual(len(devs), 2)224 if 'BAT' in devs[0] == ac_up:225 (bat0_up, ac_up) = devs226 else:227 (ac_up, bat0_up) = devs228 # we don't have any known online power device now, but still no battery229 self.assertEqual(self.get_dbus_property('OnBattery'), True)230 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)231 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)232 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)233 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 80.0)234 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 48.0)235 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFull'), 60.0)236 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFullDesign'), 80.0)237 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Voltage'), 12.0)238 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'NativePath'), 'BAT0')239 self.stop_daemon()240 # offline AC + discharging low battery241 self.testbed.set_attribute(bat0, 'energy_now', '1500000')242 self.start_daemon()243 self.assertEqual(self.get_dbus_property('OnBattery'), True)244 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_CRITICAL)245 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)246 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)247 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 2.5)248 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'PowerSupply'), True)249 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Type'), 2)250 self.stop_daemon()251 # now connect AC again252 self.testbed.set_attribute(ac, 'online', '1')253 self.start_daemon()254 devs = self.proxy.EnumerateDevices()255 self.assertEqual(len(devs), 2)256 # we don't have any known online power device now, but still no battery257 self.assertEqual(self.get_dbus_property('OnBattery'), False)258 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)259 self.assertEqual(self.get_dbus_dev_property(ac_up, 'Online'), True)260 self.stop_daemon()261 def test_multiple_batteries(self):262 '''Multiple batteries'''263 # one well charged, one low264 bat0 = self.testbed.add_device('power_supply', 'BAT0', None,265 ['type', 'Battery',266 'present', '1',267 'status', 'Discharging',268 'energy_full', '60000000',269 'energy_full_design', '80000000',270 'energy_now', '48000000',271 'voltage_now', '12000000'], [])272 self.testbed.add_device('power_supply', 'BAT1', None,273 ['type', 'Battery',274 'present', '1',275 'status', 'Discharging',276 'energy_full', '60000000',277 'energy_full_design', '80000000',278 'energy_now', '1500000',279 'voltage_now', '12000000'], [])280 self.start_daemon()281 devs = self.proxy.EnumerateDevices()282 self.assertEqual(len(devs), 2)283 # as we have one which is well-charged, the summary state is "not low284 # battery"285 self.assertEqual(self.get_dbus_property('OnBattery'), True)286 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)287 self.stop_daemon()288 # now set both to low289 self.testbed.set_attribute(bat0, 'energy_now', '1500000')290 self.start_daemon()291 self.assertEqual(self.get_dbus_property('OnBattery'), True)292 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_CRITICAL)293 self.stop_daemon()294 def test_unknown_battery_status_no_ac(self):295 '''Unknown battery charge status, no AC'''296 self.testbed.add_device('power_supply', 'BAT0', None,297 ['type', 'Battery',298 'present', '1',299 'status', 'unknown',300 'energy_full', '60000000',301 'energy_full_design', '80000000',302 'energy_now', '48000000',303 'voltage_now', '12000000'], [])304 # with no other power sources, the OnBattery value here is really305 # arbitrary, so don't test it. The only thing we know for sure is that306 # we aren't on low battery307 self.start_daemon()308 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)309 def test_unknown_battery_status_with_ac(self):310 '''Unknown battery charge status, with AC'''311 self.testbed.add_device('power_supply', 'BAT0', None,312 ['type', 'Battery',313 'present', '1',314 'status', 'unknown',315 'energy_full', '60000000',316 'energy_full_design', '80000000',317 'energy_now', '48000000',318 'voltage_now', '12000000'], [])319 ac = self.testbed.add_device('power_supply', 'AC', None,320 ['type', 'Mains', 'online', '0'], [])321 self.start_daemon()322 self.assertEqual(self.get_dbus_property('OnBattery'), True)323 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)324 self.stop_daemon()325 self.testbed.set_attribute(ac, 'online', '1')326 self.start_daemon()327 self.assertEqual(self.get_dbus_property('OnBattery'), False)328 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)329 self.stop_daemon()330 def test_battery_charge(self):331 '''battery which reports charge instead of energy332 energy_* is in uWh, while charge_* is in uAh.333 '''334 self.testbed.add_device('power_supply', 'BAT0', None,335 ['type', 'Battery',336 'present', '1',337 'status', 'Discharging',338 'charge_full', '10500000',339 'charge_full_design', '11000000',340 'charge_now', '7875000',341 'current_now', '787000',342 'voltage_now', '12000000'], [])343 self.start_daemon()344 devs = self.proxy.EnumerateDevices()345 self.assertEqual(len(devs), 1)346 bat0_up = devs[0]347 self.assertEqual(self.get_dbus_property('OnBattery'), True)348 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)349 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)350 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)351 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 75.0)352 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 94.5)353 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFull'), 126.0)354 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFullDesign'), 132.0)355 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Voltage'), 12.0)356 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Temperature'), 0.0)357 self.stop_daemon()358 def test_battery_energy_charge_mixed(self):359 '''battery which reports current energy, but full charge'''360 self.testbed.add_device('power_supply', 'BAT0', None,361 ['type', 'Battery',362 'present', '1',363 'status', 'Discharging',364 'charge_full', '10500000',365 'charge_full_design', '11000000',366 'energy_now', '50400000',367 'voltage_now', '12000000'], [])368 self.start_daemon()369 devs = self.proxy.EnumerateDevices()370 self.assertEqual(len(devs), 1)371 bat0_up = devs[0]372 self.assertEqual(self.get_dbus_property('OnBattery'), True)373 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)374 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)375 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)376 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 50.4)377 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFull'), 126.0)378 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFullDesign'), 132.0)379 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Voltage'), 12.0)380 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 40.0)381 self.stop_daemon()382 def test_battery_capacity_and_charge(self):383 '''battery which reports capacity and charge_full'''384 self.testbed.add_device('power_supply', 'BAT0', None,385 ['type', 'Battery',386 'present', '1',387 'status', 'Discharging',388 'charge_full', '10500000',389 'charge_full_design', '11000000',390 'capacity', '40',391 'voltage_now', '12000000'], [])392 self.start_daemon()393 devs = self.proxy.EnumerateDevices()394 self.assertEqual(len(devs), 1)395 bat0_up = devs[0]396 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 40.0)397 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)398 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)399 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 50.4)400 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFull'), 126.0)401 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFullDesign'), 132.0)402 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Voltage'), 12.0)403 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'PowerSupply'), True)404 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Type'), 2)405 self.assertEqual(self.get_dbus_property('OnBattery'), True)406 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)407 self.stop_daemon()408 def test_battery_overfull(self):409 '''battery which reports a > 100% percentage for a full battery'''410 self.testbed.add_device('power_supply', 'BAT0', None,411 ['type', 'Battery',412 'present', '1',413 'status', 'Full',414 'current_now', '1000',415 'charge_now', '11000000',416 'charge_full', '10000000',417 'charge_full_design', '11000000',418 'capacity', '110',419 'voltage_now', '12000000'], [])420 self.start_daemon()421 devs = self.proxy.EnumerateDevices()422 self.assertEqual(len(devs), 1)423 bat0_up = devs[0]424 # should clamp percentage425 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 100.0)426 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)427 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'),428 UP_DEVICE_STATE_FULLY_CHARGED)429 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 132.0)430 # should adjust EnergyFull to reality, not what the battery claims431 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFull'), 132.0)432 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFullDesign'), 132.0)433 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Voltage'), 12.0)434 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'PowerSupply'), True)435 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Type'), 2)436 self.stop_daemon()437 def test_battery_temperature(self):438 '''battery which reports temperature'''439 self.testbed.add_device('power_supply', 'BAT0', None,440 ['type', 'Battery',441 'present', '1',442 'status', 'Discharging',443 'temp', '254',444 'energy_full', '60000000',445 'energy_full_design', '80000000',446 'energy_now', '1500000',447 'voltage_now', '12000000'], [])448 self.start_daemon()449 devs = self.proxy.EnumerateDevices()450 self.assertEqual(len(devs), 1)451 bat0_up = devs[0]452 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Temperature'), 25.4)453 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 2.5)454 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 1.5)455 self.assertEqual(self.get_dbus_property('OnBattery'), True)456 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_CRITICAL)457 self.stop_daemon()458 def test_ups_no_ac(self):459 '''UPS properties without AC'''460 # add a charging UPS461 ups0 = self.testbed.add_device('usb', 'hiddev0', None, [],462 ['DEVNAME', 'null', 'UPOWER_VENDOR', 'APC',463 'UPOWER_BATTERY_TYPE', 'ups',464 'UPOWER_FAKE_DEVICE', '1',465 'UPOWER_FAKE_HID_CHARGING', '1',466 'UPOWER_FAKE_HID_PERCENTAGE', '70'])467 self.start_daemon()468 devs = self.proxy.EnumerateDevices()469 self.assertEqual(len(devs), 1)470 ups0_up = devs[0]471 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'Vendor'), 'APC')472 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'IsPresent'), True)473 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'Percentage'), 70.0)474 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'State'), UP_DEVICE_STATE_CHARGING)475 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'PowerSupply'), True)476 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'Type'), 3)477 self.assertEqual(self.get_dbus_property('OnBattery'), False)478 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)479 self.stop_daemon()480 # now switch to discharging UPS481 self.testbed.set_property(ups0, 'UPOWER_FAKE_HID_CHARGING', '0')482 self.start_daemon()483 devs = self.proxy.EnumerateDevices()484 self.assertEqual(len(devs), 1)485 self.assertEqual(devs[0], ups0_up)486 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'IsPresent'), True)487 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'Percentage'), 70.0)488 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)489 self.assertEqual(self.get_dbus_property('OnBattery'), True)490 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_DISCHARGING)491 self.stop_daemon()492 # low UPS charge493 self.testbed.set_property(ups0, 'UPOWER_FAKE_HID_PERCENTAGE', '2')494 self.start_daemon()495 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'Percentage'), 2.0)496 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)497 self.assertEqual(self.get_dbus_property('OnBattery'), True)498 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_ACTION)499 def test_ups_offline_ac(self):500 '''UPS properties with offline AC'''501 # add low charge UPS502 ups0 = self.testbed.add_device('usb', 'hiddev0', None, [],503 ['DEVNAME', 'null', 'UPOWER_VENDOR', 'APC',504 'UPOWER_BATTERY_TYPE', 'ups',505 'UPOWER_FAKE_DEVICE', '1',506 'UPOWER_FAKE_HID_CHARGING', '0',507 'UPOWER_FAKE_HID_PERCENTAGE', '2'])508 # add an offline AC, should still be on battery509 ac = self.testbed.add_device('power_supply', 'AC', None,510 ['type', 'Mains', 'online', '0'], [])511 self.start_daemon()512 devs = self.proxy.EnumerateDevices()513 if 'AC' in devs[0]:514 ups0_up = devs[1]515 else:516 ups0_up = devs[0]517 self.assertEqual(len(devs), 2)518 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'Percentage'), 2.0)519 self.assertEqual(self.get_dbus_dev_property(ups0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)520 self.assertEqual(self.get_dbus_property('OnBattery'), True)521 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_ACTION)522 self.stop_daemon()523 # now plug in the AC, should switch to OnBattery=False524 self.testbed.set_attribute(ac, 'online', '1')525 self.start_daemon()526 self.assertEqual(self.get_dbus_property('OnBattery'), False)527 # FIXME this is completely wrong528 # The AC status doesn't change anything, the AC is what powers the UPS529 # and the UPS powers the desktop530 #531 # A plugged in UPS is always the one supplying the computer532 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_ACTION)533 self.stop_daemon()534 def test_vendor_strings(self):535 '''manufacturer/model_name/serial_number with valid and invalid strings'''536 bat0 = self.testbed.add_device('power_supply', 'BAT0', None,537 ['type', 'Battery',538 'present', '1',539 'status', 'Discharging',540 'energy_full', '60000000',541 'energy_full_design', '80000000',542 'energy_now', '1500000',543 'voltage_now', '12000000',544 # valid ASCII string545 'serial_number', '123ABC',546 # valid UTF-8 string547 'manufacturer', 'â¾ Batt Inc. â¢'],548 [])549 # string with invalid chars550 self.testbed.set_attribute_binary(bat0, 'model_name', b'AB\xFFC12\x013')551 self.start_daemon()552 devs = self.proxy.EnumerateDevices()553 self.assertEqual(len(devs), 1)554 bat0_up = devs[0]555 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Serial'), '123ABC')556 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Vendor'), 'â¾ Batt Inc. â¢')557 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Model'), 'ABC123')558 self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 1.5)559 self.stop_daemon()560 def _add_bt_mouse(self):561 '''Add a bluetooth mouse to testbed'''562 self.testbed.add_device('bluetooth',563 'usb1/bluetooth/hci0/hci0:01',564 None,565 [], [])566 self.testbed.add_device(567 'input',568 'usb1/bluetooth/hci0/hci0:01/input2/mouse3',569 None,570 [], ['DEVNAME', 'input/mouse3', 'ID_INPUT_MOUSE', '1'])571 mousebat0 = self.testbed.add_device(572 'power_supply',573 'usb1/bluetooth/hci0/hci0:01/1/power_supply/hid-00:11:22:33:44:55-battery',574 None,575 ['type', 'Battery',576 'scope', 'Device',577 'present', '1',578 'online', '1',579 'status', 'Discharging',580 'capacity', '30',581 'model_name', 'Fancy BT mouse'],582 [])583 return mousebat0584 def test_bluetooth_mouse(self):585 '''bluetooth mouse battery'''586 self._add_bt_mouse()587 self.start_daemon()588 devs = self.proxy.EnumerateDevices()589 self.assertEqual(len(devs), 1)590 mousebat0_up = devs[0]591 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'Model'), 'Fancy BT mouse')592 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'Percentage'), 30)593 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'PowerSupply'), False)594 # 5 == mouse595 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'Type'), 5)596 self.assertEqual(self.get_dbus_property('OnBattery'), False)597 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)598 self.stop_daemon()599 def test_bluetooth_mouse_reconnect(self):600 '''bluetooth mouse powerdown/reconnect'''601 mb = self._add_bt_mouse()602 self.start_daemon()603 devs_before = self.proxy.EnumerateDevices()604 self.assertEqual(len(devs_before), 1)605 self.testbed.uevent(mb, 'remove')606 time.sleep(1)607 self.assertEqual(self.proxy.EnumerateDevices(), [])608 self.testbed.uevent(mb, 'add')609 time.sleep(0.5)610 devs_after = self.proxy.EnumerateDevices()611 self.assertEqual(devs_before, devs_after)612 # second add, which should be treated as change613 self.testbed.uevent(mb, 'add')614 time.sleep(0.5)615 devs_after = self.proxy.EnumerateDevices()616 self.assertEqual(devs_before, devs_after)617 # with BT devices, original devices don't get removed on powerdown, but618 # on wakeup we'll get a new one which ought to replace the previous;619 # emulate that kernel bug620 os.unlink(os.path.join(self.testbed.get_sys_dir(), 'class',621 'power_supply', 'hid-00:11:22:33:44:55-battery'))622 mb1 = self.testbed.add_device(623 'power_supply',624 'usb1/bluetooth/hci0/hci0:01/2/power_supply/hid-00:11:22:33:44:55-battery',625 None,626 ['type', 'Battery',627 'scope', 'Device',628 'present', '1',629 'online', '1',630 'status', 'Discharging',631 'capacity', '30',632 'model_name', 'Fancy BT mouse'],633 [])634 self.testbed.uevent(mb1, 'add')635 time.sleep(0.5)636 devs_after = self.proxy.EnumerateDevices()637 self.assertEqual(devs_before, devs_after)638 mb1_up = devs_after[0]639 self.assertEqual(self.get_dbus_dev_property(mb1_up, 'Model'), 'Fancy BT mouse')640 self.assertEqual(self.get_dbus_dev_property(mb1_up, 'Percentage'), 30)641 self.assertEqual(self.get_dbus_dev_property(mb1_up, 'PowerSupply'), False)642 def test_bluetooth_hid_mouse(self):643 '''bluetooth HID mouse battery'''644 self.testbed.add_device('hid',645 'usb1/bluetooth/hci0/hci0:01',646 None,647 [], [])648 self.testbed.add_device(649 'input',650 'usb1/bluetooth/hci0/hci0:01/input/input2/mouse3',651 None,652 [], ['DEVNAME', 'input/mouse3', 'ID_INPUT_MOUSE', '1'])653 self.testbed.add_device(654 'power_supply',655 'usb1/bluetooth/hci0/hci0:01/1/power_supply/hid-00:11:22:33:44:55-battery',656 None,657 ['type', 'Battery',658 'scope', 'Device',659 'present', '1',660 'online', '1',661 'status', 'Discharging',662 'capacity', '30',663 'model_name', 'Fancy BT mouse'],664 [])665 self.start_daemon()666 devs = self.proxy.EnumerateDevices()667 self.assertEqual(len(devs), 1)668 mousebat0_up = devs[0]669 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'Model'), 'Fancy BT mouse')670 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'Percentage'), 30)671 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'PowerSupply'), False)672 # 5 == mouse673 self.assertEqual(self.get_dbus_dev_property(mousebat0_up, 'Type'), 5)674 self.assertEqual(self.get_dbus_property('OnBattery'), False)675 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)676 self.stop_daemon()677 def test_bluetooth_keyboard(self):678 '''bluetooth keyboard battery'''679 self.testbed.add_device('bluetooth',680 'usb2/bluetooth/hci0/hci0:1',681 None,682 [], [])683 self.testbed.add_device(684 'input',685 'usb2/bluetooth/hci0/hci0:1/input3/event4',686 None,687 [], ['DEVNAME', 'input/event4', 'ID_INPUT_KEYBOARD', '1'])688 self.testbed.add_device(689 'power_supply',690 'usb2/bluetooth/hci0/hci0:1/power_supply/hid-00:22:33:44:55:66-battery',691 None,692 ['type', 'Battery',693 'scope', 'Device',694 'present', '1',695 'online', '1',696 'status', 'Discharging',697 'capacity', '40',698 'model_name', 'Monster Typist'],699 [])700 self.start_daemon()701 devs = self.proxy.EnumerateDevices()702 self.assertEqual(len(devs), 1)703 kbdbat0_up = devs[0]704 self.assertEqual(self.get_dbus_dev_property(kbdbat0_up, 'Model'), 'Monster Typist')705 self.assertEqual(self.get_dbus_dev_property(kbdbat0_up, 'Percentage'), 40)706 self.assertEqual(self.get_dbus_dev_property(kbdbat0_up, 'PowerSupply'), False)707 # 6 == keyboard708 self.assertEqual(self.get_dbus_dev_property(kbdbat0_up, 'Type'), 6)709 self.assertEqual(self.get_dbus_property('OnBattery'), False)710 self.assertEqual(self.get_dbus_display_property('WarningLevel'), UP_DEVICE_LEVEL_NONE)711 self.stop_daemon()712 def test_broken_onda_battery(self):713 '''Onda v975w battery: https://bugzilla.kernel.org/show_bug.cgi?id=83941'''714 batc = self.testbed.add_device('power_supply', 'BATC', None,715 ['type', 'Battery',716 'capacity', '0',717 'capacity_level', 'Normal',718 'present', '1',719 'energy_full', '0',720 'energy_full_design', '0',721 'energy_now', '5549000',722 'power_now', '97000',723 'voltage_now', '3970000'], [])724 self.start_daemon()725 devs = self.proxy.EnumerateDevices()726 self.assertEqual(len(devs), 1)727 batc_up = devs[0]728 self.assertEqual(self.get_dbus_dev_property(batc_up, 'Percentage'), 100.0)729 self.testbed.set_attribute(batc, 'energy_now', '2774500')730 time.sleep(5)731 self.assertEqual(int(self.get_dbus_dev_property(batc_up, 'Percentage')), 50)732 self.stop_daemon()733 #734 # libupower-glib tests (through introspection)735 #736 def test_lib_daemon_properties(self):737 '''library GI: daemon properties'''738 self.start_daemon()739 client = UPowerGlib.Client.new()740 self.assertRegex(client.get_daemon_version(), '^[0-9.]+$')741 self.assertIn(client.get_lid_is_present(), [False, True])742 self.assertIn(client.get_lid_is_closed(), [False, True])743 self.assertEqual(client.get_on_battery(), False)744 # FIXME: When we have a fake logind745 # self.assertEqual(client.get_critical_action(), 'PowerOff')746 #747 # Helper methods748 #749 @classmethod750 def _props_to_str(cls, properties):751 '''Convert a properties dictionary to uevent text representation.'''752 prop_str = ''...
start_daemons.py
Source:start_daemons.py
2import os3POWERSHELL = r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe'4IPFS_PATH_1 = 'C:\\Users\\david\\.ipfs1'5IPFS_PATH_2 = 'C:\\Users\\david\\.ipfs2'6def start_daemon(ipfs_path):7 os.environ["IPFS_PATH"] = ipfs_path8 subprocess.Popen([POWERSHELL, '-ExecutionPolicy', 'Unrestricted', 'ipfs daemon'])9def kill_daemons():10 os.system("taskkill /IM ipfs.exe /F")11start_daemon(IPFS_PATH_1)12start_daemon(IPFS_PATH_2)13input()...
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!!