File: tracker_test.go

package info (click to toggle)
prometheus-process-exporter 0.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 600 kB
  • sloc: sh: 329; makefile: 94
file content (182 lines) | stat: -rw-r--r-- 5,622 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package proc

import (
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

// Verify that the tracker finds and tracks or ignores procs based on the
// namer, and that it can distinguish between two procs with the same pid
// but different start time.
func TestTrackerBasic(t *testing.T) {
	p1, p2, p3 := 1, 2, 3
	n1, n2, n3, n4 := "g1", "g2", "g3", "g4"
	t1, t2, t3 := time.Unix(1, 0).UTC(), time.Unix(2, 0).UTC(), time.Unix(3, 0).UTC()

	tests := []struct {
		procs []IDInfo
		want  []Update
	}{
		{
			[]IDInfo{newProcStart(p1, n1, 1), newProcStart(p3, n3, 1)},
			[]Update{{GroupName: n1, Start: t1, Wchans: msi{}}},
		},
		{
			// p3 (ignored) has exited and p2 has appeared
			[]IDInfo{newProcStart(p1, n1, 1), newProcStart(p2, n2, 2)},
			[]Update{{GroupName: n1, Start: t1, Wchans: msi{}}, {GroupName: n2, Start: t2, Wchans: msi{}}},
		},
		{
			// p1 has exited and a new proc with a new name has taken its pid
			[]IDInfo{newProcStart(p1, n4, 3), newProcStart(p2, n2, 2)},
			[]Update{{GroupName: n4, Start: t3, Wchans: msi{}}, {GroupName: n2, Start: t2, Wchans: msi{}}},
		},
	}
	// Note that n3 should not be tracked according to our namer.
	tr := NewTracker(newNamer(n1, n2, n4), false, false, false)

	opts := cmpopts.SortSlices(lessUpdateGroupName)
	for i, tc := range tests {
		_, got, err := tr.Update(procInfoIter(tc.procs...))
		noerr(t, err)
		if diff := cmp.Diff(got, tc.want, opts); diff != "" {
			t.Errorf("%d: update differs: (-got +want)\n%s", i, diff)
		}
	}
}

// TestTrackerChildren verifies that when the tracker is asked to track
// children, processes not selected by the namer are still tracked if
// they're children of ones that are.
func TestTrackerChildren(t *testing.T) {
	p1, p2, p3 := 1, 2, 3
	n1, n2, n3 := "g1", "g2", "g3"
	// In this test everything starts at time t1 for simplicity
	t1 := time.Unix(0, 0).UTC()

	tests := []struct {
		procs []IDInfo
		want  []Update
	}{
		{
			[]IDInfo{
				newProcParent(p1, n1, 0),
				newProcParent(p2, n2, p1),
			},
			[]Update{{GroupName: n2, Start: t1, Wchans: msi{}}},
		},
		{
			[]IDInfo{
				newProcParent(p1, n1, 0),
				newProcParent(p2, n2, p1),
				newProcParent(p3, n3, p2),
			},
			[]Update{{GroupName: n2, Start: t1, Wchans: msi{}}, {GroupName: n2, Start: t1, Wchans: msi{}}},
		},
	}
	// Only n2 and children of n2s should be tracked
	tr := NewTracker(newNamer(n2), true, false, false)

	for i, tc := range tests {
		_, got, err := tr.Update(procInfoIter(tc.procs...))
		noerr(t, err)
		if diff := cmp.Diff(got, tc.want); diff != "" {
			t.Errorf("%d: update differs: (-got +want)\n%s", i, diff)
		}
	}
}

// TestTrackerMetrics verifies that the updates returned by the tracker
// match the input we're giving it.
func TestTrackerMetrics(t *testing.T) {
	p, n, tm := 1, "g1", time.Unix(0, 0).UTC()

	tests := []struct {
		proc IDInfo
		want Update
	}{
		{
			piinfost(p, n, Counts{1, 2, 3, 4, 5, 6, 0, 0}, Memory{7, 8, 0},
				Filedesc{1, 10}, 9, States{Sleeping: 1}),
			Update{n, Delta{}, Memory{7, 8, 0}, Filedesc{1, 10}, tm,
				9, States{Sleeping: 1}, msi{}, nil},
		},
		{
			piinfost(p, n, Counts{2, 3, 4, 5, 6, 7, 0, 0}, Memory{1, 2, 0},
				Filedesc{2, 20}, 1, States{Running: 1}),
			Update{n, Delta{1, 1, 1, 1, 1, 1, 0, 0}, Memory{1, 2, 0},
				Filedesc{2, 20}, tm, 1, States{Running: 1}, msi{}, nil},
		},
	}
	tr := NewTracker(newNamer(n), false, false, false)

	for i, tc := range tests {
		_, got, err := tr.Update(procInfoIter(tc.proc))
		noerr(t, err)
		if diff := cmp.Diff(got, []Update{tc.want}); diff != "" {
			t.Errorf("%d: update differs: (-got +want)\n%s", i, diff)
		}
	}
}

func TestTrackerThreads(t *testing.T) {
	p, n, tm := 1, "g1", time.Unix(0, 0).UTC()

	tests := []struct {
		proc IDInfo
		want Update
	}{
		{
			piinfo(p, n, Counts{}, Memory{}, Filedesc{1, 1}, 1),
			Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 1, States{}, msi{}, nil},
		}, {
			piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
				{ThreadID(ID{p, 0}), "t1", Counts{1, 2, 3, 4, 5, 6, 0, 0}, "", States{}},
				{ThreadID(ID{p + 1, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 0, 0}, "", States{}},
			}),
			Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
				[]ThreadUpdate{
					{"t1", Delta{}},
					{"t2", Delta{}},
				},
			},
		}, {
			piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
				{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 0, 0}, "", States{}},
				{ThreadID(ID{p + 1, 0}), "t2", Counts{2, 2, 2, 2, 2, 2, 0, 0}, "", States{}},
				{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 1, 1, 1, 1, 1, 0, 0}, "", States{}},
			}),
			Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 3, States{}, msi{},
				[]ThreadUpdate{
					{"t1", Delta{1, 1, 1, 1, 1, 1, 0, 0}},
					{"t2", Delta{1, 1, 1, 1, 1, 1, 0, 0}},
					{"t2", Delta{}},
				},
			},
		}, {
			piinfot(p, n, Counts{}, Memory{}, Filedesc{1, 1}, []Thread{
				{ThreadID(ID{p, 0}), "t1", Counts{2, 3, 4, 5, 6, 7, 0, 0}, "", States{}},
				{ThreadID(ID{p + 2, 0}), "t2", Counts{1, 2, 3, 4, 5, 6, 0, 0}, "", States{}},
			}),
			Update{n, Delta{}, Memory{}, Filedesc{1, 1}, tm, 2, States{}, msi{},
				[]ThreadUpdate{
					{"t1", Delta{}},
					{"t2", Delta{0, 1, 2, 3, 4, 5, 0, 0}},
				},
			},
		},
	}
	tr := NewTracker(newNamer(n), false, false, false)

	opts := cmpopts.SortSlices(lessThreadUpdate)
	for i, tc := range tests {
		_, got, err := tr.Update(procInfoIter(tc.proc))
		noerr(t, err)
		if diff := cmp.Diff(got, []Update{tc.want}, opts); diff != "" {
			t.Errorf("%d: update differs: (-got +want)\n%s, %v, %v", i, diff, got[0].Threads, tc.want.Threads)
		}
	}
}