github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/sshhostkey_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  type SSHHostKeySerializationSuite struct {
    13  	SliceSerializationSuite
    14  }
    15  
    16  var _ = gc.Suite(&SSHHostKeySerializationSuite{})
    17  
    18  func (s *SSHHostKeySerializationSuite) SetUpTest(c *gc.C) {
    19  	s.SliceSerializationSuite.SetUpTest(c)
    20  	s.importName = "ssh-host-keys"
    21  	s.sliceName = "ssh-host-keys"
    22  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    23  		return importSSHHostKeys(m)
    24  	}
    25  	s.testFields = func(m map[string]interface{}) {
    26  		m["ssh-host-keys"] = []interface{}{}
    27  	}
    28  }
    29  
    30  func (s *SSHHostKeySerializationSuite) TestNewSSHHostKey(c *gc.C) {
    31  	args := SSHHostKeyArgs{
    32  		MachineID: "foo",
    33  		Keys:      []string{"one", "two", "three"},
    34  	}
    35  	key := newSSHHostKey(args)
    36  	c.Assert(key.MachineID(), gc.Equals, args.MachineID)
    37  	c.Assert(key.Keys(), jc.DeepEquals, args.Keys)
    38  }
    39  
    40  func (s *SSHHostKeySerializationSuite) TestParsingSerializedData(c *gc.C) {
    41  	initial := sshHostKeys{
    42  		Version: 1,
    43  		SSHHostKeys_: []*sshHostKey{
    44  			newSSHHostKey(SSHHostKeyArgs{
    45  				MachineID: "foo",
    46  				Keys:      []string{"one", "two", "three"},
    47  			}),
    48  			newSSHHostKey(SSHHostKeyArgs{MachineID: "bar"}),
    49  		},
    50  	}
    51  
    52  	bytes, err := yaml.Marshal(initial)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  
    55  	var source map[string]interface{}
    56  	err = yaml.Unmarshal(bytes, &source)
    57  	c.Assert(err, jc.ErrorIsNil)
    58  
    59  	keys, err := importSSHHostKeys(source)
    60  	c.Assert(err, jc.ErrorIsNil)
    61  
    62  	c.Assert(keys, jc.DeepEquals, initial.SSHHostKeys_)
    63  }