github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/service/windows/password_windows_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Copyright 2015 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 // +build windows 6 7 package windows_test 8 9 import ( 10 "github.com/juju/errors" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/service/windows" 15 coretesting "github.com/juju/juju/testing" 16 ) 17 18 var ( 19 _ = gc.Suite(&ServicePasswordChangerSuite{}) 20 _ = gc.Suite(&EnsurePasswordSuite{}) 21 ) 22 23 type myAmazingServiceManager struct { 24 windows.SvcManager 25 svcNames []string 26 pwd string 27 } 28 29 func (mgr *myAmazingServiceManager) ChangeServicePassword(name, newPassword string) error { 30 mgr.svcNames = append(mgr.svcNames, name) 31 mgr.pwd = newPassword 32 if name == "failme" { 33 return errors.New("wubwub") 34 } 35 return nil 36 } 37 38 type ServicePasswordChangerSuite struct { 39 coretesting.BaseSuite 40 c *windows.PasswordChanger 41 mgr *myAmazingServiceManager 42 } 43 44 func (s *ServicePasswordChangerSuite) SetUpTest(c *gc.C) { 45 s.BaseSuite.SetUpTest(c) 46 s.c = &windows.PasswordChanger{} 47 s.mgr = &myAmazingServiceManager{} 48 } 49 50 func listServices() ([]string, error) { 51 return []string{"boom", "pow"}, nil 52 } 53 54 func listServicesFailingService() ([]string, error) { 55 return []string{"boom", "failme", "pow"}, nil 56 } 57 58 func brokenListServices() ([]string, error) { 59 return nil, errors.New("ludicrous") 60 } 61 62 func (s *ServicePasswordChangerSuite) TestChangeServicePasswordListSucceeds(c *gc.C) { 63 err := s.c.ChangeJujudServicesPassword("newPass", s.mgr, listServices) 64 c.Assert(err, jc.ErrorIsNil) 65 c.Assert(s.mgr.svcNames, gc.DeepEquals, []string{"boom", "pow"}) 66 c.Assert(s.mgr.pwd, gc.Equals, "newPass") 67 } 68 69 func (s *ServicePasswordChangerSuite) TestChangeServicePasswordListFails(c *gc.C) { 70 err := s.c.ChangeJujudServicesPassword("newPass", s.mgr, brokenListServices) 71 c.Assert(err, gc.ErrorMatches, "ludicrous") 72 } 73 74 func (s *ServicePasswordChangerSuite) TestChangePasswordFails(c *gc.C) { 75 err := s.c.ChangeJujudServicesPassword("newPass", s.mgr, listServicesFailingService) 76 c.Assert(err, gc.ErrorMatches, "wubwub") 77 c.Assert(s.mgr.svcNames, gc.DeepEquals, []string{"boom", "failme"}) 78 c.Assert(s.mgr.pwd, gc.Equals, "newPass") 79 } 80 81 type helpersStub struct { 82 failLocalhost bool 83 failServices bool 84 localhostCalled bool 85 serviceCalled bool 86 } 87 88 func (s *helpersStub) reset() { 89 s.localhostCalled = false 90 s.serviceCalled = false 91 } 92 93 func (s *helpersStub) ChangeUserPasswordLocalhost(newPassword string) error { 94 s.localhostCalled = true 95 if s.failLocalhost { 96 return errors.New("zzz") 97 } 98 return nil 99 } 100 101 func (s *helpersStub) ChangeJujudServicesPassword(newPassword string, mgr windows.ServiceManager, listServices func() ([]string, error)) error { 102 s.serviceCalled = true 103 if s.failServices { 104 return errors.New("splat") 105 } 106 return nil 107 } 108 109 type EnsurePasswordSuite struct { 110 coretesting.BaseSuite 111 username string 112 newPassword string 113 } 114 115 func (s *EnsurePasswordSuite) SetUpSuite(c *gc.C) { 116 s.BaseSuite.SetUpSuite(c) 117 s.username = "jujud" 118 s.newPassword = "pass" 119 } 120 121 func (s *EnsurePasswordSuite) TestBothCalledAndSucceed(c *gc.C) { 122 stub := helpersStub{} 123 err := windows.EnsureJujudPasswordHelper(s.username, s.newPassword, nil, &stub) 124 c.Assert(stub.localhostCalled, jc.IsTrue) 125 c.Assert(stub.serviceCalled, jc.IsTrue) 126 c.Assert(err, jc.ErrorIsNil) 127 } 128 129 func (s *EnsurePasswordSuite) TestChangePasswordFails(c *gc.C) { 130 stub := helpersStub{failLocalhost: true} 131 err := windows.EnsureJujudPasswordHelper(s.username, s.newPassword, nil, &stub) 132 c.Assert(err, gc.ErrorMatches, "could not change user password: zzz") 133 c.Assert(errors.Cause(err), gc.ErrorMatches, "zzz") 134 c.Assert(stub.localhostCalled, jc.IsTrue) 135 c.Assert(stub.serviceCalled, jc.IsFalse) 136 } 137 138 func (s *EnsurePasswordSuite) TestChangeServicesFails(c *gc.C) { 139 stub := helpersStub{failServices: true} 140 err := windows.EnsureJujudPasswordHelper(s.username, s.newPassword, nil, &stub) 141 c.Assert(err, gc.ErrorMatches, "could not change password for all jujud services: splat") 142 c.Assert(errors.Cause(err), gc.ErrorMatches, "splat") 143 c.Assert(stub.localhostCalled, jc.IsTrue) 144 c.Assert(stub.serviceCalled, jc.IsTrue) 145 }