github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/service/windows/stubwinsvc_test.go (about) 1 // Copyright 2015 Cloudbase Solutions 2 // Copyright 2015 Canonical Ltd. 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 // +build !linux windows 6 7 package windows 8 9 import ( 10 "github.com/gabriel-samfira/sys/windows" 11 "github.com/gabriel-samfira/sys/windows/svc" 12 "github.com/gabriel-samfira/sys/windows/svc/mgr" 13 "github.com/juju/testing" 14 ) 15 16 // Unfortunately this cannot be moved inside StubMgr because the Delete() method 17 // is attached to the service itself 18 var Services map[string]*StubService 19 20 type StubService struct { 21 *testing.Stub 22 23 Name string 24 config mgr.Config 25 ExecStart string 26 Closed bool 27 28 Status svc.Status 29 } 30 31 func AddService(name, execStart string, stub *testing.Stub, status svc.Status) { 32 Services[name] = &StubService{ 33 Stub: stub, 34 Name: name, 35 ExecStart: execStart, 36 config: mgr.Config{}, 37 Status: status, 38 } 39 } 40 41 func (s *StubService) Close() error { 42 s.Stub.AddCall("Close") 43 s.Closed = true 44 return s.NextErr() 45 } 46 47 func (s *StubService) UpdateConfig(c mgr.Config) error { 48 s.config = c 49 return s.NextErr() 50 } 51 52 func (s *StubService) SetStatus(status svc.Status) { 53 s.Status = status 54 } 55 56 func (s *StubService) Config() (mgr.Config, error) { 57 return s.config, s.NextErr() 58 } 59 60 func (s *StubService) Control(c svc.Cmd) (svc.Status, error) { 61 s.Stub.AddCall("Control", c) 62 63 switch c { 64 case svc.Interrogate: 65 case svc.Stop: 66 s.Status = svc.Status{State: svc.Stopped} 67 case svc.Pause: 68 s.Status = svc.Status{State: svc.Paused} 69 case svc.Continue: 70 s.Status = svc.Status{State: svc.Running} 71 case svc.Shutdown: 72 s.Status = svc.Status{State: svc.Stopped} 73 } 74 return s.Status, s.NextErr() 75 } 76 77 func (s *StubService) Delete() error { 78 s.Stub.AddCall("Control") 79 80 if _, ok := Services[s.Name]; ok { 81 delete(Services, s.Name) 82 return s.NextErr() 83 } 84 return c_ERROR_SERVICE_DOES_NOT_EXIST 85 } 86 87 func (s *StubService) Query() (svc.Status, error) { 88 s.Stub.AddCall("Query") 89 90 return s.Status, s.NextErr() 91 } 92 93 func (s *StubService) Start(args ...string) error { 94 s.Stub.AddCall("Start", args) 95 96 s.Status = svc.Status{State: svc.Running} 97 return s.NextErr() 98 } 99 100 type StubMgr struct { 101 *testing.Stub 102 } 103 104 func (m *StubMgr) CreateService(name, exepath string, c mgr.Config, args ...string) (windowsService, error) { 105 m.Stub.AddCall("CreateService", name, exepath, c) 106 107 if _, ok := Services[name]; ok { 108 return nil, c_ERROR_SERVICE_EXISTS 109 } 110 stubSvc := &StubService{ 111 Name: name, 112 ExecStart: exepath, 113 config: c, 114 Status: svc.Status{State: svc.Stopped}, 115 Stub: m.Stub, 116 } 117 Services[name] = stubSvc 118 return stubSvc, m.NextErr() 119 } 120 121 func (m *StubMgr) Disconnect() error { 122 m.Stub.AddCall("Disconnect") 123 return m.NextErr() 124 } 125 126 func (m *StubMgr) OpenService(name string) (windowsService, error) { 127 m.Stub.AddCall("OpenService", name) 128 if stubSvc, ok := Services[name]; ok { 129 return stubSvc, m.NextErr() 130 } 131 return nil, c_ERROR_SERVICE_DOES_NOT_EXIST 132 } 133 134 func (m *StubMgr) GetHandle(name string) (handle windows.Handle, err error) { 135 m.Stub.AddCall("GetHandle", name) 136 if _, ok := Services[name]; ok { 137 return handle, m.NextErr() 138 } 139 return handle, c_ERROR_SERVICE_DOES_NOT_EXIST 140 } 141 142 func (m *StubMgr) CloseHandle(handle windows.Handle) (err error) { 143 m.Stub.AddCall("CloseHandle") 144 return m.NextErr() 145 } 146 147 func (m *StubMgr) Exists(name string) bool { 148 if _, ok := Services[name]; ok { 149 return true 150 } 151 return false 152 } 153 154 func (m *StubMgr) List() []string { 155 svcs := []string{} 156 for i := range Services { 157 svcs = append(svcs, i) 158 } 159 return svcs 160 } 161 162 func (m *StubMgr) Clear() { 163 Services = map[string]*StubService{} 164 }