Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1321)

Unified Diff: install_test/install_test.py

Issue 10384104: Chrome updater test framework (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/test/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: install_test/install_test.py
===================================================================
--- install_test/install_test.py (revision 0)
+++ install_test/install_test.py (revision 0)
@@ -0,0 +1,366 @@
+#!/usr/bin/env python
Nirnimesh 2012/07/04 00:39:19 Why is the dir called 'install_test' if the intent
nkang 2012/07/13 23:50:50 I asked Ken about this on Tuesday. His said that i
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import httplib
+import logging
+import optparse
+import os
+import platform
+import shutil
+import sys
+import tempfile
+import unittest
+import urllib
+import urlparse
+
+import chrome_checkout
+import chrome_installer
+from py_unittest_util import GTestTextTestRunner
+
+sys.path.append(os.path.join(os.path.pardir, 'pyautolib'))
+
+# This import should go after sys.path is set appropriately.
+from fetch_prebuilt_pyauto import FetchPrebuilt
+
+_DOWNLOAD_DIR = []
Nirnimesh 2012/07/04 00:39:19 Do not use globals please
nkang 2012/07/13 23:50:50 The naming scheme being used previously for downlo
+_INSTALLERS = []
+_B_FIRST_TIME = True
+_OPTIONS = None
+
+
+class InstallTest(unittest.TestCase):
+ """Test fixture for tests involving installing/updating Chrome.
+
+ Provides an interface to install or update chrome from within a testcase, and
+ allows users to run pyauto tests using the installed version. User and system
+ level installations are supported, and either one can be used for running the
+ pyauto tests. Pyautolib files are downloaded at runtime and a PyUITest object
+ is created when Chrome is installed or updated. Users can utilize that object
+ to run updater tests. All Updater tests should derive from this class.
+ """
+
+ _build_iterator = None
+ _current_build = ''
+ _current_location = None
+ _dir_prefix = '__CHRBLD__'
+ _dir_iterator = None
+ _installer = None
+ _pyauto = None
+
+ def __init__(self, methodName='runTest'):
+ global _B_FIRST_TIME
+ global _DOWNLOAD_DIR
+ unittest.TestCase.__init__(self, methodName)
+ self._platform = self._GetPlatform()
+ self._SetUp()
+ self._InitSourceFolderNames()
+ if _B_FIRST_TIME:
+ if self._installation.GetChromeInstallationType():
+ self._DeleteBuild()
+ for build in self._builds:
+ if not self._DownloadDeps(build):
+ raise RuntimeError('Could not download dependencies.')
+ _B_FIRST_TIME = False
+ self._installer_iter = iter(_INSTALLERS)
+ self._dir_iterator = iter(_DOWNLOAD_DIR)
+ self._current_location = next(self._dir_iterator, None)
+
+ def _SetUp(self):
Nirnimesh 2012/07/04 00:39:19 Choose a different name, not to confuse with setUp
nkang 2012/07/13 23:50:50 I chose the name _SetUp because PyUITest also has
+ """Sets test parameters."""
+ global _OPTIONS
+ self._url = _OPTIONS.url
+ self._builds = _OPTIONS.builds and _OPTIONS.builds.split(',') or []
+ if not self._url or not self._builds:
+ raise RuntimeError('Please specify a valid URL and two Chrome builds.')
+ self._builds.sort()
+ self._build_iterator = iter(self._builds)
+ self._current_build = next(self._build_iterator, None)
+ self._url = self._url.endswith('/') and self._url or self._url + '/'
+ self._dir = os.path.isdir(_OPTIONS.dir) and _OPTIONS.dir or os.getcwd()
+ self._options = (_OPTIONS.options and _OPTIONS.options.replace(',', ' ')
+ or '')
+ self._install_type = ('system-level' in self._options and
+ chrome_installer.InstallationType.SYSTEM or
+ chrome_installer.InstallationType.USER)
+ self._installation = chrome_installer.Installation(self._install_type)
+
+ def _InitSourceFolderNames(self):
+ """Sets Chrome source folder names.
+
+ The folder names are used after checkout to confirm their existence.
+ """
+ self._source_dirs = [os.path.join('src', 'chrome', 'test', 'functional'),
Nirnimesh 2012/07/04 00:39:19 This is a maintenance burden. Please do not do thi
nkang 2012/07/13 23:50:50 Got rid of this method. Its main purpose was to ma
+ os.path.join('src', 'chrome', 'test', 'pyautolib'),
+ os.path.join('src', 'third_party', 'pyftpdlib'),
+ os.path.join('src', 'third_party', 'simplejson'),
+ os.path.join('src', 'third_party', 'tlslite'),
+ os.path.join('src', 'net', 'tools', 'testserver'),
+ os.path.join('src', 'third_party', 'webdriver',
+ 'pylib', 'selenium')]
+
+ def setUp(self):
+ """Called before each unittest to prepare the test fixture."""
+ self.InstallBuild()
+ self.failIf(self._pyauto == None)
+
+ def tearDown(self):
+ """Called at the end of each unittest to do any test related cleanup."""
+ self._Refresh()
+ self._DeleteBuild()
+
+ def _GetPlatform(self):
+ """Returns the platform name."""
+ return ({'Windows': 'win',
+ 'Darwin': 'mac',
+ 'Linux': 'linux'}).get(platform.system())
+
+ def _Refresh(self):
+ """Deletes the PyUITest object and clears the modules registry."""
+ if self._pyauto:
+ del(self._pyauto)
+ for module in ['pyauto', 'pyautolib', '_pyautolib']:
+ if module in sys.modules:
+ sys.modules.pop(module)
+
+ def _RemovePaths(self):
+ """Restores the sys.path variable to its original state."""
+ sys.path = list(frozenset(sys.path))
+ if self._current_location in sys.path:
+ sys.path.remove(self._current_location)
+ if os.path.join(self._current_location, 'pyautolib') in sys.path:
+ sys.path.remove(os.path.join(self._current_location, 'pyautolib'))
+
+ def _Install(self):
+ """Helper method that installs Chrome and creates a PyUITest object."""
+ self._pyauto = None
+ installer_path = next(self._installer_iter, None)
+ if not installer_path:
+ raise RuntimeError('No more builds left to install.')
+ ret = chrome_installer.Install(installer_path, self._install_type,
+ self._current_build, self._options)
+ if not ret:
+ raise RuntimeError('Chrome installation for build %s failed.' %
+ self._current_build)
+ try:
+ import pyauto
+ self._pyauto = pyauto.PyUITest(methodName='runTest',
+ browser_path=os.path.dirname(
+ self._installation.GetChromeExePath()))
+ self._pyauto.suite_holder = pyauto.PyUITestSuite(['test.py'])
+ self._pyauto.setUp()
+ except ImportError, err:
+ logging.log(logging.ERROR, err)
+ self._pyauto = None
+
+ def InstallBuild(self):
+ self._RemovePaths()
+ sys.path.insert(0, self._current_location)
+ sys.path.insert(1, os.path.join(self._current_location, 'pyautolib'))
+ self._Install()
+
+ def _Update(self):
+ """Helper method for updating Chrome."""
+ self._RemovePaths()
+ self._current_location = next(self._dir_iterator, None)
+ assert (self._current_location)
+ sys.path.insert(0, self._current_location)
+ sys.path.insert(1, os.path.join(self._current_location, 'pyautolib'))
+ build = next(self._build_iterator, None)
+ if not build:
+ raise RuntimeError('No more builds left to install. Following builds '
+ 'have already been installed: %r' % self._builds)
+ self._current_build = build
+ self._Install()
+
+ def UpdateBuild(self):
+ """Installs the second Chrome build specified in the command args."""
+ if self._pyauto:
+ self._pyauto.TearDown()
+ self._Refresh()
+ self._Update()
+
+ def _SrcFilesExist(self, root, items):
+ """Checks if specified files/folders exist at specified 'root' folder.
+
+ Args:
+ root: Parent folder where all the source directories reside.
+ items: List of files/folders to be verified for existence in the root.
+
+ Returns:
+ True, if all sub-folders exist in the root, otherwise False.
+ """
+ return all(map(lambda p: os.path.exists(p) and True or False,
+ [os.path.join(root, path) for path in items]))
+
+ def _CheckoutSourceFiles(self, build, location):
+ """Checks out source files associated with the current build.
+
+ Args:
+ build: Chrome release version number.
+ location: Destination where source files will be saved.
+ Returns:
+ Zero if successful, otherwise a negative value.
+ """
+ if(not os.path.isdir(location) or not
+ self._SrcFilesExist(location, self._source_dirs)):
+ chrome_checkout.CheckOut(build, location)
+ return self._SrcFilesExist(location, self._source_dirs)
+ else:
+ return True
+
+ def _DownloadDeps(self, build):
+ """Downloads Chrome build, source files, and chrome installer.
+
+ Args:
+ build: Chrome build number.
+ """
+ global _DOWNLOAD_DIR
+ ret = -1
+ url = '%s%s/%s' % (self._url, build, self._platform)
+ download_dir = os.path.join('%s', '%s%s') % (tempfile.gettempdir(),
+ self._dir_prefix, build)
+ _DOWNLOAD_DIR.append(download_dir)
+ if not os.path.isdir(download_dir):
+ try:
+ os.mkdir(download_dir)
+ except(OSError, IOError), err:
+ logging.log(logging.ERROR, err)
+ return False
+ if not self._SrcFilesExist(download_dir, ['pyautolib.py',
+ '_pyautolib.pyd']):
+ fpb = FetchPrebuilt(url, download_dir, self._platform)
+ if fpb.DoesUrlExist(url):
+ ret = fpb.Run()
+ else:
+ ret = 0
+ if ret == 0:
+ if self._Download('mini_installer.exe', url, download_dir):
+ _INSTALLERS.append(os.path.join(download_dir, 'mini_installer.exe'))
+ else:
+ return False
+ return self._CheckoutSourceFiles(build, download_dir)
+ return False
+
+ def _DeleteBuild(self):
+ """Uninstalls Chrome."""
+ global _DOWNLOAD_DIR
+ global _B_FIRST_TIME
+ ret = self._installation.UninstallChrome()
+ if not _B_FIRST_TIME:
+ if ret:
+ # Reposition iterators to the beginning of their respective lists.
+ self._build_iterator = iter(self._builds)
+ self._dir_iterator = iter(_DOWNLOAD_DIR)
+ self._current_build = next(self._build_iterator, None)
+ self._current_location = next(self._dir_iterator, None)
+ self._RemovePaths()
+ return ret
+
+ def _DeleteDepFiles(self):
+ """Deletes Chrome related files that were downloaded for testing."""
+ global _DOWNLOAD_DIR
+ for path in _DOWNLOAD_DIR:
+ try:
+ shutil.rmtree(path)
+ except shutil.Error, err:
+ logging.log(logging.ERROR, err)
+ return -1
+ return 0
+
+ def _Download(self, dfile, url, dest=None):
+ """Downloads a file from the specified URL.
+
+ Args:
+ dfile: Name of the file to download.
+ url: URL where the file is located.
+ dest: Location where file will be downloaded. Default is CWD.
+
+ Returns:
+ Zero if successful, otherwise a non-zero value.
+ """
+ filename = ((dest and os.path.exists(dest)) and os.path.join(dest, dfile)
+ or os.path.join(tempfile.gettempdir(), dfile))
+ file_url = '%s/%s' % (url, dfile)
+ if not self._DoesUrlExist(file_url):
+ raise RuntimeError('Either the URL or the file name is invalid.')
+ try:
+ d = urllib.urlretrieve(file_url, filename)
+ except IOError, err:
+ raise err
+ return os.path.isfile(d[0])
+
+ def _DoesUrlExist(self, url):
+ """Checks if a URL exists.
+
+ Args:
+ url: URL to be verified.
+
+ Returns:
+ True if the URL exists, otherwise False.
+ """
+ parse = urlparse.urlparse(url)
+ if parse[0] == '' or parse[1] == '':
+ return False
+ try:
+ connection = httplib.HTTPConnection(parse.netloc)
+ connection.request('HEAD', parse.path)
+ response = connection.getresponse()
+ except (socket.error, socket.gaierror):
+ return False
+ finally:
+ connection.close()
+ if response.status == 302 or response.status == 301:
+ return self._DoesUrlExist(response.getheader('location'))
+ return response.status == 200
+
+
+class Main(object):
+ """Main program for running Updater tests."""
+
+ _tests_file = 'PYAUTO_TESTS'
+ _mod_path = sys.argv[0]
+ _pyauto_doc_url = 'http://dev.chromium.org/developers/testing/pyauto'
+
+ def __init__(self):
+ self._ParseArgs()
+ self._Run()
+
+ def _GetUnitTests(self):
+ """Returns a list of unittests from the calling script."""
+ mod_name = [os.path.splitext(os.path.basename(self._mod_path))[0]]
+ if os.path.dirname(self._mod_path) not in sys.path:
+ sys.path.append(os.path.dirname(self._mod_path))
+ return unittest.defaultTestLoader.loadTestsFromNames(mod_name)
+
+ def _Run(self):
+ """Runs the unit tests."""
+ tests = self._GetUnitTests()
+ result = GTestTextTestRunner(verbosity=1).run(tests)
+ del(tests)
+ if not result.wasSuccessful():
+ print >>sys.stderr, ('Tests can be disabled by editing %s. Ref: %s'
+ % (self._tests_file, self._pyauto_doc_url))
+ sys.exit(1)
+ else:
+ sys.exit(0)
+
+ def _ParseArgs(self):
+ """Parses command line arguments."""
+ global _OPTIONS
+ parser = optparse.OptionParser()
+ parser.add_option(
+ '-b', '--builds', type='string', default='', dest='builds',
+ help='Specifies the two (or more) builds needed for testing.')
+ parser.add_option(
+ '-u', '--url', type='string', default='', dest='url',
+ help='Specifies the chrome-master2 url, without the build number.')
+ parser.add_option(
+ '-d', '--dir', type='string', default=os.getcwd(),
+ help='Specifies directory where the installer will be downloaded.')
+ parser.add_option(
+ '-o', '--options', type='string', default='',
+ help='Specifies any additional Chrome options (i.e. --system-level).')
+ opts, args = parser.parse_args()
+ _OPTIONS = opts
Property changes on: install_test\install_test.py
___________________________________________________________________
Added: svn:eol-style
+ LF

Powered by Google App Engine
This is Rietveld 408576698