2021-01-15 03:17:50 +00:00
|
|
|
# Copyright (C) 2021 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.
|
|
|
|
|
|
|
|
"""Unittests for the git_superproject.py module."""
|
|
|
|
|
|
|
|
import os
|
2021-03-09 23:19:06 +00:00
|
|
|
import platform
|
2021-01-15 03:17:50 +00:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import git_superproject
|
2021-02-04 22:39:38 +00:00
|
|
|
import manifest_xml
|
2021-01-15 03:17:50 +00:00
|
|
|
import platform_utils
|
2021-04-12 19:16:36 +00:00
|
|
|
from test_manifest_xml import sort_attributes
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SuperprojectTestCase(unittest.TestCase):
|
|
|
|
"""TestCase for the Superproject module."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set up superproject every time."""
|
|
|
|
self.tempdir = tempfile.mkdtemp(prefix='repo_tests')
|
|
|
|
self.repodir = os.path.join(self.tempdir, '.repo')
|
2021-02-04 22:39:38 +00:00
|
|
|
self.manifest_file = os.path.join(
|
|
|
|
self.repodir, manifest_xml.MANIFEST_FILE_NAME)
|
|
|
|
os.mkdir(self.repodir)
|
2021-03-09 23:19:06 +00:00
|
|
|
self.platform = platform.system().lower()
|
2021-02-04 22:39:38 +00:00
|
|
|
|
|
|
|
# The manifest parsing really wants a git repo currently.
|
|
|
|
gitdir = os.path.join(self.repodir, 'manifests.git')
|
|
|
|
os.mkdir(gitdir)
|
|
|
|
with open(os.path.join(gitdir, 'config'), 'w') as fp:
|
|
|
|
fp.write("""[remote "origin"]
|
|
|
|
url = https://localhost:0/manifest
|
|
|
|
""")
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest = self.getXmlManifest("""
|
|
|
|
<manifest>
|
|
|
|
<remote name="default-remote" fetch="http://localhost" />
|
|
|
|
<default remote="default-remote" revision="refs/heads/main" />
|
|
|
|
<superproject name="superproject"/>
|
2021-03-09 23:19:06 +00:00
|
|
|
<project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """
|
|
|
|
" /></manifest>
|
2021-02-09 08:26:31 +00:00
|
|
|
""")
|
|
|
|
self._superproject = git_superproject.Superproject(manifest, self.repodir)
|
|
|
|
|
2021-01-15 03:17:50 +00:00
|
|
|
def tearDown(self):
|
|
|
|
"""Tear down superproject every time."""
|
|
|
|
platform_utils.rmtree(self.tempdir)
|
|
|
|
|
2021-02-04 22:39:38 +00:00
|
|
|
def getXmlManifest(self, data):
|
|
|
|
"""Helper to initialize a manifest for testing."""
|
|
|
|
with open(self.manifest_file, 'w') as fp:
|
|
|
|
fp.write(data)
|
|
|
|
return manifest_xml.XmlManifest(self.repodir, self.manifest_file)
|
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def test_superproject_get_superproject_no_superproject(self):
|
2021-01-15 03:17:50 +00:00
|
|
|
"""Test with no url."""
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest = self.getXmlManifest("""
|
|
|
|
<manifest>
|
|
|
|
</manifest>
|
|
|
|
""")
|
|
|
|
superproject = git_superproject.Superproject(manifest, self.repodir)
|
|
|
|
self.assertFalse(superproject.Sync())
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def test_superproject_get_superproject_invalid_url(self):
|
2021-01-15 03:17:50 +00:00
|
|
|
"""Test with an invalid url."""
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest = self.getXmlManifest("""
|
|
|
|
<manifest>
|
|
|
|
<remote name="test-remote" fetch="localhost" />
|
|
|
|
<default remote="test-remote" revision="refs/heads/main" />
|
|
|
|
<superproject name="superproject"/>
|
|
|
|
</manifest>
|
|
|
|
""")
|
|
|
|
superproject = git_superproject.Superproject(manifest, self.repodir)
|
|
|
|
self.assertFalse(superproject.Sync())
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-09 08:26:31 +00:00
|
|
|
def test_superproject_get_superproject_invalid_branch(self):
|
2021-01-15 03:17:50 +00:00
|
|
|
"""Test with an invalid branch."""
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest = self.getXmlManifest("""
|
|
|
|
<manifest>
|
|
|
|
<remote name="test-remote" fetch="localhost" />
|
|
|
|
<default remote="test-remote" revision="refs/heads/main" />
|
|
|
|
<superproject name="superproject"/>
|
|
|
|
</manifest>
|
|
|
|
""")
|
|
|
|
superproject = git_superproject.Superproject(manifest, self.repodir)
|
|
|
|
with mock.patch.object(self._superproject, '_GetBranch', return_value='junk'):
|
|
|
|
self.assertFalse(superproject.Sync())
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-23 00:54:56 +00:00
|
|
|
def test_superproject_get_superproject_mock_init(self):
|
|
|
|
"""Test with _Init failing."""
|
|
|
|
with mock.patch.object(self._superproject, '_Init', return_value=False):
|
2021-02-09 08:26:31 +00:00
|
|
|
self.assertFalse(self._superproject.Sync())
|
|
|
|
|
|
|
|
def test_superproject_get_superproject_mock_fetch(self):
|
2021-02-23 00:54:56 +00:00
|
|
|
"""Test with _Fetch failing."""
|
|
|
|
with mock.patch.object(self._superproject, '_Init', return_value=True):
|
2021-02-09 08:26:31 +00:00
|
|
|
os.mkdir(self._superproject._superproject_path)
|
|
|
|
with mock.patch.object(self._superproject, '_Fetch', return_value=False):
|
2021-02-23 00:54:56 +00:00
|
|
|
self.assertFalse(self._superproject.Sync())
|
2021-02-09 08:26:31 +00:00
|
|
|
|
|
|
|
def test_superproject_get_all_project_commit_ids_mock_ls_tree(self):
|
2021-01-15 03:17:50 +00:00
|
|
|
"""Test with LsTree being a mock."""
|
|
|
|
data = ('120000 blob 158258bdf146f159218e2b90f8b699c4d85b5804\tAndroid.bp\x00'
|
|
|
|
'160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00'
|
|
|
|
'160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00'
|
|
|
|
'120000 blob acc2cbdf438f9d2141f0ae424cec1d8fc4b5d97f\tbootstrap.bash\x00'
|
|
|
|
'160000 commit ade9b7a0d874e25fff4bf2552488825c6f111928\tbuild/bazel\x00')
|
2021-02-23 00:54:56 +00:00
|
|
|
with mock.patch.object(self._superproject, '_Init', return_value=True):
|
|
|
|
with mock.patch.object(self._superproject, '_Fetch', return_value=True):
|
|
|
|
with mock.patch.object(self._superproject, '_LsTree', return_value=data):
|
|
|
|
commit_ids = self._superproject._GetAllProjectsCommitIds()
|
|
|
|
self.assertEqual(commit_ids, {
|
|
|
|
'art': '2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea',
|
|
|
|
'bootable/recovery': 'e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06',
|
|
|
|
'build/bazel': 'ade9b7a0d874e25fff4bf2552488825c6f111928'
|
|
|
|
})
|
2021-01-15 03:17:50 +00:00
|
|
|
|
2021-02-04 22:39:38 +00:00
|
|
|
def test_superproject_write_manifest_file(self):
|
|
|
|
"""Test with writing manifest to a file after setting revisionId."""
|
2021-02-09 08:26:31 +00:00
|
|
|
self.assertEqual(len(self._superproject._manifest.projects), 1)
|
|
|
|
project = self._superproject._manifest.projects[0]
|
2021-02-04 22:39:38 +00:00
|
|
|
project.SetRevisionId('ABCDEF')
|
|
|
|
# Create temporary directory so that it can write the file.
|
|
|
|
os.mkdir(self._superproject._superproject_path)
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest_path = self._superproject._WriteManfiestFile()
|
2021-02-04 22:39:38 +00:00
|
|
|
self.assertIsNotNone(manifest_path)
|
|
|
|
with open(manifest_path, 'r') as fp:
|
2021-06-07 20:27:37 +00:00
|
|
|
manifest_xml_data = fp.read()
|
2021-02-04 22:39:38 +00:00
|
|
|
self.assertEqual(
|
2021-06-07 20:27:37 +00:00
|
|
|
sort_attributes(manifest_xml_data),
|
2021-05-03 02:47:29 +00:00
|
|
|
'<?xml version="1.0" ?><manifest>'
|
2021-04-12 19:16:36 +00:00
|
|
|
'<remote fetch="http://localhost" name="default-remote"/>'
|
2021-05-03 02:47:29 +00:00
|
|
|
'<default remote="default-remote" revision="refs/heads/main"/>'
|
2021-04-12 19:16:36 +00:00
|
|
|
'<project groups="notdefault,platform-' + self.platform + '" '
|
|
|
|
'name="platform/art" path="art" revision="ABCDEF"/>'
|
2021-05-03 02:47:29 +00:00
|
|
|
'<superproject name="superproject"/>'
|
2021-02-04 22:39:38 +00:00
|
|
|
'</manifest>')
|
|
|
|
|
|
|
|
def test_superproject_update_project_revision_id(self):
|
|
|
|
"""Test with LsTree being a mock."""
|
2021-02-09 08:26:31 +00:00
|
|
|
self.assertEqual(len(self._superproject._manifest.projects), 1)
|
|
|
|
projects = self._superproject._manifest.projects
|
2021-02-04 22:39:38 +00:00
|
|
|
data = ('160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00'
|
|
|
|
'160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00')
|
2021-02-23 00:54:56 +00:00
|
|
|
with mock.patch.object(self._superproject, '_Init', return_value=True):
|
2021-02-08 00:30:27 +00:00
|
|
|
with mock.patch.object(self._superproject, '_Fetch', return_value=True):
|
2021-02-09 08:26:31 +00:00
|
|
|
with mock.patch.object(self._superproject,
|
|
|
|
'_LsTree',
|
|
|
|
return_value=data):
|
2021-02-04 22:39:38 +00:00
|
|
|
# Create temporary directory so that it can write the file.
|
|
|
|
os.mkdir(self._superproject._superproject_path)
|
2021-02-09 08:26:31 +00:00
|
|
|
manifest_path = self._superproject.UpdateProjectsRevisionId(projects)
|
2021-02-04 22:39:38 +00:00
|
|
|
self.assertIsNotNone(manifest_path)
|
|
|
|
with open(manifest_path, 'r') as fp:
|
2021-06-07 20:27:37 +00:00
|
|
|
manifest_xml_data = fp.read()
|
2021-02-04 22:39:38 +00:00
|
|
|
self.assertEqual(
|
2021-06-07 20:27:37 +00:00
|
|
|
sort_attributes(manifest_xml_data),
|
2021-05-03 02:47:29 +00:00
|
|
|
'<?xml version="1.0" ?><manifest>'
|
2021-04-12 19:16:36 +00:00
|
|
|
'<remote fetch="http://localhost" name="default-remote"/>'
|
2021-05-03 02:47:29 +00:00
|
|
|
'<default remote="default-remote" revision="refs/heads/main"/>'
|
2021-04-12 19:16:36 +00:00
|
|
|
'<project groups="notdefault,platform-' + self.platform + '" '
|
|
|
|
'name="platform/art" path="art" '
|
|
|
|
'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea"/>'
|
2021-05-03 02:47:29 +00:00
|
|
|
'<superproject name="superproject"/>'
|
|
|
|
'</manifest>')
|
|
|
|
|
2021-06-07 20:27:37 +00:00
|
|
|
def test_superproject_update_project_revision_id_from_local_manifest_group(self):
|
|
|
|
"""Test update of commit ids of a manifest that have local manifest no superproject group."""
|
|
|
|
local_group = manifest_xml.LOCAL_MANIFEST_GROUP_PREFIX + ':local'
|
|
|
|
manifest = self.getXmlManifest("""
|
|
|
|
<manifest>
|
|
|
|
<remote name="default-remote" fetch="http://localhost" />
|
|
|
|
<remote name="goog" fetch="http://localhost2" />
|
|
|
|
<default remote="default-remote" revision="refs/heads/main" />
|
|
|
|
<superproject name="superproject"/>
|
|
|
|
<project path="vendor/x" name="platform/vendor/x" remote="goog"
|
|
|
|
groups=\"""" + local_group + """
|
|
|
|
" revision="master-with-vendor" clone-depth="1" />
|
|
|
|
<project path="art" name="platform/art" groups="notdefault,platform-""" + self.platform + """
|
|
|
|
" /></manifest>
|
|
|
|
""")
|
|
|
|
self.maxDiff = None
|
|
|
|
self._superproject = git_superproject.Superproject(manifest, self.repodir)
|
|
|
|
self.assertEqual(len(self._superproject._manifest.projects), 2)
|
|
|
|
projects = self._superproject._manifest.projects
|
|
|
|
data = ('160000 commit 2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea\tart\x00'
|
|
|
|
'160000 commit e9d25da64d8d365dbba7c8ee00fe8c4473fe9a06\tbootable/recovery\x00')
|
|
|
|
with mock.patch.object(self._superproject, '_Init', return_value=True):
|
|
|
|
with mock.patch.object(self._superproject, '_Fetch', return_value=True):
|
|
|
|
with mock.patch.object(self._superproject,
|
|
|
|
'_LsTree',
|
|
|
|
return_value=data):
|
|
|
|
# Create temporary directory so that it can write the file.
|
|
|
|
os.mkdir(self._superproject._superproject_path)
|
|
|
|
manifest_path = self._superproject.UpdateProjectsRevisionId(projects)
|
|
|
|
self.assertIsNotNone(manifest_path)
|
|
|
|
with open(manifest_path, 'r') as fp:
|
|
|
|
manifest_xml_data = fp.read()
|
|
|
|
# Verify platform/vendor/x's project revision hasn't changed.
|
|
|
|
self.assertEqual(
|
|
|
|
sort_attributes(manifest_xml_data),
|
|
|
|
'<?xml version="1.0" ?><manifest>'
|
|
|
|
'<remote fetch="http://localhost" name="default-remote"/>'
|
|
|
|
'<remote fetch="http://localhost2" name="goog"/>'
|
|
|
|
'<default remote="default-remote" revision="refs/heads/main"/>'
|
|
|
|
'<project groups="notdefault,platform-' + self.platform + '" '
|
|
|
|
'name="platform/art" path="art" '
|
|
|
|
'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea"/>'
|
|
|
|
'<project clone-depth="1" groups="' + local_group + '" '
|
|
|
|
'name="platform/vendor/x" path="vendor/x" remote="goog" '
|
|
|
|
'revision="master-with-vendor"/>'
|
|
|
|
'<superproject name="superproject"/>'
|
|
|
|
'</manifest>')
|
|
|
|
|
2021-01-15 03:17:50 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|