github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/service/service_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package service_test 5 6 import ( 7 "strings" 8 9 "github.com/juju/errors" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/service" 14 svctesting "github.com/juju/juju/service/common/testing" 15 "github.com/juju/juju/service/systemd" 16 "github.com/juju/juju/service/upstart" 17 "github.com/juju/juju/service/windows" 18 ) 19 20 type serviceSuite struct { 21 service.BaseSuite 22 } 23 24 var _ = gc.Suite(&serviceSuite{}) 25 26 func (s *serviceSuite) TestNewServiceKnown(c *gc.C) { 27 for _, test := range []struct { 28 series string 29 initSystem string 30 }{ 31 { 32 series: "vivid", 33 initSystem: service.InitSystemSystemd, 34 }, { 35 series: "trusty", 36 initSystem: service.InitSystemUpstart, 37 }, { 38 series: "win2012", 39 initSystem: service.InitSystemWindows, 40 }, 41 } { 42 svc, err := service.NewService(s.Name, s.Conf, test.series) 43 if !c.Check(err, jc.ErrorIsNil) { 44 continue 45 } 46 47 switch test.initSystem { 48 case service.InitSystemSystemd: 49 c.Check(svc, gc.FitsTypeOf, &systemd.Service{}) 50 case service.InitSystemUpstart: 51 c.Check(svc, gc.FitsTypeOf, &upstart.Service{}) 52 case service.InitSystemWindows: 53 c.Check(svc, gc.FitsTypeOf, &windows.Service{}) 54 } 55 c.Check(svc.Name(), gc.Equals, s.Name) 56 c.Check(svc.Conf(), jc.DeepEquals, s.Conf) 57 } 58 } 59 60 func (s *serviceSuite) TestNewServiceMissingName(c *gc.C) { 61 _, err := service.NewService("", s.Conf, service.InitSystemUpstart) 62 63 c.Check(err, gc.ErrorMatches, `.*missing name.*`) 64 } 65 66 func (s *serviceSuite) TestNewServiceUnknown(c *gc.C) { 67 _, err := service.NewService(s.Name, s.Conf, "<unknown>") 68 69 c.Check(err, jc.Satisfies, errors.IsNotFound) 70 } 71 72 func (s *serviceSuite) TestListServices(c *gc.C) { 73 _, err := service.ListServices() 74 75 c.Check(err, jc.ErrorIsNil) 76 } 77 78 func (*serviceSuite) TestListServicesScript(c *gc.C) { 79 script := service.ListServicesScript() 80 81 expected := strings.Split(service.DiscoverInitSystemScript(), "\n") 82 expected[0] = "init_system=$(" + expected[0] 83 expected[len(expected)-1] += ")" 84 expected = append(expected, 85 `case "$init_system" in`, 86 `systemd)`, 87 ` /bin/systemctl list-unit-files --no-legend --no-page -t service`+ 88 ` | grep -o -P '^\w[\S]*(?=\.service)'`, 89 ` ;;`, 90 `upstart)`, 91 ` sudo initctl list | awk '{print $1}' | sort | uniq`, 92 ` ;;`, 93 `*)`, 94 ` exit 1`, 95 ` ;;`, 96 `esac`, 97 ) 98 c.Check(strings.Split(script, "\n"), jc.DeepEquals, expected) 99 } 100 101 func (s *serviceSuite) TestInstallAndStartOkay(c *gc.C) { 102 s.PatchAttempts(5) 103 104 err := service.InstallAndStart(s.Service) 105 c.Assert(err, jc.ErrorIsNil) 106 107 s.Service.CheckCallNames(c, "Install", "Start") 108 } 109 110 func (s *serviceSuite) TestInstallAndStartRetry(c *gc.C) { 111 s.PatchAttempts(5) 112 s.Service.SetErrors(nil, s.Failure, s.Failure) 113 114 err := service.InstallAndStart(s.Service) 115 c.Assert(err, jc.ErrorIsNil) 116 117 s.Service.CheckCallNames(c, "Install", "Start", "Start", "Start") 118 } 119 120 func (s *serviceSuite) TestInstallAndStartFail(c *gc.C) { 121 s.PatchAttempts(3) 122 s.Service.SetErrors(nil, s.Failure, s.Failure, s.Failure) 123 124 err := service.InstallAndStart(s.Service) 125 126 s.CheckFailure(c, err) 127 s.Service.CheckCallNames(c, "Install", "Start", "Start", "Start") 128 } 129 130 type restartSuite struct { 131 service.BaseSuite 132 } 133 134 var _ = gc.Suite(&restartSuite{}) 135 136 func (s *restartSuite) SetUpTest(c *gc.C) { 137 s.BaseSuite.SetUpTest(c) 138 139 s.Patched.Service = s.Service 140 } 141 142 func (s *restartSuite) TestRestartStopAndStart(c *gc.C) { 143 err := service.Restart(s.Name) 144 c.Assert(err, jc.ErrorIsNil) 145 146 s.Stub.CheckCallNames(c, "DiscoverService", "Stop", "Start") 147 } 148 149 type restartable struct { 150 *svctesting.FakeService 151 } 152 153 func (s *restartable) Restart() error { 154 s.AddCall("Restart") 155 156 return s.NextErr() 157 } 158 159 func (s *restartSuite) TestRestartRestartable(c *gc.C) { 160 s.Patched.Service = &restartable{s.Service} 161 162 err := service.Restart(s.Name) 163 c.Assert(err, jc.ErrorIsNil) 164 165 s.Stub.CheckCallNames(c, "DiscoverService", "Restart") 166 } 167 168 func (s *restartSuite) TestRestartFailDiscovery(c *gc.C) { 169 s.Stub.SetErrors(s.Failure) 170 171 err := service.Restart(s.Name) 172 173 s.CheckFailure(c, err) 174 s.Stub.CheckCallNames(c, "DiscoverService") 175 } 176 177 func (s *restartSuite) TestRestartFailStop(c *gc.C) { 178 s.Stub.SetErrors(nil, s.Failure) // DiscoverService, Stop 179 180 err := service.Restart(s.Name) 181 182 s.CheckFailure(c, err) 183 s.Stub.CheckCallNames(c, "DiscoverService", "Stop") 184 } 185 186 func (s *restartSuite) TestRestartFailStart(c *gc.C) { 187 s.Stub.SetErrors(nil, nil, s.Failure) // DiscoverService, Stop, Start 188 189 err := service.Restart(s.Name) 190 191 s.CheckFailure(c, err) 192 s.Stub.CheckCallNames(c, "DiscoverService", "Stop", "Start") 193 } 194 195 func (s *restartSuite) TestRestartFailRestart(c *gc.C) { 196 s.Patched.Service = &restartable{s.Service} 197 s.Stub.SetErrors(nil, s.Failure) // DiscoverService, Restart 198 199 err := service.Restart(s.Name) 200 201 s.CheckFailure(c, err) 202 s.Stub.CheckCallNames(c, "DiscoverService", "Restart") 203 }