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