Skip to content

Commit 5a7b72a

Browse files
committed
Add a test
1 parent 9859284 commit 5a7b72a

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

windows/svc/mgr/mgr_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"testing"
1818
"time"
1919

20+
"golang.org/x/sys/windows"
2021
"golang.org/x/sys/windows/svc"
2122
"golang.org/x/sys/windows/svc/mgr"
2223
)
@@ -109,7 +110,7 @@ func testRecoveryActions(t *testing.T, s *mgr.Service, should []mgr.RecoveryActi
109110
if len(should) != len(is) {
110111
t.Errorf("recovery action mismatch: contains %v actions, but should have %v", len(is), len(should))
111112
}
112-
for i, _ := range is {
113+
for i := range is {
113114
if should[i].Type != is[i].Type {
114115
t.Errorf("recovery action mismatch: Type is %v, but should have %v", is[i].Type, should[i].Type)
115116
}
@@ -131,19 +132,19 @@ func testResetPeriod(t *testing.T, s *mgr.Service, should uint32) {
131132

132133
func testSetRecoveryActions(t *testing.T, s *mgr.Service) {
133134
r := []mgr.RecoveryAction{
134-
mgr.RecoveryAction{
135+
{
135136
Type: mgr.NoAction,
136137
Delay: 60000 * time.Millisecond,
137138
},
138-
mgr.RecoveryAction{
139+
{
139140
Type: mgr.ServiceRestart,
140141
Delay: 4 * time.Minute,
141142
},
142-
mgr.RecoveryAction{
143+
{
143144
Type: mgr.ServiceRestart,
144145
Delay: time.Minute,
145146
},
146-
mgr.RecoveryAction{
147+
{
147148
Type: mgr.RunCommand,
148149
Delay: 4000 * time.Millisecond,
149150
},
@@ -251,6 +252,9 @@ func TestMyService(t *testing.T) {
251252
t.Fatalf("service %s is not installed", name)
252253
}
253254
defer s.Close()
255+
defer func() {
256+
_ = s.Delete()
257+
}()
254258

255259
c.BinaryPathName = exepath
256260
c = testConfig(t, s, c)
@@ -293,6 +297,17 @@ func TestMyService(t *testing.T) {
293297
testRecoveryCommand(t, s, fmt.Sprintf("sc query %s", name))
294298
testRecoveryCommand(t, s, "") // delete recovery command
295299

300+
expectedStatus := svc.Status{
301+
State: svc.Stopped,
302+
}
303+
status, err := s.Control(svc.Stop)
304+
if err != windows.ERROR_SERVICE_NOT_ACTIVE {
305+
t.Fatalf("Unexpected return from s.Control: %v (expected %v)", err, windows.ERROR_SERVICE_NOT_ACTIVE)
306+
}
307+
if expectedStatus != status {
308+
t.Fatalf("Unexpected status from s.Control: %+v (expected %+v)", status, expectedStatus)
309+
}
310+
296311
remove(t, s)
297312
}
298313

0 commit comments

Comments
 (0)