1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
. "launchpad.net/gocheck"
11
"launchpad.net/juju-core/errors"
12
"launchpad.net/juju-core/state"
13
"launchpad.net/juju-core/state/api/params"
14
"launchpad.net/juju-core/state/apiserver/common"
17
type lifeSuite struct{}
19
var _ = Suite(&lifeSuite{})
21
func (*lifeSuite) TestLife(c *C) {
23
entities: map[string]*fakeLifer{
24
"x0": &fakeLifer{life: state.Alive},
25
"x1": &fakeLifer{life: state.Dying},
26
"x2": &fakeLifer{life: state.Dead},
27
"x3": &fakeLifer{err: fmt.Errorf("x3 error")},
30
getCanRead := func() (common.AuthFunc, error) {
31
return func(tag string) bool {
33
case "x0", "x2", "x3":
39
lg := common.NewLifeGetter(st, getCanRead)
40
entities := params.Entities{[]params.Entity{
41
{"x0"}, {"x1"}, {"x2"}, {"x3"}, {"x4"},
43
results, err := lg.Life(entities)
45
unauth := ¶ms.Error{
46
Message: "permission denied",
47
Code: params.CodeUnauthorized,
49
c.Assert(results, DeepEquals, params.LifeResults{
50
Results: []params.LifeResult{
54
{Error: ¶ms.Error{
62
func (*lifeSuite) TestLifeError(c *C) {
63
getCanRead := func() (common.AuthFunc, error) {
64
return nil, fmt.Errorf("pow")
66
lg := common.NewLifeGetter(&fakeLifeState{}, getCanRead)
67
_, err := lg.Life(params.Entities{[]params.Entity{{"x0"}}})
68
c.Assert(err, ErrorMatches, "pow")
71
func (*lifeSuite) TestLifeNoArgsNoError(c *C) {
72
getCanRead := func() (common.AuthFunc, error) {
73
return nil, fmt.Errorf("pow")
75
lg := common.NewLifeGetter(&fakeLifeState{}, getCanRead)
76
result, err := lg.Life(params.Entities{})
78
c.Assert(result.Results, HasLen, 0)
81
type fakeLifeState struct {
82
entities map[string]*fakeLifer
85
func (st *fakeLifeState) Lifer(tag string) (state.Lifer, error) {
86
if lifer, ok := st.entities[tag]; ok {
92
return nil, errors.NotFoundf("entity %q", tag)
95
type fakeLifer struct {
100
func (l *fakeLifer) Tag() string {
104
func (l *fakeLifer) Life() state.Life {