Skip to content

Commit

Permalink
Standardize config env var and paths. (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcipriano committed Feb 27, 2022
1 parent b0faee1 commit bdbd8c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
6 changes: 5 additions & 1 deletion cuesubmit/cuesubmit/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import os
import yaml

import opencue.config


CONFIG_FILE_ENV_VAR = 'CUESUBMIT_CONFIG_FILE'

Expand All @@ -35,7 +37,9 @@ def getConfigValues():
"""Reads the config file from disk and returns the values it defines."""
configData = {}
configFile = os.environ.get(CONFIG_FILE_ENV_VAR)
if configFile and os.path.exists(configFile):
if not configFile:
configFile = os.path.join(opencue.config.config_base_directory(), 'cuesubmit.yaml')
if os.path.exists(configFile):
with open(configFile, 'r') as data:
try:
configData = yaml.load(data, Loader=yaml.SafeLoader)
Expand Down
57 changes: 39 additions & 18 deletions cuesubmit/tests/Config_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
from __future__ import absolute_import

import os
import tempfile
import unittest

import mock
import pyfakefs.fake_filesystem_unittest

import cuesubmit.Config


Expand All @@ -36,28 +38,47 @@
CONFIG_YAML_INVALID = b' " some text in an unclosed quote'


class ConfigTests(unittest.TestCase):
class ConfigTests(pyfakefs.fake_filesystem_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs()
if 'CUESUBMIT_CONFIG_FILE' in os.environ:
del os.environ['CUESUBMIT_CONFIG_FILE']

def test__should_skip_missing_files_without_error(self):
configData = cuesubmit.Config.getConfigValues()

self.assertDictEqual({}, configData)

def test__should_load_config_from_env_var(self):
config_file_path = '/path/to/config.yaml'
self.fs.create_file(config_file_path, contents=CONFIG_YAML)
os.environ['CUESUBMIT_CONFIG_FILE'] = config_file_path

configData = cuesubmit.Config.getConfigValues()

self.assertEqual('OPENCUESUBMIT', configData.get('UI_NAME'))
self.assertEqual('OpenCue Submit', configData.get('SUBMIT_APP_WINDOW_TITLE'))
self.assertEqual(None, configData.get('SOME_UNKNOWN_SETTING'))

def testGetConfigValues(self):
with tempfile.NamedTemporaryFile() as fp:
fp.write(CONFIG_YAML)
fp.flush()
os.environ[cuesubmit.Config.CONFIG_FILE_ENV_VAR] = fp.name
@mock.patch('platform.system', new=mock.Mock(return_value='Linux'))
@mock.patch('os.path.expanduser', new=mock.Mock(return_value='/home/username'))
def test__should_load_config_from_user_profile(self):
config_file_path = '/home/username/.config/opencue/cuesubmit.yaml'
self.fs.create_file(config_file_path, contents=CONFIG_YAML)

configData = cuesubmit.Config.getConfigValues()
configData = cuesubmit.Config.getConfigValues()

self.assertEqual('OPENCUESUBMIT', configData.get('UI_NAME'))
self.assertEqual('OpenCue Submit', configData.get('SUBMIT_APP_WINDOW_TITLE'))
self.assertEqual(None, configData.get('SOME_UNKNOWN_SETTING'))
self.assertEqual('OPENCUESUBMIT', configData.get('UI_NAME'))
self.assertEqual('OpenCue Submit', configData.get('SUBMIT_APP_WINDOW_TITLE'))
self.assertEqual(None, configData.get('SOME_UNKNOWN_SETTING'))

def testFailOnInvalidYaml(self):
with tempfile.NamedTemporaryFile() as fp:
fp.write(CONFIG_YAML_INVALID)
fp.flush()
os.environ[cuesubmit.Config.CONFIG_FILE_ENV_VAR] = fp.name
def test__should_fail_on_invalid_yaml(self):
config_file_path = '/path/to/config.yaml'
self.fs.create_file(config_file_path, contents=CONFIG_YAML_INVALID)
os.environ['CUESUBMIT_CONFIG_FILE'] = config_file_path

with self.assertRaises(cuesubmit.Config.CuesubmitConfigError):
cuesubmit.Config.getConfigValues()
with self.assertRaises(cuesubmit.Config.CuesubmitConfigError):
cuesubmit.Config.getConfigValues()


if __name__ == '__main__':
Expand Down
8 changes: 5 additions & 3 deletions pycue/tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ def setUp(self):
self.setUpPyfakefs()
self.fs.add_real_file(
os.path.join(os.path.dirname(opencue.__file__), 'default.yaml'), read_only=True)
os.unsetenv('OPENCUE_CONFIG_FILE')
os.unsetenv('OPENCUE_CONF')
if 'OPENCUE_CONFIG_FILE' in os.environ:
del os.environ['OPENCUE_CONFIG_FILE']
if 'OPENCUE_CONF' in os.environ:
del os.environ['OPENCUE_CONF']

@mock.patch('platform.system', new=mock.Mock(return_value='Linux'))
@mock.patch('os.path.expanduser', new=mock.Mock(return_value='/home/username'))
Expand Down Expand Up @@ -106,7 +108,7 @@ def test__should_load_user_config(self):
self.assertEqual(3, config['cuebot.exception_retries'])

def test__should_load_user_config_from_legacy_var(self):
config_file_path = '/path/to/config.yaml'
config_file_path = '/path/to/legacy/config.yaml'
self.fs.create_file(config_file_path, contents=USER_CONFIG)
os.environ['OPENCUE_CONF'] = config_file_path

Expand Down

0 comments on commit bdbd8c9

Please sign in to comment.