github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/subnet_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 SubnetSerializationSuite struct { 13 SliceSerializationSuite 14 } 15 16 var _ = gc.Suite(&SubnetSerializationSuite{}) 17 18 func (s *SubnetSerializationSuite) SetUpTest(c *gc.C) { 19 s.SliceSerializationSuite.SetUpTest(c) 20 s.importName = "subnets" 21 s.sliceName = "subnets" 22 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 23 return importSubnets(m) 24 } 25 s.testFields = func(m map[string]interface{}) { 26 m["subnets"] = []interface{}{} 27 } 28 } 29 30 func (s *SubnetSerializationSuite) TestNewSubnet(c *gc.C) { 31 args := SubnetArgs{ 32 CIDR: "10.0.0.0/24", 33 ProviderId: "magic", 34 VLANTag: 64, 35 SpaceName: "foo", 36 AvailabilityZone: "bar", 37 AllocatableIPHigh: "10.0.0.255", 38 AllocatableIPLow: "10.0.0.0", 39 } 40 subnet := newSubnet(args) 41 c.Assert(subnet.CIDR(), gc.Equals, args.CIDR) 42 c.Assert(subnet.ProviderId(), gc.Equals, args.ProviderId) 43 c.Assert(subnet.VLANTag(), gc.Equals, args.VLANTag) 44 c.Assert(subnet.SpaceName(), gc.Equals, args.SpaceName) 45 c.Assert(subnet.AvailabilityZone(), gc.Equals, args.AvailabilityZone) 46 c.Assert(subnet.AllocatableIPHigh(), gc.Equals, args.AllocatableIPHigh) 47 c.Assert(subnet.AllocatableIPLow(), gc.Equals, args.AllocatableIPLow) 48 } 49 50 func (s *SubnetSerializationSuite) TestParsingSerializedData(c *gc.C) { 51 initial := subnets{ 52 Version: 1, 53 Subnets_: []*subnet{ 54 newSubnet(SubnetArgs{ 55 CIDR: "10.0.0.0/24", 56 ProviderId: "magic", 57 VLANTag: 64, 58 SpaceName: "foo", 59 AvailabilityZone: "bar", 60 AllocatableIPHigh: "10.0.0.255", 61 AllocatableIPLow: "10.0.0.0", 62 }), 63 newSubnet(SubnetArgs{CIDR: "10.0.1.0/24"}), 64 }, 65 } 66 67 bytes, err := yaml.Marshal(initial) 68 c.Assert(err, jc.ErrorIsNil) 69 70 var source map[string]interface{} 71 err = yaml.Unmarshal(bytes, &source) 72 c.Assert(err, jc.ErrorIsNil) 73 74 subnets, err := importSubnets(source) 75 c.Assert(err, jc.ErrorIsNil) 76 77 c.Assert(subnets, jc.DeepEquals, initial.Subnets_) 78 }