launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/state/apiserver/common/setstatus_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 statusSetterSuite struct{} 17 18 var _ = gc.Suite(&statusSetterSuite{}) 19 20 type fakeStatusSetter struct { 21 state.Entity 22 status params.Status 23 info string 24 data params.StatusData 25 err error 26 fetchError 27 } 28 29 func (s *fakeStatusSetter) SetStatus(status params.Status, info string, data params.StatusData) error { 30 s.status = status 31 s.info = info 32 s.data = data 33 return s.err 34 } 35 36 func (*statusSetterSuite) TestSetStatus(c *gc.C) { 37 st := &fakeState{ 38 entities: map[string]entityWithError{ 39 "x0": &fakeStatusSetter{status: params.StatusPending, info: "blah", err: errors.Newf("x0 fails")}, 40 "x1": &fakeStatusSetter{status: params.StatusStarted, info: "foo"}, 41 "x2": &fakeStatusSetter{status: params.StatusError, info: "some info"}, 42 "x3": &fakeStatusSetter{fetchError: "x3 error"}, 43 "x4": &fakeStatusSetter{status: params.StatusStopped, info: ""}, 44 }, 45 } 46 getCanModify := func() (common.AuthFunc, error) { 47 return func(tag string) bool { 48 switch tag { 49 case "x0", "x1", "x2", "x3": 50 return true 51 } 52 return false 53 }, nil 54 } 55 s := common.NewStatusSetter(st, getCanModify) 56 args := params.SetStatus{ 57 Entities: []params.SetEntityStatus{ 58 {"x0", params.StatusStarted, "bar", nil}, 59 {"x1", params.StatusStopped, "", nil}, 60 {"x2", params.StatusPending, "not really", nil}, 61 {"x3", params.StatusStopped, "", nil}, 62 {"x4", params.StatusError, "blarg", nil}, 63 {"x5", params.StatusStarted, "42", nil}, 64 }, 65 } 66 result, err := s.SetStatus(args) 67 c.Assert(err, gc.IsNil) 68 c.Assert(result, gc.DeepEquals, params.ErrorResults{ 69 Results: []params.ErrorResult{ 70 {¶ms.Error{Message: "x0 fails"}}, 71 {nil}, 72 {nil}, 73 {¶ms.Error{Message: "x3 error"}}, 74 {apiservertesting.ErrUnauthorized}, 75 {apiservertesting.ErrUnauthorized}, 76 }, 77 }) 78 get := func(tag string) *fakeStatusSetter { 79 return st.entities[tag].(*fakeStatusSetter) 80 } 81 c.Assert(get("x1").status, gc.Equals, params.StatusStopped) 82 c.Assert(get("x1").info, gc.Equals, "") 83 c.Assert(get("x2").status, gc.Equals, params.StatusPending) 84 c.Assert(get("x2").info, gc.Equals, "not really") 85 } 86 87 func (*statusSetterSuite) TestSetStatusError(c *gc.C) { 88 getCanModify := func() (common.AuthFunc, error) { 89 return nil, errors.Newf("pow") 90 } 91 s := common.NewStatusSetter(&fakeState{}, getCanModify) 92 args := params.SetStatus{ 93 Entities: []params.SetEntityStatus{{"x0", "", "", nil}}, 94 } 95 _, err := s.SetStatus(args) 96 c.Assert(err, gc.ErrorMatches, "pow") 97 } 98 99 func (*statusSetterSuite) TestSetStatusNoArgsNoError(c *gc.C) { 100 getCanModify := func() (common.AuthFunc, error) { 101 return nil, errors.Newf("pow") 102 } 103 s := common.NewStatusSetter(&fakeState{}, getCanModify) 104 result, err := s.SetStatus(params.SetStatus{}) 105 c.Assert(err, gc.IsNil) 106 c.Assert(result.Results, gc.HasLen, 0) 107 }