xref: /aosp_15_r20/external/toolchain-utils/crosperf/download_images_buildid_test.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2014 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Test translation of xbuddy names."""
8
9
10import argparse
11import sys
12
13import download_images
14
15
16# On May 1, 2014:
17# latest         : lumpy-release/R34-5500.132.0
18# latest-beta    : lumpy-release/R35-5712.43.0
19# latest-official: lumpy-release/R36-5814.0.0
20# latest-dev     : lumpy-release/R36-5814.0.0
21# latest-canary  : lumpy-release/R36-5814.0.0
22
23
24class ImageDownloaderBuildIDTest(object):
25    """Test translation of xbuddy names."""
26
27    def __init__(self):
28        parser = argparse.ArgumentParser()
29        parser.add_argument(
30            "-c",
31            "--chromeos_root",
32            dest="chromeos_root",
33            help="Directory containing ChromeOS root.",
34        )
35
36        options = parser.parse_known_args(sys.argv[1:])[0]
37        if options.chromeos_root is None:
38            self._usage(parser, "--chromeos_root must be set")
39        self.chromeos_root = options.chromeos_root
40        self.tests_passed = 0
41        self.tests_run = 0
42        self.tests_failed = 0
43
44    def _usage(self, parser, message):
45        print("ERROR: " + message)
46        parser.print_help()
47        sys.exit(0)
48
49    def print_test_status(self):
50        print("----------------------------------------\n")
51        print("Tests attempted: %d" % self.tests_run)
52        print("Tests passed:    %d" % self.tests_passed)
53        print("Tests failed:    %d" % self.tests_failed)
54        print("\n----------------------------------------")
55
56    def assert_failure(self, msg):
57        print("Assert failure: %s" % msg)
58        self.print_test_status()
59        sys.exit(1)
60
61    def assertIsNotNone(self, arg, arg_name):
62        if arg is None:
63            self.tests_failed = self.tests_failed + 1
64            self.assert_failure("%s is not None" % arg_name)
65
66    def assertNotEqual(self, arg1, arg2, arg1_name, arg2_name):
67        if arg1 == arg2:
68            self.tests_failed = self.tests_failed + 1
69            self.assert_failure(
70                "%s is not NotEqual to %s" % (arg1_name, arg2_name)
71            )
72
73    def assertEqual(self, arg1, arg2, arg1_name, arg2_name):
74        if arg1 != arg2:
75            self.tests_failed = self.tests_failed + 1
76            self.assert_failure(
77                "%s is not Equal to %s" % (arg1_name, arg2_name)
78            )
79
80    def test_one_id(self, downloader, test_id, result_string, exact_match):
81        print("Translating '%s'" % test_id)
82        self.tests_run = self.tests_run + 1
83
84        result = downloader.GetBuildID(self.chromeos_root, test_id)
85        # Verify that we got a build id back.
86        self.assertIsNotNone(result, "result")
87
88        # Verify that the result either contains or exactly matches the
89        # result_string, depending on the exact_match argument.
90        if exact_match:
91            self.assertEqual(result, result_string, "result", result_string)
92        else:
93            self.assertNotEqual(
94                result.find(result_string), -1, "result.find", "-1"
95            )
96        self.tests_passed = self.tests_passed + 1
97
98    def test_get_build_id(self):
99        """Test that the actual translating of xbuddy names is working properly."""
100        downloader = download_images.ImageDownloader(log_level="quiet")
101
102        self.test_one_id(
103            downloader, "remote/lumpy/latest-dev", "lumpy-release/R", False
104        )
105        self.test_one_id(
106            downloader,
107            "remote/trybot-lumpy-release-afdo-use/R35-5672.0.0-b86",
108            "trybot-lumpy-release-afdo-use/R35-5672.0.0-b86",
109            True,
110        )
111        self.test_one_id(
112            downloader,
113            "remote/lumpy-release/R35-5672.0.0",
114            "lumpy-release/R35-5672.0.0",
115            True,
116        )
117        self.test_one_id(
118            downloader, "remote/lumpy/latest-dev", "lumpy-release/R", False
119        )
120        self.test_one_id(
121            downloader, "remote/lumpy/latest-official", "lumpy-release/R", False
122        )
123        self.test_one_id(
124            downloader, "remote/lumpy/latest-beta", "lumpy-release/R", False
125        )
126
127        self.print_test_status()
128
129
130if __name__ == "__main__":
131    tester = ImageDownloaderBuildIDTest()
132    tester.test_get_build_id()
133