Exemple #1
0
def module(request):

    m = FileOut(ActorConfig("jsonout", 100, 1, {}, ""),
                path=os.path.join(here, 'results'))
    m.pool.queue.inbox.disableFallThrough()
    request.addfinalizer(partial(shutil.rmtree, os.path.join(here, 'results')))
    return m
Exemple #2
0
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_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_http_response_token_auth_denied():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config,
                      resource={
                          ".*": {
                              "users": [],
                              "tokens": ["abc"],
                              "response": "OK {{uuid}}"
                          }
                      })

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/outbox',
                     headers={"Authorization": "Token abcd"})
    http.stop()
    r.close()

    assert r.status_code == 403
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()
Exemple #6
0
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()
Exemple #7
0
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'
Exemple #8
0
def test_coucdb_filter(db):
    config = ActorConfig('couchdbfilter', 100, 1, {}, "")
    module = CouchdbFilter(
        actor_config=config,
        couchdb_url="{}/{}".format(couchdb_url, DB_NAME),
        view_path="test/all",
        filter_key='._id',
        filter_value='if .mode == "test" then "" else ._id end',
        op="eq")
    data = {"data": "data", "_id": "test_doc", "id": "test_doc"}
    module.couchdb.save(data)
    module.logging.debug = mock.MagicMock()

    module.pool.queue.outbox.disableFallThrough()
    module.pool.queue.inbox.disableFallThrough()
    module.start()

    e = Event(data)
    module.pool.queue.inbox.put(e)
    sleep(1)
    module.logging.debug.assert_called_with("Event from inbox {}".format(e))

    data = module.couchdb.get("test_doc")
    data["mode"] = "test"
    module.couchdb.save(data)
    e = Event(data)
    module.pool.queue.inbox.put(e)
    sleep(1)
    module.logging.debug.assert_called_with(
        'Skipped test_doc by filter. Op: eq old: test_doc new ')
def test_module_http_response_format_extra_params():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config,
                      resource={
                          ".*": {
                              "users": [],
                              "tokens": [],
                              "response": "12345 {{tmp.http.params.one}}"
                          }
                      },
                      htpasswd={})

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283?one=1')
    http.stop()
    r.close()

    assert r.status_code == 200
    assert r.text == "12345 1"
Exemple #10
0
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_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_http_response_user_auth_bad_header1():

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(
        actor_config,
        resource={
            ".*": {
                "users": ["test"],
                "tokens": [],
                "response": "OK {{uuid}}"
            }
        },
        htpasswd={"test": "$apr1$rUKXjcuX$hqdIeoE2Q1Z/GMFhYsNO91"})

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('http://localhost:19283/outbox',
                     headers={"Authorization": "bad test"})
    http.stop()
    r.close()

    assert r.status_code == 400
def test_module_https_response_default():

    requests.packages.urllib3.disable_warnings()
    with open('/tmp/ssl.key', 'w') as f:
        f.write(SSL_KEY)

    with open('/tmp/ssl.cert', 'w') as f:
        f.write(SSL_CERT)

    actor_config = ActorConfig('http',
                               100,
                               1, {},
                               "",
                               disable_exception_handling=True)
    http = HTTPServer(actor_config,
                      ssl_key="/tmp/ssl.key",
                      ssl_cert="/tmp/ssl.cert")

    http.pool.createQueue("outbox")
    http.pool.queue.outbox.disableFallThrough()
    http.start()

    r = requests.get('https://localhost:19283', verify=False)
    http.stop()
    r.close()

    assert r.status_code == 200
    assert r.text.startswith("200 OK")

    unlink("/tmp/ssl.key")
    unlink("/tmp/ssl.cert")
Exemple #14
0
def get_actor(expression):
    actor_config = ActorConfig('modify', 100, 1, {}, "")
    modify = Modify(actor_config, expressions=[expression])

    modify.pool.queue.inbox.disableFallThrough()
    modify.pool.queue.outbox.disableFallThrough()
    modify.start()

    return modify
Exemple #15
0
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()
Exemple #16
0
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_initialize_default():

    actor_config = ActorConfig(
        "elasticsearch", 100, 1, {}, "", disable_exception_handling=True
    )
    try:
        ElasticSearchOut(actor_config)
    except Exception as e:
        assert False, e
    else:
        assert True
Exemple #18
0
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
Exemple #19
0
def generate_actor(rules):

    actor_config = ActorConfig('match', 100, 1, {}, "")
    match = Match(actor_config, rules=rules)

    match.pool.queue.inbox.disableFallThrough()
    for queue in rules.keys():
        match.pool.createQueue(queue)
        getattr(match.pool.queue, queue).disableFallThrough()

    match.start()
    return match
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"
Exemple #21
0
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()
Exemple #22
0
def test_module_fresh_timeout():

    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(2)
    one = getter(fresh.pool.queue.timeout)
    fresh.stop()

    assert one.get() == "timeout"
Exemple #23
0
def test_module_humanlogformat():

    actor_config = ActorConfig('humanlogformat', 100, 1, {}, "")
    humanlogformat = HumanLogFormat(actor_config, colorize=False, ident='setup.py')
    humanlogformat.pool.queue.inbox.disableFallThrough()
    humanlogformat.pool.queue.outbox.disableFallThrough()
    humanlogformat.start()

    e = Event('test')
    e.set(Log(1367682301.430527, 6, 3342, 'Router', 'Received SIGINT. Shutting down.'))

    humanlogformat.pool.queue.inbox.put(e)
    one = getter(humanlogformat.pool.queue.outbox)
    assert one.get() == "2013-05-04T17:45:01 setup.py[3342]: informational Router: Received SIGINT. Shutting down."
Exemple #24
0
def test_module_jsonencode():

    actor_config = ActorConfig('jsonencode', 100, 1, {}, "")
    jsonencode = JSONEncode(actor_config)

    jsonencode.pool.queue.inbox.disableFallThrough()
    jsonencode.pool.queue.outbox.disableFallThrough()
    jsonencode.start()

    e = Event(["one", "two", "three"])

    jsonencode.pool.queue.inbox.put(e)
    one = getter(jsonencode.pool.queue.outbox)
    assert one.get() == '["one", "two", "three"]'
Exemple #25
0
def test_module_dictgenerator_num_values():

    actor_config = ActorConfig('template', 100, 1, {}, "")
    dictgenerator = DictGenerator(actor_config, num_values=True, num_values_min=1, num_values_max=2)

    dictgenerator.pool.queue.outbox.disableFallThrough()
    dictgenerator.start()

    event = getter(dictgenerator.pool.queue.outbox)

    for key, value in event.get().items():
        assert isinstance(value, int)

    assert isinstance(list(event.get().items())[0][1], int)
    assert list(event.get().items())[0][1] >= 1 and list(event.get().items())[0][1] <= 2
Exemple #26
0
def test_module_tippingbucket_time():

    # Bucket spills in 1 second

    actor_config = ActorConfig('tippingbucket', 100, 1, {}, "")
    bucket = TippingBucket(actor_config, bucket_age=1)
    bucket.pool.queue.inbox.disableFallThrough()
    bucket.pool.queue.outbox.disableFallThrough()
    bucket.start()

    bucket.pool.queue.inbox.put(Event("hello"))
    sleep(1)
    b = getter(bucket.pool.queue.outbox)
    assert b.size() == 1
    bucket.stop()
def test_module_jq_valid_jq_expression_no_bool():

    condition = {"name": "test", "expression": '.greeting', "queue": "outbox"}

    actor_config = ActorConfig('jq', 100, 1, {}, "")
    jq = JQ(actor_config, conditions=[condition])

    jq.pool.queue.inbox.disableFallThrough()
    jq.pool.queue.no_match.disableFallThrough()
    jq.start()

    e = Event({"greeting": "hello"})

    jq.pool.queue.inbox.put(e)
    one = getter(jq.pool.queue.no_match)
    assert one.get() == {"greeting": "hello"}
def test_couchdb_output(db):
    config = ActorConfig('couchdbpoller', 100, 1, {}, "")
    module = CouchdbOutput(config,
                           couchdb_url="{}/{}".format(couchdb_url, DB_NAME),
                           bulk=1)

    module.pool.queue.inbox.disableFallThrough()
    module.start()

    data = {"data": "data", "_id": "test_doc"}
    e = Event(data)
    module.pool.queue.inbox.put(e)
    data = module.couchdb.changes(feed="longpoll")
    doc = module.couchdb.get(data['results'][0]['id'])
    assert doc
    assert doc['data'] == 'data'
def test_module_http_ok_server_response():

    webserver = WebServer()
    webserver.start()

    actor_config = ActorConfig('httpoutclient', 100, 1, {}, "")
    http = HTTPOutClient(actor_config, url="http://*****:*****@tmp.httpoutclient.server_response_json")["message"] == "hello world!")
    http.stop()
    webserver.stop()
Exemple #30
0
def test_module_tippingbucket_size():

    # Wraps  10 events into a bulk event.

    actor_config = ActorConfig('tippingbucket', 100, 1, {}, "")
    bucket = TippingBucket(actor_config, bucket_size=10)
    bucket.pool.queue.inbox.disableFallThrough()
    bucket.pool.queue.outbox.disableFallThrough()
    bucket.start()

    for c in range(0, 11):
        bucket.pool.queue.inbox.put(Event(c))

    b = getter(bucket.pool.queue.outbox)
    assert isinstance(b, Bulk)
    assert b.size() == 10
    bucket.stop()