blob: 61636b26bc69ce833fe37958a8a34968bc1269d4 [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
Dan Willemsen745b4ad2015-10-06 15:23:19 -070069 def test_get_gitc_manifest_dir_no_gitc(self):
70 """
71 Test reading a missing gitc config file
72 """
73 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
74 val = self.wrapper.get_gitc_manifest_dir()
75 self.assertEqual(val, '')
76
77 def test_get_gitc_manifest_dir(self):
78 """
79 Test reading the gitc config file and parsing the directory
80 """
81 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
82 val = self.wrapper.get_gitc_manifest_dir()
83 self.assertEqual(val, '/test/usr/local/google/gitc')
84
85 def test_gitc_parse_clientdir_no_gitc(self):
86 """
87 Test parsing the gitc clientdir without gitc running
88 """
89 self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
90 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
91 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
92
93 def test_gitc_parse_clientdir(self):
94 """
95 Test parsing the gitc clientdir
96 """
97 self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
98 self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
99 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
100 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
101 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
102 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
103 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
104 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'), 'test')
105 self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
106 self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
107
David Pursehouse819827a2020-02-12 15:20:19 +0900108
Mike Frysinger84094102020-02-11 02:10:28 -0500109class SetGitTrace2ParentSid(RepoWrapperTestCase):
110 """Check SetGitTrace2ParentSid behavior."""
111
112 KEY = 'GIT_TRACE2_PARENT_SID'
113 VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
114
115 def test_first_set(self):
116 """Test env var not yet set."""
117 env = {}
118 self.wrapper.SetGitTrace2ParentSid(env)
119 self.assertIn(self.KEY, env)
120 value = env[self.KEY]
121 self.assertRegex(value, self.VALID_FORMAT)
122
123 def test_append(self):
124 """Test env var is appended."""
125 env = {self.KEY: 'pfx'}
126 self.wrapper.SetGitTrace2ParentSid(env)
127 self.assertIn(self.KEY, env)
128 value = env[self.KEY]
129 self.assertTrue(value.startswith('pfx/'))
130 self.assertRegex(value[4:], self.VALID_FORMAT)
131
132 def test_global_context(self):
133 """Check os.environ gets updated by default."""
134 os.environ.pop(self.KEY, None)
135 self.wrapper.SetGitTrace2ParentSid()
136 self.assertIn(self.KEY, os.environ)
137 value = os.environ[self.KEY]
138 self.assertRegex(value, self.VALID_FORMAT)
139
140
Dan Willemsen745b4ad2015-10-06 15:23:19 -0700141if __name__ == '__main__':
142 unittest.main()