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