-
Notifications
You must be signed in to change notification settings - Fork 66
/
winsvc.go
106 lines (95 loc) · 2.88 KB
/
winsvc.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
//go:build windows
package fake
import (
"fmt"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)
type FakeService struct {
name string
config mgr.Config
status svc.Status
serviceList *fakeServiceList
}
func (f *FakeService) Close() error {
return nil
}
func (f *FakeService) Start(_ ...string) error {
if f.status.State == svc.Running {
return fmt.Errorf("service already running")
}
// each of the service's dependencies must be started before the service is started
for _, dependency := range f.config.Dependencies {
dependencyService, present := f.serviceList.read(dependency)
if !present {
return fmt.Errorf("dependent service doesnt exist")
}
// Windows will attempt to start the service only if it is not already running
dependencyStatus, err := dependencyService.Query()
if err != nil {
return err
}
if dependencyStatus.State != svc.Running {
err = dependencyService.Start()
if err != nil {
return fmt.Errorf("error starting dependency %s: %w", dependency, err)
}
}
}
f.status.State = svc.Running
return nil
}
func (f *FakeService) Config() (mgr.Config, error) {
return f.config, nil
}
func (f *FakeService) Control(cmd svc.Cmd) (svc.Status, error) {
switch cmd {
case svc.Stop:
if f.status.State == svc.Stopped {
return svc.Status{}, fmt.Errorf("service already stopped")
}
// Windows has a hard time stopping services that other services are dependent on. To most safely model this
// functionality it is better to make it so that our mock manager is completely unable to stop services in that
// scenario.
existingServices := f.serviceList.listServiceNames()
for _, serviceName := range existingServices {
service, present := f.serviceList.read(serviceName)
if !present {
return svc.Status{}, fmt.Errorf("unable to open service " + serviceName)
}
config, err := service.Config()
if err != nil {
return svc.Status{}, fmt.Errorf("error getting %s service config: %w", serviceName, err)
}
for _, dependency := range config.Dependencies {
// Found a service that has this one as a dependency, ensure it is not running
if dependency == f.name {
status, err := service.Query()
if err != nil {
return svc.Status{}, fmt.Errorf("error querying %s service status: %w", serviceName, err)
}
if status.State != svc.Stopped {
return svc.Status{}, fmt.Errorf("cannot stop service as other service " + serviceName +
" is dependent on it")
}
}
}
}
f.status.State = svc.Stopped
}
return f.status, nil
}
func (f *FakeService) Query() (svc.Status, error) {
return f.status, nil
}
func (f *FakeService) UpdateConfig(config mgr.Config) error {
f.config = config
return nil
}
func NewFakeService(name string, config mgr.Config, status svc.Status) *FakeService {
return &FakeService{
name: name,
config: config,
status: status,
}
}