launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/apiserver/common/life_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 "launchpad.net/errgo/errors" 8 gc "launchpad.net/gocheck" 9 10 "launchpad.net/juju-core/state" 11 "launchpad.net/juju-core/state/api/params" 12 "launchpad.net/juju-core/state/apiserver/common" 13 apiservertesting "launchpad.net/juju-core/state/apiserver/testing" 14 ) 15 16 type lifeSuite struct{} 17 18 var _ = gc.Suite(&lifeSuite{}) 19 20 type fakeLifer struct { 21 state.Entity 22 life state.Life 23 fetchError 24 } 25 26 func (l *fakeLifer) Life() state.Life { 27 return l.life 28 } 29 30 func (*lifeSuite) TestLife(c *gc.C) { 31 st := &fakeState{ 32 entities: map[string]entityWithError{ 33 "x0": &fakeLifer{life: state.Alive}, 34 "x1": &fakeLifer{life: state.Dying}, 35 "x2": &fakeLifer{life: state.Dead}, 36 "x3": &fakeLifer{fetchError: "x3 error"}, 37 }, 38 } 39 getCanRead := func() (common.AuthFunc, error) { 40 return func(tag string) bool { 41 switch tag { 42 case "x0", "x2", "x3": 43 return true 44 } 45 return false 46 }, nil 47 } 48 lg := common.NewLifeGetter(st, getCanRead) 49 entities := params.Entities{[]params.Entity{ 50 {"x0"}, {"x1"}, {"x2"}, {"x3"}, {"x4"}, 51 }} 52 results, err := lg.Life(entities) 53 c.Assert(err, gc.IsNil) 54 c.Assert(results, gc.DeepEquals, params.LifeResults{ 55 Results: []params.LifeResult{ 56 {Life: params.Alive}, 57 {Error: apiservertesting.ErrUnauthorized}, 58 {Life: params.Dead}, 59 {Error: ¶ms.Error{Message: "x3 error"}}, 60 {Error: apiservertesting.ErrUnauthorized}, 61 }, 62 }) 63 } 64 65 func (*lifeSuite) TestLifeError(c *gc.C) { 66 getCanRead := func() (common.AuthFunc, error) { 67 return nil, errors.Newf("pow") 68 } 69 lg := common.NewLifeGetter(&fakeState{}, getCanRead) 70 _, err := lg.Life(params.Entities{[]params.Entity{{"x0"}}}) 71 c.Assert(err, gc.ErrorMatches, "pow") 72 } 73 74 func (*lifeSuite) TestLifeNoArgsNoError(c *gc.C) { 75 getCanRead := func() (common.AuthFunc, error) { 76 return nil, errors.Newf("pow") 77 } 78 lg := common.NewLifeGetter(&fakeState{}, getCanRead) 79 result, err := lg.Life(params.Entities{}) 80 c.Assert(err, gc.IsNil) 81 c.Assert(result.Results, gc.HasLen, 0) 82 }