Hi, @robertpostill.
I have no insight on the philosophy of the thing, but I think--if I am understanding correctly--that the parameterization of the current-output-port
is the issue, in the sense that no swapping is necessary.
I ran this on a linux-box I have to hand.
#lang racket/base
(require
mock rackunit
racket/system
racket/port
racket/string
racket/function)
(define IFCONFIG-OUTPUT
#<<$$$
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.100.10.104 netmask 255.255.255.128 broadcast 10.100.10.127
inet6 fe80::2372:f133:f24d:ed75 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:10:55:86 txqueuelen 1000 (Ethernet)
RX packets 12789 bytes 18594238 (17.7 MiB)
RX errors 0 dropped 222 overruns 0 frame 0
TX packets 8968 bytes 857556 (837.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 8 bytes 480 (480.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 480 (480.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
$$$
)
(define (create-pseudo-mac-address) "000000000000")
(define (get-mac-address #:ifconfig-invoker [system system])
(define mac-address-regex #px"ether.+?\n")
(define network-config (with-output-to-string (lambda () (system "/sbin/ifconfig"))))
(displayln network-config)
(let ([mac-output (regexp-match mac-address-regex network-config)])
(if mac-output
(string-replace (string-replace (string-trim (car mac-output)) "ether " "") ":" "")
(create-pseudo-mac-address))))
(test-case
"it generates the mac from ifconfig output"
(define system-mock
(mock #:behavior (thunk* (write IFCONFIG-OUTPUT))))
(define result (get-mac-address #:ifconfig-invoker system))
(check-equal? result (create-pseudo-mac-address)))
Output when setting the #:ifconfig-invoker
to the system-mock
:
"eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500\n inet 10.100.10.104 netmask 255.255.255.128 broadcast 10.100.10.127\n inet6 fe80::2372:f133:f24d:ed75 prefixlen 64 scopeid 0x20<link>\n ether 00:0c:29:10:55:86 txqueuelen 1000 (Ethernet)\n RX packets 12789 bytes 18594238 (17.7 MiB)\n RX errors 0 dropped 222 overruns 0 frame 0\n TX packets 8968 bytes 857556 (837.4 KiB)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0\n\nlo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536\n inet 127.0.0.1 netmask 255.0.0.0\n inet6 ::1 prefixlen 128 scopeid 0x10<host>\n loop txqueuelen 1000 (Local Loopback)\n RX packets 8 bytes 480 (480.0 B)\n RX errors 0 dropped 0 overruns 0 frame 0\n TX packets 8 bytes 480 (480.0 B)\n TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0"
Output when setting the invoker to the normal system
:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.100.10.104 netmask 255.255.255.128 broadcast 10.100.10.127
inet6 fe80::2372:f133:f24d:ed75 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:10:55:86 txqueuelen 1000 (Ethernet)
RX packets 39157 bytes 44694777 (42.6 MiB)
RX errors 0 dropped 1068 overruns 0 frame 0
TX packets 24502 bytes 5595740 (5.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 8 bytes 480 (480.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 480 (480.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
--------------------
it generates the mac from ifconfig output
. FAILURE
name: check-equal?
location: tmp.rkt:48:1
actual: "000c29105586 txqueuelen 1000 (Ethernet)"
expected: "000000000000"
--------------------
When using with-output-to-string
, it automatically parameterizes the current-output-port
, so whatever you write in the thunk, stays in the thunk, so to speak.
Apologies if I have misunderstood, though.
Edit: eh, I think I may have put my foot in my mouth.
So, the regexp
as I have it above does not match at all (so the mac-output
is always false), but this is because of the write
in the mock-call.
If you swap it out for a display
things look different.
#lang at-exp racket/base
(require
mock rackunit
racket/system
racket/port
racket/string
racket/format
racket/function)
(define IFCONFIG-OUTPUT
@~a{
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.100.10.104 netmask 255.255.255.128 broadcast 10.100.10.127
inet6 fe80::2372:f133:f24d:ed75 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:10:55:86 txqueuelen 1000 (Ethernet)
RX packets 12789 bytes 18594238 (17.7 MiB)
RX errors 0 dropped 222 overruns 0 frame 0
TX packets 8968 bytes 857556 (837.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 8 bytes 480 (480.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 480 (480.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0})
(define (create-pseudo-mac-address) "000000000000")
(define (get-mac-address #:ifconfig-invoker [system system])
(define mac-address-regex #px"ether.+?\n")
(define network-config (with-output-to-string (lambda () (system "/sbin/ifconfig"))))
(displayln network-config)
(let ([mac-output (regexp-match mac-address-regex network-config)])
(if mac-output
(string-replace (string-replace (string-trim (car mac-output)) "ether " "") ":" "")
(create-pseudo-mac-address))))
(test-case
"it generates the mac from ifconfig output"
(define system-mock
(mock #:behavior (thunk* (display IFCONFIG-OUTPUT))))
(define result (get-mac-address #:ifconfig-invoker system-mock))
(check-equal? result (create-pseudo-mac-address)))
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.100.10.104 netmask 255.255.255.128 broadcast 10.100.10.127
inet6 fe80::2372:f133:f24d:ed75 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:10:55:86 txqueuelen 1000 (Ethernet)
RX packets 12789 bytes 18594238 (17.7 MiB)
RX errors 0 dropped 222 overruns 0 frame 0
TX packets 8968 bytes 857556 (837.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 8 bytes 480 (480.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 480 (480.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
--------------------
it generates the mac from ifconfig output
. FAILURE
name: check-equal?
location: robertpostill-mock-testing.rkt:47:1
actual: "000c29105586 txqueuelen 1000 (Ethernet)"
expected: "000000000000"
--------------------
Unless, it is always supposed to be false?
Setting the mac-address to the something like
00:00:00:00:00:00 txqueuelen 1000 (Ethernet)
works for the appropriate psuedo-address, but I am not confident that I follow the logic, anymore.