1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
jc "github.com/juju/testing/checkers"
10
"gopkg.in/juju/names.v2"
12
"github.com/juju/juju/state"
13
"github.com/juju/juju/testing/factory"
16
type SSHHostKeysSuite struct {
18
machineTag names.MachineTag
21
var _ = gc.Suite(new(SSHHostKeysSuite))
23
func (s *SSHHostKeysSuite) SetUpTest(c *gc.C) {
24
s.ConnSuite.SetUpTest(c)
25
s.machineTag = s.Factory.MakeMachine(c, nil).MachineTag()
28
func (s *SSHHostKeysSuite) TestGetWithNoKeys(c *gc.C) {
29
checkKeysNotFound(c, s.State, s.machineTag)
32
func (s *SSHHostKeysSuite) TestSetGet(c *gc.C) {
33
for i := 0; i < 3; i++ {
34
keys := state.SSHHostKeys{"rsa foo", "dsa bar"}
35
err := s.State.SetSSHHostKeys(s.machineTag, keys)
36
c.Assert(err, jc.ErrorIsNil)
37
checkGet(c, s.State, s.machineTag, keys)
41
func (s *SSHHostKeysSuite) TestModelIsolation(c *gc.C) {
44
keysA := state.SSHHostKeys{"rsaA", "dsaA"}
45
c.Assert(stA.SetSSHHostKeys(tagA, keysA), jc.ErrorIsNil)
47
stB := s.Factory.MakeModel(c, nil)
49
factoryB := factory.NewFactory(stB)
50
tagB := factoryB.MakeMachine(c, nil).MachineTag()
51
keysB := state.SSHHostKeys{"rsaB", "dsaB"}
52
c.Assert(stB.SetSSHHostKeys(tagB, keysB), jc.ErrorIsNil)
54
checkGet(c, stA, tagA, keysA)
55
checkGet(c, stB, tagB, keysB)
58
func checkKeysNotFound(c *gc.C, st *state.State, tag names.MachineTag) {
59
_, err := st.GetSSHHostKeys(tag)
60
c.Check(errors.IsNotFound(err), jc.IsTrue)
61
c.Check(err, gc.ErrorMatches, "keys not found")
64
func checkGet(c *gc.C, st *state.State, tag names.MachineTag, expected state.SSHHostKeys) {
65
keysGot, err := st.GetSSHHostKeys(tag)
66
c.Assert(err, jc.ErrorIsNil)
67
c.Check(keysGot, gc.DeepEquals, expected)