~jameinel/juju-core/api-registry-tracks-type

« back to all changes in this revision

Viewing changes to state/apiserver/common/life_test.go

Merged local-sudo-caller into local-provider-bootstrap.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package common_test
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        . "launchpad.net/gocheck"
 
10
 
 
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"
 
15
)
 
16
 
 
17
type lifeSuite struct{}
 
18
 
 
19
var _ = Suite(&lifeSuite{})
 
20
 
 
21
func (*lifeSuite) TestLife(c *C) {
 
22
        st := &fakeLifeState{
 
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")},
 
28
                },
 
29
        }
 
30
        getCanRead := func() (common.AuthFunc, error) {
 
31
                return func(tag string) bool {
 
32
                        switch tag {
 
33
                        case "x0", "x2", "x3":
 
34
                                return true
 
35
                        }
 
36
                        return false
 
37
                }, nil
 
38
        }
 
39
        lg := common.NewLifeGetter(st, getCanRead)
 
40
        entities := params.Entities{[]params.Entity{
 
41
                {"x0"}, {"x1"}, {"x2"}, {"x3"}, {"x4"},
 
42
        }}
 
43
        results, err := lg.Life(entities)
 
44
        c.Assert(err, IsNil)
 
45
        unauth := &params.Error{
 
46
                Message: "permission denied",
 
47
                Code:    params.CodeUnauthorized,
 
48
        }
 
49
        c.Assert(results, DeepEquals, params.LifeResults{
 
50
                Results: []params.LifeResult{
 
51
                        {Life: params.Alive},
 
52
                        {Error: unauth},
 
53
                        {Life: params.Dead},
 
54
                        {Error: &params.Error{
 
55
                                Message: "x3 error",
 
56
                        }},
 
57
                        {Error: unauth},
 
58
                },
 
59
        })
 
60
}
 
61
 
 
62
func (*lifeSuite) TestLifeError(c *C) {
 
63
        getCanRead := func() (common.AuthFunc, error) {
 
64
                return nil, fmt.Errorf("pow")
 
65
        }
 
66
        lg := common.NewLifeGetter(&fakeLifeState{}, getCanRead)
 
67
        _, err := lg.Life(params.Entities{[]params.Entity{{"x0"}}})
 
68
        c.Assert(err, ErrorMatches, "pow")
 
69
}
 
70
 
 
71
func (*lifeSuite) TestLifeNoArgsNoError(c *C) {
 
72
        getCanRead := func() (common.AuthFunc, error) {
 
73
                return nil, fmt.Errorf("pow")
 
74
        }
 
75
        lg := common.NewLifeGetter(&fakeLifeState{}, getCanRead)
 
76
        result, err := lg.Life(params.Entities{})
 
77
        c.Assert(err, IsNil)
 
78
        c.Assert(result.Results, HasLen, 0)
 
79
}
 
80
 
 
81
type fakeLifeState struct {
 
82
        entities map[string]*fakeLifer
 
83
}
 
84
 
 
85
func (st *fakeLifeState) Lifer(tag string) (state.Lifer, error) {
 
86
        if lifer, ok := st.entities[tag]; ok {
 
87
                if lifer.err != nil {
 
88
                        return nil, lifer.err
 
89
                }
 
90
                return lifer, nil
 
91
        }
 
92
        return nil, errors.NotFoundf("entity %q", tag)
 
93
}
 
94
 
 
95
type fakeLifer struct {
 
96
        life state.Life
 
97
        err  error
 
98
}
 
99
 
 
100
func (l *fakeLifer) Tag() string {
 
101
        panic("not needed")
 
102
}
 
103
 
 
104
func (l *fakeLifer) Life() state.Life {
 
105
        return l.life
 
106
}