1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2014 The Chromium OS Authors. All rights reserved.
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
9from __future__ import print_function
10
11import argparse
12import sys
13
14import download_images
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    options = parser.parse_known_args(sys.argv[1:])[0]
36    if options.chromeos_root is None:
37      self._usage(parser, '--chromeos_root must be set')
38    self.chromeos_root = options.chromeos_root
39    self.tests_passed = 0
40    self.tests_run = 0
41    self.tests_failed = 0
42
43  def _usage(self, parser, message):
44    print('ERROR: ' + message)
45    parser.print_help()
46    sys.exit(0)
47
48  def print_test_status(self):
49    print('----------------------------------------\n')
50    print('Tests attempted: %d' % self.tests_run)
51    print('Tests passed:    %d' % self.tests_passed)
52    print('Tests failed:    %d' % self.tests_failed)
53    print('\n----------------------------------------')
54
55  def assert_failure(self, msg):
56    print('Assert failure: %s' % msg)
57    self.print_test_status()
58    sys.exit(1)
59
60  def assertIsNotNone(self, arg, arg_name):
61    if arg is None:
62      self.tests_failed = self.tests_failed + 1
63      self.assert_failure('%s is not None' % arg_name)
64
65  def assertNotEqual(self, arg1, arg2, arg1_name, arg2_name):
66    if arg1 == arg2:
67      self.tests_failed = self.tests_failed + 1
68      self.assert_failure('%s is not NotEqual to %s' % (arg1_name, arg2_name))
69
70  def assertEqual(self, arg1, arg2, arg1_name, arg2_name):
71    if arg1 != arg2:
72      self.tests_failed = self.tests_failed + 1
73      self.assert_failure('%s is not Equal to %s' % (arg1_name, arg2_name))
74
75  def test_one_id(self, downloader, test_id, result_string, exact_match):
76    print("Translating '%s'" % test_id)
77    self.tests_run = self.tests_run + 1
78
79    result = downloader.GetBuildID(self.chromeos_root, test_id)
80    # Verify that we got a build id back.
81    self.assertIsNotNone(result, 'result')
82
83    # Verify that the result either contains or exactly matches the
84    # result_string, depending on the exact_match argument.
85    if exact_match:
86      self.assertEqual(result, result_string, 'result', result_string)
87    else:
88      self.assertNotEqual(result.find(result_string), -1, 'result.find', '-1')
89    self.tests_passed = self.tests_passed + 1
90
91  def test_get_build_id(self):
92    """Test that the actual translating of xbuddy names is working properly."""
93    downloader = download_images.ImageDownloader(log_level='quiet')
94
95    self.test_one_id(downloader, 'remote/lumpy/latest-dev', 'lumpy-release/R',
96                     False)
97    self.test_one_id(downloader,
98                     'remote/trybot-lumpy-release-afdo-use/R35-5672.0.0-b86',
99                     'trybot-lumpy-release-afdo-use/R35-5672.0.0-b86', True)
100    self.test_one_id(downloader, 'remote/lumpy-release/R35-5672.0.0',
101                     'lumpy-release/R35-5672.0.0', True)
102    self.test_one_id(downloader, 'remote/lumpy/latest-dev', 'lumpy-release/R',
103                     False)
104    self.test_one_id(downloader, 'remote/lumpy/latest-official',
105                     'lumpy-release/R', False)
106    self.test_one_id(downloader, 'remote/lumpy/latest-beta', 'lumpy-release/R',
107                     False)
108
109    self.print_test_status()
110
111
112if __name__ == '__main__':
113  tester = ImageDownloaderBuildIDTest()
114  tester.test_get_build_id()
115