Skip to content

Commit

Permalink
Finalized PR after review.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Mueller committed Feb 20, 2015
1 parent fa7d39e commit bbdc742
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 53 deletions.
15 changes: 15 additions & 0 deletions api/networker/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package networker

import (
"github.com/juju/juju/api/base/testing"
)

// PatchFacadeCall patches the State's facade such that
// FacadeCall method calls are diverted to the provided
// function.
func PatchFacadeCall(p testing.Patcher, st *State, f func(request string, params, response interface{}) error) {
testing.PatchFacadeCall(p, &st.facade, f)
}
21 changes: 9 additions & 12 deletions api/networker/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,23 @@ func NewState(caller base.APICaller) *State {
return &State{base.NewFacadeCaller(caller, networkerFacade)}
}

// MachineNetworkInfo returns information about network interfaces to
// setup only for a single machine.
// DEPRECATED: Use MachineNetworkConfig() instead.
func (st *State) MachineNetworkInfo(tag names.MachineTag) ([]network.InterfaceInfo, error) {
return st.MachineNetworkConfig(tag)
}

// MachineNetworkConfig returns information about network interfaces to
// setup only for a single machine.
func (st *State) MachineNetworkConfig(tag names.MachineTag) ([]network.InterfaceInfo, error) {
args := params.Entities{
Entities: []params.Entity{{Tag: tag.String()}},
}
var results params.MachineNetworkConfigResults
// TODO(mue): New name is now MachineNetworkConfig, this variant
// is kept due to compatibility reasons.
err := st.facade.FacadeCall("MachineNetworkInfo", args, &results)
err := st.facade.FacadeCall("MachineNetworkConfig", args, &results)
if err != nil {
// TODO: Not directly tested
return nil, err
if params.IsCodeNotImplemented(err) {
// Fallback to former name.
err = st.facade.FacadeCall("MachineNetworkInfo", args, &results)
}
if err != nil {
// TODO: Not directly tested.
return nil, err
}
}
if len(results.Results) != 1 {
// TODO: Not directly tested
Expand Down
44 changes: 25 additions & 19 deletions api/networker/networker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/juju/juju/api"
"github.com/juju/juju/api/networker"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/instance"
"github.com/juju/juju/juju/testing"
Expand Down Expand Up @@ -169,13 +170,32 @@ func (s *networkerSuite) SetUpTest(c *gc.C) {
}

func (s *networkerSuite) TestMachineNetworkConfigPermissionDenied(c *gc.C) {
// Test for old API name.
info, err := s.networker.MachineNetworkInfo(names.NewMachineTag("1"))
info, err := s.networker.MachineNetworkConfig(names.NewMachineTag("1"))
c.Assert(err, gc.ErrorMatches, "permission denied")
c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
c.Assert(info, gc.IsNil)
// Test for new API name.
info, err = s.networker.MachineNetworkConfig(names.NewMachineTag("1"))
}

func (s *networkerSuite) TestMachineNetworkConfigNameChange(c *gc.C) {
var called bool
networker.PatchFacadeCall(s, s.networker, func(request string, args, response interface{}) error {
if !called {
called = true
c.Assert(request, gc.Equals, "MachineNetworkConfig")
return &params.Error{"MachineNetworkConfig", params.CodeNotImplemented}
}
c.Assert(request, gc.Equals, "MachineNetworkInfo")
expected := params.Entities{
Entities: []params.Entity{{Tag: names.NewMachineTag("42").String()}},
}
c.Assert(args, gc.DeepEquals, expected)
result := response.(*params.MachineNetworkConfigResults)
result.Results = make([]params.MachineNetworkConfigResult, 1)
result.Results[0].Error = common.ServerError(common.ErrPerm)
return nil
})
// Make a call, in this case result is "permission denied".
info, err := s.networker.MachineNetworkConfig(names.NewMachineTag("42"))
c.Assert(err, gc.ErrorMatches, "permission denied")
c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
c.Assert(info, gc.IsNil)
Expand Down Expand Up @@ -256,21 +276,7 @@ func (s *networkerSuite) TestMachineNetworkConfig(c *gc.C) {
InterfaceName: "eth0",
}}

// Test for old API name.
results, err := s.networker.MachineNetworkInfo(names.NewMachineTag("0"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, expectedMachineInfo)

results, err = s.networker.MachineNetworkInfo(names.NewMachineTag("0/lxc/0"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, expectedContainerInfo)

results, err = s.networker.MachineNetworkInfo(names.NewMachineTag("0/lxc/0/lxc/0"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, expectedNestedContainerInfo)

// Test for new API name.
results, err = s.networker.MachineNetworkConfig(names.NewMachineTag("0"))
results, err := s.networker.MachineNetworkConfig(names.NewMachineTag("0"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, expectedMachineInfo)

Expand Down
35 changes: 13 additions & 22 deletions apiserver/networker/networker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,34 +284,25 @@ func (s *networkerSuite) TestMachineNetworkConfig(c *gc.C) {
VLANTag: 0,
InterfaceName: "eth0",
}}

args := params.Entities{Entities: []params.Entity{
{Tag: "machine-0"},
{Tag: "machine-0-lxc-0"},
{Tag: "machine-0-lxc-0-lxc-0"},
}}

// Test for old API name.
results, err := s.networker.MachineNetworkInfo(args)
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, params.MachineNetworkConfigResults{
Results: []params.MachineNetworkConfigResult{
{Error: nil, Config: expectedMachineConfig},
{Error: nil, Config: expectedContainerConfig},
{Error: nil, Config: expectedNestedContainerConfig},
},
})

// Test for new API name.
results, err = s.networker.MachineNetworkConfig(args)
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, params.MachineNetworkConfigResults{
Results: []params.MachineNetworkConfigResult{
{Error: nil, Config: expectedMachineConfig},
{Error: nil, Config: expectedContainerConfig},
{Error: nil, Config: expectedNestedContainerConfig},
},
})
assert := func(f func(params.Entities) (params.MachineNetworkConfigResults, error)) {
results, err := f(args)
c.Assert(err, jc.ErrorIsNil)
c.Assert(results, gc.DeepEquals, params.MachineNetworkConfigResults{
Results: []params.MachineNetworkConfigResult{
{Error: nil, Config: expectedMachineConfig},
{Error: nil, Config: expectedContainerConfig},
{Error: nil, Config: expectedNestedContainerConfig},
},
})
}
assert(s.networker.MachineNetworkInfo)
assert(s.networker.MachineNetworkConfig)
}

func (s *networkerSuite) TestWatchInterfacesPermissions(c *gc.C) {
Expand Down
1 change: 1 addition & 0 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
// InterfaceInfo describes a single network interface available on an
// instance. For providers that support networks, this will be
// available at StartInstance() time.
// TODO(mue): Rename to InterfaceConfig due to consistency later.
type InterfaceInfo struct {
// DeviceIndex specifies the order in which the network interface
// appears on the host. The primary interface has an index of 0.
Expand Down

0 comments on commit bbdc742

Please sign in to comment.