1# Copyright 2019 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import json 6import os 7import shutil 8import unittest 9import tempfile 10 11import common 12from autotest_lib.utils.side_effects import config_loader 13from autotest_lib.utils.side_effects.proto import config_pb2 14 15 16class LoadTestCase(unittest.TestCase): 17 """Test loading side_effects_config.json.""" 18 19 def setUp(self): 20 self._results_dir = tempfile.mkdtemp() 21 22 def tearDown(self): 23 shutil.rmtree(self._results_dir) 24 25 def test_missing_config_file(self): 26 parsed_config = config_loader.load(self._results_dir) 27 self.assertIsNone(parsed_config) 28 29 def test_full_config(self): 30 config = { 31 'tko': { 32 'proxy_socket': '/file-system/foo-socket', 33 'mysql_user': 'foo-user', 34 'mysql_password_file': '/file-system/foo-password-file' 35 }, 36 'google_storage': { 37 'bucket': 'foo-bucket', 38 'credentials_file': '/file-system/foo-creds' 39 }, 40 'this_field_is_ignored': True 41 } 42 path = os.path.join(self._results_dir, 43 'side_effects_config.json') 44 with open(path, 'w') as f: 45 f.write(json.dumps(config)) 46 parsed_config = config_loader.load(self._results_dir) 47 48 self.assertEqual(parsed_config.tko.proxy_socket, 49 '/file-system/foo-socket') 50 self.assertEqual(parsed_config.tko.mysql_user, 'foo-user') 51 self.assertEqual(parsed_config.tko.mysql_password_file, 52 '/file-system/foo-password-file') 53 self.assertEqual(parsed_config.google_storage.bucket, 'foo-bucket') 54 self.assertEqual(parsed_config.google_storage.credentials_file, 55 '/file-system/foo-creds') 56 self.assertEqual(parsed_config.chrome_perf.enabled, False) 57 self.assertEqual(parsed_config.cts.enabled, False) 58 59 def test_bad_config(self): 60 path = os.path.join(self._results_dir, 61 'side_effects_config.json') 62 with open(path, 'w') as f: 63 f.write('Not a JSON, hence unparseable') 64 65 with self.assertRaisesRegexp(Exception, 'JSON'): 66 parsed_config = config_loader.load(self._results_dir) 67 68 69class ValidateTKOTestCase(unittest.TestCase): 70 """Test validating the TKO-related fields of a side_effects.Config proto.""" 71 72 def setUp(self): 73 self._tempdir = tempfile.mkdtemp() 74 self._config = config_pb2.Config( 75 tko=config_pb2.TKOConfig( 76 proxy_socket=_tempfile(self._tempdir), 77 mysql_user='foo-user', 78 mysql_password_file=_tempfile(self._tempdir) 79 )) 80 81 def tearDown(self): 82 shutil.rmtree(self._tempdir) 83 84 def test_complete_config(self): 85 config_loader.validate_tko(self._config) 86 87 def test_missing_proxy_socket_path(self): 88 self._config.tko.proxy_socket = '' 89 with self.assertRaisesRegexp(ValueError, 'proxy socket'): 90 config_loader.validate_tko(self._config) 91 92 def test_missing_mysql_user(self): 93 self._config.tko.mysql_user = '' 94 with self.assertRaisesRegexp(ValueError, 'MySQL user'): 95 config_loader.validate_tko(self._config) 96 97 def test_missing_mysql_password_file_path(self): 98 self._config.tko.mysql_password_file = '' 99 with self.assertRaisesRegexp(ValueError, 'MySQL password'): 100 config_loader.validate_tko(self._config) 101 102 def test_missing_proxy_socket_file(self): 103 os.remove(self._config.tko.proxy_socket) 104 with self.assertRaisesRegexp(OSError, 'proxy socket'): 105 config_loader.validate_tko(self._config) 106 107 def test_missing_mysql_password_file(self): 108 os.remove(self._config.tko.mysql_password_file) 109 with self.assertRaisesRegexp(OSError, 'MySQL password'): 110 config_loader.validate_tko(self._config) 111 112 113class ValidateGoogleStorageTestCase(unittest.TestCase): 114 """Test validating the TKO-related fields of a side_effects.Config proto.""" 115 116 def setUp(self): 117 self._tempdir = tempfile.mkdtemp() 118 self._config = config_pb2.Config( 119 google_storage=config_pb2.GoogleStorageConfig( 120 bucket='foo-bucket', 121 credentials_file=_tempfile(self._tempdir) 122 )) 123 124 def tearDown(self): 125 shutil.rmtree(self._tempdir) 126 127 def test_complete_config(self): 128 config_loader.validate_google_storage(self._config) 129 130 def test_missing_bucket(self): 131 self._config.google_storage.bucket = '' 132 with self.assertRaisesRegexp(ValueError, 'bucket'): 133 config_loader.validate_google_storage(self._config) 134 135 def test_missing_credentials_file_path(self): 136 self._config.google_storage.credentials_file = '' 137 with self.assertRaisesRegexp(ValueError, 'credentials'): 138 config_loader.validate_google_storage(self._config) 139 140 def test_missing_credentials_file(self): 141 os.remove(self._config.google_storage.credentials_file) 142 with self.assertRaisesRegexp(OSError, 'credentials'): 143 config_loader.validate_google_storage(self._config) 144 145 146def _tempfile(tempdir): 147 _, name = tempfile.mkstemp(dir=tempdir) 148 return name 149 150 151if __name__ == '__main__': 152 unittest.main() 153