diff --git a/osc/core.py b/osc/core.py index 5005a977..bda78ddf 100644 --- a/osc/core.py +++ b/osc/core.py @@ -8905,7 +8905,7 @@ def vc_export_env(apiurl: str, quiet=False): for (tag, envs) in tag2envs.items(): env_present = [env for env in envs if env in os.environ] - config_present = tag in conf.config['api_host_options'][apiurl] + config_present = bool(conf.config['api_host_options'][apiurl].get(tag, None)) if not env_present and not config_present: missing_tags.append(tag) elif config_present: diff --git a/tests/test_vc.py b/tests/test_vc.py new file mode 100644 index 00000000..491a677a --- /dev/null +++ b/tests/test_vc.py @@ -0,0 +1,85 @@ +import importlib +import os +import unittest + +import osc.conf +from osc.core import vc_export_env + +from .common import GET +from .common import patch + + +class TestVC(unittest.TestCase): + def setUp(self): + importlib.reload(osc.conf) + + config = osc.conf.config + host_options = osc.conf.HostOptions( + config, apiurl="http://localhost", username="Admin" + ) + config.api_host_options[host_options["apiurl"]] = host_options + config["apiurl"] = host_options["apiurl"] + self.host_options = host_options + + def tearDown(self): + importlib.reload(osc.conf) + + @patch.dict(os.environ, {}, clear=True) + def test_vc_export_env_conf(self): + self.host_options.realname = "" + self.host_options.email = "" + vc_export_env("http://localhost") + expected = { + "VC_REALNAME": "", + "VC_MAILADDR": "", + "mailaddr": "", + } + self.assertEqual(os.environ, expected) + + @patch.dict(os.environ, {}, clear=True) + @GET( + "http://localhost/person/Admin", + text="Adminroot@localhostOBS Instance Superuser", + ) + def test_vc_export_env_conf_realname(self): + self.host_options.realname = "" + vc_export_env("http://localhost") + expected = { + "VC_REALNAME": "", + "VC_MAILADDR": "root@localhost", + "mailaddr": "root@localhost", + } + self.assertEqual(os.environ, expected) + + @patch.dict(os.environ, {}, clear=True) + @GET( + "http://localhost/person/Admin", + text="Adminroot@localhostOBS Instance Superuser", + ) + def test_vc_export_env_conf_email(self): + self.host_options.email = "" + vc_export_env("http://localhost") + expected = { + "VC_REALNAME": "OBS Instance Superuser", + "VC_MAILADDR": "", + "mailaddr": "", + } + self.assertEqual(os.environ, expected) + + @patch.dict(os.environ, {}, clear=True) + @GET( + "http://localhost/person/Admin", + text="Adminroot@localhostOBS Instance Superuser", + ) + def test_vc_export_env_api_call(self): + vc_export_env("http://localhost") + expected = { + "VC_REALNAME": "OBS Instance Superuser", + "VC_MAILADDR": "root@localhost", + "mailaddr": "root@localhost", + } + self.assertEqual(os.environ, expected) + + +if __name__ == "__main__": + unittest.main()