| 1 | package managedproxy |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestLinuxPhysicalRouteSkipsTUNAndUsesLowestMetric(t *testing.T) { |
| 6 | const routes = `Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT |
| 7 | tun0 00000000 00000000 0001 0 0 0 00000000 0 0 0 |
| 8 | ens5 00000000 0100000A 0003 0 0 100 00000000 0 0 0 |
| 9 | eth1 00000000 0100000A 0003 0 0 20 00000000 0 0 0 |
| 10 | eth2 00000000 0100000A 0203 0 0 1 00000000 0 0 0 |
| 11 | eth3 00000000 0100000A 0002 0 0 1 00000000 0 0 0 |
| 12 | eth4 00000000 0100000A 0003 0 0 1 0000FFFF 0 0 0 |
| 13 | ` |
| 14 | for _, tc := range []struct { |
| 15 | name string |
| 16 | usable map[string]bool |
| 17 | want string |
| 18 | }{ |
| 19 | {"physical metric", map[string]bool{"ens5": true, "eth1": true, "eth2": true, "eth3": true, "eth4": true}, "eth1"}, |
| 20 | {"cloud virtio", map[string]bool{"ens5": true}, "ens5"}, |
| 21 | {"disconnected physical interfaces", map[string]bool{}, ""}, |
| 22 | {"reject and inactive routes", map[string]bool{"eth2": true, "eth3": true, "eth4": true}, ""}, |
| 23 | } { |
| 24 | t.Run(tc.name, func(t *testing.T) { |
| 25 | got, err := selectLinuxPhysicalInterface(routes, tc.usable) |
| 26 | if got != tc.want || (err != nil) != (tc.want == "") { |
| 27 | t.Fatalf("got %q, %v; want %q", got, err, tc.want) |
| 28 | } |
| 29 | }) |
| 30 | } |
| 31 | } |