Skip to content

Commit

Permalink
add interfaces for mocks apiserver/../provisioner
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlanigan committed Nov 8, 2018
1 parent 8ad2672 commit 2170f8a
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions network/containerizer/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package containerizer
import (
"github.com/juju/collections/set"
"github.com/juju/errors"
jujucharm "gopkg.in/juju/charm.v6"

"github.com/juju/juju/constraints"
"github.com/juju/juju/instance"
Expand Down Expand Up @@ -127,3 +128,62 @@ type Container interface {
}

var _ Container = (*MachineShim)(nil)

func (m *MachineShim) Units() ([]Unit, error) {
units, err := m.Machine.Units()
if err != nil {
return nil, errors.Trace(err)
}
wrapped := make([]Unit, len(units))
for i, u := range units {
wrapped[i] = &unitShim{u}
}
return wrapped, nil
}

// Unit, Application & Charm are used to facilitate mocks
// for testing in apiserver/.../agent/provisioner. This is a
// by product of bad design.

// unitShim implements Unit.
// It is required to mock the return of Units from MachineShim.
type unitShim struct {
*state.Unit
}

var _ Unit = (*unitShim)(nil)

type Unit interface {
Application() (Application, error)
Name() string
}

func (u *unitShim) Application() (Application, error) {
app, err := u.Unit.Application()
if err != nil {
return nil, errors.Trace(err)
}
return &applicationShim{app}, nil
}

// applicationShim implements Application.
// It is required to mock the return an Application from unitShim.
type applicationShim struct {
*state.Application
}

var _ Application = (*applicationShim)(nil)

type Application interface {
Charm() (Charm, bool, error)
Name() string
}

func (a *applicationShim) Charm() (Charm, bool, error) {
return a.Application.Charm()
}

type Charm interface {
LXDProfile() *jujucharm.LXDProfile
Revision() int
}

0 comments on commit 2170f8a

Please sign in to comment.