Edward Lemur | f417d3a | 2019-10-02 20:28:59 +0000 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for git_cache.py""" |
| 7 | |
| 8 | import os |
| 9 | import shutil |
Edward Lemur | a11fc9b | 2018-07-17 22:35:47 +0000 | [diff] [blame] | 10 | import subprocess |
szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 11 | import sys |
| 12 | import tempfile |
| 13 | import unittest |
| 14 | |
| 15 | DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 16 | sys.path.insert(0, DEPOT_TOOLS_ROOT) |
| 17 | |
| 18 | from testing_support import coverage_utils |
| 19 | import git_cache |
| 20 | |
| 21 | class GitCacheTest(unittest.TestCase): |
Edward Lemur | a11fc9b | 2018-07-17 22:35:47 +0000 | [diff] [blame] | 22 | def setUp(self): |
| 23 | self.cache_dir = tempfile.mkdtemp(prefix='git_cache_test_') |
| 24 | self.addCleanup(shutil.rmtree, self.cache_dir, ignore_errors=True) |
| 25 | self.origin_dir = tempfile.mkdtemp(suffix='origin.git') |
| 26 | self.addCleanup(shutil.rmtree, self.origin_dir, ignore_errors=True) |
| 27 | git_cache.Mirror.SetCachePath(self.cache_dir) |
szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 28 | |
Edward Lemur | a11fc9b | 2018-07-17 22:35:47 +0000 | [diff] [blame] | 29 | def git(self, cmd, cwd=None): |
| 30 | cwd = cwd or self.origin_dir |
| 31 | subprocess.check_call(['git'] + cmd, cwd=cwd) |
szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 32 | |
| 33 | def testParseFetchSpec(self): |
| 34 | testData = [ |
| 35 | ([], []), |
| 36 | (['master'], [('+refs/heads/master:refs/heads/master', |
| 37 | r'\+refs/heads/master:.*')]), |
| 38 | (['master/'], [('+refs/heads/master:refs/heads/master', |
| 39 | r'\+refs/heads/master:.*')]), |
| 40 | (['+master'], [('+refs/heads/master:refs/heads/master', |
| 41 | r'\+refs/heads/master:.*')]), |
| 42 | (['refs/heads/*'], [('+refs/heads/*:refs/heads/*', |
| 43 | r'\+refs/heads/\*:.*')]), |
| 44 | (['foo/bar/*', 'baz'], [('+refs/heads/foo/bar/*:refs/heads/foo/bar/*', |
| 45 | r'\+refs/heads/foo/bar/\*:.*'), |
| 46 | ('+refs/heads/baz:refs/heads/baz', |
| 47 | r'\+refs/heads/baz:.*')]), |
| 48 | (['refs/foo/*:refs/bar/*'], [('+refs/foo/*:refs/bar/*', |
| 49 | r'\+refs/foo/\*:.*')]) |
| 50 | ] |
| 51 | |
| 52 | mirror = git_cache.Mirror('test://phony.example.biz') |
| 53 | for fetch_specs, expected in testData: |
| 54 | mirror = git_cache.Mirror('test://phony.example.biz', refs=fetch_specs) |
Edward Lemur | df746d0 | 2019-07-27 00:42:46 +0000 | [diff] [blame] | 55 | self.assertEqual(mirror.fetch_specs, set(expected)) |
szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 56 | |
Edward Lemur | a11fc9b | 2018-07-17 22:35:47 +0000 | [diff] [blame] | 57 | def testPopulate(self): |
| 58 | self.git(['init', '-q']) |
| 59 | with open(os.path.join(self.origin_dir, 'foo'), 'w') as f: |
| 60 | f.write('touched\n') |
| 61 | self.git(['add', 'foo']) |
| 62 | self.git(['commit', '-m', 'foo']) |
| 63 | |
| 64 | mirror = git_cache.Mirror(self.origin_dir) |
| 65 | mirror.populate() |
| 66 | |
| 67 | def testPopulateResetFetchConfig(self): |
| 68 | self.git(['init', '-q']) |
| 69 | with open(os.path.join(self.origin_dir, 'foo'), 'w') as f: |
| 70 | f.write('touched\n') |
| 71 | self.git(['add', 'foo']) |
| 72 | self.git(['commit', '-m', 'foo']) |
| 73 | |
| 74 | mirror = git_cache.Mirror(self.origin_dir) |
| 75 | mirror.populate() |
| 76 | |
| 77 | # Add a bad refspec to the cache's fetch config. |
| 78 | cache_dir = os.path.join( |
| 79 | self.cache_dir, mirror.UrlToCacheDir(self.origin_dir)) |
| 80 | self.git(['config', '--add', 'remote.origin.fetch', |
| 81 | '+refs/heads/foo:refs/heads/foo'], |
| 82 | cwd=cache_dir) |
| 83 | |
| 84 | mirror.populate(reset_fetch_config=True) |
| 85 | |
| 86 | |
| 87 | def testPopulateResetFetchConfigEmptyFetchConfig(self): |
| 88 | self.git(['init', '-q']) |
| 89 | with open(os.path.join(self.origin_dir, 'foo'), 'w') as f: |
| 90 | f.write('touched\n') |
| 91 | self.git(['add', 'foo']) |
| 92 | self.git(['commit', '-m', 'foo']) |
| 93 | |
| 94 | mirror = git_cache.Mirror(self.origin_dir) |
| 95 | mirror.populate(reset_fetch_config=True) |
| 96 | |
Robert Iannucci | a19649b | 2018-06-29 16:31:45 +0000 | [diff] [blame] | 97 | |
| 98 | class GitCacheDirTest(unittest.TestCase): |
| 99 | def setUp(self): |
| 100 | try: |
| 101 | delattr(git_cache.Mirror, 'cachepath') |
| 102 | except AttributeError: |
| 103 | pass |
| 104 | super(GitCacheDirTest, self).setUp() |
| 105 | |
| 106 | def tearDown(self): |
| 107 | try: |
| 108 | delattr(git_cache.Mirror, 'cachepath') |
| 109 | except AttributeError: |
| 110 | pass |
| 111 | super(GitCacheDirTest, self).tearDown() |
| 112 | |
| 113 | def test_git_config_read(self): |
| 114 | (fd, tmpFile) = tempfile.mkstemp() |
| 115 | old = git_cache.Mirror._GIT_CONFIG_LOCATION |
| 116 | try: |
| 117 | try: |
Edward Lemur | df746d0 | 2019-07-27 00:42:46 +0000 | [diff] [blame] | 118 | os.write(fd, b'[cache]\n cachepath="hello world"\n') |
Robert Iannucci | a19649b | 2018-06-29 16:31:45 +0000 | [diff] [blame] | 119 | finally: |
| 120 | os.close(fd) |
| 121 | |
| 122 | git_cache.Mirror._GIT_CONFIG_LOCATION = ['-f', tmpFile] |
| 123 | |
Edward Lemur | df746d0 | 2019-07-27 00:42:46 +0000 | [diff] [blame] | 124 | self.assertEqual(git_cache.Mirror.GetCachePath(), b'hello world') |
Robert Iannucci | a19649b | 2018-06-29 16:31:45 +0000 | [diff] [blame] | 125 | finally: |
| 126 | git_cache.Mirror._GIT_CONFIG_LOCATION = old |
| 127 | os.remove(tmpFile) |
| 128 | |
| 129 | def test_environ_read(self): |
| 130 | path = os.environ.get('GIT_CACHE_PATH') |
| 131 | config = os.environ.get('GIT_CONFIG') |
| 132 | try: |
| 133 | os.environ['GIT_CACHE_PATH'] = 'hello world' |
| 134 | os.environ['GIT_CONFIG'] = 'disabled' |
| 135 | |
| 136 | self.assertEqual(git_cache.Mirror.GetCachePath(), 'hello world') |
| 137 | finally: |
| 138 | for name, val in zip(('GIT_CACHE_PATH', 'GIT_CONFIG'), (path, config)): |
| 139 | if val is None: |
| 140 | os.environ.pop(name, None) |
| 141 | else: |
| 142 | os.environ[name] = val |
| 143 | |
| 144 | def test_manual_set(self): |
| 145 | git_cache.Mirror.SetCachePath('hello world') |
| 146 | self.assertEqual(git_cache.Mirror.GetCachePath(), 'hello world') |
| 147 | |
| 148 | def test_unconfigured(self): |
| 149 | path = os.environ.get('GIT_CACHE_PATH') |
| 150 | config = os.environ.get('GIT_CONFIG') |
| 151 | try: |
| 152 | os.environ.pop('GIT_CACHE_PATH', None) |
| 153 | os.environ['GIT_CONFIG'] = 'disabled' |
| 154 | |
| 155 | with self.assertRaisesRegexp(RuntimeError, 'cache\.cachepath'): |
| 156 | git_cache.Mirror.GetCachePath() |
| 157 | |
| 158 | # negatively cached value still raises |
| 159 | with self.assertRaisesRegexp(RuntimeError, 'cache\.cachepath'): |
| 160 | git_cache.Mirror.GetCachePath() |
| 161 | finally: |
| 162 | for name, val in zip(('GIT_CACHE_PATH', 'GIT_CONFIG'), (path, config)): |
| 163 | if val is None: |
| 164 | os.environ.pop(name, None) |
| 165 | else: |
| 166 | os.environ[name] = val |
| 167 | |
| 168 | |
Dirk Pranke | db58954 | 2019-04-12 21:07:01 +0000 | [diff] [blame] | 169 | class MirrorTest(unittest.TestCase): |
| 170 | def test_same_cache_for_authenticated_and_unauthenticated_urls(self): |
| 171 | # GoB can fetch a repo via two different URLs; if the url contains '/a/' |
| 172 | # it forces authenticated access instead of allowing anonymous access, |
| 173 | # even in the case where a repo is public. We want this in order to make |
| 174 | # sure bots are authenticated and get the right quotas. However, we |
| 175 | # only want to maintain a single cache for the repo. |
| 176 | self.assertEqual(git_cache.Mirror.UrlToCacheDir( |
| 177 | 'https://chromium.googlesource.com/a/chromium/src.git'), |
| 178 | 'chromium.googlesource.com-chromium-src') |
| 179 | |
| 180 | |
szager@chromium.org | 66c8b85 | 2015-09-22 23:19:07 +0000 | [diff] [blame] | 181 | if __name__ == '__main__': |
| 182 | sys.exit(coverage_utils.covered_main(( |
| 183 | os.path.join(DEPOT_TOOLS_ROOT, 'git_cache.py') |
| 184 | ), required_percentage=0)) |