rtnetlink icon indicating copy to clipboard operation
rtnetlink copied to clipboard

Provide a way for the consumers of this library to create a dummy netlink connection for testing purposes

Open n-peugnet opened this issue 10 months ago • 4 comments

For example, the underlying netlink package provides a nltest package, with a Dial() function that allows to create a dummy connection, and test that expected netlink messages are sent.

I see that this library already has an internal newConn() helper used for its own tests, so a way to fulfil this request would simply be to export this function, and let consumers call it together with nltest.Dial(). But maybe it would be better to put it in its own package, as netlink did it?

What do you think about this?

n-peugnet avatar Feb 04 '25 14:02 n-peugnet

I started writing some tests with a local copy of rtnetlink, and only exporting newConn() is not very helpful. It would be more interesting if at least unpackMessages() were also available.

So I think it would make more sense to provide some kind of rtnltest package that would contain a few helpers to use for testing, to prevent polluting the main rtnetlink package. If more helpers are needed, it will be simple to add them to this testing package later.

n-peugnet avatar Feb 06 '25 17:02 n-peugnet

I am open to adding an rtnltest package. Would you be willing to make an initial pr to discuss this further?

jsimonetti avatar Feb 06 '25 18:02 jsimonetti

Great! I will continue writing some tests for myself before submitting a PR, so that I can see what helpers I need.

n-peugnet avatar Feb 07 '25 16:02 n-peugnet

In the end, I chose to run my tests with unshare -rn, because intercepting all netlink calls was tedious and caused problems with stdlib functions that I couldn't intercept (e.g. net.Interfaces()). However, I think an rtnltest package may still be useful for simulating error cases. I didn't get to it yet so I'll keep you updated when I start doing this kind of tests.

n-peugnet avatar Feb 10 '25 12:02 n-peugnet