@@ -17,6 +17,7 @@ import (
17
17
"testing"
18
18
"time"
19
19
20
+ "golang.org/x/sys/windows"
20
21
"golang.org/x/sys/windows/svc"
21
22
"golang.org/x/sys/windows/svc/mgr"
22
23
)
@@ -109,7 +110,7 @@ func testRecoveryActions(t *testing.T, s *mgr.Service, should []mgr.RecoveryActi
109
110
if len (should ) != len (is ) {
110
111
t .Errorf ("recovery action mismatch: contains %v actions, but should have %v" , len (is ), len (should ))
111
112
}
112
- for i , _ := range is {
113
+ for i := range is {
113
114
if should [i ].Type != is [i ].Type {
114
115
t .Errorf ("recovery action mismatch: Type is %v, but should have %v" , is [i ].Type , should [i ].Type )
115
116
}
@@ -131,19 +132,19 @@ func testResetPeriod(t *testing.T, s *mgr.Service, should uint32) {
131
132
132
133
func testSetRecoveryActions (t * testing.T , s * mgr.Service ) {
133
134
r := []mgr.RecoveryAction {
134
- mgr. RecoveryAction {
135
+ {
135
136
Type : mgr .NoAction ,
136
137
Delay : 60000 * time .Millisecond ,
137
138
},
138
- mgr. RecoveryAction {
139
+ {
139
140
Type : mgr .ServiceRestart ,
140
141
Delay : 4 * time .Minute ,
141
142
},
142
- mgr. RecoveryAction {
143
+ {
143
144
Type : mgr .ServiceRestart ,
144
145
Delay : time .Minute ,
145
146
},
146
- mgr. RecoveryAction {
147
+ {
147
148
Type : mgr .RunCommand ,
148
149
Delay : 4000 * time .Millisecond ,
149
150
},
@@ -208,6 +209,16 @@ func testRecoveryCommand(t *testing.T, s *mgr.Service, should string) {
208
209
}
209
210
}
210
211
212
+ func testControl (t * testing.T , s * mgr.Service , c svc.Cmd , expectedErr error , expectedStatus svc.Status ) {
213
+ status , err := s .Control (c )
214
+ if err != expectedErr {
215
+ t .Fatalf ("Unexpected return from s.Control: %v (expected %v)" , err , expectedErr )
216
+ }
217
+ if expectedStatus != status {
218
+ t .Fatalf ("Unexpected status from s.Control: %+v (expected %+v)" , status , expectedStatus )
219
+ }
220
+ }
221
+
211
222
func remove (t * testing.T , s * mgr.Service ) {
212
223
err := s .Delete ()
213
224
if err != nil {
@@ -251,6 +262,7 @@ func TestMyService(t *testing.T) {
251
262
t .Fatalf ("service %s is not installed" , name )
252
263
}
253
264
defer s .Close ()
265
+ defer s .Delete ()
254
266
255
267
c .BinaryPathName = exepath
256
268
c = testConfig (t , s , c )
@@ -293,6 +305,11 @@ func TestMyService(t *testing.T) {
293
305
testRecoveryCommand (t , s , fmt .Sprintf ("sc query %s" , name ))
294
306
testRecoveryCommand (t , s , "" ) // delete recovery command
295
307
308
+ expectedStatus := svc.Status {
309
+ State : svc .Stopped ,
310
+ }
311
+ testControl (t , s , svc .Stop , windows .ERROR_SERVICE_NOT_ACTIVE , expectedStatus )
312
+
296
313
remove (t , s )
297
314
}
298
315
0 commit comments