github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/ports_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 PortRangeCheck struct{} 13 14 func (*PortRangeCheck) AssertPortRange(c *gc.C, pr PortRange, args PortRangeArgs) { 15 c.Assert(pr.UnitName(), gc.Equals, args.UnitName) 16 c.Assert(pr.FromPort(), gc.Equals, args.FromPort) 17 c.Assert(pr.ToPort(), gc.Equals, args.ToPort) 18 c.Assert(pr.Protocol(), gc.Equals, args.Protocol) 19 } 20 21 type OpenedPortsSerializationSuite struct { 22 SliceSerializationSuite 23 PortRangeCheck 24 } 25 26 var _ = gc.Suite(&OpenedPortsSerializationSuite{}) 27 28 func (s *OpenedPortsSerializationSuite) SetUpTest(c *gc.C) { 29 s.SliceSerializationSuite.SetUpTest(c) 30 s.importName = "opened-ports" 31 s.sliceName = "opened-ports" 32 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 33 return importOpenedPorts(m) 34 } 35 s.testFields = func(m map[string]interface{}) { 36 m["opened-ports"] = []interface{}{} 37 } 38 } 39 40 func (s *OpenedPortsSerializationSuite) TestNewOpenedPorts(c *gc.C) { 41 args := OpenedPortsArgs{ 42 SubnetID: "0.1.2.0/24", 43 OpenedPorts: []PortRangeArgs{ 44 PortRangeArgs{ 45 UnitName: "magic/0", 46 FromPort: 1234, 47 ToPort: 2345, 48 Protocol: "tcp", 49 }, 50 PortRangeArgs{ 51 UnitName: "magic/0", 52 FromPort: 1234, 53 ToPort: 2345, 54 Protocol: "udp", 55 }, 56 }, 57 } 58 59 ports := newOpenedPorts(args) 60 c.Assert(ports.SubnetID(), gc.Equals, args.SubnetID) 61 opened := ports.OpenPorts() 62 c.Assert(opened, gc.HasLen, 2) 63 s.AssertPortRange(c, opened[0], args.OpenedPorts[0]) 64 s.AssertPortRange(c, opened[1], args.OpenedPorts[1]) 65 } 66 67 func (*OpenedPortsSerializationSuite) TestParsingSerializedData(c *gc.C) { 68 initial := &versionedOpenedPorts{ 69 Version: 1, 70 OpenedPorts_: []*openedPorts{ 71 &openedPorts{ 72 SubnetID_: "fc00::/64", 73 OpenedPorts_: &portRanges{ 74 Version: 1, 75 OpenedPorts_: []*portRange{ 76 &portRange{ 77 UnitName_: "magic/0", 78 FromPort_: 1234, 79 ToPort_: 2345, 80 Protocol_: "tcp", 81 }, 82 }, 83 }, 84 }, 85 &openedPorts{ 86 SubnetID_: "192.168.0.0/16", 87 OpenedPorts_: &portRanges{ 88 Version: 1, 89 OpenedPorts_: []*portRange{ 90 &portRange{ 91 UnitName_: "unicorn/0", 92 FromPort_: 80, 93 ToPort_: 80, 94 Protocol_: "tcp", 95 }, 96 }, 97 }, 98 }, 99 }, 100 } 101 102 bytes, err := yaml.Marshal(initial) 103 c.Assert(err, jc.ErrorIsNil) 104 105 var source map[string]interface{} 106 err = yaml.Unmarshal(bytes, &source) 107 c.Assert(err, jc.ErrorIsNil) 108 109 imported, err := importOpenedPorts(source) 110 c.Assert(err, jc.ErrorIsNil) 111 112 c.Assert(imported, jc.DeepEquals, initial.OpenedPorts_) 113 } 114 115 type PortRangeSerializationSuite struct { 116 SliceSerializationSuite 117 PortRangeCheck 118 } 119 120 var _ = gc.Suite(&PortRangeSerializationSuite{}) 121 122 func (s *PortRangeSerializationSuite) SetUpTest(c *gc.C) { 123 s.SliceSerializationSuite.SetUpTest(c) 124 s.importName = "port-range" 125 s.sliceName = "opened-ports" 126 s.importFunc = func(m map[string]interface{}) (interface{}, error) { 127 return importPortRanges(m) 128 } 129 s.testFields = func(m map[string]interface{}) { 130 m["opened-ports"] = []interface{}{} 131 } 132 } 133 134 func (s *PortRangeSerializationSuite) TestNewPortRange(c *gc.C) { 135 args := PortRangeArgs{ 136 UnitName: "magic/0", 137 FromPort: 1234, 138 ToPort: 2345, 139 Protocol: "tcp", 140 } 141 pr := newPortRange(args) 142 s.AssertPortRange(c, pr, args) 143 } 144 145 func (*PortRangeSerializationSuite) TestParsingSerializedData(c *gc.C) { 146 initial := &portRanges{ 147 Version: 1, 148 OpenedPorts_: []*portRange{ 149 &portRange{ 150 UnitName_: "magic/0", 151 FromPort_: 1234, 152 ToPort_: 2345, 153 Protocol_: "tcp", 154 }, 155 &portRange{ 156 UnitName_: "unicorn/1", 157 FromPort_: 8080, 158 ToPort_: 8080, 159 Protocol_: "tcp", 160 }, 161 }, 162 } 163 164 bytes, err := yaml.Marshal(initial) 165 c.Assert(err, jc.ErrorIsNil) 166 167 var source map[string]interface{} 168 err = yaml.Unmarshal(bytes, &source) 169 c.Assert(err, jc.ErrorIsNil) 170 171 imported, err := importPortRanges(source) 172 c.Assert(err, jc.ErrorIsNil) 173 174 c.Assert(imported, jc.DeepEquals, initial.OpenedPorts_) 175 }