(t *testing.T)
| 33 | ) |
| 34 | |
| 35 | func TestProcessCollector(t *testing.T) { |
| 36 | if _, err := procfs.Self(); err != nil { |
| 37 | t.Skipf("skipping TestProcessCollector, procfs not available: %s", err) |
| 38 | } |
| 39 | |
| 40 | registry := NewPedanticRegistry() |
| 41 | if err := registry.Register(NewProcessCollector(ProcessCollectorOpts{})); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if err := registry.Register(NewProcessCollector(ProcessCollectorOpts{ |
| 45 | PidFn: func() (int, error) { return os.Getpid(), nil }, |
| 46 | Namespace: "foobar", |
| 47 | ReportErrors: true, // No errors expected, just to see if none are reported. |
| 48 | })); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | |
| 52 | mfs, err := registry.Gather() |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | |
| 57 | var buf bytes.Buffer |
| 58 | for _, mf := range mfs { |
| 59 | if _, err := expfmt.MetricFamilyToText(&buf, mf); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | for _, re := range []*regexp.Regexp{ |
| 65 | regexp.MustCompile("\nprocess_cpu_seconds_total [0-9]"), |
| 66 | regexp.MustCompile("\nprocess_max_fds [1-9]"), |
| 67 | regexp.MustCompile("\nprocess_open_fds [1-9]"), |
| 68 | regexp.MustCompile("\nprocess_virtual_memory_max_bytes (-1|[1-9])"), |
| 69 | regexp.MustCompile("\nprocess_virtual_memory_bytes [1-9]"), |
| 70 | regexp.MustCompile("\nprocess_resident_memory_bytes [1-9]"), |
| 71 | regexp.MustCompile("\nprocess_start_time_seconds [0-9.]{10,}"), |
| 72 | regexp.MustCompile("\nprocess_network_receive_bytes_total [0-9]+"), |
| 73 | regexp.MustCompile("\nprocess_network_transmit_bytes_total [0-9]+"), |
| 74 | regexp.MustCompile("\nfoobar_process_cpu_seconds_total [0-9]"), |
| 75 | regexp.MustCompile("\nfoobar_process_max_fds [1-9]"), |
| 76 | regexp.MustCompile("\nfoobar_process_open_fds [1-9]"), |
| 77 | regexp.MustCompile("\nfoobar_process_virtual_memory_max_bytes (-1|[1-9])"), |
| 78 | regexp.MustCompile("\nfoobar_process_virtual_memory_bytes [1-9]"), |
| 79 | regexp.MustCompile("\nfoobar_process_resident_memory_bytes [1-9]"), |
| 80 | regexp.MustCompile("\nfoobar_process_start_time_seconds [0-9.]{10,}"), |
| 81 | regexp.MustCompile("\nfoobar_process_network_receive_bytes_total [0-9]+"), |
| 82 | regexp.MustCompile("\nfoobar_process_network_transmit_bytes_total [0-9]+"), |
| 83 | } { |
| 84 | if !re.Match(buf.Bytes()) { |
| 85 | t.Errorf("want body to match %s\n%s", re, buf.String()) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | brokenProcessCollector := NewProcessCollector(ProcessCollectorOpts{ |
| 90 | PidFn: func() (int, error) { return 0, errors.New("boo") }, |
| 91 | ReportErrors: true, |
| 92 | }) |
nothing calls this directly
no test coverage detected