• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2019 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"""unittest for settings."""
8
9from __future__ import print_function
10
11import unittest
12import unittest.mock as mock
13
14import settings
15import settings_factory
16
17from field import IntegerField
18from field import ListField
19import download_images
20
21from cros_utils import logger
22
23
24class TestSettings(unittest.TestCase):
25  """setting test class."""
26
27  def setUp(self):
28    self.settings = settings.Settings('global_name', 'global')
29
30  def test_init(self):
31    self.assertEqual(self.settings.name, 'global_name')
32    self.assertEqual(self.settings.settings_type, 'global')
33    self.assertIsNone(self.settings.parent)
34
35  def test_set_parent_settings(self):
36    self.assertIsNone(self.settings.parent)
37    settings_parent = {'fake_parent_entry': 0}
38    self.settings.SetParentSettings(settings_parent)
39    self.assertIsNotNone(self.settings.parent)
40    self.assertTrue(isinstance(self.settings.parent, dict))
41    self.assertEqual(self.settings.parent, settings_parent)
42
43  def test_add_field(self):
44    self.assertEqual(self.settings.fields, {})
45    self.settings.AddField(
46        IntegerField(
47            'iterations',
48            default=1,
49            required=False,
50            description='Number of iterations to '
51            'run the test.'))
52    self.assertEqual(len(self.settings.fields), 1)
53    # Adding the same field twice raises an exception.
54    self.assertRaises(Exception, self.settings.AddField, (IntegerField(
55        'iterations',
56        default=1,
57        required=False,
58        description='Number of iterations to run '
59        'the test.')))
60    res = self.settings.fields['iterations']
61    self.assertIsInstance(res, IntegerField)
62    self.assertEqual(res.Get(), 1)
63
64  def test_set_field(self):
65    self.assertEqual(self.settings.fields, {})
66    self.settings.AddField(
67        IntegerField(
68            'iterations',
69            default=1,
70            required=False,
71            description='Number of iterations to run the '
72            'test.'))
73    res = self.settings.fields['iterations']
74    self.assertEqual(res.Get(), 1)
75
76    self.settings.SetField('iterations', 10)
77    res = self.settings.fields['iterations']
78    self.assertEqual(res.Get(), 10)
79
80    # Setting a field that's not there raises an exception.
81    self.assertRaises(Exception, self.settings.SetField, 'remote',
82                      'lumpy1.cros')
83
84    self.settings.AddField(
85        ListField(
86            'remote',
87            default=[],
88            description="A comma-separated list of ip's of "
89            'chromeos devices to run '
90            'experiments on.'))
91    self.assertTrue(isinstance(self.settings.fields, dict))
92    self.assertEqual(len(self.settings.fields), 2)
93    res = self.settings.fields['remote']
94    self.assertEqual(res.Get(), [])
95    self.settings.SetField('remote', 'lumpy1.cros', append=True)
96    self.settings.SetField('remote', 'lumpy2.cros', append=True)
97    res = self.settings.fields['remote']
98    self.assertEqual(res.Get(), ['lumpy1.cros', 'lumpy2.cros'])
99
100  def test_get_field(self):
101    # Getting a field that's not there raises an exception.
102    self.assertRaises(Exception, self.settings.GetField, 'iterations')
103
104    # Getting a required field that hasn't been assigned raises an exception.
105    self.settings.AddField(
106        IntegerField(
107            'iterations',
108            required=True,
109            description='Number of iterations to '
110            'run the test.'))
111    self.assertIsNotNone(self.settings.fields['iterations'])
112    self.assertRaises(Exception, self.settings.GetField, 'iterations')
113
114    # Set the value, then get it.
115    self.settings.SetField('iterations', 5)
116    res = self.settings.GetField('iterations')
117    self.assertEqual(res, 5)
118
119  def test_inherit(self):
120    parent_settings = settings_factory.SettingsFactory().GetSettings(
121        'global', 'global')
122    label_settings = settings_factory.SettingsFactory().GetSettings(
123        'label', 'label')
124    self.assertEqual(parent_settings.GetField('chromeos_root'), '')
125    self.assertEqual(label_settings.GetField('chromeos_root'), '')
126    self.assertIsNone(label_settings.parent)
127
128    parent_settings.SetField('chromeos_root', '/tmp/chromeos')
129    label_settings.SetParentSettings(parent_settings)
130    self.assertEqual(parent_settings.GetField('chromeos_root'), '/tmp/chromeos')
131    self.assertEqual(label_settings.GetField('chromeos_root'), '')
132    label_settings.Inherit()
133    self.assertEqual(label_settings.GetField('chromeos_root'), '/tmp/chromeos')
134
135  def test_override(self):
136    self.settings.AddField(
137        ListField(
138            'email',
139            default=[],
140            description='Space-seperated'
141            'list of email addresses to send '
142            'email to.'))
143
144    global_settings = settings_factory.SettingsFactory().GetSettings(
145        'global', 'global')
146
147    global_settings.SetField('email', 'john.doe@google.com', append=True)
148    global_settings.SetField('email', 'jane.smith@google.com', append=True)
149
150    res = self.settings.GetField('email')
151    self.assertEqual(res, [])
152
153    self.settings.Override(global_settings)
154    res = self.settings.GetField('email')
155    self.assertEqual(res, ['john.doe@google.com', 'jane.smith@google.com'])
156
157  def test_validate(self):
158
159    self.settings.AddField(
160        IntegerField(
161            'iterations',
162            required=True,
163            description='Number of iterations '
164            'to run the test.'))
165    self.settings.AddField(
166        ListField(
167            'remote',
168            default=[],
169            required=True,
170            description='A comma-separated list '
171            "of ip's of chromeos "
172            'devices to run experiments on.'))
173    self.settings.AddField(
174        ListField(
175            'email',
176            default=[],
177            description='Space-seperated'
178            'list of email addresses to '
179            'send email to.'))
180
181    # 'required' fields have not been assigned; should raise an exception.
182    self.assertRaises(Exception, self.settings.Validate)
183    self.settings.SetField('iterations', 2)
184    self.settings.SetField('remote', 'x86-alex.cros', append=True)
185    # Should run without exception now.
186    self.settings.Validate()
187
188  @mock.patch.object(logger, 'GetLogger')
189  @mock.patch.object(download_images.ImageDownloader, 'Run')
190  @mock.patch.object(download_images, 'ImageDownloader')
191  def test_get_xbuddy_path(self, mock_downloader, mock_run, mock_logger):
192
193    mock_run.return_value = 'fake_xbuddy_translation'
194    mock_downloader.Run = mock_run
195    board = 'lumpy'
196    chromeos_root = '/tmp/chromeos'
197    log_level = 'average'
198
199    trybot_str = 'trybot-lumpy-paladin/R34-5417.0.0-b1506'
200    official_str = 'lumpy-release/R34-5417.0.0'
201    xbuddy_str = 'latest-dev'
202    autotest_path = ''
203    debug_path = ''
204    download_debug = False
205
206    self.settings.GetXbuddyPath(trybot_str, autotest_path, debug_path, board,
207                                chromeos_root, log_level, download_debug)
208    self.assertEqual(mock_run.call_count, 1)
209    self.assertEqual(mock_run.call_args_list[0][0], (
210        '/tmp/chromeos',
211        'remote/trybot-lumpy-paladin/R34-5417.0.0-b1506',
212        '',
213        '',
214        False,
215    ))
216
217    mock_run.reset_mock()
218    self.settings.GetXbuddyPath(official_str, autotest_path, debug_path, board,
219                                chromeos_root, log_level, download_debug)
220    self.assertEqual(mock_run.call_count, 1)
221    self.assertEqual(
222        mock_run.call_args_list[0][0],
223        ('/tmp/chromeos', 'remote/lumpy-release/R34-5417.0.0', '', '', False))
224
225    mock_run.reset_mock()
226    self.settings.GetXbuddyPath(xbuddy_str, autotest_path, debug_path, board,
227                                chromeos_root, log_level, download_debug)
228    self.assertEqual(mock_run.call_count, 1)
229    self.assertEqual(
230        mock_run.call_args_list[0][0],
231        ('/tmp/chromeos', 'remote/lumpy/latest-dev', '', '', False))
232
233    if mock_logger:
234      return
235
236
237if __name__ == '__main__':
238  unittest.main()
239