github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/sshhostkeys_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state_test 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 "github.com/juju/names/v5" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/state" 15 "github.com/juju/juju/testing/factory" 16 ) 17 18 type SSHHostKeysSuite struct { 19 ConnSuite 20 machineTag names.MachineTag 21 } 22 23 var _ = gc.Suite(new(SSHHostKeysSuite)) 24 25 func (s *SSHHostKeysSuite) SetUpTest(c *gc.C) { 26 s.ConnSuite.SetUpTest(c) 27 s.machineTag = s.Factory.MakeMachine(c, nil).MachineTag() 28 } 29 30 func (s *SSHHostKeysSuite) TestGetWithNoKeys(c *gc.C) { 31 checkKeysNotFound(c, s.State, s.machineTag) 32 } 33 34 func (s *SSHHostKeysSuite) TestSetGet(c *gc.C) { 35 for i := 0; i < 3; i++ { 36 keys := state.SSHHostKeys{fmt.Sprintf("rsa foo %d", i), "dsa bar"} 37 err := s.State.SetSSHHostKeys(s.machineTag, keys) 38 c.Assert(err, jc.ErrorIsNil) 39 checkGet(c, s.State, s.machineTag, keys) 40 } 41 } 42 43 func (s *SSHHostKeysSuite) TestModelIsolation(c *gc.C) { 44 stA := s.State 45 tagA := s.machineTag 46 keysA := state.SSHHostKeys{"rsaA", "dsaA"} 47 c.Assert(stA.SetSSHHostKeys(tagA, keysA), jc.ErrorIsNil) 48 49 stB := s.Factory.MakeModel(c, nil) 50 defer stB.Close() 51 factoryB := factory.NewFactory(stB, s.StatePool) 52 tagB := factoryB.MakeMachine(c, nil).MachineTag() 53 keysB := state.SSHHostKeys{"rsaB", "dsaB"} 54 c.Assert(stB.SetSSHHostKeys(tagB, keysB), jc.ErrorIsNil) 55 56 checkGet(c, stA, tagA, keysA) 57 checkGet(c, stB, tagB, keysB) 58 } 59 60 func checkKeysNotFound(c *gc.C, st *state.State, tag names.MachineTag) { 61 _, err := st.GetSSHHostKeys(tag) 62 c.Check(errors.IsNotFound(err), jc.IsTrue) 63 c.Check(err, gc.ErrorMatches, "keys not found") 64 } 65 66 func checkGet(c *gc.C, st *state.State, tag names.MachineTag, expected state.SSHHostKeys) { 67 keysGot, err := st.GetSSHHostKeys(tag) 68 c.Assert(err, jc.ErrorIsNil) 69 c.Check(keysGot, gc.DeepEquals, expected) 70 }