|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright Contributors to the OpenCue Project |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Tests for `opencue.config`.""" |
| 18 | + |
| 19 | +import os |
| 20 | +import unittest |
| 21 | + |
| 22 | +import mock |
| 23 | +import pyfakefs.fake_filesystem_unittest |
| 24 | + |
| 25 | +import opencue.config |
| 26 | + |
| 27 | + |
| 28 | +EXPECTED_DEFAULT_CONFIG = { |
| 29 | + 'logger.format': '%(levelname)-9s %(module)-10s %(message)s', |
| 30 | + 'logger.level': 'WARNING', |
| 31 | + 'cuebot.protocol': 'tcp', |
| 32 | + 'cuebot.grpc_port': 8443, |
| 33 | + 'cuebot.timeout': 10000, |
| 34 | + 'cuebot.max_message_bytes': 104857600, |
| 35 | + 'cuebot.exception_retries': 3, |
| 36 | + 'cuebot.facility_default': 'local', |
| 37 | + 'cuebot.facility': { |
| 38 | + 'local': ['localhost:8443'], |
| 39 | + 'dev': ['cuetest02-vm.example.com:8443'], |
| 40 | + 'cloud': [ |
| 41 | + 'cuebot1.example.com:8443', |
| 42 | + 'cuebot2.example.com:8443', |
| 43 | + 'cuebot3.example.com:8443' |
| 44 | + ], |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +USER_CONFIG = """ |
| 49 | +cuebot.facility_default: fake-facility-01 |
| 50 | +cuebot.facility: |
| 51 | + fake-facility-01: |
| 52 | + - fake-cuebot-01:1234 |
| 53 | + fake-facility-02: |
| 54 | + - fake-cuebot-02:5678 |
| 55 | + - fake-cuebot-03:9012 |
| 56 | +""" |
| 57 | + |
| 58 | + |
| 59 | +class ConfigTests(pyfakefs.fake_filesystem_unittest.TestCase): |
| 60 | + def setUp(self): |
| 61 | + self.setUpPyfakefs() |
| 62 | + self.fs.add_real_file( |
| 63 | + os.path.join(os.path.dirname(opencue.__file__), 'default.yaml'), read_only=True) |
| 64 | + os.unsetenv('OPENCUE_CONFIG_FILE') |
| 65 | + os.unsetenv('OPENCUE_CONF') |
| 66 | + |
| 67 | + @mock.patch('platform.system', new=mock.Mock(return_value='Linux')) |
| 68 | + @mock.patch('os.path.expanduser', new=mock.Mock(return_value='/home/username')) |
| 69 | + def test__should_return_config_dir_unix(self): |
| 70 | + self.assertEqual('/home/username/.config/opencue', opencue.config.config_base_directory()) |
| 71 | + |
| 72 | + @mock.patch('platform.system', new=mock.Mock(return_value='Windows')) |
| 73 | + @mock.patch( |
| 74 | + 'os.path.expandvars', new=mock.Mock(return_value='C:/Users/username/AppData/Roaming')) |
| 75 | + def test__should_return_config_dir_windows(self): |
| 76 | + self.assertEqual( |
| 77 | + 'C:/Users/username/AppData/Roaming/opencue', opencue.config.config_base_directory()) |
| 78 | + |
| 79 | + def test__should_load_default_config(self): |
| 80 | + self.assertIsNone(os.environ.get('OPENCUE_CONFIG_FILE')) |
| 81 | + self.assertIsNone(os.environ.get('OPENCUE_CONF')) |
| 82 | + |
| 83 | + config = opencue.config.load_config_from_file() |
| 84 | + |
| 85 | + self.assertEqual(EXPECTED_DEFAULT_CONFIG, config) |
| 86 | + |
| 87 | + def test__should_load_user_config(self): |
| 88 | + config_file_path = '/path/to/config.yaml' |
| 89 | + self.fs.create_file(config_file_path, contents=USER_CONFIG) |
| 90 | + os.environ['OPENCUE_CONFIG_FILE'] = config_file_path |
| 91 | + # Define some invalid config using the old setting name, this ensures the old env var |
| 92 | + # will be ignored if the new one is set. |
| 93 | + config_file_path_legacy = '/path/to/legacy/config.yaml' |
| 94 | + self.fs.create_file(config_file_path_legacy, contents='invalid yaml') |
| 95 | + os.environ['OPENCUE_CONF'] = config_file_path_legacy |
| 96 | + |
| 97 | + config = opencue.config.load_config_from_file() |
| 98 | + |
| 99 | + self.assertEqual('fake-facility-01', config['cuebot.facility_default']) |
| 100 | + self.assertEqual(['fake-cuebot-01:1234'], config['cuebot.facility']['fake-facility-01']) |
| 101 | + self.assertEqual( |
| 102 | + ['fake-cuebot-02:5678', 'fake-cuebot-03:9012'], |
| 103 | + config['cuebot.facility']['fake-facility-02']) |
| 104 | + # Settings not defined in user config should still have default values. |
| 105 | + self.assertEqual(10000, config['cuebot.timeout']) |
| 106 | + self.assertEqual(3, config['cuebot.exception_retries']) |
| 107 | + |
| 108 | + def test__should_load_user_config_from_legacy_var(self): |
| 109 | + config_file_path = '/path/to/config.yaml' |
| 110 | + self.fs.create_file(config_file_path, contents=USER_CONFIG) |
| 111 | + os.environ['OPENCUE_CONF'] = config_file_path |
| 112 | + |
| 113 | + config = opencue.config.load_config_from_file() |
| 114 | + |
| 115 | + self.assertEqual('fake-facility-01', config['cuebot.facility_default']) |
| 116 | + self.assertEqual(['fake-cuebot-01:1234'], config['cuebot.facility']['fake-facility-01']) |
| 117 | + self.assertEqual( |
| 118 | + ['fake-cuebot-02:5678', 'fake-cuebot-03:9012'], |
| 119 | + config['cuebot.facility']['fake-facility-02']) |
| 120 | + # Settings not defined in user config should still have default values. |
| 121 | + self.assertEqual(10000, config['cuebot.timeout']) |
| 122 | + self.assertEqual(3, config['cuebot.exception_retries']) |
| 123 | + |
| 124 | + @mock.patch('platform.system', new=mock.Mock(return_value='Linux')) |
| 125 | + @mock.patch('os.path.expanduser', new=mock.Mock(return_value='/home/username')) |
| 126 | + def test__should_load_user_config_from_user_profile(self): |
| 127 | + config_file_path = '/home/username/.config/opencue/opencue.yaml' |
| 128 | + self.fs.create_file(config_file_path, contents=USER_CONFIG) |
| 129 | + |
| 130 | + config = opencue.config.load_config_from_file() |
| 131 | + |
| 132 | + self.assertEqual('fake-facility-01', config['cuebot.facility_default']) |
| 133 | + self.assertEqual(['fake-cuebot-01:1234'], config['cuebot.facility']['fake-facility-01']) |
| 134 | + self.assertEqual( |
| 135 | + ['fake-cuebot-02:5678', 'fake-cuebot-03:9012'], |
| 136 | + config['cuebot.facility']['fake-facility-02']) |
| 137 | + # Settings not defined in user config should still have default values. |
| 138 | + self.assertEqual(10000, config['cuebot.timeout']) |
| 139 | + self.assertEqual(3, config['cuebot.exception_retries']) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + unittest.main() |
0 commit comments