-
Notifications
You must be signed in to change notification settings - Fork 23
/
port_ranges_test.go
72 lines (60 loc) · 1.99 KB
/
port_ranges_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)
type MachinePortRangeSerializationSuite struct {
}
func assertUnitPortRangeMatches(c *gc.C, prA, prB UnitPortRange) {
c.Assert(prA.FromPort(), gc.Equals, prB.FromPort())
c.Assert(prA.ToPort(), gc.Equals, prB.ToPort())
c.Assert(prA.Protocol(), gc.Equals, prB.Protocol())
}
var _ = gc.Suite(&MachinePortRangeSerializationSuite{})
func (*MachinePortRangeSerializationSuite) TestParsingSerializedData(c *gc.C) {
initial := &deployedPortRanges{
Version: 1,
ByUnit_: map[string]*unitPortRanges{
"lorem/0": {
ByEndpoint_: map[string][]*unitPortRange{
"dmz": {
newUnitPortRange(1234, 2345, "tcp"),
newUnitPortRange(1337, 1337, "udp"),
},
},
},
"ipsum/0": {
ByEndpoint_: map[string][]*unitPortRange{
"": {
newUnitPortRange(8080, 8080, "tcp"),
},
},
},
},
}
bytes, err := yaml.Marshal(initial)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
imported, err := importMachinePortRanges(source)
c.Assert(err, jc.ErrorIsNil)
byUnit := imported.ByUnit()
c.Assert(byUnit, gc.HasLen, 2)
// Check lorem/0 ports
loremPortsByEndpoint := byUnit["lorem/0"].ByEndpoint()
c.Assert(loremPortsByEndpoint, gc.HasLen, 1)
loremDMZPorts := loremPortsByEndpoint["dmz"]
c.Assert(loremDMZPorts, gc.HasLen, 2)
c.Assert(loremDMZPorts[0], gc.DeepEquals, newUnitPortRange(1234, 2345, "tcp"))
c.Assert(loremDMZPorts[1], gc.DeepEquals, newUnitPortRange(1337, 1337, "udp"))
// Check ipsum/0 ports
ipsumPortsByEndpoint := byUnit["ipsum/0"].ByEndpoint()
c.Assert(ipsumPortsByEndpoint, gc.HasLen, 1)
ipsumAllEndpointPorts := ipsumPortsByEndpoint[""]
c.Assert(ipsumAllEndpointPorts, gc.HasLen, 1)
c.Assert(ipsumAllEndpointPorts[0], gc.DeepEquals, newUnitPortRange(8080, 8080, "tcp"))
}