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 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 29 | def fixture(*paths): |
| 30 | """Return a path relative to tests/fixtures. |
| 31 | """ |
| 32 | return os.path.join(os.path.dirname(__file__), 'fixtures', *paths) |
| 33 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 34 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame^] | 35 | class RepoWrapperTestCase(unittest.TestCase): |
| 36 | """TestCase for the wrapper module.""" |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 37 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 38 | def setUp(self): |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame^] | 39 | """Load the wrapper module every time.""" |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 40 | wrapper._wrapper_module = None |
| 41 | self.wrapper = wrapper.Wrapper() |
| 42 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame^] | 43 | if not is_python3(): |
| 44 | self.assertRegex = self.assertRegexpMatches |
| 45 | |
| 46 | |
| 47 | class RepoWrapperUnitTest(RepoWrapperTestCase): |
| 48 | """Tests helper functions in the repo wrapper |
| 49 | """ |
| 50 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 51 | def test_get_gitc_manifest_dir_no_gitc(self): |
| 52 | """ |
| 53 | Test reading a missing gitc config file |
| 54 | """ |
| 55 | self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config') |
| 56 | val = self.wrapper.get_gitc_manifest_dir() |
| 57 | self.assertEqual(val, '') |
| 58 | |
| 59 | def test_get_gitc_manifest_dir(self): |
| 60 | """ |
| 61 | Test reading the gitc config file and parsing the directory |
| 62 | """ |
| 63 | self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config') |
| 64 | val = self.wrapper.get_gitc_manifest_dir() |
| 65 | self.assertEqual(val, '/test/usr/local/google/gitc') |
| 66 | |
| 67 | def test_gitc_parse_clientdir_no_gitc(self): |
| 68 | """ |
| 69 | Test parsing the gitc clientdir without gitc running |
| 70 | """ |
| 71 | self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config') |
| 72 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None) |
| 73 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test') |
| 74 | |
| 75 | def test_gitc_parse_clientdir(self): |
| 76 | """ |
| 77 | Test parsing the gitc clientdir |
| 78 | """ |
| 79 | self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config') |
| 80 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None) |
| 81 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test') |
| 82 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test') |
| 83 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test') |
| 84 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test') |
| 85 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test') |
| 86 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'), 'test') |
| 87 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None) |
| 88 | self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None) |
| 89 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 90 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame^] | 91 | class SetGitTrace2ParentSid(RepoWrapperTestCase): |
| 92 | """Check SetGitTrace2ParentSid behavior.""" |
| 93 | |
| 94 | KEY = 'GIT_TRACE2_PARENT_SID' |
| 95 | VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$') |
| 96 | |
| 97 | def test_first_set(self): |
| 98 | """Test env var not yet set.""" |
| 99 | env = {} |
| 100 | self.wrapper.SetGitTrace2ParentSid(env) |
| 101 | self.assertIn(self.KEY, env) |
| 102 | value = env[self.KEY] |
| 103 | self.assertRegex(value, self.VALID_FORMAT) |
| 104 | |
| 105 | def test_append(self): |
| 106 | """Test env var is appended.""" |
| 107 | env = {self.KEY: 'pfx'} |
| 108 | self.wrapper.SetGitTrace2ParentSid(env) |
| 109 | self.assertIn(self.KEY, env) |
| 110 | value = env[self.KEY] |
| 111 | self.assertTrue(value.startswith('pfx/')) |
| 112 | self.assertRegex(value[4:], self.VALID_FORMAT) |
| 113 | |
| 114 | def test_global_context(self): |
| 115 | """Check os.environ gets updated by default.""" |
| 116 | os.environ.pop(self.KEY, None) |
| 117 | self.wrapper.SetGitTrace2ParentSid() |
| 118 | self.assertIn(self.KEY, os.environ) |
| 119 | value = os.environ[self.KEY] |
| 120 | self.assertRegex(value, self.VALID_FORMAT) |
| 121 | |
| 122 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 123 | if __name__ == '__main__': |
| 124 | unittest.main() |