diff --git a/insights/core/serde.py b/insights/core/serde.py index f711a90185..d068da6717 100644 --- a/insights/core/serde.py +++ b/insights/core/serde.py @@ -200,8 +200,9 @@ def dehydrate(self, comp, broker): doc = None try: name = dr.get_name(c) - errors = [t for e in broker.exceptions.get(c, []) - for t in broker.tracebacks[e]] + # The `broker.trackbacks` is a dict in which the values are string + # but not list of strings + errors = [broker.tracebacks[e] for e in broker.exceptions.get(c, [])] doc = { "name": name, "exec_time": broker.exec_times.get(c), diff --git a/insights/tests/test_add_exception.py b/insights/tests/test_add_exception.py new file mode 100644 index 0000000000..7abac0433a --- /dev/null +++ b/insights/tests/test_add_exception.py @@ -0,0 +1,31 @@ +from insights.core import dr +from insights.core.plugins import datasource, rule, make_info, ContentException +from insights.core.spec_factory import RegistryPoint, SpecSet + + +class Specs(SpecSet): + the_data = RegistryPoint() + + +class TestSpecs(Specs): + @datasource() + def the_data(broker): + raise ContentException('Fake Datasource') + + +@rule(Specs.the_data) +def report(dt): + return make_info('INFO_1') + + +def test_broker_add_exception(): + broker = dr.run(report) + assert report in broker + assert TestSpecs.the_data in broker.exceptions + spec_exs = broker.exceptions[TestSpecs.the_data] + exs = [ex for ex in spec_exs if isinstance(ex, ContentException) and str(ex) == "Fake Datasource"] + assert len(exs) == 1 + tb = broker.tracebacks[exs[0]] + assert type(tb) is str + assert "Traceback" in tb + assert "Fake Datasource" in tb diff --git a/insights/tests/test_serde.py b/insights/tests/test_serde.py index 2a71854cec..9ebd60e462 100644 --- a/insights/tests/test_serde.py +++ b/insights/tests/test_serde.py @@ -1,14 +1,20 @@ import os +import json from tempfile import mkdtemp from insights import dr -from insights.core.plugins import component +from insights.core.plugins import (component, + datasource, + rule, + make_info, + ContentException) from insights.core.serde import (serializer, deserializer, Hydration, marshal, unmarshal) from insights.util import fs +from insights.core.spec_factory import RegistryPoint, SpecSet class Foo(object): @@ -52,6 +58,21 @@ def deserialize_foo(_type, data, root=None): return foo +class Specs(SpecSet): + the_data = RegistryPoint() + + +class TestSpecs(Specs): + @datasource() + def the_data(broker): + raise ContentException('Fake Datasource') + + +@rule(Specs.the_data) +def report(dt): + return make_info('INFO_1') + + def test_marshal(): broker = dr.Broker() foo = Foo() @@ -231,3 +252,26 @@ def test_round_trip(): pass if os.path.exists(tmp_path): fs.remove(tmp_path) + + +def test_dehydrate(): + broker = dr.run(report) + exc = next(iter(broker.tracebacks)) + tb = broker.tracebacks[exc] + + tmp_path = mkdtemp() + spec_the_data = TestSpecs.the_data + try: + h = Hydration(tmp_path) + h.dehydrate(spec_the_data, broker) + fn = ".".join([dr.get_name(spec_the_data), h.ser_name]) + meta_data_json = os.path.join(h.meta_data, fn) + assert os.path.exists(meta_data_json) + with open(meta_data_json, 'r') as fp: + ret = json.load(fp) + assert "errors" in ret + assert ret["errors"][0] == tb + assert "Traceback" in tb + assert "Fake Datasource" in tb + finally: + fs.remove(tmp_path)