-
Notifications
You must be signed in to change notification settings - Fork 30
/
metrics_test.go
203 lines (180 loc) · 6.34 KB
/
metrics_test.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package metrics_test
import (
"context"
"encoding/json"
"os/exec"
"path/filepath"
"testing"
"github.com/ubuntu/ubuntu-report/internal/helper"
"github.com/ubuntu/ubuntu-report/internal/metrics"
)
func TestGetIDS(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
root string
wantDistro string
wantVersion string
wantErr bool
}{
{"regular", "testdata/good", "ubuntu", "18.04", false},
{"doesn't exist", "testdata/none", "", "", true},
{"empty file", "testdata/empty", "", "", true},
{"missing distro", "testdata/missing-fields/ids/distro", "", "", true},
{"missing version", "testdata/missing-fields/ids/version", "", "", true},
{"missing both", "testdata/missing-fields/ids/both", "", "", true},
{"empty distro", "testdata/empty-fields/ids/distro", "", "", true},
{"empty version", "testdata/empty-fields/ids/version", "", "", true},
{"empty both", "testdata/empty-fields/ids/both", "", "", true},
{"garbage content", "testdata/garbage", "", "", true},
}
for _, tc := range testCases {
tc := tc // capture range variable for parallel execution
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
a := helper.Asserter{T: t}
m := newTestMetrics(t, metrics.WithRootAt(tc.root))
d, v, err := m.GetIDS()
a.CheckWantedErr(err, tc.wantErr)
a.Equal(d, tc.wantDistro)
a.Equal(v, tc.wantVersion)
})
}
}
func TestCollect(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
root string
caseGPU string
caseCPU string
caseScreen string
casePartition string
caseArchitecture string
env map[string]string
// note that only an internal json package error can make it returning an error
wantErr bool
}{
{"regular",
"testdata/good", "one gpu", "regular", "one screen", "one partition", "regular",
map[string]string{"XDG_CURRENT_DESKTOP": "some:thing", "XDG_SESSION_DESKTOP": "ubuntusession", "XDG_SESSION_TYPE": "x12", "LANG": "fr_FR.UTF-8", "LANGUAGE": "fr_FR.UTF-8"},
false},
{"empty",
"testdata/none", "empty", "empty", "empty", "empty", "empty",
nil,
false},
}
for _, tc := range testCases {
tc := tc // capture range variable for parallel execution
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
a := helper.Asserter{T: t}
cmdGPU, cancel := newMockShortCmd(t, "lspci", "-n", tc.caseGPU)
defer cancel()
cmdCPU, cancel := newMockShortCmd(t, "lscpu", "-J", tc.caseCPU)
defer cancel()
cmdScreen, cancel := newMockShortCmd(t, "xrandr", tc.caseScreen)
defer cancel()
cmdPartition, cancel := newMockShortCmd(t, "df", tc.casePartition)
defer cancel()
cmdArchitecture, cancel := newMockShortCmd(t, "dpkg", "--print-architecture", tc.caseArchitecture)
defer cancel()
m := newTestMetrics(t, metrics.WithRootAt(tc.root),
metrics.WithGPUInfoCommand(cmdGPU),
metrics.WithCPUInfoCommand(cmdCPU),
metrics.WithScreenInfoCommand(cmdScreen),
metrics.WithSpaceInfoCommand(cmdPartition),
metrics.WithArchitectureCommand(cmdArchitecture),
metrics.WithMapForEnv(tc.env))
got, err := m.Collect()
want := helper.LoadOrUpdateGolden(t, filepath.Join(tc.root, "gold", "collect"), got, *metrics.Update)
a.CheckWantedErr(err, tc.wantErr)
a.Equal(got, want)
})
}
}
func TestRunCollectTwice(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
root string
caseGPU string
caseCPU string
caseScreen string
casePartition string
caseArchitecture string
env map[string]string
// note that only an internal json package error can make it returning an error
wantErr bool
}{
{"regular",
"testdata/good", "one gpu", "regular", "one screen", "one partition", "regular",
map[string]string{"XDG_CURRENT_DESKTOP": "some:thing", "XDG_SESSION_DESKTOP": "ubuntusession", "XDG_SESSION_TYPE": "x12", "LANG": "fr_FR.UTF-8", "LANGUAGE": "fr_FR.UTF-8"},
false},
{"empty",
"testdata/none", "empty", "empty", "empty", "empty", "empty",
nil,
false},
}
for _, tc := range testCases {
tc := tc // capture range variable for parallel execution
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
a := helper.Asserter{T: t}
cmdGPU, cancel := newMockShortCmd(t, "lspci", "-n", tc.caseGPU)
defer cancel()
cmdCPU, cancel := newMockShortCmd(t, "lscpu", "-J", tc.caseCPU)
defer cancel()
cmdScreen, cancel := newMockShortCmd(t, "xrandr", tc.caseScreen)
defer cancel()
cmdPartition, cancel := newMockShortCmd(t, "df", tc.casePartition)
defer cancel()
cmdArchitecture, cancel := newMockShortCmd(t, "dpkg", "--print-architecture", tc.caseArchitecture)
defer cancel()
m := newTestMetrics(t, metrics.WithRootAt(tc.root),
metrics.WithGPUInfoCommand(cmdGPU),
metrics.WithCPUInfoCommand(cmdCPU),
metrics.WithScreenInfoCommand(cmdScreen),
metrics.WithSpaceInfoCommand(cmdPartition),
metrics.WithArchitectureCommand(cmdArchitecture),
metrics.WithMapForEnv(tc.env))
b1, err1 := m.Collect()
cmdGPU, cancel = newMockShortCmd(t, "lspci", "-n", tc.caseGPU)
defer cancel()
cmdCPU, cancel = newMockShortCmd(t, "lscpu", "-J", tc.caseCPU)
defer cancel()
cmdScreen, cancel = newMockShortCmd(t, "xrandr", tc.caseScreen)
defer cancel()
cmdPartition, cancel = newMockShortCmd(t, "df", tc.casePartition)
defer cancel()
cmdArchitecture, cancel = newMockShortCmd(t, "dpkg", "--print-architecture", tc.caseArchitecture)
defer cancel()
m = newTestMetrics(t, metrics.WithRootAt(tc.root),
metrics.WithGPUInfoCommand(cmdGPU),
metrics.WithCPUInfoCommand(cmdCPU),
metrics.WithScreenInfoCommand(cmdScreen),
metrics.WithSpaceInfoCommand(cmdPartition),
metrics.WithArchitectureCommand(cmdArchitecture),
metrics.WithMapForEnv(tc.env))
b2, err2 := m.Collect()
a.CheckWantedErr(err1, tc.wantErr)
a.CheckWantedErr(err2, tc.wantErr)
var got1, got2 json.RawMessage
json.Unmarshal(b1, &got1)
json.Unmarshal(b2, &got2)
a.Equal(got1, got2)
})
}
}
func newTestMetrics(t *testing.T, fixtures ...func(m *metrics.Metrics) error) metrics.Metrics {
t.Helper()
m, err := metrics.New(fixtures...)
if err != nil {
t.Fatal("can't create metrics object", err)
}
return m
}
func newMockShortCmd(t *testing.T, s ...string) (*exec.Cmd, context.CancelFunc) {
t.Helper()
return helper.ShortProcess(t, "TestMetricsHelperProcess", s...)
}