github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/service/testing_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package service 5 6 import ( 7 "os" 8 "time" 9 10 "github.com/juju/errors" 11 "github.com/juju/testing" 12 "github.com/juju/utils" 13 "github.com/juju/utils/series" 14 "github.com/juju/version" 15 gc "gopkg.in/check.v1" 16 17 "github.com/juju/juju/service/common" 18 svctesting "github.com/juju/juju/service/common/testing" 19 ) 20 21 // Stub stubs out the external functions used in the service package. 22 type Stub struct { 23 *testing.Stub 24 25 Version version.Binary 26 Service Service 27 } 28 29 // GetVersion stubs out . 30 func (s *Stub) GetVersion() version.Binary { 31 s.AddCall("GetVersion") 32 33 // Pop the next error off the queue, even though we don't use it. 34 s.NextErr() 35 return s.Version 36 } 37 38 // DiscoverService stubs out service.DiscoverService. 39 func (s *Stub) DiscoverService(name string) (Service, error) { 40 s.AddCall("DiscoverService", name) 41 42 return s.Service, s.NextErr() 43 } 44 45 // TODO(ericsnow) StubFileInfo belongs in utils/fs. 46 47 // StubFileInfo implements os.FileInfo. 48 type StubFileInfo struct{} 49 50 func (StubFileInfo) Name() string { return "" } 51 func (StubFileInfo) Size() int64 { return 0 } 52 func (StubFileInfo) Mode() os.FileMode { return 0 } 53 func (StubFileInfo) ModTime() time.Time { return time.Time{} } 54 func (StubFileInfo) IsDir() bool { return false } 55 func (StubFileInfo) Sys() interface{} { return nil } 56 57 // StubFileInfo implements os.FileInfo for symlinks. 58 type StubSymlinkInfo struct{ StubFileInfo } 59 60 func (StubSymlinkInfo) Mode() os.FileMode { return os.ModeSymlink } 61 62 // BaseSuite is the base test suite for the service package. 63 type BaseSuite struct { 64 testing.IsolationSuite 65 66 Dirname string 67 Name string 68 Conf common.Conf 69 Failure error 70 71 Stub *testing.Stub 72 Service *svctesting.FakeService 73 Patched *Stub 74 } 75 76 func (s *BaseSuite) SetUpTest(c *gc.C) { 77 s.IsolationSuite.SetUpTest(c) 78 79 s.Dirname = c.MkDir() 80 s.Name = "juju-agent-machine-0" 81 s.Conf = common.Conf{ 82 Desc: "some service", 83 ExecStart: "/bin/jujud machine 0", 84 } 85 s.Failure = errors.New("<failed>") 86 87 s.Service = svctesting.NewFakeService(s.Name, s.Conf) 88 s.Stub = &s.Service.Stub 89 s.Patched = &Stub{Stub: s.Stub} 90 s.PatchValue(&discoverService, s.Patched.DiscoverService) 91 } 92 93 func (s *BaseSuite) PatchAttempts(retries int) { 94 s.PatchValue(&installStartRetryAttempts, utils.AttemptStrategy{ 95 Min: retries, 96 }) 97 } 98 99 func (s *BaseSuite) PatchSeries(ser string) { 100 s.PatchValue(&series.HostSeries, func() string { return ser }) 101 } 102 103 func NewDiscoveryCheck(name string, running bool, failure error) discoveryCheck { 104 return discoveryCheck{ 105 name: name, 106 isRunning: func() (bool, error) { 107 return running, failure 108 }, 109 } 110 } 111 112 func (s *BaseSuite) PatchLocalDiscovery(checks ...discoveryCheck) { 113 s.PatchValue(&discoveryFuncs, checks) 114 } 115 116 func (s *BaseSuite) PatchLocalDiscoveryDisable() { 117 s.PatchLocalDiscovery() 118 } 119 120 func (s *BaseSuite) PatchLocalDiscoveryNoMatch(expected string) { 121 // TODO(ericsnow) Pull from a list of supported init systems. 122 names := []string{ 123 InitSystemUpstart, 124 InitSystemSystemd, 125 InitSystemWindows, 126 } 127 var checks []discoveryCheck 128 for _, name := range names { 129 checks = append(checks, NewDiscoveryCheck(name, name == expected, nil)) 130 } 131 s.PatchLocalDiscovery(checks...) 132 } 133 134 func (s *BaseSuite) CheckFailure(c *gc.C, err error) { 135 c.Check(errors.Cause(err), gc.Equals, s.Failure) 136 }