summaryrefslogtreecommitdiff
path: root/init_test.py
blob: 65e88da3fa6a133836a723cde3460279b3ba1245 (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
# Copyright (C) 2024 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.

"""Tests for init.py"""

import argparse
import io
import json
import unittest
import urllib.parse
from typing import Generator

from init import (KleafBootstrap, _API_ENDPOINT_PREFIX,
                  _ARTIFACT_URL_FMT, _DEFAULT_BUILD_TARGET)

# pylint: disable=protected-access


def _get_branches() -> Generator[str, None, None]:
    common_url = _API_ENDPOINT_PREFIX + "branches"

    page_token = None
    while True:
        parsed = urllib.parse.urlparse(common_url)
        query_dict = urllib.parse.parse_qs(parsed.query)
        if page_token:
            query_dict["pageToken"] = page_token
        query_dict["fields"] = "branches.name,nextPageToken"
        parsed = parsed._replace(
            query=urllib.parse.urlencode(query_dict, doseq=True))
        url = parsed.geturl()

        buf = io.BytesIO()
        KleafBootstrap._download(url, "branches", buf)
        buf.seek(0)
        json_obj = json.load(buf)
        page_token = json_obj.get("nextPageToken")
        for branch in json_obj.get("branches", []):
            yield branch["name"]

        if not page_token:
            return


def _get_supported_branches() -> Generator[str, None, None]:
    for branch in _get_branches():
        if not branch.startswith("aosp_kernel-common-android"):
            continue
        if branch == "aosp_kernel-common-android-mainline":
            yield branch
            continue
        android_release = branch.removeprefix(
            "aosp_kernel-common-android").split("-", maxsplit=1)[0]
        if not android_release.isdecimal():
            continue
        android_release = int(android_release)
        if android_release < 15:
            continue
        yield branch


class KleafBootstrapTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        print("INFO: Calculating supported branches")
        cls.supported_branches = _get_supported_branches()
        return super().setUpClass()

    def test_infer_build_id(self):
        for branch in self.supported_branches:
            with self.subTest(branch=branch):
                obj = KleafBootstrap(argparse.Namespace(
                    branch=branch,
                    build_target=_DEFAULT_BUILD_TARGET,
                    build_id=None,
                    ddk_workspace=None,
                    kleaf_repo=None,
                    local=None,
                    url_fmt=_ARTIFACT_URL_FMT,
                ), [])
                obj._set_build_id()
                print(f"For branch {branch}, checking {obj.build_id}")
                buf = io.BytesIO()
                obj._download_artifact("BUILD_INFO", buf)

    @unittest.skip("init_ddk.zip is not published yet")
    def test_run_with_help(self):
        for branch in self.supported_branches:
            with self.subTest(branch=branch):
                obj = KleafBootstrap(argparse.Namespace(
                    branch=branch,
                    build_target=_DEFAULT_BUILD_TARGET,
                    build_id=None,
                    url_fmt=_ARTIFACT_URL_FMT,
                ), ["-h"])
                obj.run()


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