def test_module_count_timeout_pass(): # Standard situation. Event passes through after it appeared 10 times. conditions = { "data": { "value": "one", "occurrence": 3, "window": 2, "action": "pass" } } actor_config = ActorConfig('funnel', 100, 1, {}, "", disable_exception_handling=True) count = Count(actor_config, conditions) count.pool.queue.inbox.disableFallThrough() count.pool.queue.outbox.disableFallThrough() count.pool.queue.dropped.disableFallThrough() count.start() count.pool.queue.inbox.put(Event("one")) assert getter(count.pool.queue.dropped).get() == "one" count.pool.queue.inbox.put(Event("one")) assert getter(count.pool.queue.dropped).get() == "one" sleep(2) count.pool.queue.inbox.put(Event("one")) assert getter(count.pool.queue.dropped).get() == "one" count.stop()
def test_module_http_dynamic_method(): from wishbone.lookup import Lookup class GetMethod(Lookup): def __init__(self): self.a = cycle(["POST", "PUT"]) def lookup(self): return next(self.a) webserver = WebServer() webserver.start() actor_config = ActorConfig('httpoutclient', 100, 1, {"method": GetMethod().lookup}, "") http = HTTPOutClient(actor_config, url="http://localhost:8088/", accept="monkeyballs", method="~~method()") http.pool.queue.inbox.disableFallThrough() http.start() http.pool.queue.inbox.put(Event('{"one": 1}')) sleep(1) assert getter(webserver.q)["REQUEST_METHOD"] == "POST" http.pool.queue.inbox.put(Event('{"one": 1}')) sleep(1) assert getter(webserver.q)["REQUEST_METHOD"] == "PUT" http.stop() webserver.stop()
def test_module_switch_event(): actor_config = ActorConfig('switch', 100, 1, {}, "", disable_exception_handling=True) switch = Switch(actor_config, outgoing="one") switch.pool.queue.inbox.disableFallThrough() switch.pool.queue.outbox.disableFallThrough() switch.pool.createQueue("one") switch.pool.queue.one.disableFallThrough() switch.pool.createQueue("two") switch.pool.queue.two.disableFallThrough() switch.start() event_one = Event("one") switch.pool.queue.inbox.put(event_one) assert getter(switch.pool.queue.one).get() == "one" event_two = Event("two") switch_event = Event("two") switch.pool.queue.switch.put(switch_event) switch.pool.queue.inbox.put(event_two) assert getter(switch.pool.queue.two).get() == "two" switch.stop()
def test_module_fanout(): actor_config = ActorConfig('fanout', 100, 1, {}, "", disable_exception_handling=True) fanout = Fanout(actor_config) fanout.pool.queue.inbox.disableFallThrough() fanout.pool.createQueue("one") fanout.pool.queue.one.disableFallThrough() fanout.pool.createQueue("two") fanout.pool.queue.two.disableFallThrough() fanout.start() e = Event(["hello"]) fanout.pool.queue.inbox.put(e) one = getter(fanout.pool.queue.one) two = getter(fanout.pool.queue.two) fanout.stop() assert one.get() == ["hello"] assert two.get() == ["hello"] assert id(one.get()) != id(two.get())
def test_module_fanout(): actor_config = ActorConfig('fanout', 100, 1, {}, "") fanout = Fanout(actor_config) fanout.pool.queue.inbox.disableFallThrough() fanout.pool.createQueue("one") fanout.pool.queue.one.disableFallThrough() fanout.pool.createQueue("two") fanout.pool.queue.two.disableFallThrough() fanout.start() e = Event(["hello"]) fanout.pool.queue.inbox.put(e) one = getter(fanout.pool.queue.one) two = getter(fanout.pool.queue.two) fanout.stop() assert one.get() == ["hello"] assert two.get() == ["hello"] assert id(one.get()) != id(two.get())
def test_module_funnel(): actor_config = ActorConfig('funnel', 100, 1, {}, "") funnel = Funnel(actor_config) funnel.pool.queue.outbox.disableFallThrough() funnel.pool.createQueue("one") funnel.pool.queue.one.disableFallThrough() funnel.pool.createQueue("two") funnel.pool.queue.two.disableFallThrough() funnel.start() event_one = Event("one") event_two = Event("two") funnel.pool.queue.one.put(event_one) funnel.pool.queue.two.put(event_two) assert getter(funnel.pool.queue.outbox).get() in ["one", "two"] assert getter(funnel.pool.queue.outbox).get() in ["one", "two"] funnel.stop()
def test_module_roundrobin(): actor_config = ActorConfig('roundrobin', 100, 1, {}, "") roundrobin = RoundRobin(actor_config) roundrobin.pool.queue.inbox.disableFallThrough() roundrobin.pool.createQueue("one") roundrobin.pool.queue.one.disableFallThrough() roundrobin.pool.createQueue("two") roundrobin.pool.queue.two.disableFallThrough() roundrobin.start() event_one = Event("one") event_two = Event("two") roundrobin.pool.queue.inbox.put(event_one) roundrobin.pool.queue.inbox.put(event_two) assert getter(roundrobin.pool.queue.one).get() in ["one", "two"] assert getter(roundrobin.pool.queue.two).get() in ["one", "two"] roundrobin.stop()
def test_module_unpack(): actor_config = ActorConfig('unpack', 100, 1, {}, "") unpack = Unpack(actor_config) unpack.pool.queue.inbox.disableFallThrough() unpack.pool.queue.outbox.disableFallThrough() unpack.start() bulk = Event(bulk=True) for _ in range(0, 10): bulk.appendBulk(Event()) unpack.pool.queue.inbox.put(bulk) for _ in range(0, 10): assert getter(unpack.pool.queue.outbox) try: getter(unpack.pool.queue.outbox) except Exception: assert True else: assert False
def test_module_jsonvalidate(): actor_config = ActorConfig('jsonvalidate', 100, 1, {}, "") with open("/tmp/jsonvalidate.jsonschema", "w") as j: j.write('{"type": "object", "properties": {"one": { "type": "integer"}}}') jsonvalidate = JSONValidate(actor_config, "/tmp/jsonvalidate.jsonschema") jsonvalidate.pool.queue.inbox.disableFallThrough() jsonvalidate.pool.queue.outbox.disableFallThrough() jsonvalidate.pool.queue.failed.disableFallThrough() jsonvalidate.start() valid = Event({"one": 1}) invalid = Event({"one": "one"}) jsonvalidate.pool.queue.inbox.put(valid) valid_event = getter(jsonvalidate.pool.queue.outbox) jsonvalidate.pool.queue.inbox.put(invalid) invalid_event = getter(jsonvalidate.pool.queue.failed) os.remove("/tmp/jsonvalidate.jsonschema") assert valid_event.get() == {"one": 1} assert invalid_event.get() == {"one": "one"}
def test_module_decode_service_checkresult(): data = re.sub(' {2,4}', "\t", service_checkresult) actor_config = ActorConfig('pd', 100, 1, {}, "") pd = CheckResult(actor_config) pd.pool.queue.inbox.disableFallThrough() pd.pool.queue.outbox.disableFallThrough() pd.start() e = Event(data) pd.pool.queue.inbox.put(e) sleep(1) assert pd.pool.queue.outbox.size() == 8 one = getter(pd.pool.queue.outbox).get() assert isinstance(one, Metric) assert one.name == 'service1.time' assert one.value == '0.02' two = getter(pd.pool.queue.outbox).get() assert isinstance(two, Metric) assert two.name == 'service1.cat' assert two.value == '20'
def test_module_switch_event(): actor_config = ActorConfig('switch', 100, 1, {}, "") switch = Switch(actor_config, outgoing="one") switch.pool.queue.inbox.disableFallThrough() switch.pool.queue.outbox.disableFallThrough() switch.pool.createQueue("one") switch.pool.queue.one.disableFallThrough() switch.pool.createQueue("two") switch.pool.queue.two.disableFallThrough() switch.start() event_one = Event("one") switch.pool.queue.inbox.put(event_one) assert getter(switch.pool.queue.one).get() == "one" event_two = Event("two") switch_event = Event("two") switch.pool.queue.switch.put(switch_event) switch.pool.queue.inbox.put(event_two) assert getter(switch.pool.queue.two).get() == "two" switch.stop()
def test_module_count_default_dropped(): # Standard situation. Events get dropped after it appeared 10 times. conditions = { "data": { "value": "one", "occurrence": 10, "window": 60, "action": "drop" } } actor_config = ActorConfig('funnel', 100, 1, {}, "", disable_exception_handling=True) count = Count(actor_config, conditions) count.pool.queue.inbox.disableFallThrough() count.pool.queue.outbox.disableFallThrough() count.pool.queue.dropped.disableFallThrough() count.start() for _ in range(0, 9): count.pool.queue.inbox.put( Event("one") ) result = getter(count.pool.queue.outbox).get() assert result == "one" count.pool.queue.inbox.put( Event("one") ) assert getter(count.pool.queue.dropped).get() == "one" count.stop()
def test_file_load(): import os import yaml import shutil os.mkdir('/tmp/wishbone_tests') actor_config = ActorConfig('match', 100, 1, {}, "") match = Match(actor_config, location="/tmp/wishbone_tests") match.pool.createQueue("file") match.pool.queue.file.disableFallThrough() match.pool.queue.inbox.disableFallThrough() #Test 1 rule_1 = { "condition": [ {"file": "re:.*?one.*"} ], "queue": [ {"file": {}} ] } with open('/tmp/wishbone_tests/one.yaml', 'w') as one: one.write(yaml.dump(rule_1, default_flow_style=False)) match.start() sleep(1) e = Event("test") e.set({"file": "zero one two"}) match.pool.queue.inbox.put(e) assert getter(match.pool.queue.file).get()["file"] == "zero one two" # Test 2 rule_2 = { "condition": [ {"file": "re:.*?two.*"} ], "queue": [ {"file": {}} ] } with open('/tmp/wishbone_tests/two.yaml', 'w') as one: one.write(yaml.dump(rule_2, default_flow_style=False)) sleep(1) e = Event("test") e.set({"file": "one two three"}) match.pool.queue.inbox.put(e) assert getter(match.pool.queue.file).get()["file"] == "one two three" shutil.rmtree('/tmp/wishbone_tests')
def test_module_httpinclientTimeout(): actor_config = ActorConfig('httpinclient', 100, 1, {}, "") http = HTTPInClient(actor_config, url="http://www.github.com", interval=1, allow_redirects=True, timeout=0.001) http.pool.queue.outbox.disableFallThrough() http.start() sleep(3) try: getter(http.pool.queue.failed) except Exception: assert True else: assert False
def test_module_copy_default(): a = get_actor({"copy": ["does.not.exist", "@tmp.copy", "default"]}) e = Event({"greeting": "hi"}) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert "default" == one.get('@tmp.copy')
def test_module_queueselect_novalidqueue(): actor_config = ActorConfig('queueselect', 100, 1, {}, "", disable_exception_handling=True) templates = [ { "name": "rule_1", "queue": "{{ 'no_such_queue_1' if data.one == 1 else 'no_such_queue_2' }}", }, { "name": "rule_2", "queue": "{{ 'no_such_queue_1' if data.one == 1 else 'no_such_queue_2' }}", } ] queueselect = QueueSelect(actor_config, templates=templates) queueselect.pool.queue.inbox.disableFallThrough() queueselect.pool.queue.outbox.disableFallThrough() queueselect.pool.queue.nomatch.disableFallThrough() queueselect.pool.createQueue("queue_1") queueselect.pool.queue.queue_1.disableFallThrough() queueselect.pool.createQueue("queue_2") queueselect.pool.queue.queue_2.disableFallThrough() queueselect.start() queueselect.pool.queue.inbox.put(Event({"one": 1, "two": 2})) assert getter(queueselect.pool.queue.nomatch).get() == {"one": 1, "two": 2} queueselect.stop()
def test_module_queueselect_regex(): actor_config = ActorConfig('queueselect', 100, 1, {}, "", disable_exception_handling=True) template = { "name": "name of the rule", "queue": "{{ 'queue_1' if regex('\d', data.one) else 'queue_2' }}", "payload": { "queue_1": { "detail_1": "some value", "detail_2": "some other value", }, "queue_2": { "detail_1": "some value", "detail_2": "some other value", } } } queueselect = QueueSelect(actor_config, templates=[template]) queueselect.pool.queue.inbox.disableFallThrough() queueselect.pool.queue.outbox.disableFallThrough() queueselect.pool.queue.nomatch.disableFallThrough() queueselect.start() queueselect.pool.queue.inbox.put(Event({"one": 1, "two": "two"})) assert getter(queueselect.pool.queue.nomatch).get() == {"one": 1, "two": "two"} queueselect.stop()
def test_module_del_item(): a = get_actor({"del_item": ["fubar", "@data"]}) e = Event(["one", "two", "three", "fubar"]) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert "fubar" not in one.get('@data')
def test_module_delete(): a = get_actor({"delete": ["@data.two"]}) e = Event({"one": 1, "two": 2}) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert "two" not in one.get('@data').keys()
def test_module_fresh_repeat(): actor_config = ActorConfig('fresh', 100, 1, {}, "") fresh = Fresh(actor_config, timeout=1, repeat_interval=1) fresh.pool.queue.inbox.disableFallThrough() fresh.pool.queue.outbox.disableFallThrough() fresh.pool.queue.timeout.disableFallThrough() fresh.start() sleep(1) getter(fresh.pool.queue.timeout) sleep(1.5) one = getter(fresh.pool.queue.timeout) fresh.stop() assert one.get() == "timeout"
def test_module_extract(): a = get_actor({"extract": ["destination", "(?P<one>.*?)\ (?P<two>.*)\ (?P<three>.*)", "@data"]}) e = Event("een twee drie") a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('destination.one') == "een"
def test_module_replace(): a = get_actor({"replace": ['\d', "X", "@data"]}) e = Event("hello 123 hello") a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('@data') == "hello XXX hello"
def test_module_jq_disk_rule_reload(): condition = { "expression": '.greeting | test("hello")', "queue": "outbox" } dumpFile(condition) actor_config = ActorConfig('jq', 100, 1, {}, "") jq = JQ(actor_config, location="./test_rules") jq.pool.queue.inbox.disableFallThrough() jq.pool.queue.no_match.disableFallThrough() jq.start() condition2 = { "expression": '.greeting | test( "hi")', "queue": "outbox" } dumpFile(condition2) e = Event({"greeting": "hi"}) jq.pool.queue.inbox.put(e) one = getter(jq.pool.queue.no_match) assert one.get() == {"greeting": "hi"}
def test_module_lowercase(): a = get_actor({"lowercase": ["@data.lower"]}) e = Event({"lower": "HELLO"}) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('@data.lower') == "hello"
def test_module_join(): a = get_actor({"join": ['@data', ",", "@tmp.joined"]}) e = Event(["one", "two", "three"]) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('@tmp.joined') == "one,two,three"
def test_module_template(): a = get_actor({"template": ["result", "Good day in {language} is {word}.", "@data"]}) e = Event({"language": "German", "word": "gutten Tag"}) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('result') == "Good day in German is gutten Tag."
def test_module_modify_set(): a = get_actor({"set": ["hi", "blah"]}) e = Event('hello') a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('blah') == "hi"
def test_module_template(): a = get_actor( {"template": ["result", "Good day in {language} is {word}.", "@data"]}) e = Event({"language": "German", "word": "gutten Tag"}) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('result') == "Good day in German is gutten Tag."
def test_module_copy(): a = get_actor({"copy": ["@data", "@tmp.copy", "n/a"]}) e = Event({"greeting": "hi"}) a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert "hi" == one.get('@tmp.copy')["greeting"] assert id(one.get("@data")) != id(one.get("@tmp.copy"))
def test_module_fresh_recovery(): actor_config = ActorConfig('fresh', 100, 1, {}, "") fresh = Fresh(actor_config, timeout=1) fresh.pool.queue.inbox.disableFallThrough() fresh.pool.queue.outbox.disableFallThrough() fresh.pool.queue.timeout.disableFallThrough() fresh.start() sleep(1) one = getter(fresh.pool.queue.timeout) event = Event("test") fresh.pool.queue.inbox.put(event) sleep(1) two = getter(fresh.pool.queue.timeout) fresh.stop() assert two.get() == "recovery"
def test_module_merge(): a = get_actor({"merge": ['@tmp.one', '@tmp.two', '@data']}) e = Event() e.set(["one"], "@tmp.one") e.set(["two"], "@tmp.two") a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get() == ["one", "two"]
def test_fileread(module): module.start() path = os.path.join(here, 'data.xml') e = Event(path) module.pool.queue.inbox.put(e) doc = getter(module.pool.queue.outbox).get() if not doc: assert False assert doc == XML
def test_module_extract(): a = get_actor({ "extract": ["destination", "(?P<one>.*?)\ (?P<two>.*)\ (?P<three>.*)", "@data"] }) e = Event("een twee drie") a.pool.queue.inbox.put(e) one = getter(a.pool.queue.outbox) assert one.get('destination.one') == "een"
def test_module_deserialize_deserialize(): actor_config = ActorConfig('deserialize', 100, 1, {}, "") deserialize = Deserialize(actor_config) deserialize.pool.queue.inbox.disableFallThrough() deserialize.pool.queue.outbox.disableFallThrough() deserialize.start() e = Event([{"one": 1}, {"two": 2}, {"three": 3}]) deserialize.pool.queue.inbox.put(e) one = getter(deserialize.pool.queue.outbox) two = getter(deserialize.pool.queue.outbox) three = getter(deserialize.pool.queue.outbox) assert one.get() == {"one": 1} assert two.get() == {"two": 2} assert three.get() == {"three": 3} assert three.get('@tmp.deserialize.generated_by') == True
def test_module_inotify_default(): # Standard situation. Monitors the changes to a file. actor_config = ActorConfig('inotify', 100, 1, {}, "") filename = "/tmp/%s" % str(uuid4()) open(filename, 'a').close() inotify = WBInotify(actor_config, initial_listing=True, paths={filename: []}) inotify.pool.queue.outbox.disableFallThrough() inotify.start() sleep(1) os.unlink(filename) sleep(1) e = getter(inotify.pool.queue.outbox) assert e.get() == {"path": filename, "inotify_type": "WISHBONE_INIT"} assert getter(inotify.pool.queue.outbox).get()["inotify_type"] == "IN_ATTRIB" assert getter(inotify.pool.queue.outbox).get()["inotify_type"] == "IN_DELETE_SELF"
def test_module_generator_dict(): actor_config = ActorConfig('generator', 100, 1, {}, "") test_event = Generator(actor_config, payload={"one": 1}) test_event.pool.queue.outbox.disableFallThrough() test_event.start() event = getter(test_event.pool.queue.outbox) assert event.get()["one"] == 1
def test_module_jsondecode_strict(): actor_config = ActorConfig('jsondecode', 100, 1, {}, "") jsondecode = JSONDecode(actor_config) jsondecode.pool.queue.inbox.disableFallThrough() jsondecode.pool.queue.outbox.disableFallThrough() jsondecode.start() e = Event('''{"one": "een\n"}''') jsondecode.pool.queue.inbox.put(e) try: getter(jsondecode.pool.queue.outbox) except Exception: assert True else: assert False
def test_module_dictgenerator_num_elements(): actor_config = ActorConfig('template', 100, 1, {}, "") dictgenerator = DictGenerator(actor_config, min_elements=1, max_elements=2) dictgenerator.pool.queue.outbox.disableFallThrough() dictgenerator.start() event = getter(dictgenerator.pool.queue.outbox) assert len(event.get().keys()) >= 1 and len(event.get().keys()) <= 2
def test_module_dictgenerator_randomize_keys(): actor_config = ActorConfig('template', 100, 1, {}, "") dictgenerator = DictGenerator(actor_config, randomize_keys=False) dictgenerator.pool.queue.outbox.disableFallThrough() dictgenerator.start() event = getter(dictgenerator.pool.queue.outbox) assert '0' in event.get().keys()
def test_xml2dict(module): module.start() e = Event(XML) module.pool.queue.inbox.put(e) doc = getter(module.pool.queue.outbox).get() if not doc: assert False assert doc['mydocument']['@has'] == 'an attribute' assert doc['mydocument']['and']['many'] == ['elements', 'more elements'] assert doc['mydocument']['plus']['@a'] == 'complex' assert doc['mydocument']['plus']['#text'] == 'element as well'
def test_jq_nomatch(): config = ActorConfig('jq', 100, 1, {}, "") module = JQFilter(actor_config=config, conditions=[{ 'name': "Doc mode filter", 'queue': 'test_docs', 'expression': '.mode == "test"' }]) event_data = {"date": "date", "_id": "id", "id": "id"} module.pool.queue.test_docs.disableFallThrough() module.pool.queue.inbox.disableFallThrough() module.start() e = Event(event_data) module.pool.queue.inbox.put(e) with pytest.raises(Exception) as e: getter(module.pool.queue.test_docs) assert e.message == 'No event from queue'
def test_module_cron_default(): actor_config = ActorConfig('cron', 100, 1, {}, "") cron = Cron(actor_config, '*/1 * * * *') cron.pool.queue.outbox.disableFallThrough() cron.start() one = getter(cron.pool.queue.outbox) cron.stop() assert one.get() == "wishbone"
def test_module_dictgenerator_keys(): actor_config = ActorConfig('template', 100, 1, {}, "") dictgenerator = DictGenerator(actor_config, keys=["one", "two"]) dictgenerator.pool.queue.outbox.disableFallThrough() dictgenerator.start() event = getter(dictgenerator.pool.queue.outbox) assert "one" in event.get().keys() assert "two" in event.get().keys()
def test_couchdb_input(db): config = ActorConfig('couchdbpoller', 100, 1, {}, "") module = CouchdbChangesInput( config, couchdb_url="{}/{}".format(couchdb_url, DB_NAME), ) module.pool.queue.outbox.disableFallThrough() module.start() module.couchdb.save({"data": "data", "id": "id"}) one = getter(module.pool.queue.outbox) assert one.get().get('data') == "data"