Skip to content

Allow passing config as a fixture #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/mctpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ struct ctx {
sd_bus *bus;

// Configuration
const char *config_filename;
char *config_filename;

mctp_nl *nl;

Expand Down Expand Up @@ -3573,7 +3573,7 @@ static int add_interface(struct ctx *ctx, int ifindex)
return -ENOENT;
}

struct link *link = malloc(sizeof(*link));
struct link *link = calloc(1, sizeof(*link));
if (!link)
return -ENOMEM;

Expand Down Expand Up @@ -3829,6 +3829,11 @@ static void setup_config_defaults(struct ctx *ctx)
ctx->default_role = ENDPOINT_ROLE_BUS_OWNER;
}

static void free_config(struct ctx *ctx)
{
free(ctx->config_filename);
}

int main(int argc, char **argv)
{
struct ctx ctxi = {0}, *ctx = &ctxi;
Expand Down Expand Up @@ -3900,6 +3905,7 @@ int main(int argc, char **argv)
free_links(ctx);
free_peers(ctx);
free_nets(ctx);
free_config(ctx);

mctp_nl_close(ctx->nl);

Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ async def dbus():
yield bus

@pytest.fixture
async def mctpd(nursery, dbus, sysnet):
m = fake_mctpd.MctpdWrapper(dbus, sysnet)
def config():
return None

@pytest.fixture
async def mctpd(nursery, dbus, sysnet, config):
m = fake_mctpd.MctpdWrapper(dbus, sysnet, config = config)
await m.start_mctpd(nursery)
yield m
res = await m.stop_mctpd()
Expand Down
7 changes: 7 additions & 0 deletions tests/mctp_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ async def mctpd_mctp_iface_obj(dbus, iface):
)
return await obj.get_interface('au.com.codeconstruct.MCTP.BusOwner1')

async def mctpd_mctp_iface_control_obj(dbus, iface):
obj = await dbus.get_proxy_object(
"au.com.codeconstruct.MCTP1",
"/au/com/codeconstruct/mctp1/interfaces/" + iface.name,
)
return await obj.get_interface("au.com.codeconstruct.MCTP.Interface1")

async def mctpd_mctp_endpoint_obj(dbus, path, iface):
obj = await dbus.get_proxy_object(
'au.com.codeconstruct.MCTP1',
Expand Down
19 changes: 17 additions & 2 deletions tests/mctpd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socket
import struct
import sys
import tempfile
import trio
import uuid

Expand Down Expand Up @@ -975,11 +976,12 @@ async def send_fd(sock, fd):


class MctpdWrapper:
def __init__(self, bus, sysnet, binary=None):
def __init__(self, bus, sysnet, binary=None, config=None):
self.bus = bus
self.system = sysnet.system
self.network = sysnet.network
self.binary = binary or './test-mctpd'
self.config = config
(self.sock_local, self.sock_remote) = self.socketpair()

def socketpair(self):
Expand Down Expand Up @@ -1056,8 +1058,18 @@ def name_owner_changed(name, new_owner, old_owner):
# start mctpd, passing our control socket
env = os.environ.copy()
env['MCTP_TEST_SOCK'] = str(self.sock_remote.fileno())

if self.config:
config_file = tempfile.NamedTemporaryFile('w', prefix="mctp.conf.")
config_file.write(self.config)
config_file.flush()
command = [self.binary, '-v', '-c', config_file.name]
else:
config_file = None
command = [self.binary, '-v']

proc = await trio.lowlevel.open_process(
[self.binary, '-v'], # todo: flexible paths
command = command,
pass_fds = (1, 2, self.sock_remote.fileno()),
env = env,
)
Expand All @@ -1074,6 +1086,9 @@ def name_owner_changed(name, new_owner, old_owner):

proc_rc = await proc.wait()

if config_file:
config_file.close()

await send_chan.send(proc_rc)

Sysnet = namedtuple('SysNet', ['system', 'network'])
Expand Down
15 changes: 15 additions & 0 deletions tests/test_mctpd_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from mctp_test_utils import *
from mctpd import *

@pytest.fixture(name="config")
def endpoint_config():
return """
mode = "endpoint"
"""

""" Test if mctpd is running as an endpoint """
async def test_endpoint_role(dbus, mctpd):
obj = await mctpd_mctp_iface_control_obj(dbus, mctpd.system.interfaces[0])
role = await obj.get_role()
assert str(role) == "Endpoint"