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