Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 2 | # |
| 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 Frysinger | 87deaef | 2019-07-26 21:14:55 -0400 | [diff] [blame] | 17 | """Unittests for the wrapper.py module.""" |
| 18 | |
| 19 | from __future__ import print_function |
| 20 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 21 | import os |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 22 | import re |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 23 | import unittest |
| 24 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 25 | from pyversion import is_python3 |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 26 | import wrapper |
| 27 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 28 | |
Mike Frysinger | 8ddff5c | 2020-02-09 15:00:25 -0500 | [diff] [blame] | 29 | if is_python3(): |
| 30 | from unittest import mock |
| 31 | from io import StringIO |
| 32 | else: |
| 33 | import mock |
| 34 | from StringIO import StringIO |
| 35 | |
| 36 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 37 | def fixture(*paths): |
| 38 | """Return a path relative to tests/fixtures. |
| 39 | """ |
| 40 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) |
| 41 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 42 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 43 | class RepoWrapperTestCase(unittest.TestCase): |
| 44 | """TestCase for the wrapper module.""" |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 45 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 46 | def setUp(self): |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 47 | """Load the wrapper module every time.""" |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 48 | wrapper._wrapper_module = None |
| 49 | self.wrapper = wrapper.Wrapper() |
| 50 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 51 | if not is_python3(): |
| 52 | self.assertRegex = self.assertRegexpMatches |
| 53 | |
| 54 | |
| 55 | class RepoWrapperUnitTest(RepoWrapperTestCase): |
| 56 | """Tests helper functions in the repo wrapper |
| 57 | """ |
| 58 | |
Mike Frysinger | 8ddff5c | 2020-02-09 15:00:25 -0500 | [diff] [blame] | 59 | 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 Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 69 | 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 Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 83 | 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 Pursehouse | 3cda50a | 2020-02-13 13:17:03 +0900 | [diff] [blame] | 118 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'), |
| 119 | 'test') |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 120 | 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 Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 123 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 124 | class 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 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 156 | if __name__ == '__main__': |
| 157 | unittest.main() |