forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
win_services_test.go
226 lines (199 loc) · 6.02 KB
/
win_services_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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// +build windows
package win_services
import (
"bytes"
"errors"
"fmt"
"log"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)
//testData is DD wrapper for unit testing of WinServices
type testData struct {
//collection that will be returned in ListServices if service array passed into WinServices constructor is empty
queryServiceList []string
mgrConnectError error
mgrListServicesError error
services []serviceTestInfo
}
type serviceTestInfo struct {
serviceOpenError error
serviceQueryError error
serviceConfigError error
serviceName string
displayName string
state int
startUpMode int
}
type FakeSvcMgr struct {
testData testData
}
func (m *FakeSvcMgr) Disconnect() error {
return nil
}
func (m *FakeSvcMgr) OpenService(name string) (WinService, error) {
for _, s := range m.testData.services {
if s.serviceName == name {
if s.serviceOpenError != nil {
return nil, s.serviceOpenError
} else {
return &FakeWinSvc{s}, nil
}
}
}
return nil, fmt.Errorf("Cannot find service %s", name)
}
func (m *FakeSvcMgr) ListServices() ([]string, error) {
if m.testData.mgrListServicesError != nil {
return nil, m.testData.mgrListServicesError
} else {
return m.testData.queryServiceList, nil
}
}
type FakeMgProvider struct {
testData testData
}
func (m *FakeMgProvider) Connect() (WinServiceManager, error) {
if m.testData.mgrConnectError != nil {
return nil, m.testData.mgrConnectError
} else {
return &FakeSvcMgr{m.testData}, nil
}
}
type FakeWinSvc struct {
testData serviceTestInfo
}
func (m *FakeWinSvc) Close() error {
return nil
}
func (m *FakeWinSvc) Config() (mgr.Config, error) {
if m.testData.serviceConfigError != nil {
return mgr.Config{}, m.testData.serviceConfigError
} else {
return mgr.Config{
ServiceType: 0,
StartType: uint32(m.testData.startUpMode),
ErrorControl: 0,
BinaryPathName: "",
LoadOrderGroup: "",
TagId: 0,
Dependencies: nil,
ServiceStartName: m.testData.serviceName,
DisplayName: m.testData.displayName,
Password: "",
Description: "",
}, nil
}
}
func (m *FakeWinSvc) Query() (svc.Status, error) {
if m.testData.serviceQueryError != nil {
return svc.Status{}, m.testData.serviceQueryError
} else {
return svc.Status{
State: svc.State(m.testData.state),
Accepts: 0,
CheckPoint: 0,
WaitHint: 0,
}, nil
}
}
var testErrors = []testData{
{nil, errors.New("Fake mgr connect error"), nil, nil},
{nil, nil, errors.New("Fake mgr list services error"), nil},
{[]string{"Fake service 1", "Fake service 2", "Fake service 3"}, nil, nil, []serviceTestInfo{
{errors.New("Fake srv open error"), nil, nil, "Fake service 1", "", 0, 0},
{nil, errors.New("Fake srv query error"), nil, "Fake service 2", "", 0, 0},
{nil, nil, errors.New("Fake srv config error"), "Fake service 3", "", 0, 0},
}},
{[]string{"Fake service 1"}, nil, nil, []serviceTestInfo{
{errors.New("Fake srv open error"), nil, nil, "Fake service 1", "", 0, 0},
}},
}
func TestBasicInfo(t *testing.T) {
winServices := &WinServices{
Log: testutil.Logger{},
mgrProvider: &FakeMgProvider{testErrors[0]},
}
winServices.Init()
assert.NotEmpty(t, winServices.SampleConfig())
assert.NotEmpty(t, winServices.Description())
}
func TestMgrErrors(t *testing.T) {
//mgr.connect error
winServices := &WinServices{
Log: testutil.Logger{},
mgrProvider: &FakeMgProvider{testErrors[0]},
}
var acc1 testutil.Accumulator
err := winServices.Gather(&acc1)
require.Error(t, err)
assert.Contains(t, err.Error(), testErrors[0].mgrConnectError.Error())
////mgr.listServices error
winServices = &WinServices{
Log: testutil.Logger{},
mgrProvider: &FakeMgProvider{testErrors[1]},
}
var acc2 testutil.Accumulator
err = winServices.Gather(&acc2)
require.Error(t, err)
assert.Contains(t, err.Error(), testErrors[1].mgrListServicesError.Error())
////mgr.listServices error 2
winServices = &WinServices{
Log: testutil.Logger{},
ServiceNames: []string{"Fake service 1"},
mgrProvider: &FakeMgProvider{testErrors[3]},
}
winServices.Init()
var acc3 testutil.Accumulator
buf := &bytes.Buffer{}
log.SetOutput(buf)
require.NoError(t, winServices.Gather(&acc3))
require.Contains(t, buf.String(), testErrors[2].services[0].serviceOpenError.Error())
}
func TestServiceErrors(t *testing.T) {
winServices := &WinServices{
Log: testutil.Logger{},
mgrProvider: &FakeMgProvider{testErrors[2]},
}
winServices.Init()
var acc1 testutil.Accumulator
buf := &bytes.Buffer{}
log.SetOutput(buf)
require.NoError(t, winServices.Gather(&acc1))
//open service error
require.Contains(t, buf.String(), testErrors[2].services[0].serviceOpenError.Error())
//query service error
require.Contains(t, buf.String(), testErrors[2].services[1].serviceQueryError.Error())
//config service error
require.Contains(t, buf.String(), testErrors[2].services[2].serviceConfigError.Error())
}
var testSimpleData = []testData{
{[]string{"Service 1", "Service 2"}, nil, nil, []serviceTestInfo{
{nil, nil, nil, "Service 1", "Fake service 1", 1, 2},
{nil, nil, nil, "Service 2", "Fake service 2", 1, 2},
}},
}
func TestGatherContainsTag(t *testing.T) {
winServices := &WinServices{
Log: testutil.Logger{},
ServiceNames: []string{"Service*"},
mgrProvider: &FakeMgProvider{testSimpleData[0]},
}
winServices.Init()
var acc1 testutil.Accumulator
require.NoError(t, winServices.Gather(&acc1))
assert.Len(t, acc1.Errors, 0, "There should be no errors after gather")
for _, s := range testSimpleData[0].services {
fields := make(map[string]interface{})
tags := make(map[string]string)
fields["state"] = int(s.state)
fields["startup_mode"] = int(s.startUpMode)
tags["service_name"] = s.serviceName
tags["display_name"] = s.displayName
acc1.AssertContainsTaggedFields(t, "win_services", fields, tags)
}
}