blob: 394555f26a48db498f12c0004d7e822df33bd97f [file] [log] [blame]
Edward Lemurf417d3a2019-10-02 20:28:59 +00001#!/usr/bin/env vpython3
szager@chromium.org66c8b852015-09-22 23:19:07 +00002# 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
8import os
9import shutil
Edward Lemura11fc9b2018-07-17 22:35:47 +000010import subprocess
szager@chromium.org66c8b852015-09-22 23:19:07 +000011import sys
12import tempfile
13import unittest
14
15DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16sys.path.insert(0, DEPOT_TOOLS_ROOT)
17
18from testing_support import coverage_utils
19import git_cache
20
21class GitCacheTest(unittest.TestCase):
Edward Lemura11fc9b2018-07-17 22:35:47 +000022 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.org66c8b852015-09-22 23:19:07 +000028
Edward Lemura11fc9b2018-07-17 22:35:47 +000029 def git(self, cmd, cwd=None):
30 cwd = cwd or self.origin_dir
Edward Lesmesab517542019-10-07 23:16:21 +000031 git = 'git.bat' if sys.platform == 'win32' else 'git'
32 subprocess.check_call([git] + cmd, cwd=cwd)
szager@chromium.org66c8b852015-09-22 23:19:07 +000033
34 def testParseFetchSpec(self):
35 testData = [
36 ([], []),
37 (['master'], [('+refs/heads/master:refs/heads/master',
38 r'\+refs/heads/master:.*')]),
39 (['master/'], [('+refs/heads/master:refs/heads/master',
40 r'\+refs/heads/master:.*')]),
41 (['+master'], [('+refs/heads/master:refs/heads/master',
42 r'\+refs/heads/master:.*')]),
43 (['refs/heads/*'], [('+refs/heads/*:refs/heads/*',
44 r'\+refs/heads/\*:.*')]),
45 (['foo/bar/*', 'baz'], [('+refs/heads/foo/bar/*:refs/heads/foo/bar/*',
46 r'\+refs/heads/foo/bar/\*:.*'),
47 ('+refs/heads/baz:refs/heads/baz',
48 r'\+refs/heads/baz:.*')]),
49 (['refs/foo/*:refs/bar/*'], [('+refs/foo/*:refs/bar/*',
50 r'\+refs/foo/\*:.*')])
51 ]
52
53 mirror = git_cache.Mirror('test://phony.example.biz')
54 for fetch_specs, expected in testData:
55 mirror = git_cache.Mirror('test://phony.example.biz', refs=fetch_specs)
Edward Lemurdf746d02019-07-27 00:42:46 +000056 self.assertEqual(mirror.fetch_specs, set(expected))
szager@chromium.org66c8b852015-09-22 23:19:07 +000057
Edward Lemura11fc9b2018-07-17 22:35:47 +000058 def testPopulate(self):
59 self.git(['init', '-q'])
60 with open(os.path.join(self.origin_dir, 'foo'), 'w') as f:
61 f.write('touched\n')
62 self.git(['add', 'foo'])
63 self.git(['commit', '-m', 'foo'])
64
65 mirror = git_cache.Mirror(self.origin_dir)
66 mirror.populate()
67
68 def testPopulateResetFetchConfig(self):
69 self.git(['init', '-q'])
70 with open(os.path.join(self.origin_dir, 'foo'), 'w') as f:
71 f.write('touched\n')
72 self.git(['add', 'foo'])
73 self.git(['commit', '-m', 'foo'])
74
75 mirror = git_cache.Mirror(self.origin_dir)
76 mirror.populate()
77
78 # Add a bad refspec to the cache's fetch config.
79 cache_dir = os.path.join(
80 self.cache_dir, mirror.UrlToCacheDir(self.origin_dir))
81 self.git(['config', '--add', 'remote.origin.fetch',
82 '+refs/heads/foo:refs/heads/foo'],
83 cwd=cache_dir)
84
85 mirror.populate(reset_fetch_config=True)
86
87
88 def testPopulateResetFetchConfigEmptyFetchConfig(self):
89 self.git(['init', '-q'])
90 with open(os.path.join(self.origin_dir, 'foo'), 'w') as f:
91 f.write('touched\n')
92 self.git(['add', 'foo'])
93 self.git(['commit', '-m', 'foo'])
94
95 mirror = git_cache.Mirror(self.origin_dir)
96 mirror.populate(reset_fetch_config=True)
97
Robert Iannuccia19649b2018-06-29 16:31:45 +000098
99class GitCacheDirTest(unittest.TestCase):
100 def setUp(self):
101 try:
102 delattr(git_cache.Mirror, 'cachepath')
103 except AttributeError:
104 pass
105 super(GitCacheDirTest, self).setUp()
106
107 def tearDown(self):
108 try:
109 delattr(git_cache.Mirror, 'cachepath')
110 except AttributeError:
111 pass
112 super(GitCacheDirTest, self).tearDown()
113
114 def test_git_config_read(self):
115 (fd, tmpFile) = tempfile.mkstemp()
116 old = git_cache.Mirror._GIT_CONFIG_LOCATION
117 try:
118 try:
Edward Lemurdf746d02019-07-27 00:42:46 +0000119 os.write(fd, b'[cache]\n cachepath="hello world"\n')
Robert Iannuccia19649b2018-06-29 16:31:45 +0000120 finally:
121 os.close(fd)
122
123 git_cache.Mirror._GIT_CONFIG_LOCATION = ['-f', tmpFile]
124
Edward Lemurdf746d02019-07-27 00:42:46 +0000125 self.assertEqual(git_cache.Mirror.GetCachePath(), b'hello world')
Robert Iannuccia19649b2018-06-29 16:31:45 +0000126 finally:
127 git_cache.Mirror._GIT_CONFIG_LOCATION = old
128 os.remove(tmpFile)
129
130 def test_environ_read(self):
131 path = os.environ.get('GIT_CACHE_PATH')
132 config = os.environ.get('GIT_CONFIG')
133 try:
134 os.environ['GIT_CACHE_PATH'] = 'hello world'
135 os.environ['GIT_CONFIG'] = 'disabled'
136
137 self.assertEqual(git_cache.Mirror.GetCachePath(), 'hello world')
138 finally:
139 for name, val in zip(('GIT_CACHE_PATH', 'GIT_CONFIG'), (path, config)):
140 if val is None:
141 os.environ.pop(name, None)
142 else:
143 os.environ[name] = val
144
145 def test_manual_set(self):
146 git_cache.Mirror.SetCachePath('hello world')
147 self.assertEqual(git_cache.Mirror.GetCachePath(), 'hello world')
148
149 def test_unconfigured(self):
150 path = os.environ.get('GIT_CACHE_PATH')
151 config = os.environ.get('GIT_CONFIG')
152 try:
153 os.environ.pop('GIT_CACHE_PATH', None)
154 os.environ['GIT_CONFIG'] = 'disabled'
155
156 with self.assertRaisesRegexp(RuntimeError, 'cache\.cachepath'):
157 git_cache.Mirror.GetCachePath()
158
159 # negatively cached value still raises
160 with self.assertRaisesRegexp(RuntimeError, 'cache\.cachepath'):
161 git_cache.Mirror.GetCachePath()
162 finally:
163 for name, val in zip(('GIT_CACHE_PATH', 'GIT_CONFIG'), (path, config)):
164 if val is None:
165 os.environ.pop(name, None)
166 else:
167 os.environ[name] = val
168
169
Dirk Prankedb589542019-04-12 21:07:01 +0000170class MirrorTest(unittest.TestCase):
171 def test_same_cache_for_authenticated_and_unauthenticated_urls(self):
172 # GoB can fetch a repo via two different URLs; if the url contains '/a/'
173 # it forces authenticated access instead of allowing anonymous access,
174 # even in the case where a repo is public. We want this in order to make
175 # sure bots are authenticated and get the right quotas. However, we
176 # only want to maintain a single cache for the repo.
177 self.assertEqual(git_cache.Mirror.UrlToCacheDir(
178 'https://chromium.googlesource.com/a/chromium/src.git'),
179 'chromium.googlesource.com-chromium-src')
180
181
szager@chromium.org66c8b852015-09-22 23:19:07 +0000182if __name__ == '__main__':
183 sys.exit(coverage_utils.covered_main((
184 os.path.join(DEPOT_TOOLS_ROOT, 'git_cache.py')
185 ), required_percentage=0))