def test_1012_shell(self): try: self.device.shell(self.test_config["shell_cmd"]) except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during shell command: " + traceback_message)
def test_5800_sync_and_off_modes_supported(self): """Verify that the required modes are supported.""" supported_modes = self.device.switch_power.supported_modes result = all(mode in supported_modes for mode in REQUIRED_MODES) if not result: asserts.fail( "{} Required switch_power modes not found. Required: {}, found: {}" .format(self.device.name, REQUIRED_MODES, supported_modes))
def test_1002_get_serial_number(self): try: asserts.assert_true(self.device.serial_number is not None, "serial number should not be None") except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during get serial number: " + traceback_message)
def _verify_firmware_version(self): try: asserts.assert_true(self.device.firmware_version is not None, "firmware version should not be None") except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during get firmware version: " + traceback_message)
def test_2002_get_device_prop_can_execute(self): """Check get_device_prop can execute.""" self.device.close() try: self.manager.get_device_prop(self.device.name) except Exception as err: asserts.fail( "manager.get_device_prop failed, Err: {!r}. Check gdm.log for more info" .format(err))
def test_1003_get_ftdi_serial_number(self): if hasattr(type(self.device), "ftdi_serial_number"): try: asserts.assert_true(self.device.ftdi_serial_number is not None, "ftdi serial number should not be None") except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during get ftdi serial number: " + traceback_message)
def _test_100_disable(self): """Test case for disable()""" try: self.device.ethernet_switch_api.disable() asserts.assert_false(self.device.ethernet_switch_api.status, "status should be False") except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during unlock: " + traceback_message)
def test_1005_get_build_date(self): if hasattr(type(self.device), "build_date"): try: asserts.assert_true(self.device.build_date is not None, "build date should not be None") except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during get build date: " + traceback_message)
def test_1009_reset(self): time.sleep(LOG_CATCH_UP_DELAY) start_time = datetime.datetime.now() try: self.device.reset() except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during reset: " + traceback_message) self._verify_no_unexpected_reboots(start_time)
def test_100_parallel_upgrade(self): """Upgrade devices in parallel. """ upgrade_params_dicts = parallel_funcs.get_parameter_dicts( self.devices, self.user_settings, 'upgrade') try: parallel_funcs.upgrade(self.devices, upgrade_params_dicts, self.logger) except Exception as err: asserts.fail("Error(s): {!r}".format(err.message))
def test_1011_send_and_expect(self): try: self.device.switchboard.send_and_expect( self.test_config["shell_cmd"], self.test_config["expect"], timeout=10) except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during send and expect: " + traceback_message)
def test_3301_shell_ssh_capability_with_return_code(self): """Test case for shell_ssh capability to verify command completes successfully.""" try: response, code = self.device.shell_capability.shell( self.test_config["shell_cmd"], include_return_code=True) asserts.assert_equal(code, SUCCESS_RETURN_CODE) except Exception: traceback_message = traceback.format_exc() asserts.fail( "Error happened during call to shell_ssh capability shell method " + traceback_message)
def wait_for_device(self): # wait for devices that use adb to reconnect deadline = time.time() + RECONNECT_TIMEOUT while time.time() < deadline: if self.device.name in self.manager.get_connected_devices(category="all"): break self.logger.info("{} is not connected. Waiting for it to reconnect..." .format(self.device.name)) time.sleep(1) else: asserts.fail("{} failed to reconnect in {}s" .format(self.device.name, RECONNECT_TIMEOUT))
def test_0000_upgrade_to_latest_verified_forced(self): """Load latest verified build before testing.""" time.sleep(LOG_CATCH_UP_DELAY) start_time = datetime.datetime.now() try: self.device.upgrade(forced_upgrade=True) except Exception: asserts.fail("Error happened during upgrade: {}".format( traceback.format_exc())) self._verify_no_unexpected_reboots(start_time)
def test_2002_get_prop_can_execute(self): """Check that FireManager.get_prop() can execute.""" device_name = self.device.name self.device.close() fire_manager_instance = fire_manager.FireManager() try: fire_manager_instance.get_prop(device_name) except Exception as err: asserts.fail( "FireManager.get_prop() failed. Error: {!r}".format(err)) finally: fire_manager_instance.close()
def test_1115_test_expect_with_bogus_logline(self): self.logger.info("testing expect with bogus log line") phrase = "garblygookand more" try: response = self.device.switchboard.expect([phrase], timeout=2) if not response.timedout: asserts.fail( "Response should time out. Instead got: {}".format( response)) except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during expect with bogus logline: " + traceback_message)
def _verify_expect_log(self): self.logger.info("Expecting logline {!r}".format( self.test_config["known_logline"])) try: res = self.device.switchboard.expect( [self.test_config["known_logline"]], timeout=30) except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during expect: " + traceback_message) asserts.assert_true(res.index is not None, "Expect returned None") asserts.assert_true( res.index == 0, "Phrase {} should have been found in the log lines".format( self.test_config["known_logline"]))
def logic(self, setting, arg, special_arg=None): asserts.assert_true(setting in itrs, ( "%s is not in acceptable settings range %s") % (setting, itrs)) asserts.assert_true(arg == static_arg, "Expected %s, got %s" % (static_arg, arg)) asserts.assert_true(arg == static_arg, "Expected %s, got %s" % (static_kwarg, special_arg)) if setting == "pass": asserts.explicit_pass( MSG_EXPECTED_EXCEPTION, extras=MOCK_EXTRA) elif setting == "fail": asserts.fail(MSG_EXPECTED_EXCEPTION, extras=MOCK_EXTRA) elif setting == "skip": asserts.skip(MSG_EXPECTED_EXCEPTION, extras=MOCK_EXTRA)
def test_metadata_pts_missing_name(self): @metadata(pts_test_id="A/B/C") def simple_pass_test(arg): pass try: simple_pass_test(1) except signals.TestFailure: pass except Exception as e: asserts.fail("should only raise signals.TestFailure, " "but raised %s with msg %s instead" % (e.__class__.__name__, str(e))) else: asserts.fail("missing pts_test_name should not work")
def test_metadata_empty_no_function_call(self): @metadata def simple_pass_test(arg): pass try: simple_pass_test(1) except signals.TestFailure: pass except Exception as e: asserts.fail("@metadata should only raise signals.TestFailure, " "but raised %s with msg %s instead" % (e.__class__.__name__, str(e))) else: asserts.fail("@metadata should not work")
def test_1008_reboot(self): """Reboot automatically verifies the reboot happens correctly. Note: Verifies device is_connected afer reboot. """ time.sleep(LOG_CATCH_UP_DELAY) try: self.device.reboot() except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during reboot: " + traceback_message) if hasattr(self.device, "device_is_connected"): self.device.device_is_connected() else: self.device.check_device_connected()
def DiscoverBluetoothDeviceByName(device, name): """Start a bluetooth scan and find the device that has the bluetooth name. Args: device: AndroidDevice. Device to start bluetooth scan. name: the bluetooth name looking for. Returns: Dict represents a bluetooth device if found. """ discovered_devices = device.android.btDiscoverAndGetResults() for device in discovered_devices: if name == device['Name']: device_discovered = device return device_discovered asserts.fail('Failed to discover the target device %s over Bluetooth.' % name)
def test_metadata_pts_test_id_and_description(self): @metadata(pts_test_id="A/B/C", pts_test_name="Hello world") def simple_pass_test(arg): pass try: simple_pass_test(1) except signals.TestPass as e: asserts.assert_true("pts_test_id" in e.extras, msg=("pts_test_id not in extra: %s" % str(e.extras))) asserts.assert_equal(e.extras["pts_test_id"], "A/B/C") asserts.assert_true("pts_test_name" in e.extras, msg=("pts_test_name not in extra: %s" % str(e.extras))) asserts.assert_equal(e.extras["pts_test_name"], "Hello world") else: asserts.fail("Must throw an exception using @metadata decorator")
def test_5805_set_and_get_all_ports_mode(self): """Verify that setting and getting all ports works as expected.""" self.device.switch_power.set_all_ports_mode("off") time.sleep(5) mode_list = self.device.switch_power.get_all_ports_mode() valid_result = ['', 'off'] result = all(mode in valid_result for mode in mode_list) if not result: asserts.fail( "{} All ports were expected to be 'off', found: {}".format( self.device.name, mode_list)) self.device.switch_power.set_all_ports_mode("sync") time.sleep(5) mode_list = self.device.switch_power.get_all_ports_mode() valid_result = ['', 'sync'] result = all(mode in valid_result for mode in mode_list) if not result: asserts.fail( "{} All ports were expected to be 'sync', found: {}".format( self.device.name, mode_list))
def test_1008_reboot_and_expect_known_logline(self): """Reboot automatically verifies the reboot happens correctly. Note: Verifies logging, firmware_version, boot_up_log available afterwards. Verifies known logline also works. """ time.sleep(LOG_CATCH_UP_DELAY) start_time = datetime.datetime.now() try: self.device.reboot() except Exception: traceback_message = traceback.format_exc() asserts.fail("Error happened during reboot: " + traceback_message) self._verify_logging() self._verify_firmware_version() self._verify_expect_log() # The last bootup event may not have been logged by the logger process yet, so wait time.sleep(3) self._verify_boot_up_log(start_time) self._verify_no_unexpected_reboots(start_time)
def AssertAsyncSuccess(callback): """Wrapping the result of an asynchronous Rpc for convenience. This wrapper handles the event posted by the corresponding `FutureCallback` object signalling whether the async operation was successful or not with `onSuccess` and `onFailure` callbacks. Args: callback: CallbackHandler, the handler that's used to poll events triggered by the async Rpc call. Returns: dictionary, the event that carries the onSuccess signal. """ try: status_event = callback.waitAndGet('onSuccess', 30) except callback_handler.TimeoutError: try: failure_event = callback.waitAndGet('onFailure', 30) asserts.fail('Operation failed: %s' % failure_event.data) except callback_handler.TimeoutError: asserts.fail('Timed out waiting for status event.') return status_event
def test_metadata_test_with_exception_stacktrace(self): @metadata(pts_test_id="A/B/C", pts_test_name="Hello world") def simple_fail_test(failure_argument): raise ValueError(failure_argument) try: simple_fail_test("BEEFBEEF") except signals.TestError as e: asserts.assert_true("pts_test_id" in e.extras, msg=("pts_test_id not in extra: %s" % str(e.extras))) asserts.assert_equal(e.extras["pts_test_id"], "A/B/C") asserts.assert_true("pts_test_name" in e.extras, msg=("pts_test_name not in extra: %s" % str(e.extras))) asserts.assert_equal(e.extras["pts_test_name"], "Hello world") trace_str = traceback.format_exc() asserts.assert_true( "raise ValueError(failure_argument)" in trace_str, msg="Failed test method not in error stack trace: %s" % trace_str) else: asserts.fail("Must throw an exception using @metadata decorator")
def setup_test(self): """Creates the first Gazoo device listed in testbed config. Raises: Fails test if unable to create each device. """ super().setup_test() here = os.path.dirname(os.path.abspath(__file__)) filters_dir_path = os.path.join(here, 'filters') if not os.path.exists(filters_dir_path): here = os.path.join(here, 'functional_tests') full_path_filter_list = [] for filter_path in self.test_config["custom_filters"]: full_path_filter_list.append(os.path.join(here, filter_path)) for i in range(2): try: self.devices = [self.manager.create_device(self.gazoo_device_configs[0]["id"], log_name_prefix=self._get_log_suffix(), filters=full_path_filter_list)] break except signals.TestFailure as err: if "DeviceNotConnectedError" in str(err): if i == 0: self.logger.warning( "Device creation failed attempt 1/2. " "Sleeping 30 seconds to allow device to fully boot up.") time.sleep(30) else: msg = "Device creation failed in two tries. Aborting all tests." self.logger.error(msg) asserts.abort_all(msg) else: raise err if not self.devices: asserts.fail( "Infrastructure Issue. No devices created for functional test. Is testbed empty?") self.device = self.devices[0] if self.device is None: asserts.fail("Infrastructure Issue. Device creation returned None.") try: if hasattr(self.device, "firmware_version"): version = self.device.firmware_version self.logger.info("DUT: {} Firmware version: {}".format(self.device.name, version)) else: # Auxiliary devices may not have support for firmware_version. self.device.device_is_connected() self.logger.info("DUT: {} device is connected.".format(self.device.name)) except (errors.GazooDeviceError, errors.DeviceNotConnectedError) as e: asserts.fail("Infrastructure Issue. Failed to communicate with device {}. " "Error: {!r}.\n{}" .format(self.device.name, e, traceback.format_exc()))
def fail(*args, **kwargs): asserts.fail("@metadata must be used in functional form such " "as @metadta(key=value)")
def fail(*args, **kwargs): asserts.fail(msg)