|
| 1 | +// Copyright 2020 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package procfs |
| 15 | + |
| 16 | +import ( |
| 17 | + "testing" |
| 18 | +) |
| 19 | + |
| 20 | +func TestNetStat(t *testing.T) { |
| 21 | + const ( |
| 22 | + filesCount = 2 |
| 23 | + CPUsCount = 2 |
| 24 | + arpCacheMetricsCount = 13 |
| 25 | + ndiscCacheMetricsCount = 13 |
| 26 | + ) |
| 27 | + |
| 28 | + fs, err := NewFS(procTestFixtures) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("failed to open procfs: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + netStats, err := fs.NetStat() |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("NetStat() error: %s", err) |
| 36 | + } |
| 37 | + |
| 38 | + if len(netStats) != filesCount { |
| 39 | + t.Fatalf("unexpected number of files parsed %d, expected %d", len(netStats), filesCount) |
| 40 | + } |
| 41 | + |
| 42 | + expectedStats := [2]NetStat{ |
| 43 | + { |
| 44 | + Filename: "arp_cache", |
| 45 | + Stats: make(map[string][]uint64), |
| 46 | + }, |
| 47 | + { |
| 48 | + Filename: "ndisc_cache", |
| 49 | + Stats: make(map[string][]uint64), |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, expected := range expectedStats { |
| 54 | + if expected.Filename == "arp_cache" { |
| 55 | + expected.Stats["entries"] = []uint64{20, 20} |
| 56 | + expected.Stats["allocs"] = []uint64{1, 13} |
| 57 | + expected.Stats["destroys"] = []uint64{2, 14} |
| 58 | + expected.Stats["hash_grows"] = []uint64{3, 15} |
| 59 | + expected.Stats["lookups"] = []uint64{4, 16} |
| 60 | + expected.Stats["hits"] = []uint64{5, 17} |
| 61 | + expected.Stats["res_failed"] = []uint64{6, 18} |
| 62 | + expected.Stats["rcv_probes_mcast"] = []uint64{7, 19} |
| 63 | + expected.Stats["rcv_probes_ucast"] = []uint64{8, 20} |
| 64 | + expected.Stats["periodic_gc_runs"] = []uint64{9, 21} |
| 65 | + expected.Stats["forced_gc_runs"] = []uint64{10, 22} |
| 66 | + expected.Stats["unresolved_discards"] = []uint64{11, 23} |
| 67 | + expected.Stats["table_fulls"] = []uint64{12, 24} |
| 68 | + } |
| 69 | + if expected.Filename == "ndisc_cache" { |
| 70 | + expected.Stats["entries"] = []uint64{36, 36} |
| 71 | + expected.Stats["allocs"] = []uint64{240, 252} |
| 72 | + expected.Stats["destroys"] = []uint64{241, 253} |
| 73 | + expected.Stats["hash_grows"] = []uint64{242, 254} |
| 74 | + expected.Stats["lookups"] = []uint64{243, 255} |
| 75 | + expected.Stats["hits"] = []uint64{244, 256} |
| 76 | + expected.Stats["res_failed"] = []uint64{245, 257} |
| 77 | + expected.Stats["rcv_probes_mcast"] = []uint64{246, 258} |
| 78 | + expected.Stats["rcv_probes_ucast"] = []uint64{247, 259} |
| 79 | + expected.Stats["periodic_gc_runs"] = []uint64{248, 260} |
| 80 | + expected.Stats["forced_gc_runs"] = []uint64{249, 261} |
| 81 | + expected.Stats["unresolved_discards"] = []uint64{250, 262} |
| 82 | + expected.Stats["table_fulls"] = []uint64{251, 263} |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + for _, netStatFile := range netStats { |
| 87 | + if netStatFile.Filename == "arp_cache" && len(netStatFile.Stats) != arpCacheMetricsCount { |
| 88 | + t.Fatalf("unexpected arp_cache metrics count %d, expected %d", len(netStatFile.Stats), arpCacheMetricsCount) |
| 89 | + } |
| 90 | + if netStatFile.Filename == "ndisc_cache" && len(netStatFile.Stats) != ndiscCacheMetricsCount { |
| 91 | + t.Fatalf("unexpected ndisc_cache metrics count %d, expected %d", len(netStatFile.Stats), ndiscCacheMetricsCount) |
| 92 | + } |
| 93 | + for _, expected := range expectedStats { |
| 94 | + for header, stats := range netStatFile.Stats { |
| 95 | + if header == "" { |
| 96 | + t.Fatalf("Found empty metric name") |
| 97 | + } |
| 98 | + if len(stats) != CPUsCount { |
| 99 | + t.Fatalf("NetStat() parsed %d lines with metrics, expected %d", len(stats), CPUsCount) |
| 100 | + } |
| 101 | + if netStatFile.Filename == expected.Filename { |
| 102 | + if expected.Stats[header] == nil { |
| 103 | + t.Fatalf("unexpected metric header: %s", header) |
| 104 | + } |
| 105 | + for cpu, value := range netStatFile.Stats[header] { |
| 106 | + if expected.Stats[header][cpu] != value { |
| 107 | + t.Fatalf("unexpected value for %s for cpu %d in %s: %d, expected %d", header, cpu, netStatFile.Filename, value, expected.Stats[header][cpu]) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments