2019-06-13 06:30:51 +00:00
|
|
|
# -*- coding:utf-8 -*-
|
2015-10-06 22:23:19 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2019-07-27 01:14:55 +00:00
|
|
|
"""Unittests for the wrapper.py module."""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-10-06 22:23:19 +00:00
|
|
|
import os
|
2020-02-11 07:10:28 +00:00
|
|
|
import re
|
2015-10-06 22:23:19 +00:00
|
|
|
import unittest
|
|
|
|
|
2020-02-11 07:10:28 +00:00
|
|
|
from pyversion import is_python3
|
2015-10-06 22:23:19 +00:00
|
|
|
import wrapper
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2020-02-09 20:00:25 +00:00
|
|
|
if is_python3():
|
|
|
|
from unittest import mock
|
|
|
|
from io import StringIO
|
|
|
|
else:
|
|
|
|
import mock
|
|
|
|
from StringIO import StringIO
|
|
|
|
|
|
|
|
|
2015-10-06 22:23:19 +00:00
|
|
|
def fixture(*paths):
|
|
|
|
"""Return a path relative to tests/fixtures.
|
|
|
|
"""
|
|
|
|
return os.path.join(os.path.dirname(__file__), 'fixtures', *paths)
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2020-02-11 07:10:28 +00:00
|
|
|
class RepoWrapperTestCase(unittest.TestCase):
|
|
|
|
"""TestCase for the wrapper module."""
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2015-10-06 22:23:19 +00:00
|
|
|
def setUp(self):
|
2020-02-11 07:10:28 +00:00
|
|
|
"""Load the wrapper module every time."""
|
2015-10-06 22:23:19 +00:00
|
|
|
wrapper._wrapper_module = None
|
|
|
|
self.wrapper = wrapper.Wrapper()
|
|
|
|
|
2020-02-11 07:10:28 +00:00
|
|
|
if not is_python3():
|
|
|
|
self.assertRegex = self.assertRegexpMatches
|
|
|
|
|
|
|
|
|
|
|
|
class RepoWrapperUnitTest(RepoWrapperTestCase):
|
|
|
|
"""Tests helper functions in the repo wrapper
|
|
|
|
"""
|
|
|
|
|
2020-02-09 20:00:25 +00:00
|
|
|
def test_version(self):
|
|
|
|
"""Make sure _Version works."""
|
|
|
|
with self.assertRaises(SystemExit) as e:
|
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as stdout:
|
|
|
|
with mock.patch('sys.stderr', new_callable=StringIO) as stderr:
|
|
|
|
self.wrapper._Version()
|
|
|
|
self.assertEqual(0, e.exception.code)
|
|
|
|
self.assertEqual('', stderr.getvalue())
|
|
|
|
self.assertIn('repo launcher version', stdout.getvalue())
|
|
|
|
|
2020-02-14 05:24:38 +00:00
|
|
|
def test_init_parser(self):
|
|
|
|
"""Make sure 'init' GetParser works."""
|
|
|
|
parser = self.wrapper.GetParser(gitc_init=False)
|
|
|
|
opts, args = parser.parse_args([])
|
|
|
|
self.assertEqual([], args)
|
|
|
|
self.assertIsNone(opts.manifest_url)
|
|
|
|
|
|
|
|
def test_gitc_init_parser(self):
|
|
|
|
"""Make sure 'gitc-init' GetParser works."""
|
|
|
|
parser = self.wrapper.GetParser(gitc_init=True)
|
|
|
|
opts, args = parser.parse_args([])
|
|
|
|
self.assertEqual([], args)
|
|
|
|
self.assertIsNone(opts.manifest_file)
|
|
|
|
|
2015-10-06 22:23:19 +00:00
|
|
|
def test_get_gitc_manifest_dir_no_gitc(self):
|
|
|
|
"""
|
|
|
|
Test reading a missing gitc config file
|
|
|
|
"""
|
|
|
|
self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
|
|
|
|
val = self.wrapper.get_gitc_manifest_dir()
|
|
|
|
self.assertEqual(val, '')
|
|
|
|
|
|
|
|
def test_get_gitc_manifest_dir(self):
|
|
|
|
"""
|
|
|
|
Test reading the gitc config file and parsing the directory
|
|
|
|
"""
|
|
|
|
self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
|
|
|
|
val = self.wrapper.get_gitc_manifest_dir()
|
|
|
|
self.assertEqual(val, '/test/usr/local/google/gitc')
|
|
|
|
|
|
|
|
def test_gitc_parse_clientdir_no_gitc(self):
|
|
|
|
"""
|
|
|
|
Test parsing the gitc clientdir without gitc running
|
|
|
|
"""
|
|
|
|
self.wrapper.GITC_CONFIG_FILE = fixture('missing_gitc_config')
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
|
|
|
|
|
|
|
|
def test_gitc_parse_clientdir(self):
|
|
|
|
"""
|
|
|
|
Test parsing the gitc clientdir
|
|
|
|
"""
|
|
|
|
self.wrapper.GITC_CONFIG_FILE = fixture('gitc_config')
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/something'), None)
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test'), 'test')
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/'), 'test')
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/test/extra'), 'test')
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test'), 'test')
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/'), 'test')
|
2020-02-13 04:17:03 +00:00
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/test/extra'),
|
|
|
|
'test')
|
2015-10-06 22:23:19 +00:00
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/gitc/manifest-rw/'), None)
|
|
|
|
self.assertEqual(self.wrapper.gitc_parse_clientdir('/test/usr/local/google/gitc/'), None)
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2020-02-11 07:10:28 +00:00
|
|
|
class SetGitTrace2ParentSid(RepoWrapperTestCase):
|
|
|
|
"""Check SetGitTrace2ParentSid behavior."""
|
|
|
|
|
|
|
|
KEY = 'GIT_TRACE2_PARENT_SID'
|
|
|
|
VALID_FORMAT = re.compile(r'^repo-[0-9]{8}T[0-9]{6}Z-P[0-9a-f]{8}$')
|
|
|
|
|
|
|
|
def test_first_set(self):
|
|
|
|
"""Test env var not yet set."""
|
|
|
|
env = {}
|
|
|
|
self.wrapper.SetGitTrace2ParentSid(env)
|
|
|
|
self.assertIn(self.KEY, env)
|
|
|
|
value = env[self.KEY]
|
|
|
|
self.assertRegex(value, self.VALID_FORMAT)
|
|
|
|
|
|
|
|
def test_append(self):
|
|
|
|
"""Test env var is appended."""
|
|
|
|
env = {self.KEY: 'pfx'}
|
|
|
|
self.wrapper.SetGitTrace2ParentSid(env)
|
|
|
|
self.assertIn(self.KEY, env)
|
|
|
|
value = env[self.KEY]
|
|
|
|
self.assertTrue(value.startswith('pfx/'))
|
|
|
|
self.assertRegex(value[4:], self.VALID_FORMAT)
|
|
|
|
|
|
|
|
def test_global_context(self):
|
|
|
|
"""Check os.environ gets updated by default."""
|
|
|
|
os.environ.pop(self.KEY, None)
|
|
|
|
self.wrapper.SetGitTrace2ParentSid()
|
|
|
|
self.assertIn(self.KEY, os.environ)
|
|
|
|
value = os.environ[self.KEY]
|
|
|
|
self.assertRegex(value, self.VALID_FORMAT)
|
|
|
|
|
|
|
|
|
2015-10-06 22:23:19 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|