Skip to content

Commit 6471906

Browse files
tests: allow passing config as a fixture
mctpd now requests a config fixture, which is None by default to ensure previous behaviour. Test authors can now override the config using pytest fixtures overriding facility. An example on overriding per file basis is included in the commit. Signed-off-by: Khang D Nguyen <[email protected]>
1 parent 96c256d commit 6471906

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

tests/conftest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ async def dbus():
2121
yield bus
2222

2323
@pytest.fixture
24-
async def mctpd(nursery, dbus, sysnet):
25-
m = fake_mctpd.MctpdWrapper(dbus, sysnet)
24+
def config():
25+
return None
26+
27+
@pytest.fixture
28+
async def mctpd(nursery, dbus, sysnet, config):
29+
m = fake_mctpd.MctpdWrapper(dbus, sysnet, config = config)
2630
await m.start_mctpd(nursery)
2731
yield m
2832
res = await m.stop_mctpd()

tests/mctp_test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ async def mctpd_mctp_iface_obj(dbus, iface):
66
)
77
return await obj.get_interface('au.com.codeconstruct.MCTP.BusOwner1')
88

9+
async def mctpd_mctp_iface_control_obj(dbus, iface):
10+
obj = await dbus.get_proxy_object(
11+
"au.com.codeconstruct.MCTP1",
12+
"/au/com/codeconstruct/mctp1/interfaces/" + iface.name,
13+
)
14+
return await obj.get_interface("au.com.codeconstruct.MCTP.Interface1")
15+
916
async def mctpd_mctp_endpoint_obj(dbus, path, iface):
1017
obj = await dbus.get_proxy_object(
1118
'au.com.codeconstruct.MCTP1',

tests/mctpd/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import socket
77
import struct
88
import sys
9+
import tempfile
910
import trio
1011
import uuid
1112

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

976977

977978
class MctpdWrapper:
978-
def __init__(self, bus, sysnet, binary=None):
979+
def __init__(self, bus, sysnet, binary=None, config=None):
979980
self.bus = bus
980981
self.system = sysnet.system
981982
self.network = sysnet.network
982983
self.binary = binary or './test-mctpd'
984+
self.config = config
983985
(self.sock_local, self.sock_remote) = self.socketpair()
984986

985987
def socketpair(self):
@@ -1056,8 +1058,18 @@ def name_owner_changed(name, new_owner, old_owner):
10561058
# start mctpd, passing our control socket
10571059
env = os.environ.copy()
10581060
env['MCTP_TEST_SOCK'] = str(self.sock_remote.fileno())
1061+
1062+
if self.config:
1063+
config_file = tempfile.NamedTemporaryFile('w', prefix="mctp.conf.")
1064+
config_file.write(self.config)
1065+
config_file.flush()
1066+
command = [self.binary, '-v', '-c', config_file.name]
1067+
else:
1068+
config_file = None
1069+
command = [self.binary, '-v']
1070+
10591071
proc = await trio.lowlevel.open_process(
1060-
[self.binary, '-v'], # todo: flexible paths
1072+
command = command,
10611073
pass_fds = (1, 2, self.sock_remote.fileno()),
10621074
env = env,
10631075
)
@@ -1074,6 +1086,9 @@ def name_owner_changed(name, new_owner, old_owner):
10741086

10751087
proc_rc = await proc.wait()
10761088

1089+
if config_file:
1090+
config_file.close()
1091+
10771092
await send_chan.send(proc_rc)
10781093

10791094
Sysnet = namedtuple('SysNet', ['system', 'network'])

tests/test_mctpd_endpoint.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from mctp_test_utils import *
3+
from mctpd import *
4+
5+
@pytest.fixture(name="config")
6+
def endpoint_config():
7+
return """
8+
mode = "endpoint"
9+
"""
10+
11+
""" Test if mctpd is running as an endpoint """
12+
async def test_endpoint_role(dbus, mctpd):
13+
obj = await mctpd_mctp_iface_control_obj(dbus, mctpd.system.interfaces[0])
14+
role = await obj.get_role()
15+
assert str(role) == "Endpoint"

0 commit comments

Comments
 (0)