github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/keyupdater/authorisedkeys_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package keyupdater_test 5 6 import ( 7 "github.com/juju/names" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/api" 12 "github.com/juju/juju/api/keyupdater" 13 jujutesting "github.com/juju/juju/juju/testing" 14 "github.com/juju/juju/state" 15 "github.com/juju/juju/state/testing" 16 ) 17 18 type keyupdaterSuite struct { 19 jujutesting.JujuConnSuite 20 21 // These are raw State objects. Use them for setup and assertions, but 22 // should never be touched by the API calls themselves 23 rawMachine *state.Machine 24 25 keyupdater *keyupdater.State 26 } 27 28 var _ = gc.Suite(&keyupdaterSuite{}) 29 30 func (s *keyupdaterSuite) SetUpTest(c *gc.C) { 31 s.JujuConnSuite.SetUpTest(c) 32 var stateAPI api.Connection 33 stateAPI, s.rawMachine = s.OpenAPIAsNewMachine(c) 34 c.Assert(stateAPI, gc.NotNil) 35 s.keyupdater = stateAPI.KeyUpdater() 36 c.Assert(s.keyupdater, gc.NotNil) 37 } 38 39 func (s *keyupdaterSuite) TestAuthorisedKeysNoSuchMachine(c *gc.C) { 40 _, err := s.keyupdater.AuthorisedKeys(names.NewMachineTag("42")) 41 c.Assert(err, gc.ErrorMatches, "permission denied") 42 } 43 44 func (s *keyupdaterSuite) TestAuthorisedKeysForbiddenMachine(c *gc.C) { 45 m, err := s.State.AddMachine("quantal", state.JobHostUnits) 46 c.Assert(err, jc.ErrorIsNil) 47 _, err = s.keyupdater.AuthorisedKeys(m.Tag().(names.MachineTag)) 48 c.Assert(err, gc.ErrorMatches, "permission denied") 49 } 50 51 func (s *keyupdaterSuite) TestAuthorisedKeys(c *gc.C) { 52 s.setAuthorisedKeys(c, "key1\nkey2") 53 keys, err := s.keyupdater.AuthorisedKeys(s.rawMachine.Tag().(names.MachineTag)) 54 c.Assert(err, jc.ErrorIsNil) 55 c.Assert(keys, gc.DeepEquals, []string{"key1", "key2"}) 56 } 57 58 func (s *keyupdaterSuite) setAuthorisedKeys(c *gc.C, keys string) { 59 err := s.BackingState.UpdateEnvironConfig(map[string]interface{}{"authorized-keys": keys}, nil, nil) 60 c.Assert(err, jc.ErrorIsNil) 61 } 62 63 func (s *keyupdaterSuite) TestWatchAuthorisedKeys(c *gc.C) { 64 watcher, err := s.keyupdater.WatchAuthorisedKeys(s.rawMachine.Tag().(names.MachineTag)) 65 c.Assert(err, jc.ErrorIsNil) 66 defer testing.AssertStop(c, watcher) 67 wc := testing.NewNotifyWatcherC(c, s.BackingState, watcher) 68 // Initial event 69 wc.AssertOneChange() 70 71 s.setAuthorisedKeys(c, "key1\nkey2") 72 // One change noticing the new version 73 wc.AssertOneChange() 74 // Setting the version to the same value doesn't trigger a change 75 s.setAuthorisedKeys(c, "key1\nkey2") 76 wc.AssertNoChange() 77 78 s.setAuthorisedKeys(c, "key1\nkey2\nkey3") 79 wc.AssertOneChange() 80 testing.AssertStop(c, watcher) 81 wc.AssertClosed() 82 }