Skip to content

Commit

Permalink
Merge pull request #1560 from dimitern/lp-1418433-restore-megawatcher…
Browse files Browse the repository at this point in the history
…-unit-ports

Fixed lp:1418433 - populate unit ports and port ranges in megawatcher

Foreport of #1558 to 1.23, no other changes except a slight improvement
inside assertEntitiesEqual.

(Review request: http://reviews.vapour.ws/r/892/)
  • Loading branch information
jujubot committed Feb 10, 2015
2 parents 3769dfd + fd8c5e3 commit 8b0a9a9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
18 changes: 11 additions & 7 deletions apiserver/params/params_test.go
Expand Up @@ -78,19 +78,23 @@ var marshalTestCases = []struct {
Service: "Shazam",
Series: "precise",
CharmURL: "cs:~user/precise/wordpress-42",
Ports: []network.Port{
{
Protocol: "http",
Number: 80},
},
Ports: []network.Port{{
Protocol: "http",
Number: 80,
}},
PortRanges: []network.PortRange{{
FromPort: 80,
ToPort: 80,
Protocol: "http",
}},
PublicAddress: "testing.invalid",
PrivateAddress: "10.0.0.1",
MachineId: "1",
Status: "error",
StatusInfo: "foo",
},
},
json: `["unit", "change", {"CharmURL": "cs:~user/precise/wordpress-42", "MachineId": "1", "Series": "precise", "Name": "Benji", "PublicAddress": "testing.invalid", "Service": "Shazam", "PrivateAddress": "10.0.0.1", "Ports": [{"Protocol": "http", "Number": 80}], "Status": "error", "StatusInfo": "foo", "StatusData": null, "Subordinate": false}]`,
json: `["unit", "change", {"CharmURL": "cs:~user/precise/wordpress-42", "MachineId": "1", "Series": "precise", "Name": "Benji", "PublicAddress": "testing.invalid", "Service": "Shazam", "PrivateAddress": "10.0.0.1", "Ports": [{"Protocol": "http", "Number": 80}], "PortRanges": [{"FromPort": 80, "ToPort": 80, "Protocol": "http"}], "Status": "error", "StatusInfo": "foo", "StatusData": null, "Subordinate": false}]`,
}, {
about: "RelationInfo Delta",
value: multiwatcher.Delta{
Expand Down Expand Up @@ -140,7 +144,7 @@ func (s *MarshalSuite) TestDeltaMarshalJSON(c *gc.C) {
var expected interface{}
err = json.Unmarshal([]byte(t.json), &expected)
c.Check(err, jc.ErrorIsNil)
c.Check(unmarshalledOutput, gc.DeepEquals, expected)
c.Check(unmarshalledOutput, jc.DeepEquals, expected)
}
}

Expand Down
37 changes: 36 additions & 1 deletion state/megawatcher.go
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/juju/errors"
"gopkg.in/mgo.v2"

"github.com/juju/juju/network"
"github.com/juju/juju/state/multiwatcher"
"github.com/juju/juju/state/watcher"
)
Expand Down Expand Up @@ -99,13 +100,47 @@ func translateLegacyUnitAgentStatus(in multiwatcher.Status) multiwatcher.Status
return in
}

func getUnitPortRangesAndPorts(st *State, unitName string) ([]network.PortRange, []network.Port, error) {
// Get opened port ranges for the unit and convert them to ports,
// as older clients/servers do not know about ranges). See bug
// http://pad.lv/1418344 for more info.
unit, err := st.Unit(unitName)
if err != nil {
return nil, nil, errors.Annotatef(err, "failed to get unit %q", unitName)
}
portRanges, err := unit.OpenedPorts()
// Since the port ranges are associated with the unit's machine,
// we need to check for NotAssignedError.
if errors.IsNotAssigned(errors.Cause(err)) {
// Not assigned, so there won't be any ports opened.
return nil, nil, nil
} else if err != nil {
return nil, nil, errors.Annotate(err, "failed to get unit port ranges")
}
var compatiblePorts []network.Port
for _, portRange := range portRanges {
for j := portRange.FromPort; j <= portRange.ToPort; j++ {
compatiblePorts = append(compatiblePorts, network.Port{
Number: j,
Protocol: portRange.Protocol,
})
}
}
return portRanges, compatiblePorts, nil
}

func (u *backingUnit) updated(st *State, store *multiwatcherStore, id interface{}) error {
portRanges, compatiblePorts, err := getUnitPortRangesAndPorts(st, u.Name)
if err != nil {
return errors.Trace(err)
}
info := &multiwatcher.UnitInfo{
Name: u.Name,
Service: u.Service,
Series: u.Series,
MachineId: u.MachineId,
Ports: u.Ports,
Ports: compatiblePorts,
PortRanges: portRanges,
Subordinate: u.Principal != "",
}
if u.CharmURL != nil {
Expand Down
51 changes: 37 additions & 14 deletions state/megawatcher_internal_test.go
Expand Up @@ -5,7 +5,6 @@ package state

import (
"fmt"
"reflect"
"sort"
"time"

Expand Down Expand Up @@ -71,14 +70,22 @@ func (s *storeManagerStateSuite) Reset(c *gc.C) {
s.SetUpTest(c)
}

func jcDeepEqualsCheck(c *gc.C, got, want interface{}) bool {
ok, message := jc.DeepEquals.Check([]interface{}{got, want}, []string{"got", "want"})
if !ok {
c.Logf(message)
}
return ok
}

func assertEntitiesEqual(c *gc.C, got, want []multiwatcher.EntityInfo) {
if len(got) == 0 {
got = nil
}
if len(want) == 0 {
want = nil
}
if reflect.DeepEqual(got, want) {
if jcDeepEqualsCheck(c, got, want) {
return
}
c.Errorf("entity mismatch; got len %d; want %d", len(got), len(want))
Expand All @@ -95,7 +102,7 @@ func assertEntitiesEqual(c *gc.C, got, want []multiwatcher.EntityInfo) {
for i := 0; i < len(got); i++ {
g := got[i]
w := want[i]
if !reflect.DeepEqual(g, w) {
if !jcDeepEqualsCheck(c, g, w) {
c.Logf("")
c.Logf("first difference at position %d", i)
c.Logf("got:")
Expand Down Expand Up @@ -232,7 +239,6 @@ func (s *storeManagerStateSuite) setUpScenario(c *gc.C, st *State, units int) (e
Service: wordpress.Name(),
Series: m.Series(),
MachineId: m.Id(),
Ports: []network.Port{},
Status: multiwatcher.Status("allocating"),
Subordinate: false,
})
Expand Down Expand Up @@ -288,7 +294,6 @@ func (s *storeManagerStateSuite) setUpScenario(c *gc.C, st *State, units int) (e
Name: fmt.Sprintf("logging/%d", i),
Service: "logging",
Series: "quantal",
Ports: []network.Port{},
Status: multiwatcher.Status("allocating"),
Subordinate: true,
})
Expand Down Expand Up @@ -419,8 +424,13 @@ func (s *storeManagerStateSuite) TestChanged(c *gc.C) {
c.Assert(err, jc.ErrorIsNil)
err = u.AssignToMachine(m)
c.Assert(err, jc.ErrorIsNil)
// Open two ports and one range.
err = u.OpenPort("tcp", 12345)
c.Assert(err, jc.ErrorIsNil)
err = u.OpenPort("udp", 54321)
c.Assert(err, jc.ErrorIsNil)
err = u.OpenPorts("tcp", 5555, 5558)
c.Assert(err, jc.ErrorIsNil)
err = u.SetStatus(StatusError, "failure", nil)
c.Assert(err, jc.ErrorIsNil)

Expand All @@ -432,11 +442,23 @@ func (s *storeManagerStateSuite) TestChanged(c *gc.C) {
},
expectContents: []multiwatcher.EntityInfo{
&multiwatcher.UnitInfo{
Name: "wordpress/0",
Service: "wordpress",
Series: "quantal",
MachineId: "0",
Ports: []network.Port{},
Name: "wordpress/0",
Service: "wordpress",
Series: "quantal",
MachineId: "0",
Ports: []network.Port{
{"tcp", 5555},
{"tcp", 5556},
{"tcp", 5557},
{"tcp", 5558},
{"tcp", 12345},
{"udp", 54321},
},
PortRanges: []network.PortRange{
{5555, 5558, "tcp"},
{12345, 12345, "tcp"},
{54321, 54321, "udp"},
},
Status: multiwatcher.Status("error"),
StatusInfo: "failure",
}}}
Expand Down Expand Up @@ -468,7 +490,8 @@ func (s *storeManagerStateSuite) TestChanged(c *gc.C) {
Service: "wordpress",
Series: "quantal",
MachineId: "0",
Ports: []network.Port{},
Ports: []network.Port{{"udp", 17070}},
PortRanges: []network.PortRange{{17070, 17070, "udp"}},
Status: multiwatcher.Status("error"),
StatusInfo: "another failure",
}}}
Expand Down Expand Up @@ -503,9 +526,9 @@ func (s *storeManagerStateSuite) TestChanged(c *gc.C) {
PublicAddress: "public",
PrivateAddress: "private",
MachineId: "0",
Ports: []network.Port{},
Status: multiwatcher.Status("error"),
StatusInfo: "failure",
Ports: []network.Port{{"tcp", 12345}},
PortRanges: []network.PortRange{{12345, 12345, "tcp"}}, Status: multiwatcher.Status("error"),
StatusInfo: "failure",
}}}
},
// Service changes
Expand Down
1 change: 1 addition & 0 deletions state/multiwatcher/multiwatcher.go
Expand Up @@ -170,6 +170,7 @@ type UnitInfo struct {
PrivateAddress string
MachineId string
Ports []network.Port
PortRanges []network.PortRange
Status Status
StatusInfo string
StatusData map[string]interface{}
Expand Down

0 comments on commit 8b0a9a9

Please sign in to comment.