github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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/svc" 11 "github.com/gabriel-samfira/sys/windows/svc/mgr" 12 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 27 Status svc.Status 28 } 29 30 func AddService(name, execStart string, stub *testing.Stub, status svc.Status) { 31 Services[name] = &StubService{ 32 Stub: stub, 33 Name: name, 34 ExecStart: execStart, 35 config: mgr.Config{}, 36 Status: status, 37 } 38 } 39 40 func (s *StubService) SetConfig(c mgr.Config) { 41 s.config = c 42 } 43 44 func (s *StubService) SetStatus(status svc.Status) { 45 s.Status = status 46 } 47 48 func (s *StubService) Config() (mgr.Config, error) { 49 return s.config, s.NextErr() 50 } 51 52 func (s *StubService) Control(c svc.Cmd) (svc.Status, error) { 53 s.Stub.AddCall("Control", c) 54 55 switch c { 56 case svc.Interrogate: 57 case svc.Stop: 58 s.Status = svc.Status{State: svc.Stopped} 59 case svc.Pause: 60 s.Status = svc.Status{State: svc.Paused} 61 case svc.Continue: 62 s.Status = svc.Status{State: svc.Running} 63 case svc.Shutdown: 64 s.Status = svc.Status{State: svc.Stopped} 65 } 66 return s.Status, s.NextErr() 67 } 68 69 func (s *StubService) Delete() error { 70 s.Stub.AddCall("Control") 71 72 if _, ok := Services[s.Name]; ok { 73 delete(Services, s.Name) 74 return s.NextErr() 75 } 76 return c_ERROR_SERVICE_DOES_NOT_EXIST 77 } 78 79 func (s *StubService) Query() (svc.Status, error) { 80 s.Stub.AddCall("Query") 81 82 return s.Status, s.NextErr() 83 } 84 85 func (s *StubService) Start(args ...string) error { 86 s.Stub.AddCall("Start", args) 87 88 s.Status = svc.Status{State: svc.Running} 89 return s.NextErr() 90 } 91 92 type StubMgr struct { 93 *testing.Stub 94 } 95 96 func (m *StubMgr) CreateService(name, exepath string, c mgr.Config, args ...string) (svcInterface, error) { 97 m.Stub.AddCall("CreateService", name, exepath, c) 98 99 if _, ok := Services[name]; ok { 100 return nil, c_ERROR_SERVICE_EXISTS 101 } 102 stubSvc := &StubService{ 103 Name: name, 104 ExecStart: exepath, 105 config: c, 106 Status: svc.Status{State: svc.Stopped}, 107 Stub: m.Stub, 108 } 109 Services[name] = stubSvc 110 return stubSvc, m.NextErr() 111 } 112 113 func (m *StubMgr) Disconnect() error { 114 m.Stub.AddCall("Disconnect") 115 return m.NextErr() 116 } 117 118 func (m *StubMgr) OpenService(name string) (svcInterface, error) { 119 m.Stub.AddCall("OpenService", name) 120 if stubSvc, ok := Services[name]; ok { 121 return stubSvc, m.NextErr() 122 } 123 return nil, c_ERROR_SERVICE_DOES_NOT_EXIST 124 } 125 126 func (m *StubMgr) Exists(name string) bool { 127 if _, ok := Services[name]; ok { 128 return true 129 } 130 return false 131 } 132 133 func (m *StubMgr) List() []string { 134 svcs := []string{} 135 for i := range Services { 136 svcs = append(svcs, i) 137 } 138 return svcs 139 } 140 141 func (m *StubMgr) Clear() { 142 Services = map[string]*StubService{} 143 }