Unit tests are meant to verify the functionality of isolated units of code. When dealing with code whose output depends on the system or system configuration, what are approaches to write effective unit tests? I feel this problem plagues lower level systems languages more so I am asking it here.
I solve this by writing "unit tests" that I then manually compare to the output of my terminal's utilities. It is the quickest way to verify units work as expected but it is obviously not automated.
Making a container or a VM to run integration tests seems like the next easiest way, not sure if there are other cost effective ways.
Scenario
Say I have a function called
get_ip_by_ifname(const char *if_name, struct in_addr *ipaddr)
Inputs:
- string of interface name
- pointer to variable where the returned IP address will be
Returns:
- -1 if interface does not exist,
- 0 if interface exists but has no IPv4 IP
- 1+ if interface exists and has at least 1 ip addr (some interfaces have multiple addresses, only 1st is written to ipaddr buffer)
Test Cases and their dependencies
- Interface doesn't exist
- easy to test, use uncommon interface name
- Interface exists has no ipv4 ip address
- requires the underlying system to have a unique interface name which I need to hard code and compare to in my unit test
- interface exists, has 1 ipv4 ip address
- requires underlying system to have the uniquely named interface with exactly 1 uniquely defined ip address. Both of which I need to hard code into my test
- interface exists, has 1+ ipv4 ip addresses
The way I might test something like this works is write a test that logs each case's output to the terminal than run ip -c a in another terminal and compare the info in the 2 outputs. I verify it works as expected manually with very minimal setup (just assigned multiple IP addresses to one of my interfaces).
I would like to test this in an automated fashion. Is there any way that wont be a time sink?
Recently I used Google maps to search for the nearest DHL near me so I could return a package. DHL is not that popular near me and when I specifically typed for DHL, I would get only their competitors in the search results.
There was a DHL service center near me and I had to scroll a bunch to find it. Oh, and apparently big box stores (or anyone) can pay Google to come up in the search on maps, even if unrelated.
I don't think they have skin the in shipping game but their algorithms are over optimized that they don't even show what your searching for, but trying to infer why you're searching for it. That or whoever pays them more. Certainly a search risk