github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/service/common" 16 svctesting "github.com/juju/juju/service/common/testing" 17 "github.com/juju/juju/version" 18 ) 19 20 // Stub stubs out the external functions used in the service package. 21 type Stub struct { 22 *testing.Stub 23 24 Version version.Binary 25 Service Service 26 } 27 28 // GetVersion stubs out . 29 func (s *Stub) GetVersion() version.Binary { 30 s.AddCall("GetVersion") 31 32 // Pop the next error off the queue, even though we don't use it. 33 s.NextErr() 34 return s.Version 35 } 36 37 // DiscoverService stubs out service.DiscoverService. 38 func (s *Stub) DiscoverService(name string) (Service, error) { 39 s.AddCall("DiscoverService", name) 40 41 return s.Service, s.NextErr() 42 } 43 44 // TODO(ericsnow) StubFileInfo belongs in utils/fs. 45 46 // StubFileInfo implements os.FileInfo. 47 type StubFileInfo struct{} 48 49 func (StubFileInfo) Name() string { return "" } 50 func (StubFileInfo) Size() int64 { return 0 } 51 func (StubFileInfo) Mode() os.FileMode { return 0 } 52 func (StubFileInfo) ModTime() time.Time { return time.Time{} } 53 func (StubFileInfo) IsDir() bool { return false } 54 func (StubFileInfo) Sys() interface{} { return nil } 55 56 // StubFileInfo implements os.FileInfo for symlinks. 57 type StubSymlinkInfo struct{ StubFileInfo } 58 59 func (StubSymlinkInfo) Mode() os.FileMode { return os.ModeSymlink } 60 61 // BaseSuite is the base test suite for the service package. 62 type BaseSuite struct { 63 testing.IsolationSuite 64 65 Dirname string 66 Name string 67 Conf common.Conf 68 Failure error 69 70 Stub *testing.Stub 71 Service *svctesting.FakeService 72 Patched *Stub 73 } 74 75 func (s *BaseSuite) SetUpTest(c *gc.C) { 76 s.IsolationSuite.SetUpTest(c) 77 78 s.Dirname = c.MkDir() 79 s.Name = "juju-agent-machine-0" 80 s.Conf = common.Conf{ 81 Desc: "some service", 82 ExecStart: "/bin/jujud machine 0", 83 } 84 s.Failure = errors.New("<failed>") 85 86 s.Service = svctesting.NewFakeService(s.Name, s.Conf) 87 s.Stub = &s.Service.Stub 88 s.Patched = &Stub{Stub: s.Stub} 89 s.PatchValue(&discoverService, s.Patched.DiscoverService) 90 } 91 92 func (s *BaseSuite) PatchAttempts(retries int) { 93 s.PatchValue(&installStartRetryAttempts, utils.AttemptStrategy{ 94 Min: retries, 95 }) 96 } 97 98 func (s *BaseSuite) PatchVersion(vers version.Binary) { 99 s.Patched.Version = vers 100 s.PatchValue(&getVersion, s.Patched.GetVersion) 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 }