def testGetRemotePortAndIsHTTPServerRunningOnPort(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) # Create local server. sock = socket.socket() sock.bind(('', 0)) port = sock.getsockname()[1] sock.listen(0) # Get remote port and ensure that it was unused. remote_port = cri.GetRemotePort() self.assertFalse(cri.IsHTTPServerRunningOnPort(remote_port)) # Forward local server's port to remote device's remote_port. forwarder = cros_browser_backend.SSHForwarder( cri, 'R', (remote_port, port)) # At this point, remote device should be able to connect to local server. self.assertTrue(cri.IsHTTPServerRunningOnPort(remote_port)) # Next remote port shouldn't be the same as remote_port, since remote_port # is now in use. self.assertTrue(cri.GetRemotePort() != remote_port) # Close forwarder and local server ports. forwarder.Close() sock.close() # Device should no longer be able to connect to remote_port since it is no # longer in use. self.assertFalse(cri.IsHTTPServerRunningOnPort(remote_port))
def testGetFileContents(self): # pylint: disable=R0201 remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) hosts = cri.GetFileContents('/etc/hosts') assert hosts.startswith('# /etc/hosts')
def testIsServiceRunning(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) self.assertTrue(cri.IsServiceRunning('openssh-server'))
def testGetFileContentsForSomethingThatDoesntExist(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) self.assertRaises( OSError, lambda: cri.GetFileContents('/tmp/209fuslfskjf/dfsfsf'))
def testExists(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) self.assertTrue(cri.FileExistsOnDevice('/proc/cpuinfo')) self.assertTrue(cri.FileExistsOnDevice('/etc/passwd')) self.assertFalse(cri.FileExistsOnDevice('/etc/sdlfsdjflskfjsflj'))
def testPushContents(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) cri.GetCmdOutput(['rm', '-rf', '/tmp/testPushContents']) cri.PushContents('hello world', '/tmp/testPushContents') contents = cri.GetFileContents('/tmp/testPushContents') self.assertEquals(contents, 'hello world')
def testDeviceSideProcessFailureToLaunch(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) def WillFail(): dsp = cros_interface.DeviceSideProcess( cri, ['sfsdfskjflwejfweoij']) dsp.Close() self.assertRaises(OSError, WillFail)
def testListProcesses(self): # pylint: disable=R0201 remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) with cros_interface.DeviceSideProcess( cri, ['sleep', '11']): procs = cri.ListProcesses() sleeps = [x for x in procs if x[1] == 'sleep 11'] assert len(sleeps) == 1
def _RebuildCredentials(self): credentials = {} if self._credentials_path == None: pass elif os.path.exists(self._credentials_path): with open(self._credentials_path, 'r') as f: credentials = json.loads(f.read()) # TODO(nduca): use system keychain, if possible. homedir_credentials_path = os.path.expanduser( '~/.telemetry-credentials') homedir_credentials = {} if (not options_for_unittests.GetCopy() and os.path.exists(homedir_credentials_path)): logging.info( "Found ~/.telemetry-credentials. Its contents will be used " "when no other credentials can be found.") with open(homedir_credentials_path, 'r') as f: homedir_credentials = json.loads(f.read()) self._credentials = {} all_keys = set(credentials.keys()).union( homedir_credentials.keys()).union(self._extra_credentials.keys()) for k in all_keys: if k in credentials: self._credentials[k] = credentials[k] if k in homedir_credentials: logging.info( "Will use ~/.telemetry-credentials for %s logins." % k) self._credentials[k] = homedir_credentials[k] if k in self._extra_credentials: self._credentials[k] = self._extra_credentials[k]
def testDeviceSideProcessCloseDoesClose(self): remote = options_for_unittests.GetCopy().cros_remote cri = cros_interface.CrOSInterface( remote, options_for_unittests.GetCopy().cros_ssh_identity) with cros_interface.DeviceSideProcess( cri, ['sleep', '111']) as dsp: procs = cri.ListProcesses() sleeps = [x for x in procs if x[1] == 'sleep 111'] assert dsp.IsAlive() procs = cri.ListProcesses() sleeps = [x for x in procs if x[1] == 'sleep 111'] self.assertEquals(len(sleeps), 0)
def testVersionDetection(self): options = options_for_unittests.GetCopy() browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: # pylint: disable=W0212 self.assertGreater(b._backend._inspector_protocol_version, 0) self.assertGreater(b._backend._chrome_branch_number, 0) self.assertGreater(b._backend._webkit_base_revision, 0)
def testBrowserCreation(self): options = options_for_unittests.GetCopy() browser_to_create = browser_finder.FindBrowser(options) if not browser_to_create: raise Exception('No browser found, cannot continue test.') with browser_to_create.Create() as b: self.assertEquals(1, len(b.tabs)) # Different browsers boot up to different things assert b.tabs[0].url
def testBasicHosting(self): unittest_data_dir = os.path.join(os.path.dirname(__file__), '..', 'unittest_data') options = options_for_unittests.GetCopy() browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: b.SetHTTPServerDirectory(unittest_data_dir) t = b.tabs[0] t.page.Navigate(b.http_server.UrlOf('/blank.html')) t.WaitForDocumentReadyStateToBeComplete() x = t.runtime.Evaluate('document.body.innerHTML') x = x.strip() self.assertEquals(x, 'Hello world')
def testRealLoginIfPossible(self): credentials_path = os.path.join( os.path.dirname(__file__), '..', '..', 'perf', 'data', 'credentials.json') if not os.path.exists(credentials_path): return options = options_for_unittests.GetCopy() with browser_finder.FindBrowser(options).Create() as b: b.credentials.credentials_path = credentials_path if not b.credentials.CanLogin('google'): return ret = b.credentials.LoginNeeded(b.tabs[0], 'google') self.assertTrue(ret)
def testCommandLineOverriding(self): # This test starts the browser with --enable-benchmarking, which should # create a chrome.Interval namespace. This tests whether the command line is # being set. options = options_for_unittests.GetCopy() flag1 = '--user-agent=telemetry' options.extra_browser_args.append(flag1) browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: t = b.tabs[0] t.page.Navigate('http://www.google.com/') t.WaitForDocumentReadyStateToBeInteractiveOrBetter() self.assertEquals(t.runtime.Evaluate('navigator.userAgent'), 'telemetry')
def testNewCloseTab(self): options = options_for_unittests.GetCopy() browser_to_create = browser_finder.FindBrowser(options) with browser_to_create.Create() as b: existing_tab = b.tabs[0] self.assertEquals(1, len(b.tabs)) existing_tab_url = existing_tab.url new_tab = b.tabs.New() self.assertEquals(2, len(b.tabs)) self.assertEquals(existing_tab.url, existing_tab_url) self.assertEquals(new_tab.url, 'about:blank') new_tab.Close() self.assertEquals(1, len(b.tabs)) self.assertEquals(existing_tab.url, existing_tab_url)
def RunBenchmark(self, benchmark, ps): """Runs a benchmark against a pageset, returning the rows its outputs.""" options = options_for_unittests.GetCopy() assert options temp_parser = options.CreateParser() benchmark.AddCommandLineOptions(temp_parser) defaults = temp_parser.get_default_values() for k, v in defaults.__dict__.items(): if hasattr(options, k): continue setattr(options, k, v) benchmark.CustomizeBrowserOptions(options) self.CustomizeOptionsForTest(options) possible_browser = browser_finder.FindBrowser(options) results = multi_page_benchmark.BenchmarkResults() with page_runner.PageRunner(ps) as runner: runner.Run(options, possible_browser, benchmark, results) return results
def setUp(self): self._browser = None self._tab = None options = options_for_unittests.GetCopy() self.CustomizeBrowserOptions(options) if self._extra_browser_args: for arg in self._extra_browser_args: options.extra_browser_args.append(arg) browser_to_create = browser_finder.FindBrowser(options) if not browser_to_create: raise Exception('No browser found, cannot continue test.') try: self._browser = browser_to_create.Create() self._tab = self._browser.tabs[0] except: self.tearDown() raise
def setUp(self): self._options = options_for_unittests.GetCopy() self._options.wpr_mode = wpr_modes.WPR_OFF
def setUp(self): super(SkPicturePrinterUnitTest, self).setUp() self._outdir = tempfile.mkdtemp() self._options = options_for_unittests.GetCopy() self._options.outdir = self._outdir