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

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, 5 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,384 @@
+#!/usr/bin/env python
+# 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 stat
+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
+
+_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_name = 'mini_installer.exe'
+ _pyauto = None
+ _installers = []
+ _download_dirs = []
+
+ def __init__(self, methodName='runTest'):
+ unittest.TestCase.__init__(self, methodName)
+ self._platform = self._GetPlatform()
+ self._Initialize()
+ if self._installation.GetChromeInstallationType():
+ self._DeleteBuild()
+ for build in self._builds:
+ if not self._DownloadDeps(build):
+ raise RuntimeError('Could not download dependencies.')
+ self._installer_iter = iter(self._installers)
+ self._dir_iterator = iter(self._download_dirs)
+
+ def _Initialize(self):
+ """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._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)
+ self._build_iterator = iter(self._builds)
+ self._current_build = next(self._build_iterator, None)
+
+ 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._current_location = next(self._dir_iterator, None)
+ 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.
+ """
+ try:
+ chrome_checkout.CheckOut(build, location)
+ return True
+ except AssertionError:
+ return False
+
+ def _FetchPrebuiltPyauto(self, url, location):
+ """Fetches the specified Chrome build.
+
+ Args:
+ url: URL where the build is located.
+ location: Location where the build will be downloaded.
+
+ Returns:
+ True if successful, otherwise False.
+ """
+ fetch_build = FetchPrebuilt(url, location, self._platform)
+ if fetch_build.DoesUrlExist(url):
+ return fetch_build.Run() == 0
+ else:
+ return False
+
+ def _DownloadInstaller(self, url, location):
+ """Downloads the Chrome installer.
+
+ Args:
+ url: URL where the installer is located.
+ location: Location where installer will be downloaded.
+
+ Returns:
+ True if successful, otherwise False.
+ """
+ try:
+ self._Download(self._installer_name, url, location)
+ return True
+ except (IOError, RuntimeError):
+ return False
+
+ def _DownloadDeps(self, build):
+ """Downloads Chrome build, source files, and Chrome installer.
+
+ Args:
+ build: Chrome release build number.
+
+ Returns:
+ True if successful, otherwise False.
+ """
+ url = '%s%s/%s' % (self._url, build, self._platform)
+ location = os.path.join('%s', '%s%s') % (tempfile.gettempdir(),
+ self._dir_prefix, build)
+ if os.path.isdir(location):
+ self._download_dirs.append(location)
+ self._installers.append(os.path.join(location, self._installer_name))
+ return True
+ else:
+ # Make temp dir. and download/checkout everything there first.
+ tmpdir = tempfile.mkdtemp()
+ # Check out Chrome source files.
+ if self._CheckoutSourceFiles(build, tmpdir):
+ # Fetch the Chrome build.
+ if self._FetchPrebuiltPyauto(url, tmpdir):
+ # Download the Chrome installer.
+ if self._DownloadInstaller(url, tmpdir):
+ try:
+ # This is a workaround because rename was causing problems.
+ shutil.copytree(tmpdir, location)
+ # Callback is there because hidden svn files are read-only, so
+ # we need to change their permissions on the fly to delete them.
+ shutil.rmtree(tmpdir, onerror=self._OnError)
+ self._download_dirs.append(location)
+ self._installers.append(os.path.join(location,
+ self._installer_name))
+ return True
+ except(OSError, IOError), err:
+ return False
+ return False
+
+ def _OnError(self, func, path, exc_info):
+ """Callback for shutil.rmtree."""
+ if not os.access(path, os.W_OK):
+ os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
+ func(path)
+
+ def _DeleteBuild(self):
+ """Uninstalls Chrome."""
+ ret = self._installation.UninstallChrome()
+ if ret:
+ self._build_iterator = iter(self._builds)
+ self._dir_iterator = iter(self._download_dirs)
+ 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."""
+ for path in self._download_dirs:
+ try:
+ shutil.rmtree(path, onerror=self._OnError)
+ 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