aboutsummaryrefslogtreecommitdiffstats
path: root/packaging-tools/tests/test_release_repo_meta_update.py
blob: 643a7ea7007bc9b9dfaa54f1f766df326f829c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#############################################################################
#
# Copyright (C) 2023 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# This file is part of the release tools of the Qt Toolkit.
#
# $QT_BEGIN_LICENSE:GPL-EXCEPT$
# Commercial License Usage
# Licensees holding valid commercial Qt licenses may use this file in
# accordance with the commercial license agreement provided with the
# Software or, alternatively, in accordance with the terms contained in
# a written agreement between you and The Qt Company. For licensing terms
# and conditions see https://www.qt.io/terms-conditions. For further
# information use the contact form at https://www.qt.io/contact-us.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 3 as published by the Free Software
# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
# included in the packaging of this file. Please review the following
# information to ensure the GNU General Public License requirements will
# be met: https://www.gnu.org/licenses/gpl-3.0.html.
#
# $QT_END_LICENSE$
#
#############################################################################

import os
import unittest
from typing import List

from ddt import ddt  # type: ignore
from temppathlib import TemporaryDirectory

from release_repo_meta_update import (
    BACKUP_SUFFIX,
    CONVERT_SUFFIX,
    IfwRepoUpdateError,
    check_repos_which_can_be_updated,
    create_converted_repositories,
    scan_repositories,
    swap_repositories,
)
from tests.testhelpers import asyncio_test


@ddt
class TestReleaseRepoMetaUpdate(unittest.TestCase):
    paths = None  # type: List[str]
    non_migrated_paths = None  # type: List[str]
    mixed_migrated_paths = None  # type: List[str]

    @classmethod
    def setUpClass(cls) -> None:
        cls.paths = [
            "repo1/Updates.xml",
            "repo2/Updates.xml",
            "repo2" + CONVERT_SUFFIX + "/Updates.xml",
            "repo2" + CONVERT_SUFFIX + "/123456_meta.7z",
            "repo3/Updates.xml",
            "repo3" + CONVERT_SUFFIX + "/Updates.xml",
            "repo3" + CONVERT_SUFFIX + "/123456_meta.7z",
            "repo4/Updates.xml",
            "repo5/Updates.xml",
            "repo5/123456_meta.7z",
            "repo6/Updates.xml",
            "repo7/Updates.xml",
            "repo7/123456_meta.7z",
            "repo7" + CONVERT_SUFFIX + "/Updates.xml",
            "repo7" + CONVERT_SUFFIX + "/123456_meta.7z",
            "repo8/Updates.xml",
            "repo9/Updates.xml",
            "repo9" + CONVERT_SUFFIX + "/Updates.xml",
            "repo9" + CONVERT_SUFFIX + "/",  # meta.7z missing
            "repo10" + BACKUP_SUFFIX + "123456"
        ]
        cls.non_migrated_paths = [
            "repo1/Updates.xml",
            "repo2/sub2/Updates.xml",
            "repo3/sub3/subsub3/Updates.xml",
            "repo4/Updates.xml",
            "repo5/Updates.xml",
        ]
        cls.mixed_migrated_paths = [
            "repo1/Updates.xml",
            "repo1" + CONVERT_SUFFIX + "/Updates.xml",
            "repo2/sub2/Updates.xml",
            "repo3/sub3/subsub3/Updates.xml",
            "repo3/sub3/subsub3" + CONVERT_SUFFIX + "/Updates.xml",
            "repo4/Updates.xml",
            "repo5/Updates.xml",
        ]

    def _write_test_repo(self, tmp_base_dir: str, paths: List[str]) -> None:
        for path in paths:
            tmp = os.path.join(tmp_base_dir, path)
            os.makedirs(os.path.dirname(tmp), exist_ok=True)
            if tmp.endswith((".xml", ".7z")):
                with open(tmp, 'w+', encoding="utf-8") as handle:
                    handle.write("\n")

    @asyncio_test
    async def test_scan_repositories(self) -> None:
        with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir:
            tmp_base_dir = tmp_dir.path
            self._write_test_repo(str(tmp_base_dir), self.paths)

            done_repos, pending_repos, unconverted_repos, broken_repos = scan_repositories(str(tmp_base_dir))
            self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in broken_repos]),
                                 sorted(["/repo9" + CONVERT_SUFFIX]))
            self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in unconverted_repos]),
                                 sorted(["/repo1", "/repo2", "/repo3", "/repo4", "/repo6", "/repo8", "/repo9"]))
            self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in pending_repos]),
                                 sorted(["/repo2" + CONVERT_SUFFIX, "/repo3" + CONVERT_SUFFIX,
                                         "/repo7" + CONVERT_SUFFIX]))
            self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in done_repos]),
                                 sorted(["/repo5", "/repo7"]))

    @asyncio_test
    async def test_check_repos_which_can_be_updated(self) -> None:
        with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir:
            tmp_base_dir = tmp_dir.path
            self._write_test_repo(str(tmp_base_dir), self.paths)
            done_repos, pending_repos, unconverted_repos, _ = scan_repositories(str(tmp_base_dir))

            updatable_repos, existing_pending_repos = check_repos_which_can_be_updated(done_repos + pending_repos + unconverted_repos)
            self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in updatable_repos]),
                                 sorted(["/repo1", "/repo4", "/repo5", "/repo6", "/repo8"]))
            self.assertListEqual(sorted([repo.split(str(tmp_base_dir))[-1] for repo in existing_pending_repos]),
                                 sorted(["/repo2", "/repo3", "/repo7", "/repo9"]))

    @asyncio_test
    async def test_swap_repositories_invalid(self) -> None:
        with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir:
            tmp_base_dir = tmp_dir.path
            self._write_test_repo(str(tmp_base_dir), self.paths)
            unconverted_repos = scan_repositories(str(tmp_base_dir))[2]
            with self.assertRaises(IfwRepoUpdateError):
                await create_converted_repositories(repogen="foobar-repogen", repositories_to_migrate=unconverted_repos,
                                                    dry_run=True)

    @asyncio_test
    async def test_swap_repositories_valid(self) -> None:
        with TemporaryDirectory(prefix="_repo_tmp_") as tmp_dir:
            tmp_base_dir = tmp_dir.path
            self._write_test_repo(str(tmp_base_dir), self.non_migrated_paths)
            unconverted_repos = scan_repositories(str(tmp_base_dir))[2]
            successful_conversions, failed_conversions = await create_converted_repositories(
                repogen="foobar-repogen", repositories_to_migrate=unconverted_repos, dry_run=True
            )
            self.assertTrue(not failed_conversions)
            # as it was dry-run we need to create the dummy migrated repo directories here
            for _, migrated_repo in successful_conversions.items():
                os.makedirs(migrated_repo)
            operations_ok, operations_nok = swap_repositories(successful_conversions)
            self.assertTrue(not operations_nok)
            self.assertListEqual(sorted(successful_conversions.keys()), sorted(operations_ok.keys()))
            for _, items in operations_ok.items():
                backup_repo_name = items[1]
                self.assertTrue(BACKUP_SUFFIX in backup_repo_name)


if __name__ == '__main__':
    unittest.main()