github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/jujud/reboot/reboot_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Copyright 2014 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package reboot_test 6 7 import ( 8 "os" 9 "path/filepath" 10 "strings" 11 stdtesting "testing" 12 13 "github.com/juju/testing" 14 jc "github.com/juju/testing/checkers" 15 gc "gopkg.in/check.v1" 16 "gopkg.in/juju/names.v2" 17 18 "github.com/juju/juju/agent" 19 "github.com/juju/juju/api" 20 "github.com/juju/juju/apiserver/params" 21 "github.com/juju/juju/cmd/jujud/reboot" 22 "github.com/juju/juju/core/instance" 23 jujutesting "github.com/juju/juju/juju/testing" 24 "github.com/juju/juju/mongo" 25 "github.com/juju/juju/service" 26 "github.com/juju/juju/service/common" 27 svctesting "github.com/juju/juju/service/common/testing" 28 coretesting "github.com/juju/juju/testing" 29 jujuversion "github.com/juju/juju/version" 30 ) 31 32 func TestAll(t *stdtesting.T) { 33 coretesting.MgoTestPackage(t) 34 } 35 36 type RebootSuite struct { 37 jujutesting.JujuConnSuite 38 39 acfg agent.Config 40 mgoInst testing.MgoInstance 41 st api.Connection 42 43 tmpDir string 44 rebootScriptName string 45 46 services []*svctesting.FakeService 47 serviceData *svctesting.FakeServiceData 48 } 49 50 var _ = gc.Suite(&RebootSuite{}) 51 52 func (s *RebootSuite) SetUpSuite(c *gc.C) { 53 s.JujuConnSuite.SetUpSuite(c) 54 55 // These tests only patch out LXC, so only run full-stack tests 56 // over LXC. 57 s.PatchValue(&instance.ContainerTypes, []instance.ContainerType{instance.LXD}) 58 } 59 60 func (s *RebootSuite) SetUpTest(c *gc.C) { 61 s.JujuConnSuite.SetUpTest(c) 62 testing.PatchExecutableAsEchoArgs(c, s, rebootBin) 63 s.PatchEnvironment("TEMP", c.MkDir()) 64 65 s.tmpDir = c.MkDir() 66 s.rebootScriptName = "juju-reboot-script" 67 s.PatchValue(reboot.TmpFile, func() (*os.File, error) { 68 script := s.rebootScript(c) 69 return os.Create(script) 70 }) 71 72 s.mgoInst.EnableAuth = true 73 err := s.mgoInst.Start(coretesting.Certs) 74 c.Assert(err, jc.ErrorIsNil) 75 76 configParams := agent.AgentConfigParams{ 77 Paths: agent.Paths{DataDir: c.MkDir()}, 78 Tag: names.NewMachineTag("0"), 79 UpgradedToVersion: jujuversion.Current, 80 APIAddresses: []string{"localhost:17070"}, 81 CACert: coretesting.CACert, 82 Password: "fake", 83 Controller: s.State.ControllerTag(), 84 Model: s.Model.ModelTag(), 85 MongoVersion: mongo.Mongo24, 86 } 87 s.st, _ = s.OpenAPIAsNewMachine(c) 88 89 s.acfg, err = agent.NewAgentConfig(configParams) 90 c.Assert(err, jc.ErrorIsNil) 91 fakeServices := []string{ 92 "jujud-machine-1", 93 "jujud-unit-drupal-1", 94 "jujud-unit-mysql-1", 95 "fake-random-service", 96 } 97 for _, fake := range fakeServices { 98 s.addService(fake) 99 } 100 testing.PatchValue(&service.NewService, s.newService) 101 testing.PatchValue(&service.ListServices, s.listServices) 102 } 103 104 func (s *RebootSuite) addService(name string) { 105 svc, _ := s.newService(name, common.Conf{}, "") 106 svc.Install() 107 svc.Start() 108 } 109 110 func (s *RebootSuite) listServices() ([]string, error) { 111 return s.serviceData.InstalledNames(), nil 112 } 113 114 func (s *RebootSuite) newService(name string, conf common.Conf, series string) (service.Service, error) { 115 for _, svc := range s.services { 116 if svc.Name() == name { 117 return svc, nil 118 } 119 } 120 if s.serviceData == nil { 121 s.serviceData = svctesting.NewFakeServiceData() 122 } 123 svc := &svctesting.FakeService{ 124 FakeServiceData: s.serviceData, 125 Service: common.Service{ 126 Name: name, 127 Conf: common.Conf{}, 128 }, 129 } 130 s.services = append(s.services, svc) 131 return svc, nil 132 } 133 134 func (s *RebootSuite) TestRebootStopUnits(c *gc.C) { 135 w, err := reboot.NewRebootWaiter(s.acfg) 136 c.Assert(err, jc.ErrorIsNil) 137 138 err = w.ExecuteReboot(params.ShouldReboot) 139 c.Assert(err, jc.ErrorIsNil) 140 141 for _, svc := range s.services { 142 name := svc.Name() 143 if strings.HasPrefix(name, `jujud-unit-`) { 144 running, err := svc.Running() 145 c.Assert(err, jc.ErrorIsNil) 146 c.Assert(running, jc.IsFalse) 147 } else { 148 running, err := svc.Running() 149 c.Assert(err, jc.ErrorIsNil) 150 c.Assert(running, jc.IsTrue) 151 } 152 } 153 } 154 155 func (s *RebootSuite) TearDownTest(c *gc.C) { 156 s.mgoInst.Destroy() 157 s.JujuConnSuite.TearDownTest(c) 158 } 159 160 func (s *RebootSuite) rebootScript(c *gc.C) string { 161 return filepath.Join(s.tmpDir, s.rebootScriptName) 162 }