blob: c105a3ce3632405cc93dfe8b44070312b67ed407 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Dan Willemsen745b4ad2015-10-06 15:23:19 -07002#
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Mike Frysinger87deaef2019-07-26 21:14:55 -040017"""Unittests for the wrapper.py module."""
18
19from __future__ import print_function
20
Dan Willemsen745b4ad2015-10-06 15:23:19 -070021import os
Mike Frysinger84094102020-02-11 02:10:28 -050022import re
Dan Willemsen745b4ad2015-10-06 15:23:19 -070023import unittest
24
Mike Frysinger84094102020-02-11 02:10:28 -050025from pyversion import is_python3
Dan Willemsen745b4ad2015-10-06 15:23:19 -070026import wrapper
27
David Pursehouse819827a2020-02-12 15:20:19 +090028
Mike Frysinger8ddff5c2020-02-09 15:00:25 -050029if is_python3():
30 from unittest import mock
31 from io import StringIO
32else:
33 import mock
34 from StringIO import StringIO
35
36
Dan Willemsen745b4ad2015-10-06 15:23:19 -070037def fixture(*paths):
38 """Return a path relative to tests/fixtures.
39 """
40 return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
41
David Pursehouse819827a2020-02-12 15:20:19 +090042
Mike Frysinger84094102020-02-11 02:10:28 -050043class RepoWrapperTestCase(unittest.TestCase):
44 """TestCase for the wrapper module."""
David Pursehouse819827a2020-02-12 15:20:19 +090045
Dan Willemsen745b4ad2015-10-06 15:23:19 -070046 def setUp(self):
Mike Frysinger84094102020-02-11 02:10:28 -050047 """Load the wrapper module every time."""
Dan Willemsen745b4ad2015-10-06 15:23:19 -070048 wrapper._wrapper_module = None
49 self.wrapper = wrapper.Wrapper()
50
Mike Frysinger84094102020-02-11 02:10:28 -050051 if not is_python3():
52 self.assertRegex = self.assertRegexpMatches
53
54
55class RepoWrapperUnitTest(RepoWrapperTestCase):
56 """Tests helper functions in the repo wrapper
57 """
58
Mike Frysinger8ddff5c2020-02-09 15:00:25 -050059 def test_version(self):
60 """Make sure _Version works."""
61 with self.assertRaises(SystemExit) as e:
62 with mock.patch('sys.stdout', new_callable=StringIO) as stdout:
63 with mock.patch('sys.stderr', new_callable=StringIO) as stderr:
64 self.wrapper._Version()
65 self.assertEqual(0, e.exception.code)
66 self.assertEqual('', stderr.getvalue())
67 self.assertIn('repo launcher version', stdout.getvalue())
68
Mike Frysingerd8fda902020-02-14 00:24:38 -050069 def test_init_parser(self):
70 """Make sure 'init' GetParser works."""
71 parser = self.wrapper.GetParser(gitc_init=False)
72 opts, args = parser.parse_args([])
73 self.assertEqual([], args)
74 self.assertIsNone(opts.manifest_url)
75
76 def test_gitc_init_parser(self):
77 """Make sure 'gitc-init' GetParser works."""
78 parser = self.wrapper.GetParser(gitc_init=True)
79 opts, args = parser.parse_args([])
80 self.assertEqual([], args)
81 self.assertIsNone(opts.manifest_file)
82
Dan Willemsen745b4ad2015-10-06 15:23:19 -070083 def test_get_gitc_manifest_dir_no_gitc(self):
84 """
85 Test reading a missing gitc config file
86 """
87 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
88 val = self.wrapper.get_gitc_manifest_dir()
89 self.assertEqual(val, '')
90
91 def test_get_gitc_manifest_dir(self):
92 """
93 Test reading the gitc config file and parsing the directory
94 """
95 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
96 val = self.wrapper.get_gitc_manifest_dir()
97 self.assertEqual(val, '/test/usr/local/google/gitc')
98
99 def test_gitc_parse_clientdir_no_gitc(self):
100 """
101 Test parsing the gitc clientdir without gitc running
102 """
103 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
104 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
105 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
106
107 def test_gitc_parse_clientdir(self):
108 """
109 Test parsing the gitc clientdir
110 """
111 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
112 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
113 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
114 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
115 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
116 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
117 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
David Pursehouse3cda50a2020-02-13 13:17:03 +0900118 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'),
119 'test')
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700120 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
121 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
122
David Pursehouse819827a2020-02-12 15:20:19 +0900123
Mike Frysinger84094102020-02-11 02:10:28 -0500124class SetGitTrace2ParentSid(RepoWrapperTestCase):
125 """Check SetGitTrace2ParentSid behavior."""
126
127 KEY = 'GIT_TRACE2_PARENT_SID'
128 VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
129
130 def test_first_set(self):
131 """Test env var not yet set."""
132 env = {}
133 self.wrapper.SetGitTrace2ParentSid(env)
134 self.assertIn(self.KEY, env)
135 value = env[self.KEY]
136 self.assertRegex(value, self.VALID_FORMAT)
137
138 def test_append(self):
139 """Test env var is appended."""
140 env = {self.KEY: 'pfx'}
141 self.wrapper.SetGitTrace2ParentSid(env)
142 self.assertIn(self.KEY, env)
143 value = env[self.KEY]
144 self.assertTrue(value.startswith('pfx/'))
145 self.assertRegex(value[4:], self.VALID_FORMAT)
146
147 def test_global_context(self):
148 """Check os.environ gets updated by default."""
149 os.environ.pop(self.KEY, None)
150 self.wrapper.SetGitTrace2ParentSid()
151 self.assertIn(self.KEY, os.environ)
152 value = os.environ[self.KEY]
153 self.assertRegex(value, self.VALID_FORMAT)
154
155
Mike Frysinger587f1622020-03-23 16:49:11 -0400156class RunCommand(RepoWrapperTestCase):
157 """Check run_command behavior."""
158
159 def test_capture(self):
160 """Check capture_output handling."""
161 ret = self.wrapper.run_command(['echo', 'hi'], capture_output=True)
162 self.assertEqual(ret.stdout, 'hi\n')
163
164 def test_check(self):
165 """Check check handling."""
166 self.wrapper.run_command(['true'], check=False)
167 self.wrapper.run_command(['true'], check=True)
168 self.wrapper.run_command(['false'], check=False)
169 with self.assertRaises(self.wrapper.RunError):
170 self.wrapper.run_command(['false'], check=True)
171
172
173class RunGit(RepoWrapperTestCase):
174 """Check run_git behavior."""
175
176 def test_capture(self):
177 """Check capture_output handling."""
178 ret = self.wrapper.run_git('--version')
179 self.assertIn('git', ret.stdout)
180
181 def test_check(self):
182 """Check check handling."""
183 with self.assertRaises(self.wrapper.CloneFailure):
184 self.wrapper.run_git('--version-asdfasdf')
185 self.wrapper.run_git('--version-asdfasdf', check=False)
186
187
188class ParseGitVersion(RepoWrapperTestCase):
189 """Check ParseGitVersion behavior."""
190
191 def test_autoload(self):
192 """Check we can load the version from the live git."""
193 ret = self.wrapper.ParseGitVersion()
194 self.assertIsNotNone(ret)
195
196 def test_bad_ver(self):
197 """Check handling of bad git versions."""
198 ret = self.wrapper.ParseGitVersion(ver_str='asdf')
199 self.assertIsNone(ret)
200
201 def test_normal_ver(self):
202 """Check handling of normal git versions."""
203 ret = self.wrapper.ParseGitVersion(ver_str='git version 2.25.1')
204 self.assertEqual(2, ret.major)
205 self.assertEqual(25, ret.minor)
206 self.assertEqual(1, ret.micro)
207 self.assertEqual('2.25.1', ret.full)
208
209 def test_extended_ver(self):
210 """Check handling of extended distro git versions."""
211 ret = self.wrapper.ParseGitVersion(
212 ver_str='git version 1.30.50.696.g5e7596f4ac-goog')
213 self.assertEqual(1, ret.major)
214 self.assertEqual(30, ret.minor)
215 self.assertEqual(50, ret.micro)
216 self.assertEqual('1.30.50.696.g5e7596f4ac-goog', ret.full)
217
218
219class CheckGitVersion(RepoWrapperTestCase):
220 """Check _CheckGitVersion behavior."""
221
222 def test_unknown(self):
223 """Unknown versions should abort."""
224 with mock.patch.object(self.wrapper, 'ParseGitVersion', return_value=None):
225 with self.assertRaises(self.wrapper.CloneFailure):
226 self.wrapper._CheckGitVersion()
227
228 def test_old(self):
229 """Old versions should abort."""
230 with mock.patch.object(
231 self.wrapper, 'ParseGitVersion',
232 return_value=self.wrapper.GitVersion(1, 0, 0, '1.0.0')):
233 with self.assertRaises(self.wrapper.CloneFailure):
234 self.wrapper._CheckGitVersion()
235
236 def test_new(self):
237 """Newer versions should run fine."""
238 with mock.patch.object(
239 self.wrapper, 'ParseGitVersion',
240 return_value=self.wrapper.GitVersion(100, 0, 0, '100.0.0')):
241 self.wrapper._CheckGitVersion()
242
243
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700244if __name__ == '__main__':
245 unittest.main()