def test_empty_context(self): """Test: Run collect_context(["tests/config/empty-context"])""" try: with self.assertLogs("ionit", level="WARNING") as context_manager: failures, context = collect_context( [os.path.join(CONFIG_DIR, "empty-context")], "utf-8" ) except AssertionError: pass self.assertEqual(failures, 0) self.assertEqual(context, {}) self.assertEqual(context_manager.output, [])
def test_ignoring_additional_files(self): """Test: Run collect_context(["tests/config/additional-file"])""" with self.assertLogs("ionit", level="INFO") as context_manager: self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "additional-file")], "utf-8"), (0, {"key": "value"}), ) self.assertEqual(len(context_manager.output), 2) self.assertRegex( context_manager.output[0], ( "INFO:ionit:Skipping configuration file '[^']*config/additional-file/echo', " "because it does not end with .*" ), )
def test_empty_python_file(self): """Test: Run collect_context(["tests/config/empty"])""" with self.assertLogs("ionit", level="WARNING") as context_manager: self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "empty")], "utf-8"), (0, {}) ) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], ( "WARNING:ionit:Python module '[^']+config/empty/empty.py' does " "neither define a collect_context function, nor export functions " r"\(using the ionit_plugin.function decorator\)." ), )
def test_non_dict_context(self): """Test failure for collect_context(["tests/config/non-dict"])""" with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "non-dict")], "utf-8"), (1, {}) ) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], ( "ERROR:ionit:Failed to update context with content from " r"'\S*config/non-dict/invalid.yaml': dictionary update sequence " "element #0 has length 1; 2 is required" ), )
def test_raise_exception(self): """Test failure for collect_context(["tests/config/exception"])""" with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "exception")], "utf-8"), (1, {}) ) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], re.compile( r"ERROR:ionit:Calling collect_context\(\) from " r"'\S*config/exception/exception.py' failed:\n.*\nException: Oops.$", flags=re.DOTALL, ), )
def test_missing_directory(self): """Test: Non-existing context directory""" with self.assertLogs("ionit", level="WARNING") as context_manager: self.assertEqual( collect_context([os.path.join(TESTS_DIR, "non-existing-directory")], "utf-8"), (0, {}), ) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], ( r"WARNING:ionit:Failed to read configuration directory: \[Errno 2\] " r"No such file or directory: '\S*non-existing-directory'" ), )
def test_invalid_yaml(self): """Test: Run collect_context(["tests/config/invalid-yaml"])""" with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "invalid-yaml")], "utf-8"), (1, {}) ) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], ( "ERROR:ionit:Failed to read YAML from " r"'[^']*config/invalid-yaml/invalid.yaml': mapping values are not allowed " r"here\s+in \"\S*config/invalid-yaml/invalid.yaml\", line 3, column 14" ), )
def test_invalid_json(self): """Test: Run collect_context(["tests/config/invalid-json"])""" with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "invalid-json")], "utf-8"), (1, {}) ) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], ( "ERROR:ionit:Failed to read JSON from " "'[^']*config/invalid-json/invalid.json': Expecting property name " r"enclosed in double quotes: line 3 column 1 \(char 22\)" ), )
def test_invalid_python(self): """Test: Run collect_context(["tests/config/invalid-python"])""" with self.assertLogs("ionit", level="ERROR") as context_manager: self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "invalid-python")], "utf-8"), (1, {}) ) self.assertEqual(len(context_manager.output), 1) self.assertRegex( context_manager.output[0], re.compile( "ERROR:ionit:Importing Python module '[^']*config/invalid-python/invalid.py' " r"failed:\n.*\nValueError: invalid literal for int\(\) with base 10: " "'invalid'$", flags=re.DOTALL, ), )
def test_context_stacking(self): """Test: Run collect_context(["tests/config/stacking"])""" self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "stacking")], "utf-8"), (0, {"big_number": 1071, "small_number": 7}), )
def test_configuration_file(self): """Test: Run collect_context(["tests/config/static/second.yaml"])""" self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "static", "second.yaml")], "utf-8"), (0, {"second": 2}), )
def test_collect_static_context(self): """Test: Run collect_context(["tests/config/static"])""" self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "static")], "utf-8"), (0, {"first": 1, "second": 2}), )
def test_collect_function(self): """Test: Run collect_context(["tests/config/function"])""" failures, context = collect_context([os.path.join(CONFIG_DIR, "function")], "utf-8") self.assertEqual(failures, 0) self.assertEqual(set(context.keys()), set(["answer_to_all_questions"])) self.assertEqual(context["answer_to_all_questions"](), 42)
def test_python_module(self): """Test: Run collect_context(["tests/config/python"])""" self.assertEqual( collect_context([os.path.join(CONFIG_DIR, "python")], "utf-8"), (0, {"small": 42, "big": 8000}), )