From 2afc35fdace1f345fd3e1d2b7e7746c6fa26f872 Mon Sep 17 00:00:00 2001 From: Ziv Nevo <79099626+zivnevo@users.noreply.github.com> Date: Wed, 26 Jul 2023 11:53:19 +0300 Subject: [PATCH] Iterating over documents to force parser throwing any exceptions it encounters (#574) --- nca/FileScanners/GenericTreeScanner.py | 9 ++++++++- tests/bad_yamls/single_bracket.json | 1 + tests/bad_yamls/tab_in_json.json | 3 +++ tests/expected_runtime/k8s_tests_expected_runtime.csv | 1 + tests/k8s_cmdline_tests.yaml | 6 ++++++ 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/bad_yamls/single_bracket.json create mode 100644 tests/bad_yamls/tab_in_json.json diff --git a/nca/FileScanners/GenericTreeScanner.py b/nca/FileScanners/GenericTreeScanner.py index 60efbe6c6..e8cd929e4 100644 --- a/nca/FileScanners/GenericTreeScanner.py +++ b/nca/FileScanners/GenericTreeScanner.py @@ -75,6 +75,13 @@ def convert_documents(documents): return [to_yaml_objects(document) for document in documents] +def leave_documents_as_is(documents): + """ + Forces the parser to yield all documents and throw parse errors (if any) at this point of time + """ + return [document for document in documents] + + class GenericTreeScanner(abc.ABC): """ A base class for reading yaml files @@ -107,7 +114,7 @@ def _yield_yaml_file(self, path, stream): """ try: if self.fast_load: - documents = yaml.load_all(stream, Loader=yaml.CSafeLoader) + documents = leave_documents_as_is(yaml.load_all(stream, Loader=yaml.CSafeLoader)) else: documents = convert_documents(yaml.compose_all(stream, Loader=yaml.CSafeLoader)) yield YamlFile(documents, path) diff --git a/tests/bad_yamls/single_bracket.json b/tests/bad_yamls/single_bracket.json new file mode 100644 index 000000000..98232c64f --- /dev/null +++ b/tests/bad_yamls/single_bracket.json @@ -0,0 +1 @@ +{ diff --git a/tests/bad_yamls/tab_in_json.json b/tests/bad_yamls/tab_in_json.json new file mode 100644 index 000000000..ead884617 --- /dev/null +++ b/tests/bad_yamls/tab_in_json.json @@ -0,0 +1,3 @@ +{ + "key": "value" +} diff --git a/tests/expected_runtime/k8s_tests_expected_runtime.csv b/tests/expected_runtime/k8s_tests_expected_runtime.csv index d214d06b6..10878f154 100644 --- a/tests/expected_runtime/k8s_tests_expected_runtime.csv +++ b/tests/expected_runtime/k8s_tests_expected_runtime.csv @@ -55,6 +55,7 @@ test,run_time(seconds) "k8s_cmdline_tests.yaml, query name: test25-expl",1.08 "k8s_cmdline_tests.yaml, query name: test4-expl",1.08 "k8s_cmdline_tests.yaml, query name: istio-ingress-expl",4.1 +"k8s_cmdline_tests.yaml, query name: bad-yamls",0.05 fw_rules_tests/policies/cyclonus-simple-example-scheme.yaml,0.08 fw_rules_tests/policies/label_expr_test_1-scheme.yaml,0.38 fw_rules_tests/policies/poc1-scheme.yaml,0.64 diff --git a/tests/k8s_cmdline_tests.yaml b/tests/k8s_cmdline_tests.yaml index 1cf9adfa5..3e11b25ff 100644 --- a/tests/k8s_cmdline_tests.yaml +++ b/tests/k8s_cmdline_tests.yaml @@ -500,3 +500,9 @@ -d --expected_output expected_cmdline_output_files/istio-ingress_expl_output.txt expected: 0 + +- name: bad-yamls + args: > + --connectivity + -r bad_yamls + expected: 4