Esempio n. 1
0
def test_add_object(ubusd_test, disconnect_after):
    path = UBUSD_TEST_SOCKET_PATH

    def fake(*args):
        pass

    arguments1 = ("new_object1", {})
    arguments2 = ("new_object2", {"test": {"method": fake, "signature": {}}})
    arguments3 = ("new_object3", {
        "test1": {
            "method": fake,
            "signature": dict(arg1=3)
        },
        "test2": {
            "method": fake,
            "signature": dict(arg2=2)
        },
    })

    with CheckRefCount(path, fake, *(arguments1 + arguments2 + arguments3)):

        ubus.connect(socket_path=path)

        assert ubus.add(*arguments1) is None
        assert ubus.add(*arguments2) is None
        assert ubus.add(*arguments3) is None

        with pytest.raises(RuntimeError):
            ubus.add(*arguments1)

        ubus.disconnect()
def init():
    def send_mail_callback(event, data):
        ret_val = {}

        fromaddr = data['fromaddr']
        toaddr = data['toaddr']
        text = data['text']
        subject = data['subject']
        signature = data['signature']

        ret = mailsender.sendmessage(fromaddr, toaddr, text, subject,
                                     signature, False, False)
        ret_val['status'] = str(ret)

        event.reply(ret_val)

    ubus.add(
        'owrt_email', {
            'send_mail': {
                'method': send_mail_callback,
                'signature': {
                    'fromaddr': ubus.BLOBMSG_TYPE_STRING,
                    'toaddr': ubus.BLOBMSG_TYPE_STRING,
                    'text': ubus.BLOBMSG_TYPE_STRING,
                    'subject': ubus.BLOBMSG_TYPE_STRING,
                    'signature': ubus.BLOBMSG_TYPE_STRING,
                    'ubus_rpc_session': ubus.BLOBMSG_TYPE_STRING
                }
            }
        })
Esempio n. 3
0
def test_multi_objects_listeners(ubusd_test, event_sender, calls_extensive,
                                 disconnect_after):
    counts = 20
    listen_test = {"pass%d" % e: False for e in range(counts)}
    object_test = {"pass%d" % e: False for e in range(counts)}
    event_name = "event_sender"
    timeout = 200

    path = UBUSD_TEST_SOCKET_PATH

    def passed_listen_gen(index):
        def passed(*args):
            listen_test["pass%d" % index] = True

        return passed

    def passed_object_gen(index):
        def passed(*args):
            object_test["pass%d" % index] = True

        return passed

    with CheckRefCount(path, time):

        for _ in range(5):
            ubus.connect(socket_path=path)

            for i in range(counts):
                ubus.listen((event_name, passed_listen_gen(i)))
                ubus.add("extensive_object_%d" % i, {
                    "method": {
                        "method": passed_object_gen(i),
                        "signature": {}
                    }
                })

            stored_counter = calls_extensive.counter.value
            while calls_extensive.counter.value - stored_counter < counts:
                ubus.loop(timeout)
            ubus.disconnect()

            for i in range(counts):
                current = "pass%d" % i
                assert listen_test[current]
                assert object_test[current]

            listen_test = {"pass%d" % e: False for e in range(counts)}
            object_test = {"pass%d" % e: False for e in range(counts)}
Esempio n. 4
0
        def process_function():
            def handler1(handler, data):
                data["passed"] = True
                handler.reply(data)

            def handler2(handler, data):
                data["passed1"] = True
                handler.reply(data)
                data["passed2"] = True
                handler.reply(data)
                data["passed3"] = True
                handler.reply(data)

            def handler_fail(handler, data):
                raise Exception("Handler Fails")

            import ubus
            ubus.connect(UBUSD_TEST_SOCKET_PATH)
            ubus.add(
                "responsive_object",
                {
                    "respond": {
                        "method": handler1,
                        "signature": {
                            "first": ubus.BLOBMSG_TYPE_STRING,
                            "second": ubus.BLOBMSG_TYPE_BOOL,
                            "third": ubus.BLOBMSG_TYPE_INT32,
                        }
                    },
                    "fail": {
                        "method": handler_fail,
                        "signature": {}
                    },
                    "multi_respond": {
                        "method": handler2,
                        "signature": {}
                    },
                    "number": {
                        "method": handler1,
                        "signature": {
                            "number": ubus.BLOBMSG_TYPE_INT32,
                        }
                    },
                },
            )
            guard.touch()
            ubus.loop()
Esempio n. 5
0
        def process_function():
            def handler1(*arg):
                pass

            import ubus
            ubus.connect(UBUSD_TEST_SOCKET_PATH)
            ubus.add(
                "registered_object1",
                {
                    "method1": {
                        "method": handler1,
                        "signature": {}
                    },
                    "method2": {
                        "method": handler1,
                        "signature": {
                            "first": ubus.BLOBMSG_TYPE_STRING,
                            "second": ubus.BLOBMSG_TYPE_BOOL,
                            "third": ubus.BLOBMSG_TYPE_INT32,
                        }
                    },
                },
            )
            ubus.add(
                "registered_object2",
                {},
            )
            ubus.add("registered_object3", {
                "method1": {
                    "method": handler1,
                    "signature": {}
                },
            })
            guard.touch()
            ubus.loop()
Esempio n. 6
0
def test_reply(ubusd_test, call_for_object, disconnect_after):
    path = UBUSD_TEST_SOCKET_PATH

    results = {e: {'data': None, 'exits': False} for e in range(1, 4)}

    def handler1(handler, data):
        results[1]['data'] = data
        handler.reply(data)
        results[1]['exits'] = True

    def handler2(handler, data):
        results[2]['data'] = data
        handler.reply(data)
        results[2]['exits'] = True

    def handler3(handler, data):
        results[3]['data'] = data
        handler.reply(data)
        results[3]['exits'] = True

    with CheckRefCount(path, results, handler1, handler2, handler3):

        ubus.connect(path)
        ubus.add(
            "callee_object",
            {
                "method1": {
                    "method": handler1,
                    "signature": {
                        "first": ubus.BLOBMSG_TYPE_INT32,
                    }
                },
                "method2": {
                    "method": handler2,
                    "signature": {
                        "second": ubus.BLOBMSG_TYPE_INT32,
                    }
                },
                "method3": {
                    "method": handler3,
                    "signature": {}
                },
            },
        )
        ubus.loop(500)

        assert results == {
            1: {
                'data': {
                    'first': 1
                },
                'exits': True
            },
            2: {
                'data': {
                    'second': 2
                },
                'exits': True
            },
            3: {
                'data': {},
                'exits': True
            },
        }

        ubus.disconnect()
Esempio n. 7
0
def test_list_objects(ubusd_test, registered_objects, disconnect_after):
    path = UBUSD_TEST_SOCKET_PATH

    def fake(*args):
        pass

    new_object = ("new_object", {
        "new_method": {
            "method": fake,
            "signature": {}
        }
    })

    expected1 = {
        u"registered_object1": {
            u"method1": {},
            u"method2": {
                u"first": ubus.BLOBMSG_TYPE_STRING,
                u"second": ubus.BLOBMSG_TYPE_BOOL,
                u"third": ubus.BLOBMSG_TYPE_INT32,
            },
        },
        u"registered_object2": {},
        u"registered_object3": {
            u"method1": {},
        },
    }

    expected2 = {
        u"registered_object2": {},
    }

    expected3 = {
        u"registered_object1": {
            u"method1": {},
            u"method2": {
                u"first": ubus.BLOBMSG_TYPE_STRING,
                u"second": ubus.BLOBMSG_TYPE_BOOL,
                u"third": ubus.BLOBMSG_TYPE_INT32,
            },
        },
        u"registered_object2": {},
        u"registered_object3": {
            u"method1": {},
        },
        u"new_object": {
            u"new_method": {},
        },
    }

    expected4 = {
        u"new_object": {
            "new_method": {}
        },
    }

    with CheckRefCount(path, fake, expected1, expected2, expected3, expected4,
                       *new_object):

        ubus.connect(socket_path=path)

        # All objects
        res1 = ubus.objects()
        assert res1 == expected1

        # Filtered objects
        res2 = ubus.objects("registered_object2")
        assert res2 == expected2

        # Append an object
        ubus.add(*new_object)

        # All objects + new
        res3 = ubus.objects()
        assert res3 == expected3

        # New object
        res4 = ubus.objects("new_object")
        assert res4 == expected4

        ubus.disconnect()
        del res1
        del res2
        del res3
        del res4
Esempio n. 8
0
def test_add_object_failed(ubusd_test, registered_objects, disconnect_after):
    path = UBUSD_TEST_SOCKET_PATH

    def fake(*args):
        pass

    type_errors = [
        [],
        ("name", ),
        ("name", {}, False),
        ("name", {
            "test": []
        }),
        ("name", {
            "test": 5
        }),
        ("name", {
            "test": {
                "method": 5,
                "signature": {}
            }
        }),
        ("name", {
            "test": {
                "method": fake
            }
        }),
        ("name", {
            "test": {
                "method": fake,
                "signature": {},
                "another": 5
            }
        }),
    ]

    runtime_errors = [
        ("registered_object1", {
            "test": {
                "method": fake,
                "signature": {}
            }
        }),
        ("registered_object2", {}),
    ]

    with CheckRefCount(path, fake, *(type_errors + runtime_errors)):

        with pytest.raises(RuntimeError):
            ubus.add(*type_errors[0])

        ubus.connect(socket_path=path)

        for wrong_args in type_errors:
            with pytest.raises(TypeError):
                ubus.add(*wrong_args)

        for wrong_args in runtime_errors:
            with pytest.raises(RuntimeError):
                ubus.add(*wrong_args)

        ubus.disconnect()
        del wrong_args
Esempio n. 9
0
db = db_connection.cursor()

# Initialize ubus
ubus.connect()

def read_chunk(handler, data):
    result = []
    for row in db.execute('SELECT date, %s FROM collected_data WHERE date > %s' %  \
            (data['field'], (int(time.time()) - data['range']))):
        result.append((row[0], row[1]))
    handler.reply({"data": result})

ubus.add(
    "ups-pfc8591", {
        "read_data": {
            "method": read_chunk,
            "signature": {"range": ubus.BLOBMSG_TYPE_INT32, "field": ubus.BLOBMSG_TYPE_STRING},
        },
    }
)

db.execute('''CREATE TABLE IF NOT EXISTS collected_data (
    date INTEGER NOT NULL,
    temperature INTEGER NOT NULL,
    light INTEGER NOT NULL,
    voltage INTEGER NOT NULL,
    trimmer INTEGER NOT NULL
)''')

def i2c_get(addr):
    msg = [I2C.Message([addr]), I2C.Message([0x00], read=True)]
    i2c.transfer(0x48, msg)