Beispiel #1
0
def test_pass_fd_invalid():
    with host.ServiceManager() as mgr:

        client = mgr.start(str("test-invalid-fd"), __file__)
        with pytest.raises(host.RemoteError):
            client.call_with_fds("invalid-fd")
        client.call_with_fds("check-fds-are-closed")
Beispiel #2
0
def test_pass_fd():
    with host.ServiceManager() as mgr:
        for i in range(3):
            client = mgr.start(str(i), __file__)

            args = ["an", "argument"]
            data = "osbuild\n"

            with tempfile.TemporaryFile("w+") as f:
                f.write(data)
                f.seek(0)

                res, fds = client.call_with_fds("echo-fd",
                                                args,
                                                fds=[f.fileno()])

            assert args == res
            with os.fdopen(fds.steal(0)) as d:
                assert data == d.read()

            client.call_with_fds("check-fds-are-closed")

            remote_id = client.call("identify")
            assert remote_id == str(i)

            with pytest.raises(ValueError, match=f"{str(i)}"):
                _ = mgr.start(str(i), __file__)

        for i in range(3):
            client = mgr.services[str(i)]
            client.stop()
Beispiel #3
0
def test_loopback_basic(tmpdir):
    index = meta.Index(os.curdir)
    info = index.get_module_info("Device", "org.osbuild.loopback")

    tree = os.path.join(tmpdir, "tree")
    os.makedirs(tree)

    devpath = os.path.join(tmpdir, "dev")
    os.makedirs(devpath)

    size = 1024 * 1024
    filename = os.path.join(tree, "image.img")
    with open(filename, "wb") as f:
        f.truncate(size)
        sb = os.fstat(f.fileno())

    testfile = os.path.join(tmpdir, "test.img")

    options = {
        "filename": "image.img",
        "start": 0,
        "size": size // 512  # size is in sectors / blocks
    }

    dev = devices.Device("loop", info, None, options)

    with host.ServiceManager() as mgr:
        devmgr = devices.DeviceManager(mgr, devpath, tree)
        reply = devmgr.open(dev)
        assert reply
        assert reply["path"]

        node = reply["path"]
        assert os.path.exists(os.path.join(devpath, node))

        minor = reply["node"]["minor"]
        lo = loop.Loop(minor)
        li = lo.get_status()

        assert li.lo_offset == 0
        assert li.lo_sizelimit == size
        assert li.lo_inode == sb.st_ino

        with pytest.raises(OSError):
            with open(testfile, "wb") as f:
                f.truncate(1)
                lo.set_fd(f.fileno())

        lo.close()

        uid = f"device/{dev.name}"
        client = mgr.services[uid]

        client.call("close", None)

        lo = loop.Loop(minor)
        with open(filename, "r") as f:
            assert not lo.is_bound_to(f.fileno())
Beispiel #4
0
def test_signals():
    with host.ServiceManager() as mgr:
        exec_callback = 0

        def check_value(item, _fds):
            nonlocal exec_callback
            assert item == exec_callback
            exec_callback += 1

        client = mgr.start("test_signal_me_3_times", __file__)
        client.call_with_fds("signal_me_3_times", on_signal=check_value)
        assert exec_callback == 3
Beispiel #5
0
def test_basic():
    with host.ServiceManager() as mgr:
        for i in range(3):
            client = mgr.start(str(i), __file__)

            args = ["an", "argument"]
            res = client.call("echo", args)

            assert args == res

            remote_id = client.call("identify")
            assert remote_id == str(i)

            with pytest.raises(ValueError, match=f"{str(i)}"):
                _ = mgr.start(str(i), __file__)

        for i in range(3):
            client = mgr.services[str(i)]
            client.stop()
Beispiel #6
0
def test_signals_on_separate_fd():
    with host.ServiceManager() as mgr:

        data = "osbuild\n"
        exec_callback = False

        def check_value(item, fds):
            nonlocal exec_callback
            exec_callback = True
            assert item == "that should do it"
            with os.fdopen(fds.steal(0)) as d:
                assert data == d.read()

        client = mgr.start("test_signal_me_on_fd", __file__)

        with tempfile.TemporaryFile("w+") as f:
            f.write(data)
            f.seek(0)

            client.call_with_fds("signal_me_on_fd",
                                 fds=[f.fileno()],
                                 on_signal=check_value)
        assert exec_callback
Beispiel #7
0
def test_exception():
    with host.ServiceManager() as mgr:
        client = mgr.start("exception", __file__)
        with pytest.raises(host.RemoteError, match=r"Remote Exception"):
            client.call("exception")
Beispiel #8
0
    httpd = socketserver.TCPServer(('', 80), Handler)
    barrier.wait()
    httpd.serve_forever()


def make_test_cases():
    sources = os.path.join(test.TestBase.locate_test_data(), "sources")
    if os.path.exists(sources):
        for source in os.listdir(sources):
            for case in os.listdir(f"{sources}/{source}/cases"):
                yield source, case


def check_case(source, case, store, libdir):
    with host.ServiceManager() as mgr:
        expects = case["expects"]
        if expects == "error":
            with pytest.raises(host.RemoteError):
                source.download(mgr, store, libdir)
        elif expects == "success":
            source.download(mgr, store, libdir)
        else:
            raise ValueError(f"invalid expectation: {expects}")


@pytest.fixture(name="tmpdir")
def tmpdir_fixture():
    with tempfile.TemporaryDirectory() as tmp:
        yield tmp