def test_get_with_unknown_section(self): """ get() will return `None` for a section name that is not known. """ config.Config().cfg.clear() config.Config()._load_from_file() self.assertTrue(config.Config().get("Anything") is None)
def test_get_with_known_section(self): """ get() will correctly return configuration data for known sections. """ content = ''' [E] f=6 g=h''' site_path = touch(content=textwrap.dedent(content)) os.environ["OQ_SITE_CFG_PATH"] = site_path config.Config().cfg.clear() config.Config()._load_from_file() self.assertEqual({"f": "6", "g": "h"}, config.Config().get("E"))
def test_get_section_merely_calls_get_on_config_data_dict(self): "config.get_section() merely makes use of Config().get()" "" orig_method = config.Config().get def fake_get(section): self.assertEqual("f@k3", section) return {"this": "is", "so": "fake"} config.Config().get = fake_get self.assertEqual({ "this": "is", "so": "fake" }, config.get_section("f@k3")) config.Config().get = orig_method
def test_load_from_file_with_local(self): """The config data in the local file is loaded correctly.""" content = ''' [C] c=3 d=e [D] d=4''' local_path = touch(content=textwrap.dedent(content)) os.environ["OQ_LOCAL_CFG_PATH"] = local_path config.Config().cfg.clear() config.Config()._load_from_file() self.assertEqual(["C", "D"], sorted(config.Config().cfg.keys())) self.assertEqual({"c": "3", "d": "e"}, config.Config().cfg.get("C")) self.assertEqual({"d": "4"}, config.Config().cfg.get("D"))
def test_load_from_file_with_global(self): """The config data in the global file is loaded correctly.""" content = ''' [A] a=1 b=c [B] b=2''' site_path = touch(content=textwrap.dedent(content)) os.environ["OQ_SITE_CFG_PATH"] = site_path config.Config().cfg.clear() config.Config()._load_from_file() self.assertEqual(["A", "B"], sorted(config.Config().cfg.keys())) self.assertEqual({"a": "1", "b": "c"}, config.Config().cfg.get("A")) self.assertEqual({"b": "2"}, config.Config().cfg.get("B"))
def test_get_paths_with_local_env_var_set(self): """ _get_paths() will honour the OQ_LOCAL_CFG_PATH variable. """ os.environ["OQ_LOCAL_CFG_PATH"] = "/e/f/g/h" self.assertEqual(["/etc/openquake/openquake.cfg", "/e/f/g/h"], config.Config()._get_paths())
def test_is_readable_one_plus_files_have_permissions(self): """ When at least one config file is present and we have permission to read it is_readable() returns `True`. """ os.environ["OQ_SITE_CFG_PATH"] = "/etc/passwd" os.environ["OQ_LOCAL_CFG_PATH"] = "/etc/passwd-" self.assertTrue(config.Config().is_readable())
def test_is_readable_all_files_lack_permissions(self): """ When we miss read permissions for all config files is_readable() returns `False`. """ os.environ["OQ_SITE_CFG_PATH"] = "/etc/sudoers" os.environ["OQ_LOCAL_CFG_PATH"] = "/etc/passwd-" self.assertFalse(config.Config().is_readable())
def test_load_from_file_with_local_and_global(self): """ The config data in the local and global files is loaded correctly. """ content = ''' [A] a=1 b=c [B] b=2''' site_path = touch(content=textwrap.dedent(content)) os.environ["OQ_SITE_CFG_PATH"] = site_path content = ''' [C] c=3 d=e [D] d=4''' local_path = touch(content=textwrap.dedent(content)) os.environ["OQ_LOCAL_CFG_PATH"] = local_path config.Config().cfg.clear() config.Config()._load_from_file() self.assertEqual(["A", "B", "C", "D"], sorted(config.Config().cfg.keys())) self.assertEqual({"a": "1", "b": "c"}, config.Config().cfg.get("A")) self.assertEqual({"b": "2"}, config.Config().cfg.get("B")) self.assertEqual({"c": "3", "d": "e"}, config.Config().cfg.get("C")) self.assertEqual({"d": "4"}, config.Config().cfg.get("D"))
def test_get_paths_with_no_environ(self): """ _get_paths() will return the hard-coded paths if the OQ_SITE_CFG_PATH and the OQ_LOCAL_CFG_PATH variables are not set. """ self.assertEqual([ "/etc/openquake/openquake.cfg", "%s/openquake.cfg" % os.path.abspath(os.getcwd()) ], config.Config()._get_paths())
def test_get_paths_with_global_env_var_set(self): """ _get_paths() will honour the OQ_SITE_CFG_PATH environment variable. """ os.environ["OQ_SITE_CFG_PATH"] = "/a/b/c/d" self.assertEqual( ["/a/b/c/d", "%s/openquake.cfg" % os.path.abspath(os.getcwd())], config.Config()._get_paths())
def test_load_from_file_with_no_config_files(self): """In the absence of config files the `cfg` dict will be empty.""" config.Config().cfg.clear() config.Config()._load_from_file() self.assertEqual([], config.Config().cfg.keys())
def test_is_readable_no_file_present(self): """When no config file is present is_readable() returns `False`.""" os.environ["OQ_SITE_CFG_PATH"] = "/this/does/not/exist.cfg" os.environ["OQ_LOCAL_CFG_PATH"] = "/nor/does/this.cfg" self.assertFalse(config.Config().is_readable())
def tearDown(self): config.Config().cfg.clear() config.Config()._load_from_file()