1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import optparse
6import os
7import platform
8import shutil
9import subprocess
10import sys
11import tempfile
12import unittest
13import zipfile
14
15from telemetry.util import cloud_storage
16from telemetry.util import find_dependencies
17
18
19class FindDependenciesTest(unittest.TestCase):
20  @unittest.skipUnless(
21      cloud_storage.SupportsProdaccess(
22          os.path.realpath(cloud_storage.FindGsutil())),
23      'Could not find a depot_tools installation with gsutil.')
24  def testGsutil(self):
25    parser = optparse.OptionParser()
26    find_dependencies.FindDependenciesCommand.AddCommandLineArgs(parser)
27    options, _ = parser.parse_args([])
28
29    try:
30      temp_dir = tempfile.mkdtemp()
31      zip_path = os.path.join(temp_dir, 'gsutil.zip')
32      options.zip = zip_path
33
34      find_dependencies.ZipDependencies([], set(), options)
35
36      if platform.system() == 'Windows':
37        with zipfile.ZipFile(zip_path, 'r') as zip_file:
38          zip_file.extractall(temp_dir)
39      else:
40        # Use unzip instead of Python zipfile to preserve file permissions.
41        with open(os.devnull, 'w') as dev_null:
42          subprocess.call(['unzip', zip_path], cwd=temp_dir, stdout=dev_null)
43      third_party_path = os.path.join(temp_dir, 'telemetry', 'src', 'tools',
44                                      'telemetry', 'third_party')
45      # __init__.py is in Chromium src, but we didn't include any repo files.
46      open(os.path.join(third_party_path, '__init__.py'), 'a').close()
47
48      gsutil_path = os.path.join(third_party_path, 'gsutil', 'gsutil')
49      self.assertTrue(os.access(gsutil_path, os.X_OK))
50
51      with open(os.devnull, 'w') as dev_null:
52        # gsutil with no args should print usage and exit with exit code 0.
53        gsutil_command = [sys.executable, gsutil_path]
54        self.assertEqual(subprocess.call(gsutil_command, stdout=dev_null), 0)
55
56        # gsutil config should wait for the user and not exit with exit code 1.
57        #gsutil_command = [sys.executable, gsutil_path, 'config',
58        #                  '-o', os.path.join(temp_dir, 'config_file')]
59        #gsutil_process = subprocess.Popen(gsutil_command, stdout=dev_null)
60        #try:
61        #  util.WaitFor(gsutil_process.poll, timeout=0.5)
62        #  self.assertEqual(gsutil_process.returncode, 0,
63        #                   msg='gsutil config failed.')
64        #except util.TimeoutException:
65        #  gsutil_process.terminate()
66        #  gsutil_process.wait()
67    finally:
68      shutil.rmtree(temp_dir)
69