xref: /aosp_15_r20/tools/external_updater/tests/endtoend/test_update.py (revision 3c875a214f382db1236d28570d1304ce57138f32)
1#
2# Copyright (C) 2024 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16"""End-to-end tests for external_updater."""
17import subprocess
18from pathlib import Path
19
20from .treebuilder import TreeBuilder
21
22
23class TestUpdate:
24
25    def update(
26        self,
27        updater_cmd: list[str],
28        paths: list[Path],
29        args: list[str] | None = None,
30    ) -> str:
31        """Runs `external_updater update` with the given arguments.
32
33        Returns:
34        The output of the command.
35        """
36        return subprocess.run(
37            updater_cmd + ["update"] +
38            (args if args is not None else []) +
39            [str(p) for p in paths],
40            check=True,
41            capture_output=True,
42            text=True,
43        ).stdout
44
45    def test_bug_number(
46        self, tree_builder: TreeBuilder, updater_cmd: list[str]
47    ) -> None:
48        """Tests that bug number is added to the commit message."""
49        tree = tree_builder.repo_tree("tree")
50        a = tree.project("platform/external/foo", "external/foo")
51        tree.create_manifest_repo()
52        a.initial_import()
53        tree.init_and_sync()
54        bug_number = "12345"
55        self.update(updater_cmd, [a.local.path], args=['--refresh', '--bug', bug_number])
56        latest_sha = a.local.head()
57        latest_commit_message = a.local.commit_message_at_revision(latest_sha)
58        assert f"Bug: {bug_number}" in latest_commit_message
59
60    def test_custom_update_to_tag_successful(
61        self, tree_builder: TreeBuilder, updater_cmd: list[str]
62    ) -> None:
63        """Tests that upgrade to a specific tag is successful."""
64        tree = tree_builder.repo_tree("tree")
65        a = tree.project("platform/external/foo", "external/foo")
66        a.upstream.commit("Initial commit.", allow_empty=True)
67        a.upstream.tag("v1.0.0")
68        tree.create_manifest_repo()
69        a.initial_import(True)
70        tree.init_and_sync()
71        a.upstream.commit("Second commit.", allow_empty=True)
72        a.upstream.tag("v2.0.0")
73        a.upstream.commit("Third commit.", allow_empty=True)
74        a.upstream.tag("v3.0.0")
75        self.update(updater_cmd, [a.local.path], args=['--custom-version', "v2.0.0"])
76        latest_sha = a.local.head()
77        latest_commit_message = a.local.commit_message_at_revision(latest_sha)
78        assert "Upgrade test to v2.0.0" in latest_commit_message
79
80    def test_custom_downgrade_to_tag_unsuccessful(
81        self, tree_builder: TreeBuilder, updater_cmd: list[str]
82    ) -> None:
83        """Tests that downgrade to a specific tag is unsuccessful."""
84        tree = tree_builder.repo_tree("tree")
85        a = tree.project("platform/external/foo", "external/foo")
86        a.upstream.commit("Initial commit.", allow_empty=True)
87        a.upstream.tag("v1.0.0")
88        a.upstream.commit("Second commit.", allow_empty=True)
89        a.upstream.tag("v2.0.0")
90        tree.create_manifest_repo()
91        a.initial_import(True)
92        tree.init_and_sync()
93        self.update(updater_cmd, [a.local.path], args=['--custom-version', "v1.0.0"])
94        latest_sha = a.local.head()
95        latest_commit_message = a.local.commit_message_at_revision(latest_sha)
96        assert "Add metadata files." in latest_commit_message
97
98    def test_custom_update_to_sha_successful(
99        self, tree_builder: TreeBuilder, updater_cmd: list[str]
100    ) -> None:
101        """Tests that upgrade to a specific sha is successful."""
102        tree = tree_builder.repo_tree("tree")
103        a = tree.project("platform/external/foo", "external/foo")
104        a.upstream.commit("Initial commit.", allow_empty=True)
105        tree.create_manifest_repo()
106        a.initial_import()
107        tree.init_and_sync()
108        a.upstream.commit("Second commit.", allow_empty=True)
109        custom_sha = a.upstream.head()
110        a.upstream.commit("Third commit.", allow_empty=True)
111        self.update(updater_cmd, [a.local.path], args=['--custom-version', custom_sha])
112        latest_sha = a.local.head()
113        latest_commit_message = a.local.commit_message_at_revision(latest_sha)
114        assert f"Upgrade test to {custom_sha}" in latest_commit_message
115
116    def test_custom_downgrade_to_sha_unsuccessful(
117        self, tree_builder: TreeBuilder, updater_cmd: list[str]
118    ) -> None:
119        """Tests that downgrade to a specific sha is unsuccessful."""
120        tree = tree_builder.repo_tree("tree")
121        a = tree.project("platform/external/foo", "external/foo")
122        a.upstream.commit("Initial commit.", allow_empty=True)
123        custom_sha = a.upstream.head()
124        a.upstream.commit("Second commit.", allow_empty=True)
125        tree.create_manifest_repo()
126        a.initial_import()
127        tree.init_and_sync()
128        self.update(updater_cmd, [a.local.path], args=['--custom-version', custom_sha])
129        latest_sha = a.local.head()
130        latest_commit_message = a.local.commit_message_at_revision(latest_sha)
131        assert "Add metadata files." in latest_commit_message
132