github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	"github.com/juju/errors"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  	"gopkg.in/juju/names.v2"
    11  
    12  	"github.com/juju/juju/state"
    13  	"github.com/juju/juju/testing/factory"
    14  )
    15  
    16  type SSHHostKeysSuite struct {
    17  	ConnSuite
    18  	machineTag names.MachineTag
    19  }
    20  
    21  var _ = gc.Suite(new(SSHHostKeysSuite))
    22  
    23  func (s *SSHHostKeysSuite) SetUpTest(c *gc.C) {
    24  	s.ConnSuite.SetUpTest(c)
    25  	s.machineTag = s.Factory.MakeMachine(c, nil).MachineTag()
    26  }
    27  
    28  func (s *SSHHostKeysSuite) TestGetWithNoKeys(c *gc.C) {
    29  	checkKeysNotFound(c, s.State, s.machineTag)
    30  }
    31  
    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)
    38  	}
    39  }
    40  
    41  func (s *SSHHostKeysSuite) TestModelIsolation(c *gc.C) {
    42  	stA := s.State
    43  	tagA := s.machineTag
    44  	keysA := state.SSHHostKeys{"rsaA", "dsaA"}
    45  	c.Assert(stA.SetSSHHostKeys(tagA, keysA), jc.ErrorIsNil)
    46  
    47  	stB := s.Factory.MakeModel(c, nil)
    48  	defer stB.Close()
    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)
    53  
    54  	checkGet(c, stA, tagA, keysA)
    55  	checkGet(c, stB, tagB, keysB)
    56  }
    57  
    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")
    62  }
    63  
    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)
    68  }