def setUp(self):
   super(GerritChangesTest, self).setUp()
   self.enabled = self.FAKE_REPOS.set_up_git()
   self.options = BaseGitWrapperTestCase.OptionsObject()
   self.url = self.git_base + 'repo_1'
   self.mirror = None
   mock.patch('sys.stdout').start()
   self.addCleanup(mock.patch.stopall)
Ejemplo n.º 2
0
    def test_Popen_defaults(self, mockPopen):
        with mock.patch('sys.platform', 'win32'):
            subprocess2.Popen(['foo'], a=True)
            mockPopen.assert_called_with(['foo'], a=True, shell=True)

        with mock.patch('sys.platform', 'non-win32'):
            subprocess2.Popen(['foo'], a=True)
            mockPopen.assert_called_with(['foo'], a=True, shell=False)
def _mockLocalAuth(account_id, secret, rpc_port):
  mock_luci_context = {
      'local_auth': {
          'default_account_id': account_id,
          'secret': secret,
          'rpc_port': rpc_port,
      }
  }
  mock.patch('auth._load_luci_context', return_value=mock_luci_context).start()
  mock.patch('os.environ', {'LUCI_CONTEXT': 'default/test/path'}).start()
Ejemplo n.º 4
0
 def setUp(self):
     mock.patch('gclient_utils.FileRead',
                return_value=self._GITCOOKIES).start()
     mock.patch('os.getenv', return_value={}).start()
     mock.patch('os.environ', {'HOME': '$HOME'}).start()
     mock.patch('os.path.exists', return_value=True).start()
     mock.patch(
         'subprocess2.check_output',
         side_effect=[
             subprocess2.CalledProcessError(1, ['cmd'], 'cwd', 'out', 'err')
         ],
     ).start()
     self.addCleanup(mock.patch.stopall)
     self.maxDiff = None
 def setUp(self):
     super(GerritUtilTest, self).setUp()
     mock.patch('gerrit_util.LOGGER').start()
     mock.patch('gerrit_util.time_sleep').start()
     mock.patch('metrics.collector').start()
     mock.patch('metrics_utils.extract_http_metrics',
                return_value='http_metrics').start()
     self.addCleanup(mock.patch.stopall)
 def setUp(self):
   self.fake_hash_1 = 't0ta11yf4k3'
   self.fake_hash_2 = '3v3nf4k3r'
   self.url = 'git://foo'
   self.root_dir = '/tmp' if sys.platform != 'win32' else 't:\\tmp'
   self.relpath = 'fake'
   self.base_path = os.path.join(self.root_dir, self.relpath)
   self.backup_base_path = os.path.join(self.root_dir,
                                        'old_%s.git' % self.relpath)
   mock.patch('gclient_scm.scm.GIT.ApplyEnvVars').start()
   mock.patch('gclient_scm.GitWrapper._CheckMinVersion').start()
   mock.patch('gclient_scm.GitWrapper._Fetch').start()
   mock.patch('gclient_scm.GitWrapper._DeleteOrMove').start()
   mock.patch('sys.stdout', StringIO()).start()
   self.addCleanup(mock.patch.stopall)
 def setUp(self):
   unittest.TestCase.setUp(self)
   test_case_utils.TestCaseUtils.setUp(self)
   self.url = 'git://foo'
   # The .git suffix allows gclient_scm to recognize the dir as a git repo
   # when cloning it locally
   self.root_dir = tempfile.mkdtemp('.git')
   self.relpath = '.'
   self.base_path = join(self.root_dir, self.relpath)
   self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path)
   self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists
   mock.patch('gclient_scm.GitWrapper.BinaryExists',
              staticmethod(lambda : True)).start()
   mock.patch('sys.stdout', StringIO()).start()
   self.addCleanup(mock.patch.stopall)
   self.addCleanup(gclient_utils.rmtree, self.root_dir)
Ejemplo n.º 8
0
    def test_get_english_env(self):
        with mock.patch('sys.platform', 'win32'):
            self.assertIsNone(subprocess2.get_english_env({}))

        with mock.patch('sys.platform', 'non-win32'):
            self.assertIsNone(subprocess2.get_english_env({}))
            self.assertIsNone(
                subprocess2.get_english_env({
                    'LANG': 'en_XX',
                    'LANGUAGE': 'en_YY'
                }))
            self.assertEqual({
                'LANG': 'en_US.UTF-8',
                'LANGUAGE': 'en_US.UTF-8'
            }, subprocess2.get_english_env({
                'LANG': 'bar',
                'LANGUAGE': 'baz'
            }))
    def testReadHttpResponse_AuthenticationIssue(self):
        for status in (302, 401, 403):
            response = mock.Mock(status=status)
            response.get.return_value = None
            conn = mock.Mock(req_params={'uri': 'uri', 'method': 'method'})
            conn.request.return_value = (response, b'')

            with mock.patch('sys.stdout', StringIO()):
                with self.assertRaises(gerrit_util.GerritError) as cm:
                    gerrit_util.ReadHttpResponse(conn)

                self.assertEqual(status, cm.exception.http_status)
                self.assertIn('Your Gerrit credentials might be misconfigured',
                              sys.stdout.getvalue())
Ejemplo n.º 10
0
  def setUp(self):
    # Create this before setting up mocks.
    self._cipd_root_dir = tempfile.mkdtemp()
    self._workdir = tempfile.mkdtemp()

    self._cipd_instance_url = 'https://chrome-infra-packages.appspot.com'
    self._cipd_root = gclient_scm.CipdRoot(
        self._cipd_root_dir,
        self._cipd_instance_url)
    self._cipd_packages = [
        self._cipd_root.add_package('f', 'foo_package', 'foo_version'),
        self._cipd_root.add_package('b', 'bar_package', 'bar_version'),
        self._cipd_root.add_package('b', 'baz_package', 'baz_version'),
    ]
    mock.patch('tempfile.mkdtemp', lambda: self._workdir).start()
    mock.patch('gclient_scm.CipdRoot.add_package').start()
    mock.patch('gclient_scm.CipdRoot.clobber').start()
    mock.patch('gclient_scm.CipdRoot.ensure').start()
    self.addCleanup(mock.patch.stopall)
Ejemplo n.º 11
0
  def setUp(self):
    # Create this before setting up mocks.
    self._cipd_root_dir = tempfile.mkdtemp()
    self._workdir = tempfile.mkdtemp()

    self._cipd_instance_url = 'http://103.210.161.2:3232/chromiumsrc/cipd/tree/master/public'
    self._cipd_root = gclient_scm.CipdRoot(
        self._cipd_root_dir,
        self._cipd_instance_url)
    self._cipd_packages = [
        self._cipd_root.add_package('f', 'foo_package', 'foo_version'),
        self._cipd_root.add_package('b', 'bar_package', 'bar_version'),
        self._cipd_root.add_package('b', 'baz_package', 'baz_version'),
    ]
    mock.patch('tempfile.mkdtemp', lambda: self._workdir).start()
    mock.patch('gclient_scm.CipdRoot.add_package').start()
    mock.patch('gclient_scm.CipdRoot.clobber').start()
    mock.patch('gclient_scm.CipdRoot.ensure').start()
    self.addCleanup(mock.patch.stopall)
    self.addCleanup(gclient_utils.rmtree, self._cipd_root_dir)
    self.addCleanup(gclient_utils.rmtree, self._workdir)
Ejemplo n.º 12
0
 def setUp(self):
     super(WatchlistsTest, self).setUp()
     mock.patch('watchlists.Watchlists._HasWatchlistsFile').start()
     mock.patch('watchlists.Watchlists._ContentsOfWatchlistsFile').start()
     mock.patch('watchlists.logging.error').start()
     self.addCleanup(mock.patch.stopall)
 def setUp(self):
     super(CheckCallAndFilterTestCase, self).setUp()
     self.printfn = io.StringIO()
     self.stdout = io.BytesIO()
     if sys.version_info.major == 2:
         mock.patch('sys.stdout', self.stdout).start()
         mock.patch('__builtin__.print', self.printfn.write).start()
     else:
         mock.patch('sys.stdout', mock.Mock()).start()
         mock.patch('sys.stdout.buffer', self.stdout).start()
         mock.patch('builtins.print', self.printfn.write).start()
     mock.patch('sys.stdout.flush', lambda: None).start()
     self.addCleanup(mock.patch.stopall)
Ejemplo n.º 14
0
def _mockResponse(status, content):
  mock_response = (mock.Mock(status=status), content)
  mock.patch('auth.httplib2.Http.request', return_value=mock_response).start()
 def setUp(self):
     super(DetectHostArchTest, self).setUp()
     mock.patch('platform.machine').start()
     mock.patch('platform.processor').start()
     mock.patch('platform.architecture').start()
     self.addCleanup(mock.patch.stopall)
Ejemplo n.º 16
0
 def testPosixpathCipdSubdirOnWindows(self):
   with mock.patch('os.path', new=ntpath), (
        mock.patch('os.sep', new=ntpath.sep)):
     self._testPosixpathImpl()
Ejemplo n.º 17
0
 def setUp(self):
     mock.patch('subprocess2.check_call').start()
     mock.patch('subprocess2.check_call_out').start()
     mock.patch('auth.datetime_now', return_value=NOW).start()
     self.addCleanup(mock.patch.stopall)
Ejemplo n.º 18
0
 def setUp(self):
     mock.patch('os.environ').start()
     mock.patch(BUILTIN_OPEN, mock.mock_open()).start()
     self.addCleanup(mock.patch.stopall)
Ejemplo n.º 19
0
 def setUp(self):
     mock.patch('auth.datetime_now', return_value=NOW).start()
     self.addCleanup(mock.patch.stopall)
Ejemplo n.º 20
0
 def setUp(self):
     super(CheckCallAndFilterTestCase, self).setUp()
     mock.patch('sys.stdout', io.StringIO()).start()
     mock.patch('sys.stdout.flush', lambda: None).start()
     self.addCleanup(mock.patch.stopall)
    def setUp(self):
        super(GitDroverTest, self).setUp()
        self.maxDiff = None
        self._temp_directory = tempfile.mkdtemp()
        self._parent_repo = os.path.join(self._temp_directory, 'parent_repo')
        self._target_repo = os.path.join(self._temp_directory,
                                         'drover_branch_123')
        os.makedirs(os.path.join(self._parent_repo, '.git'))
        with open(os.path.join(self._parent_repo, '.git', 'config'), 'w') as f:
            f.write('config')
        with open(os.path.join(self._parent_repo, '.git', 'HEAD'), 'w') as f:
            f.write('HEAD')
        os.mkdir(os.path.join(self._parent_repo, '.git', 'info'))
        with open(os.path.join(self._parent_repo, '.git', 'info', 'refs'),
                  'w') as f:
            f.write('refs')
        mock.patch('tempfile.mkdtemp', self._mkdtemp).start()
        mock.patch('git_drover._raw_input', self._get_input).start()
        mock.patch('subprocess.check_call', self._check_call).start()
        mock.patch('subprocess.check_output', self._check_call).start()
        self.real_popen = subprocess.Popen
        mock.patch('subprocess.Popen', self._Popen).start()
        self.addCleanup(mock.patch.stopall)

        self._commands = []
        self._input = []
        self._fail_on_command = None
        self._reviewers = ''

        self.REPO_CHECK_COMMANDS = [
            (['git', '--help'], self._parent_repo),
            (['git', 'status'], self._parent_repo),
            (['git', 'fetch', 'origin'], self._parent_repo),
            (['git', 'rev-parse',
              'refs/remotes/branch-heads/branch^{commit}'], self._parent_repo),
            (['git', 'rev-parse', 'cl^{commit}'], self._parent_repo),
            (['git', 'show', '-s', 'cl'], self._parent_repo),
        ]
        self.LOCAL_REPO_COMMANDS = [
            (['git', 'rev-parse', '--git-dir'], self._parent_repo),
            (['git', 'config', 'core.sparsecheckout',
              'true'], self._target_repo),
            ([
                'git', 'checkout', '-b', 'drover_branch_123',
                'refs/remotes/branch-heads/branch'
            ], self._target_repo),
            (['git', 'cherry-pick', '-x', 'cl'], self._target_repo),
        ]
        self.UPLOAD_COMMANDS = [
            (['git', 'reset', '--hard'], self._target_repo),
            (['git', 'log', '-1', '--format=%ae'], self._target_repo),
            ([
                'git', 'cl', 'upload', '--send-mail', '--tbrs',
                '*****@*****.**'
            ], self._target_repo),
        ]
        self.LAND_COMMAND = [
            (['git', 'cl', 'land', '--bypass-hooks'], self._target_repo),
        ]
        if os.name == 'nt':
            self.BRANCH_CLEANUP_COMMANDS = [
                (['rmdir', '/s', '/q', self._target_repo], None),
                (['git', 'branch', '-D',
                  'drover_branch_123'], self._parent_repo),
            ]
        else:
            self.BRANCH_CLEANUP_COMMANDS = [
                (['git', 'branch', '-D',
                  'drover_branch_123'], self._parent_repo),
            ]
        self.MANUAL_RESOLVE_PREPARATION_COMMANDS = [
            (['git', '-c', 'core.quotePath=false', 'status',
              '--porcelain'], self._target_repo),
            (['git', 'update-index', '--skip-worktree',
              '--stdin'], self._target_repo),
        ]
        self.FINISH_MANUAL_RESOLVE_COMMANDS = [
            (['git', 'commit', '--no-edit'], self._target_repo),
        ]
Ejemplo n.º 22
0
    def setUp(self):
        self.config_file = os.path.join(ROOT_DIR, 'metrics.cfg')
        self.collector = metrics.MetricsCollector()

        # Keep track of the URL requests, file reads/writes and subprocess spawned.
        self.urllib2 = mock.Mock()
        self.print_notice = mock.Mock()
        self.Popen = mock.Mock()
        self.FileWrite = mock.Mock()
        self.FileRead = mock.Mock()

        mock.patch('metrics.urllib2', self.urllib2).start()
        mock.patch('metrics.subprocess.Popen', self.Popen).start()
        mock.patch('metrics.gclient_utils.FileWrite', self.FileWrite).start()
        mock.patch('metrics.gclient_utils.FileRead', self.FileRead).start()
        mock.patch('metrics.metrics_utils.print_notice',
                   self.print_notice).start()

        # Patch the methods used to get the system information, so we have a known
        # environment.
        mock.patch('metrics.tempfile.mkstemp', lambda:
                   (None, '/tmp/metrics.json')).start()
        mock.patch('metrics.time.time', TimeMock()).start()
        mock.patch('metrics.metrics_utils.get_python_version',
                   lambda: '2.7.13').start()
        mock.patch('metrics.gclient_utils.GetMacWinOrLinux',
                   lambda: 'linux').start()
        mock.patch('metrics.detect_host_arch.HostArch', lambda: 'x86').start()
        mock.patch('metrics_utils.get_repo_timestamp', lambda _: 1234).start()

        self.default_metrics = {
            "python_version": "2.7.13",
            "execution_time": 1000,
            "timestamp": 0,
            "exit_code": 0,
            "command": "fun",
            "depot_tools_age": 1234,
            "host_arch": "x86",
            "host_os": "linux",
        }

        self.addCleanup(mock.patch.stopall)
Ejemplo n.º 23
0
 def testPosixpathCipdSubdirOnWindows(self):
     with mock.patch('os.path', new=ntpath), (mock.patch('os.sep',
                                                         new=ntpath.sep)):
         self._testPosixpathImpl()