github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/ipaddress_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 IPAddressSerializationSuite struct {
    13  	SliceSerializationSuite
    14  }
    15  
    16  var _ = gc.Suite(&IPAddressSerializationSuite{})
    17  
    18  func (s *IPAddressSerializationSuite) SetUpTest(c *gc.C) {
    19  	s.SliceSerializationSuite.SetUpTest(c)
    20  	s.importName = "ip-addresses"
    21  	s.sliceName = "ip-addresses"
    22  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    23  		return importIPAddresses(m)
    24  	}
    25  	s.testFields = func(m map[string]interface{}) {
    26  		m["ip-addresses"] = []interface{}{}
    27  	}
    28  }
    29  
    30  func (s *IPAddressSerializationSuite) TestNewIPAddress(c *gc.C) {
    31  	args := IPAddressArgs{
    32  		SubnetCIDR:       "10.0.0.0/24",
    33  		ProviderID:       "magic",
    34  		DeviceName:       "foo",
    35  		MachineID:        "bar",
    36  		ConfigMethod:     "static",
    37  		Value:            "10.0.0.4",
    38  		DNSServers:       []string{"10.1.0.1", "10.2.0.1"},
    39  		DNSSearchDomains: []string{"bam", "mam"},
    40  		GatewayAddress:   "10.0.0.1",
    41  	}
    42  	address := newIPAddress(args)
    43  	c.Assert(address.SubnetCIDR(), gc.Equals, args.SubnetCIDR)
    44  	c.Assert(address.ProviderID(), gc.Equals, args.ProviderID)
    45  	c.Assert(address.DeviceName(), gc.Equals, args.DeviceName)
    46  	c.Assert(address.MachineID(), gc.Equals, args.MachineID)
    47  	c.Assert(address.ConfigMethod(), gc.Equals, args.ConfigMethod)
    48  	c.Assert(address.Value(), gc.Equals, args.Value)
    49  	c.Assert(address.DNSServers(), jc.DeepEquals, args.DNSServers)
    50  	c.Assert(address.DNSSearchDomains(), jc.DeepEquals, args.DNSSearchDomains)
    51  	c.Assert(address.GatewayAddress(), gc.Equals, args.GatewayAddress)
    52  }
    53  
    54  func (s *IPAddressSerializationSuite) TestParsingSerializedData(c *gc.C) {
    55  	initial := ipaddresses{
    56  		Version: 1,
    57  		IPAddresses_: []*ipaddress{
    58  			newIPAddress(IPAddressArgs{
    59  				SubnetCIDR:       "10.0.0.0/24",
    60  				ProviderID:       "magic",
    61  				DeviceName:       "foo",
    62  				MachineID:        "bar",
    63  				ConfigMethod:     "static",
    64  				Value:            "10.0.0.4",
    65  				DNSServers:       []string{"10.1.0.1", "10.2.0.1"},
    66  				DNSSearchDomains: []string{"bam", "mam"},
    67  				GatewayAddress:   "10.0.0.1",
    68  			}),
    69  			newIPAddress(IPAddressArgs{Value: "10.0.0.5"}),
    70  		},
    71  	}
    72  
    73  	bytes, err := yaml.Marshal(initial)
    74  	c.Assert(err, jc.ErrorIsNil)
    75  
    76  	var source map[string]interface{}
    77  	err = yaml.Unmarshal(bytes, &source)
    78  	c.Assert(err, jc.ErrorIsNil)
    79  
    80  	addresses, err := importIPAddresses(source)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  
    83  	c.Assert(addresses, jc.DeepEquals, initial.IPAddresses_)
    84  }