|
| 1 | +package systemstatsmonitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "regexp" |
| 8 | + "testing" |
| 9 | + |
| 10 | + ssmtypes "k8s.io/node-problem-detector/pkg/systemstatsmonitor/types" |
| 11 | + "k8s.io/node-problem-detector/pkg/util/metrics" |
| 12 | +) |
| 13 | + |
| 14 | +var defaultMetricsConfig = map[string]ssmtypes.MetricConfig{ |
| 15 | + string(metrics.NetDevRxBytes): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxBytes)}, |
| 16 | + string(metrics.NetDevRxPackets): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxPackets)}, |
| 17 | + string(metrics.NetDevRxErrors): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxErrors)}, |
| 18 | + string(metrics.NetDevRxDropped): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxDropped)}, |
| 19 | + string(metrics.NetDevRxFifo): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxFifo)}, |
| 20 | + string(metrics.NetDevRxFrame): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxFrame)}, |
| 21 | + string(metrics.NetDevRxCompressed): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxCompressed)}, |
| 22 | + string(metrics.NetDevRxMulticast): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevRxMulticast)}, |
| 23 | + string(metrics.NetDevTxBytes): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxBytes)}, |
| 24 | + string(metrics.NetDevTxPackets): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxPackets)}, |
| 25 | + string(metrics.NetDevTxErrors): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxErrors)}, |
| 26 | + string(metrics.NetDevTxDropped): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxDropped)}, |
| 27 | + string(metrics.NetDevTxFifo): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxFifo)}, |
| 28 | + string(metrics.NetDevTxCollisions): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxCollisions)}, |
| 29 | + string(metrics.NetDevTxCarrier): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxCarrier)}, |
| 30 | + string(metrics.NetDevTxCompressed): ssmtypes.MetricConfig{DisplayName: string(metrics.NetDevTxCompressed)}, |
| 31 | +} |
| 32 | + |
| 33 | +// To get a similar output, run `cat /proc/net/dev` on a Linux machine |
| 34 | +// docker: 1500 100 8 7 0 0 0 0 9000 450 565 200 20 30 0 0 |
| 35 | +const fakeNetProcContent = `Inter-| Receive | Transmit |
| 36 | +face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed |
| 37 | +eth0: 5000 100 0 0 0 0 0 0 2500 30 0 0 0 0 0 0 |
| 38 | +docker0: 1000 90 8 7 0 0 0 0 0 0 0 0 0 0 0 0 |
| 39 | +docker1: 500 10 0 0 0 0 0 0 3000 150 15 0 20 30 0 0 |
| 40 | +docker2: 0 0 0 0 0 0 0 0 6000 300 550 200 0 0 0 0 |
| 41 | +` |
| 42 | + |
| 43 | +// newFakeInt64Metric is a wrapper around metrics.NewFakeInt64Metric |
| 44 | +func newFakeInt64Metric(metricID metrics.MetricID, viewName string, description string, unit string, aggregation metrics.Aggregation, tagNames []string) (metrics.Int64MetricInterface, error) { |
| 45 | + return metrics.NewFakeInt64Metric(viewName, aggregation, tagNames), nil |
| 46 | +} |
| 47 | + |
| 48 | +// testCollectAux is a test auxiliary function used for testing netCollector.Collect |
| 49 | +func testCollectAux(t *testing.T, name string, excludeInterfaceRegexp ssmtypes.NetStatsInterfaceRegexp, validate func(*testing.T, *netCollector)) { |
| 50 | + // mkdir /tmp/proc-X |
| 51 | + procDir, err := ioutil.TempDir(os.TempDir(), "proc-") |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to create temp proc directory: %v", err) |
| 54 | + } |
| 55 | + // rm -r /tmp/proc-X |
| 56 | + defer os.RemoveAll(procDir) |
| 57 | + // mkdir -C /tmp/proc-X/net |
| 58 | + procNetDir := path.Join(procDir, "net") |
| 59 | + if err := os.Mkdir(procNetDir, 0777); err != nil { |
| 60 | + t.Fatalf("Failed to create directory %q: %v", procNetDir, err) |
| 61 | + } |
| 62 | + |
| 63 | + // touch /tmp/proc-X/net/dev |
| 64 | + filename := path.Join(procNetDir, "dev") |
| 65 | + f, err := os.Create(filename) |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("Failed to create file %q: %v", filename, err) |
| 68 | + } |
| 69 | + // echo $FILE_CONTENT > /tmp/proc-X/net/dev |
| 70 | + if _, err = f.WriteString(fakeNetProcContent); err != nil { |
| 71 | + t.Fatalf("Failed to write to file %q: %v", filename, err) |
| 72 | + } |
| 73 | + if err = f.Close(); err != nil { |
| 74 | + t.Fatalf("Failed to close file %q: %v", filename, err) |
| 75 | + } |
| 76 | + |
| 77 | + // Build the netCollector |
| 78 | + config := &ssmtypes.NetStatsConfig{ |
| 79 | + ExcludeInterfaceRegexp: excludeInterfaceRegexp, |
| 80 | + MetricsConfigs: defaultMetricsConfig, |
| 81 | + } |
| 82 | + netCollector := &netCollector{ |
| 83 | + config: config, |
| 84 | + procPath: procDir, |
| 85 | + recorder: newIfaceStatRecorder(newFakeInt64Metric), |
| 86 | + } |
| 87 | + netCollector.initOrDie() |
| 88 | + netCollector.collect() |
| 89 | + validate(t, netCollector) |
| 90 | +} |
| 91 | + |
| 92 | +func TestCollect(t *testing.T) { |
| 93 | + tcs := []struct { |
| 94 | + Name string |
| 95 | + ExcludeInterfaceRegexp ssmtypes.NetStatsInterfaceRegexp |
| 96 | + Validate func(t *testing.T, nc *netCollector) |
| 97 | + }{ |
| 98 | + { |
| 99 | + Name: "NoFilterMatch", |
| 100 | + ExcludeInterfaceRegexp: ssmtypes.NetStatsInterfaceRegexp{R: regexp.MustCompile(`^fake$`)}, |
| 101 | + Validate: func(t *testing.T, nc *netCollector) { |
| 102 | + // We just validate two metrics, no need to check all of them |
| 103 | + expectedValues := map[metrics.MetricID]map[string]int64{ |
| 104 | + metrics.NetDevRxBytes: map[string]int64{ |
| 105 | + "eth0": 5000, |
| 106 | + "docker0": 1000, |
| 107 | + "docker1": 500, |
| 108 | + "docker2": 0, |
| 109 | + }, |
| 110 | + metrics.NetDevTxBytes: map[string]int64{ |
| 111 | + "eth0": 2500, |
| 112 | + "docker0": 0, |
| 113 | + "docker1": 3000, |
| 114 | + "docker2": 6000, |
| 115 | + }, |
| 116 | + } |
| 117 | + for metricID, interfaceValues := range expectedValues { |
| 118 | + collector, ok := nc.recorder.collectors[metricID] |
| 119 | + if !ok { |
| 120 | + t.Errorf("Failed to get collector of metric %s", metricID) |
| 121 | + continue |
| 122 | + } |
| 123 | + fakeInt64Metric, ok := collector.metric.(*metrics.FakeInt64Metric) |
| 124 | + if !ok { |
| 125 | + t.Fatalf("Failed to convert metric %s to fakeMetric", string(metricID)) |
| 126 | + } |
| 127 | + for _, repr := range fakeInt64Metric.ListMetrics() { |
| 128 | + interfaceName, ok := repr.Labels[interfaceNameLabel] |
| 129 | + if !ok { |
| 130 | + t.Fatalf("Failed to get label %q for ", interfaceNameLabel) |
| 131 | + } |
| 132 | + expectedValue, ok := interfaceValues[interfaceName] |
| 133 | + if !ok { |
| 134 | + t.Fatalf("Failed to get metric value for interface %q", interfaceName) |
| 135 | + } |
| 136 | + if repr.Value != expectedValue { |
| 137 | + t.Errorf("Mismatch in metric %q for interface %q: expected %d, got %d", metricID, interfaceName, expectedValue, repr.Value) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + Name: "FilterMatch", |
| 145 | + ExcludeInterfaceRegexp: ssmtypes.NetStatsInterfaceRegexp{R: regexp.MustCompile(`docker\d+`)}, |
| 146 | + Validate: func(t *testing.T, nc *netCollector) { |
| 147 | + // We just validate two metrics, no need to check all of them |
| 148 | + expectedValues := map[metrics.MetricID]map[string]int64{ |
| 149 | + metrics.NetDevRxBytes: map[string]int64{ |
| 150 | + "eth0": 5000, |
| 151 | + }, |
| 152 | + metrics.NetDevTxBytes: map[string]int64{ |
| 153 | + "eth0": 2500, |
| 154 | + }, |
| 155 | + } |
| 156 | + for metricID, interfaceValues := range expectedValues { |
| 157 | + collector, ok := nc.recorder.collectors[metricID] |
| 158 | + if !ok { |
| 159 | + t.Errorf("Failed to get collector of metric %s", metricID) |
| 160 | + continue |
| 161 | + } |
| 162 | + fakeInt64Metric, ok := collector.metric.(*metrics.FakeInt64Metric) |
| 163 | + if !ok { |
| 164 | + t.Fatalf("Failed to convert metric %s to fakeMetric", string(metricID)) |
| 165 | + } |
| 166 | + for _, repr := range fakeInt64Metric.ListMetrics() { |
| 167 | + interfaceName, ok := repr.Labels[interfaceNameLabel] |
| 168 | + if !ok { |
| 169 | + t.Fatalf("Failed to get label %q for ", interfaceNameLabel) |
| 170 | + } |
| 171 | + expectedValue, ok := interfaceValues[interfaceName] |
| 172 | + if !ok { |
| 173 | + t.Fatalf("Failed to get metric value for interface %q", interfaceName) |
| 174 | + } |
| 175 | + if repr.Value != expectedValue { |
| 176 | + t.Errorf("Mismatch in metric %q for interface %q: expected %d, got %d", metricID, interfaceName, expectedValue, repr.Value) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | + }, |
| 182 | + } |
| 183 | + for _, tc := range tcs { |
| 184 | + tc := tc |
| 185 | + t.Run(tc.Name, func(t *testing.T) { |
| 186 | + t.Parallel() |
| 187 | + testCollectAux(t, tc.Name, tc.ExcludeInterfaceRegexp, tc.Validate) |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
0 commit comments