master
go 208 lines 4.34 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package intelgpu
4
5 import (
6 "context"
7 "errors"
8 "os"
9 "testing"
10
11 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 )
16
17 var (
18 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
19 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
20
21 dataIntelTopGpuJSON, _ = os.ReadFile("testdata/igt.json")
22 )
23
24 func Test_testDataIsValid(t *testing.T) {
25
26 for name, data := range map[string][]byte{
27 "dataConfigJSON": dataConfigJSON,
28 "dataConfigYAML": dataConfigYAML,
29 "dataIntelTopGpuJSON": dataIntelTopGpuJSON,
30 } {
31 require.NotNil(t, data, name)
32 }
33 }
34
35 func TestCollector_ConfigurationSerialize(t *testing.T) {
36 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
37 }
38
39 func TestCollector_Init(t *testing.T) {
40 tests := map[string]struct {
41 prepare func(collr *Collector)
42 wantFail bool
43 }{
44 "fails if can't locate ndsudo": {
45 wantFail: true,
46 prepare: func(collr *Collector) {
47 collr.ndsudoName += "!!!"
48 },
49 },
50 }
51
52 for name, test := range tests {
53 t.Run(name, func(t *testing.T) {
54 collr := New()
55
56 test.prepare(collr)
57
58 if test.wantFail {
59 assert.Error(t, collr.Init(context.Background()))
60 } else {
61 assert.NoError(t, collr.Init(context.Background()))
62 }
63 })
64 }
65 }
66
67 func TestCollector_Check(t *testing.T) {
68 tests := map[string]struct {
69 prepareMock func() *mockIntelGpuTop
70 wantFail bool
71 }{
72 "success case": {
73 prepareMock: prepareMockOK,
74 wantFail: false,
75 },
76 "fail on error": {
77 prepareMock: prepareMockErrOnGPUSummaryJson,
78 wantFail: true,
79 },
80 }
81
82 for name, test := range tests {
83 t.Run(name, func(t *testing.T) {
84 collr := New()
85 mock := test.prepareMock()
86 collr.exec = mock
87
88 if test.wantFail {
89 assert.Error(t, collr.Check(context.Background()))
90 } else {
91 assert.NoError(t, collr.Check(context.Background()))
92 }
93 })
94 }
95 }
96
97 func TestCollector_Collect(t *testing.T) {
98 tests := map[string]struct {
99 prepareMock func() *mockIntelGpuTop
100 wantMetrics map[string]int64
101 }{
102 "success case": {
103 prepareMock: prepareMockOK,
104 wantMetrics: map[string]int64{
105 "engine_Blitter/0_busy": 0,
106 "engine_Render/3D/0_busy": 9609,
107 "engine_Video/0_busy": 7295,
108 "engine_Video/1_busy": 7740,
109 "engine_VideoEnhance/0_busy": 0,
110 "frequency_actual": 125308,
111 "power_gpu": 323,
112 "power_package": 1665,
113 },
114 },
115 "fail on error": {
116 prepareMock: prepareMockErrOnGPUSummaryJson,
117 wantMetrics: nil,
118 },
119 }
120
121 for name, test := range tests {
122 t.Run(name, func(t *testing.T) {
123 collr := New()
124 mock := test.prepareMock()
125 collr.exec = mock
126
127 mx := collr.Collect(context.Background())
128
129 assert.Equal(t, test.wantMetrics, mx)
130 if len(test.wantMetrics) > 0 {
131 assert.Len(t, *collr.Charts(), len(charts)+len(collr.engines))
132 }
133 })
134 }
135 }
136
137 func TestCollector_Cleanup(t *testing.T) {
138 tests := map[string]struct {
139 prepare func() *Collector
140 }{
141 "not initialized exec": {
142 prepare: func() *Collector {
143 return New()
144 },
145 },
146 "after check": {
147 prepare: func() *Collector {
148 collr := New()
149 collr.exec = prepareMockOK()
150 _ = collr.Check(context.Background())
151 return collr
152 },
153 },
154 "after collect": {
155 prepare: func() *Collector {
156 collr := New()
157 collr.exec = prepareMockOK()
158 _ = collr.Collect(context.Background())
159 return collr
160 },
161 },
162 }
163
164 for name, test := range tests {
165 t.Run(name, func(t *testing.T) {
166 collr := test.prepare()
167
168 mock, ok := collr.exec.(*mockIntelGpuTop)
169
170 assert.NotPanics(t, func() { collr.Cleanup(context.Background()) })
171
172 if ok {
173 assert.True(t, mock.stopCalled)
174 }
175 })
176 }
177 }
178
179 func prepareMockOK() *mockIntelGpuTop {
180 return &mockIntelGpuTop{
181 gpuSummaryJson: dataIntelTopGpuJSON,
182 }
183 }
184
185 func prepareMockErrOnGPUSummaryJson() *mockIntelGpuTop {
186 return &mockIntelGpuTop{
187 errOnQueryGPUSummaryJson: true,
188 }
189 }
190
191 type mockIntelGpuTop struct {
192 errOnQueryGPUSummaryJson bool
193 gpuSummaryJson []byte
194
195 stopCalled bool
196 }
197
198 func (m *mockIntelGpuTop) queryGPUSummaryJson() ([]byte, error) {
199 if m.errOnQueryGPUSummaryJson {
200 return nil, errors.New("error on mock.queryGPUSummaryJson()")
201 }
202 return m.gpuSummaryJson, nil
203 }
204
205 func (m *mockIntelGpuTop) stop() error {
206 m.stopCalled = true
207 return nil
208 }