|
| 1 | +package healthsdk |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "net/netip" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "golang.org/x/exp/slices" |
| 11 | + "tailscale.com/net/interfaces" |
| 12 | + |
| 13 | + "github.com/coder/coder/v2/coderd/healthcheck/health" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_generateInterfacesReport(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + testCases := []struct { |
| 19 | + name string |
| 20 | + state interfaces.State |
| 21 | + severity health.Severity |
| 22 | + expectedInterfaces []string |
| 23 | + expectedWarnings []string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "Empty", |
| 27 | + state: interfaces.State{}, |
| 28 | + severity: health.SeverityOK, |
| 29 | + expectedInterfaces: []string{}, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Normal", |
| 33 | + state: interfaces.State{ |
| 34 | + Interface: map[string]interfaces.Interface{ |
| 35 | + "en0": {Interface: &net.Interface{ |
| 36 | + MTU: 1500, |
| 37 | + Name: "en0", |
| 38 | + Flags: net.FlagUp, |
| 39 | + }}, |
| 40 | + "lo0": {Interface: &net.Interface{ |
| 41 | + MTU: 65535, |
| 42 | + Name: "lo0", |
| 43 | + Flags: net.FlagUp, |
| 44 | + }}, |
| 45 | + }, |
| 46 | + InterfaceIPs: map[string][]netip.Prefix{ |
| 47 | + "en0": { |
| 48 | + netip.MustParsePrefix("192.168.100.1/24"), |
| 49 | + netip.MustParsePrefix("fe80::c13:1a92:3fa5:dd7e/64"), |
| 50 | + }, |
| 51 | + "lo0": { |
| 52 | + netip.MustParsePrefix("127.0.0.1/8"), |
| 53 | + netip.MustParsePrefix("::1/128"), |
| 54 | + netip.MustParsePrefix("fe80::1/64"), |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + severity: health.SeverityOK, |
| 59 | + expectedInterfaces: []string{"en0", "lo0"}, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "IgnoreDisabled", |
| 63 | + state: interfaces.State{ |
| 64 | + Interface: map[string]interfaces.Interface{ |
| 65 | + "en0": {Interface: &net.Interface{ |
| 66 | + MTU: 1300, |
| 67 | + Name: "en0", |
| 68 | + Flags: 0, |
| 69 | + }}, |
| 70 | + "lo0": {Interface: &net.Interface{ |
| 71 | + MTU: 65535, |
| 72 | + Name: "lo0", |
| 73 | + Flags: net.FlagUp, |
| 74 | + }}, |
| 75 | + }, |
| 76 | + InterfaceIPs: map[string][]netip.Prefix{ |
| 77 | + "en0": {netip.MustParsePrefix("192.168.100.1/24")}, |
| 78 | + "lo0": {netip.MustParsePrefix("127.0.0.1/8")}, |
| 79 | + }, |
| 80 | + }, |
| 81 | + severity: health.SeverityOK, |
| 82 | + expectedInterfaces: []string{"lo0"}, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "IgnoreLinkLocalOnly", |
| 86 | + state: interfaces.State{ |
| 87 | + Interface: map[string]interfaces.Interface{ |
| 88 | + "en0": {Interface: &net.Interface{ |
| 89 | + MTU: 1300, |
| 90 | + Name: "en0", |
| 91 | + Flags: net.FlagUp, |
| 92 | + }}, |
| 93 | + "lo0": {Interface: &net.Interface{ |
| 94 | + MTU: 65535, |
| 95 | + Name: "lo0", |
| 96 | + Flags: net.FlagUp, |
| 97 | + }}, |
| 98 | + }, |
| 99 | + InterfaceIPs: map[string][]netip.Prefix{ |
| 100 | + "en0": {netip.MustParsePrefix("fe80::1:1/64")}, |
| 101 | + "lo0": {netip.MustParsePrefix("127.0.0.1/8")}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + severity: health.SeverityOK, |
| 105 | + expectedInterfaces: []string{"lo0"}, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "IgnoreNoAddress", |
| 109 | + state: interfaces.State{ |
| 110 | + Interface: map[string]interfaces.Interface{ |
| 111 | + "en0": {Interface: &net.Interface{ |
| 112 | + MTU: 1300, |
| 113 | + Name: "en0", |
| 114 | + Flags: net.FlagUp, |
| 115 | + }}, |
| 116 | + "lo0": {Interface: &net.Interface{ |
| 117 | + MTU: 65535, |
| 118 | + Name: "lo0", |
| 119 | + Flags: net.FlagUp, |
| 120 | + }}, |
| 121 | + }, |
| 122 | + InterfaceIPs: map[string][]netip.Prefix{ |
| 123 | + "en0": {}, |
| 124 | + "lo0": {netip.MustParsePrefix("127.0.0.1/8")}, |
| 125 | + }, |
| 126 | + }, |
| 127 | + severity: health.SeverityOK, |
| 128 | + expectedInterfaces: []string{"lo0"}, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "SmallMTUTunnel", |
| 132 | + state: interfaces.State{ |
| 133 | + Interface: map[string]interfaces.Interface{ |
| 134 | + "en0": {Interface: &net.Interface{ |
| 135 | + MTU: 1500, |
| 136 | + Name: "en0", |
| 137 | + Flags: net.FlagUp, |
| 138 | + }}, |
| 139 | + "lo0": {Interface: &net.Interface{ |
| 140 | + MTU: 65535, |
| 141 | + Name: "lo0", |
| 142 | + Flags: net.FlagUp, |
| 143 | + }}, |
| 144 | + "tun0": {Interface: &net.Interface{ |
| 145 | + MTU: 1280, |
| 146 | + Name: "tun0", |
| 147 | + Flags: net.FlagUp, |
| 148 | + }}, |
| 149 | + }, |
| 150 | + InterfaceIPs: map[string][]netip.Prefix{ |
| 151 | + "en0": {netip.MustParsePrefix("192.168.100.1/24")}, |
| 152 | + "tun0": {netip.MustParsePrefix("10.3.55.9/8")}, |
| 153 | + "lo0": {netip.MustParsePrefix("127.0.0.1/8")}, |
| 154 | + }, |
| 155 | + }, |
| 156 | + severity: health.SeverityWarning, |
| 157 | + expectedInterfaces: []string{"en0", "lo0", "tun0"}, |
| 158 | + expectedWarnings: []string{"tun0"}, |
| 159 | + }, |
| 160 | + } |
| 161 | + |
| 162 | + for _, tc := range testCases { |
| 163 | + tc := tc |
| 164 | + t.Run(tc.name, func(t *testing.T) { |
| 165 | + t.Parallel() |
| 166 | + r := generateInterfacesReport(&tc.state) |
| 167 | + require.Equal(t, tc.severity, r.Severity) |
| 168 | + gotInterfaces := []string{} |
| 169 | + for _, i := range r.Interfaces { |
| 170 | + gotInterfaces = append(gotInterfaces, i.Name) |
| 171 | + } |
| 172 | + slices.Sort(gotInterfaces) |
| 173 | + slices.Sort(tc.expectedInterfaces) |
| 174 | + require.Equal(t, tc.expectedInterfaces, gotInterfaces) |
| 175 | + |
| 176 | + require.Len(t, r.Warnings, len(tc.expectedWarnings), |
| 177 | + "expected %d warnings, got %d", len(tc.expectedWarnings), len(r.Warnings)) |
| 178 | + for _, name := range tc.expectedWarnings { |
| 179 | + found := false |
| 180 | + for _, w := range r.Warnings { |
| 181 | + if strings.Contains(w.String(), name) { |
| 182 | + found = true |
| 183 | + break |
| 184 | + } |
| 185 | + } |
| 186 | + if !found { |
| 187 | + t.Errorf("missing warning for %s", name) |
| 188 | + } |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
0 commit comments