|
1 | 1 | package metrics |
2 | 2 |
|
3 | | -import "testing" |
4 | | - |
5 | | -func TestGetMaxFilesLimit(t *testing.T) { |
6 | | - f := func(want uint64, path string, wantErr bool) { |
7 | | - t.Helper() |
8 | | - got, err := getMaxFilesLimit(path) |
9 | | - if err != nil && !wantErr { |
10 | | - t.Fatalf("unexpected error: %v", err) |
11 | | - } |
12 | | - if got != want { |
13 | | - t.Fatalf("unexpected result: %d, want: %d at getMaxFilesLimit", got, want) |
14 | | - } |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
15 | 12 |
|
| 13 | +var testdir string |
| 14 | + |
| 15 | +func init() { |
| 16 | + testdir, _ = os.Getwd() |
| 17 | + testdir += "/testdata/" |
| 18 | +} |
| 19 | + |
| 20 | +func getTestData(filename string, t *testing.T) string { |
| 21 | + data, err := os.ReadFile(testdir + filename) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("%v", err) |
| 24 | + } |
| 25 | + s := string(data) |
| 26 | + if filename == "linux.proc_metrics.out" { |
| 27 | + // since linux stat.starttime is relative to boot, we need to adjust |
| 28 | + // the expected results regarding this. |
| 29 | + m := regexp.MustCompile("process_start_time_seconds [0-9]+") |
| 30 | + n := fmt.Sprintf("process_start_time_seconds %d", startTimeSeconds) |
| 31 | + return m.ReplaceAllString(s, n) |
16 | 32 | } |
17 | | - f(1024, "testdata/limits", false) |
18 | | - f(0, "testdata/bad_path", true) |
19 | | - f(0, "testdata/limits_bad", true) |
| 33 | + return s |
20 | 34 | } |
21 | 35 |
|
22 | | -func TestGetOpenFDsCount(t *testing.T) { |
23 | | - f := func(want uint64, path string, wantErr bool) { |
24 | | - t.Helper() |
25 | | - got, err := getOpenFDsCount(path) |
26 | | - if (err != nil && !wantErr) || (err == nil && wantErr) { |
27 | | - t.Fatalf("unexpected error: %v", err) |
28 | | - } |
29 | | - if got != want { |
30 | | - t.Fatalf("unexpected result: %d, want: %d at getOpenFDsCount", got, want) |
| 36 | +func stripComments(input string) string { |
| 37 | + var builder strings.Builder |
| 38 | + lines := strings.Split(input, "\n") |
| 39 | + for _, line := range lines { |
| 40 | + s := strings.TrimSpace(line) |
| 41 | + if strings.HasPrefix(s, "#") || s == "" { |
| 42 | + continue |
31 | 43 | } |
| 44 | + builder.WriteString(line + "\n") |
32 | 45 | } |
33 | | - f(5, "testdata/fd/", false) |
34 | | - f(0, "testdata/fd/0", true) |
35 | | - f(0, "testdata/limits", true) |
| 46 | + return builder.String() |
36 | 47 | } |
37 | 48 |
|
38 | | -func TestGetMemStats(t *testing.T) { |
39 | | - f := func(want memStats, path string, wantErr bool) { |
40 | | - t.Helper() |
41 | | - got, err := getMemStats(path) |
42 | | - if (err != nil && !wantErr) || (err == nil && wantErr) { |
43 | | - t.Fatalf("unexpected error: %v", err) |
44 | | - } |
45 | | - if got != nil && *got != want { |
46 | | - t.Fatalf("unexpected result: %d, want: %d at getMemStats", *got, want) |
| 49 | +func Test_processMetrics(t *testing.T) { |
| 50 | + diffFormat := "Test %s:\n\tgot:\n'%v'\n\twant:\n'%v'" |
| 51 | + tests := []struct { |
| 52 | + name string |
| 53 | + wantW string |
| 54 | + fn func(w io.Writer) |
| 55 | + }{ |
| 56 | + {"pm", getTestData("linux.proc_metrics.out", t), writeProcessMetrics}, |
| 57 | + {"fdm", getTestData("linux.fd_metrics.out", t), writeFDMetrics}, |
| 58 | + } |
| 59 | + for _, compact := range []bool{true, false} { |
| 60 | + ExposeMetadata(!compact) |
| 61 | + for _, tt := range tests { |
| 62 | + want := tt.wantW |
| 63 | + if compact { |
| 64 | + want = stripComments(want) |
| 65 | + } |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + w := &bytes.Buffer{} |
| 68 | + tt.fn(w) |
| 69 | + if gotW := w.String(); gotW != want { |
| 70 | + t.Errorf(diffFormat, tt.name, gotW, want) |
| 71 | + } |
| 72 | + }) |
47 | 73 | } |
48 | 74 | } |
49 | | - f(memStats{vmPeak: 2130489344, rssPeak: 200679424, rssAnon: 121602048, rssFile: 11362304}, "testdata/status", false) |
50 | | - f(memStats{}, "testdata/status_bad", true) |
| 75 | + |
| 76 | + // missing /proc/<pid>/io file - just omit the process_io_* metric entries |
| 77 | + // see https://github.com/VictoriaMetrics/metrics/issues/42 |
| 78 | + tt := tests[0] |
| 79 | + want := stripComments(tt.wantW) |
| 80 | + m := regexp.MustCompile("process_io_[_a-z]+ [0-9]+\n") |
| 81 | + wantW := m.ReplaceAllString(want, "") |
| 82 | + testfiles[FD_IO] = "/doesNotExist" |
| 83 | + ExposeMetadata(false) // no need to check comments again |
| 84 | + init2() |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + w := &bytes.Buffer{} |
| 87 | + tt.fn(w) |
| 88 | + if gotW := w.String(); gotW != wantW { |
| 89 | + t.Errorf(diffFormat, tt.name, gotW, wantW) |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + // bad limits: just omit the process_max_fds metric entry |
| 94 | + tt = tests[1] |
| 95 | + want = stripComments(tt.wantW) |
| 96 | + m = regexp.MustCompile("process_max_fds [0-9]+\n") |
| 97 | + wantW = m.ReplaceAllString(want, "") |
| 98 | + testfiles[FD_LIMITS] = "/limits_bad" |
| 99 | + init2() |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + w := &bytes.Buffer{} |
| 102 | + tt.fn(w) |
| 103 | + if gotW := w.String(); gotW != wantW { |
| 104 | + t.Errorf(diffFormat, tt.name, gotW, wantW) |
| 105 | + } |
| 106 | + }) |
| 107 | + |
51 | 108 | } |
0 commit comments